{"metadata":{"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"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import pandas as pd\nimport numpy as np","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-09-18T19:04:00.390564Z","iopub.execute_input":"2023-09-18T19:04:00.391538Z","iopub.status.idle":"2023-09-18T19:04:00.828478Z","shell.execute_reply.started":"2023-09-18T19:04:00.391501Z","shell.execute_reply":"2023-09-18T19:04:00.827149Z"},"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Read in the ground truth file","metadata":{}},{"cell_type":"code","source":"ground_truth = pd.read_csv(\"/kaggle/input/child-mind-institute-detect-sleep-states/train_events.csv\")","metadata":{"execution":{"iopub.status.busy":"2023-09-18T19:04:00.830880Z","iopub.execute_input":"2023-09-18T19:04:00.831517Z","iopub.status.idle":"2023-09-18T19:04:00.880736Z","shell.execute_reply.started":"2023-09-18T19:04:00.831475Z","shell.execute_reply":"2023-09-18T19:04:00.878975Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Convert timestamp using UTC","metadata":{}},{"cell_type":"code","source":"df = ground_truth.copy()\ndf[\"timestamp\"] = pd.to_datetime(df[\"timestamp\"],utc=True)\ndf[\"hour\"] = df[\"timestamp\"].dt.hour\n# filter for mornings and afternons\ndf_morning   = df.loc[df[\"hour\"] <= 12].copy()\ndf_afternoon = df.loc[df[\"hour\"]  > 12].copy()\n\nprint(\"Wake in the morning:        \",df_morning.loc[df_morning[\"event\"] == \"wakeup\"].shape[0])\nprint(\"Go to sleep in the morning: \",df_morning.loc[df_morning[\"event\"] == \"onset\"].shape[0])\nprint(\"Sleep in the afternoon:     \",df_afternoon.loc[df_afternoon[\"event\"] == \"onset\"].shape[0])\nprint(\"Wake in the afternoon:      \",df_afternoon.loc[df_afternoon[\"event\"] == \"wakeup\"].shape[0])","metadata":{"execution":{"iopub.status.busy":"2023-09-18T19:04:00.882626Z","iopub.execute_input":"2023-09-18T19:04:00.883223Z","iopub.status.idle":"2023-09-18T19:04:01.021993Z","shell.execute_reply.started":"2023-09-18T19:04:00.883177Z","shell.execute_reply":"2023-09-18T19:04:01.020870Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Using local time","metadata":{}},{"cell_type":"code","source":"df = ground_truth.copy()\ndf['timestamp'] = pd.to_datetime(df['timestamp']).apply(lambda t: t.tz_localize(None))\ndf[\"hour\"] = df[\"timestamp\"].dt.hour\n# filter for mornings and afternons\ndf_morning   = df.loc[df[\"hour\"] <= 12].copy()\ndf_afternoon = df.loc[df[\"hour\"]  > 12].copy()\n\nprint(\"Wake in the morning:        \",df_morning.loc[df_morning[\"event\"] == \"wakeup\"].shape[0])\nprint(\"Go to sleep in the morning: \",df_morning.loc[df_morning[\"event\"] == \"onset\"].shape[0])\nprint(\"Sleep in the afternoon:     \",df_afternoon.loc[df_afternoon[\"event\"] == \"onset\"].shape[0])\nprint(\"Wake in the afternoon:      \",df_afternoon.loc[df_afternoon[\"event\"] == \"wakeup\"].shape[0])","metadata":{"execution":{"iopub.status.busy":"2023-09-18T19:04:01.024332Z","iopub.execute_input":"2023-09-18T19:04:01.024653Z","iopub.status.idle":"2023-09-18T19:04:01.269628Z","shell.execute_reply.started":"2023-09-18T19:04:01.024627Z","shell.execute_reply":"2023-09-18T19:04:01.268442Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Conclusion: local time makes more sense!","metadata":{}}]}