{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.14","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":82611,"databundleVersionId":9553358,"sourceType":"competition"},{"sourceId":199742556,"sourceType":"kernelVersion"}],"dockerImageVersionId":30786,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# BrisT1D - Full Interactive Timeseries\n\nThis notebook displays all of the data in the `train.csv` dataset for one study participant. The data is presented in an interactive Plotly graph, so you can zoom in on different data modalities and easily see how features interact.\n\nIn order to achieve this from the available data, we:\n+ add a synthetic date range\n+ melt lagged feature columns\n+ de-duplicate the resulting measurements\n+ rejoin all data onto a common 5-minute granularity data spline.\n\n**Enjoy!**","metadata":{}},{"cell_type":"code","source":"import datetime\nimport pandas as pd\nimport plotly.express as px\n\nfrom pathlib import Path\nfrom typing import Optional\nfrom plotly.subplots import make_subplots\n\nplotly_colors = px.colors.qualitative.Plotly\n\n\ninput_path = Path('/kaggle/input')\nprocessed_data_root = input_path / 'brist1d-to-timeseries/processed' / '241006_130843'\n\nstudy_start = datetime.datetime(2000, 1, 1)\nstudy_end = datetime.datetime(2000, 4, 1)\nstudy_range = pd.date_range(study_start, study_end, freq='5min')\n\nfeatures = ['bg', 'activity', 'insulin', 'carbs', 'cals', 'steps', 'hr']\n\n\ndef compile_features(p_num: str) -> pd.DataFrame:\n    df_core = pd.DataFrame({'timestamp': study_range})\n    \n    for f in features: \n        df_feat = pd.read_parquet(processed_data_root / f'p_num={p_num}/feature={f}')\n        df_core = df_core.merge(df_feat, how='left', on='timestamp')\n    \n    return df_core\n\n\ndef generate_figure(df: pd.DataFrame, p_num: Optional[str] = None):\n    \n    fig = make_subplots(\n        rows=7,\n        cols=1,\n        shared_xaxes=True,\n        subplot_titles=['bg', 'hr', 'insulin', 'activity', 'carbs', 'cals', 'steps'],\n        vertical_spacing=0.05\n    )\n\n    fig.add_traces(\n        px.scatter(\n            df,\n            x='timestamp',\n            y='bg',\n            color_discrete_sequence=[plotly_colors[0]]\n        ).data,\n        rows=1,\n        cols=1\n    )\n\n    fig.add_traces(\n        px.scatter(\n            df,\n            x='timestamp',\n            y='hr',\n            color_discrete_sequence=[plotly_colors[1]]\n        ).data,\n        rows=2,\n        cols=1\n    )\n\n    fig.add_traces(\n        px.scatter(\n            df,\n            x='timestamp',\n            y='insulin',\n            color_discrete_sequence=[plotly_colors[2]]\n        ).data,\n        rows=3,\n        cols=1\n    )\n\n    fig.add_traces(\n        px.scatter(\n            df,\n            x='timestamp',\n            y='activity',\n            color='activity'\n        ).data,\n        rows=4,\n        cols=1\n    )\n\n    fig.add_traces(\n        px.scatter(\n            df,\n            x='timestamp',\n            y='carbs',\n            color_discrete_sequence=[plotly_colors[3]]\n        ).data,\n        rows=5,\n        cols=1\n    )\n\n    fig.add_traces(\n        px.scatter(\n            df,\n            x='timestamp',\n            y='cals',\n            color_discrete_sequence=[plotly_colors[4]]\n        ).data,\n        rows=6,\n        cols=1\n    )\n\n    fig.add_traces(\n        px.scatter(\n            df,\n            x='timestamp',\n            y='steps',\n            color_discrete_sequence=['grey']\n        ).data,\n        rows=7,\n        cols=1\n    )\n    \n    title_suffix = f' for {p_num.upper()}' if p_num is not None else ''\n    \n    fig.update_layout(\n        height=1500,\n        title=f'Timeseries' + title_suffix + ' (Train Dataset)',\n        yaxis2_range=[30, 200]\n    )\n\n    return fig","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-10-06T14:49:13.450710Z","iopub.execute_input":"2024-10-06T14:49:13.451922Z","iopub.status.idle":"2024-10-06T14:49:13.457779Z","shell.execute_reply.started":"2024-10-06T14:49:13.451859Z","shell.execute_reply":"2024-10-06T14:49:13.456630Z"},"_kg_hide-input":true,"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df = compile_features('p11')\nfig = generate_figure(df, 'p11')\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2024-10-06T14:54:34.941260Z","iopub.execute_input":"2024-10-06T14:54:34.941751Z","iopub.status.idle":"2024-10-06T14:54:39.725405Z","shell.execute_reply.started":"2024-10-06T14:54:34.941704Z","shell.execute_reply":"2024-10-06T14:54:39.719519Z"},"trusted":true},"outputs":[],"execution_count":null}]}