{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19"}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\n\nnp.random.seed(0)","metadata":{"execution":{"iopub.status.busy":"2023-09-08T07:56:43.639707Z","iopub.execute_input":"2023-09-08T07:56:43.641016Z","iopub.status.idle":"2023-09-08T07:56:43.646324Z","shell.execute_reply.started":"2023-09-08T07:56:43.640943Z","shell.execute_reply":"2023-09-08T07:56:43.645436Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_series = pd.read_parquet('/kaggle/input/child-mind-institute-detect-sleep-states/train_series.parquet')\ntest_series = pd.read_parquet('/kaggle/input/child-mind-institute-detect-sleep-states/test_series.parquet')\ntrain_series","metadata":{"execution":{"iopub.status.busy":"2023-09-08T07:56:43.909386Z","iopub.execute_input":"2023-09-08T07:56:43.909824Z","iopub.status.idle":"2023-09-08T07:57:41.643190Z","shell.execute_reply.started":"2023-09-08T07:56:43.909780Z","shell.execute_reply":"2023-09-08T07:57:41.641911Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_events = pd.read_csv('/kaggle/input/child-mind-institute-detect-sleep-states/train_events.csv')\n\ntrain_events['timestamp'] = pd.to_datetime(train_events['timestamp'], utc=True) - pd.Timedelta(hours=4)\ntrain_events['date'] = train_events['timestamp'].dt.date\ntrain_events['time'] = train_events['timestamp'].dt.time\ntrain_events['hour'] = train_events['timestamp'].dt.hour\ntrain_events.head()","metadata":{"execution":{"iopub.status.busy":"2023-09-08T07:57:41.645500Z","iopub.execute_input":"2023-09-08T07:57:41.646517Z","iopub.status.idle":"2023-09-08T07:57:41.855570Z","shell.execute_reply.started":"2023-09-08T07:57:41.646471Z","shell.execute_reply":"2023-09-08T07:57:41.854565Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Show how anglez and enmo change over time","metadata":{}},{"cell_type":"code","source":"from datetime import datetime, timedelta\n\ndef plot_ser(ser,evt,data, ax):\n    ser[data].plot(ax=ax, color='b', alpha=0.5)\n    for i, e in evt.iterrows():\n        ax.axvline(x=i, color= 'r' if e['event'] == 'onset' else 'g', linestyle='--', label=e['event'])\n    ax.set_ylabel(data)\n    handles, labels = ax.get_legend_handles_labels()\n    new_labels, new_handles = [], []\n    for handle, label in zip(handles, labels):\n        if label not in new_labels:\n            new_handles.append(handle)\n            new_labels.append(label)\n\n    ax.legend(new_handles, new_labels)\n\n\ndef plot_multi_ser(ser_nums,days):\n    \"\"\"\n    Display the number of people: ser_nums, display the number of days: days\n    \"\"\"\n    multi_ser = train_events.groupby('series_id')['timestamp'].agg(['min']).dropna()[:ser_nums]\n    for series_id,ser_grp in multi_ser.iterrows():\n        # get event data \n        start_date = ser_grp['min'] -  timedelta(minutes=5)\n        end_date = start_date + timedelta(days=days,  minutes=10)\n        \n        evt = train_events.loc[train_events['series_id'] == series_id].copy().dropna()\n        evt.set_index('timestamp', inplace=True)\n        evt = evt[start_date:end_date]\n        # get series data, and convert timestamp columns\n        ser = train_series.loc[train_series['series_id'] == series_id].copy().dropna()\n        ser['timestamp'] = pd.to_datetime(ser['timestamp'], utc=True) - pd.Timedelta(hours=4)\n        ser['date'] = ser['timestamp'].dt.date\n        ser['time'] = ser['timestamp'].dt.time\n        ser['hour'] = ser['timestamp'].dt.hour\n        ser.set_index('timestamp', inplace=True)\n        ser = ser[start_date:end_date]\n        \n        fig, axes = plt.subplots(2, 1, figsize=(30, 12))\n        fig.suptitle(f'anglez and enmo values for {series_id}')\n        plot_ser(ser,evt,'anglez', axes[0])\n        plot_ser(ser,evt,'enmo', axes[1])        \n        plt.show()\n\n# Display the changes of 3 people in 1 day\nplot_multi_ser(3, 1)","metadata":{"execution":{"iopub.status.busy":"2023-09-08T07:57:41.857708Z","iopub.execute_input":"2023-09-08T07:57:41.858535Z","iopub.status.idle":"2023-09-08T07:59:13.065243Z","shell.execute_reply.started":"2023-09-08T07:57:41.858487Z","shell.execute_reply":"2023-09-08T07:59:13.064191Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}],"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}}