{"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":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport matplotlib.pyplot as plt\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-10-06T04:24:26.773474Z","iopub.execute_input":"2023-10-06T04:24:26.773951Z","iopub.status.idle":"2023-10-06T04:24:26.783044Z","shell.execute_reply.started":"2023-10-06T04:24:26.773906Z","shell.execute_reply":"2023-10-06T04:24:26.782317Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Read the train and test series\n\ntrain = pd.read_parquet('/kaggle/input/child-mind-institute-detect-sleep-states/train_series.parquet')\ntest = pd.read_parquet('/kaggle/input/child-mind-institute-detect-sleep-states/test_series.parquet')\ntrain_events = pd.read_csv('/kaggle/input/child-mind-institute-detect-sleep-states/train_events.csv')\n","metadata":{"execution":{"iopub.status.busy":"2023-10-06T04:24:26.784404Z","iopub.execute_input":"2023-10-06T04:24:26.785450Z","iopub.status.idle":"2023-10-06T04:25:39.959064Z","shell.execute_reply.started":"2023-10-06T04:24:26.785411Z","shell.execute_reply":"2023-10-06T04:25:39.957543Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Check the unique series ID in test data\ntest.series_id.unique()","metadata":{"execution":{"iopub.status.busy":"2023-10-06T04:58:40.960791Z","iopub.execute_input":"2023-10-06T04:58:40.961153Z","iopub.status.idle":"2023-10-06T04:58:40.969354Z","shell.execute_reply.started":"2023-10-06T04:58:40.961127Z","shell.execute_reply":"2023-10-06T04:58:40.968110Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.head()","metadata":{"execution":{"iopub.status.busy":"2023-10-06T05:03:41.428687Z","iopub.execute_input":"2023-10-06T05:03:41.429068Z","iopub.status.idle":"2023-10-06T05:03:41.445115Z","shell.execute_reply.started":"2023-10-06T05:03:41.429040Z","shell.execute_reply":"2023-10-06T05:03:41.443971Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2023-10-06T04:33:09.416171Z","iopub.execute_input":"2023-10-06T04:33:09.417921Z","iopub.status.idle":"2023-10-06T04:33:09.430409Z","shell.execute_reply.started":"2023-10-06T04:33:09.417877Z","shell.execute_reply":"2023-10-06T04:33:09.429045Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Get the number of series_id in train data & test data\n\nprint(\"number of unique series id in train data: \"+ str(train_events.series_id.nunique()))\nprint(\"number of unique series id in test data: \"+ str(test.series_id.nunique()))\n\n\n","metadata":{"execution":{"iopub.status.busy":"2023-10-06T04:25:39.962977Z","iopub.execute_input":"2023-10-06T04:25:39.964151Z","iopub.status.idle":"2023-10-06T04:25:39.981416Z","shell.execute_reply.started":"2023-10-06T04:25:39.964065Z","shell.execute_reply":"2023-10-06T04:25:39.977424Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_events['timestamp'] = pd.to_datetime(train_events['timestamp'], utc=True)\ntrain_events","metadata":{"execution":{"iopub.status.busy":"2023-10-06T04:25:39.992631Z","iopub.execute_input":"2023-10-06T04:25:39.995699Z","iopub.status.idle":"2023-10-06T04:25:40.168118Z","shell.execute_reply.started":"2023-10-06T04:25:39.995572Z","shell.execute_reply":"2023-10-06T04:25:40.164563Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Count of train events having no timestamp --4923 out of 14508 records\n#train_events[train_events['timestamp'].isnull()]\n\n# check if the null records pertain to a specific series id\nnull_counts = train_events.groupby('series_id')['timestamp'].apply(lambda x: x.isnull().sum())\nprint(null_counts)","metadata":{"execution":{"iopub.status.busy":"2023-10-06T04:55:42.272320Z","iopub.execute_input":"2023-10-06T04:55:42.272979Z","iopub.status.idle":"2023-10-06T04:55:42.329009Z","shell.execute_reply.started":"2023-10-06T04:55:42.272947Z","shell.execute_reply":"2023-10-06T04:55:42.327840Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(train_events)","metadata":{"execution":{"iopub.status.busy":"2023-10-06T04:38:31.228494Z","iopub.execute_input":"2023-10-06T04:38:31.228924Z","iopub.status.idle":"2023-10-06T04:38:31.235810Z","shell.execute_reply.started":"2023-10-06T04:38:31.228890Z","shell.execute_reply":"2023-10-06T04:38:31.234675Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Extract the date time from the timestamp\n\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['min'] = train_events['timestamp'].dt.minute\ntrain_events['month'] = train_events['timestamp'].dt.month\ntrain_events['D_O_W'] = train_events['timestamp'].dt.weekday","metadata":{"execution":{"iopub.status.busy":"2023-10-06T04:32:05.910794Z","iopub.execute_input":"2023-10-06T04:32:05.911172Z","iopub.status.idle":"2023-10-06T04:32:05.935219Z","shell.execute_reply.started":"2023-10-06T04:32:05.911141Z","shell.execute_reply":"2023-10-06T04:32:05.933831Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_events","metadata":{"execution":{"iopub.status.busy":"2023-10-06T04:32:10.170832Z","iopub.execute_input":"2023-10-06T04:32:10.171195Z","iopub.status.idle":"2023-10-06T04:32:10.202651Z","shell.execute_reply.started":"2023-10-06T04:32:10.171167Z","shell.execute_reply":"2023-10-06T04:32:10.201461Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Plot the onset and wakeup events on 24 hour timeline\nfrom matplotlib.dates import DateFormatter\nimport seaborn as sns\ntrain_events['date_str'] = train_events['date'].astype(str)\ntrain_events['min_str'] = train_events['min'].astype(str)\ntrain_events['hour_str'] = train_events['hour'].astype(str)\ntrain_events.sort_values(by= 'hour', ascending=False, inplace=True)\nsns.scatterplot(data=train_events, x='date_str', y='hour_str', hue='event')\n\n\n","metadata":{"execution":{"iopub.status.busy":"2023-10-06T05:06:50.737720Z","iopub.execute_input":"2023-10-06T05:06:50.738101Z","iopub.status.idle":"2023-10-06T05:06:56.629159Z","shell.execute_reply.started":"2023-10-06T05:06:50.738069Z","shell.execute_reply":"2023-10-06T05:06:56.627925Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"On most of the days, the onset is during early hours of the day (between 0 and 6) and wakeup hours are the later hours (between 9 and 16)","metadata":{}},{"cell_type":"code","source":"# Plot the time of onset and wake events against the weekday\n\ntrain_events['dow_str'] = train_events['D_O_W'].astype(str)\ntrain_events.sort_values(by= 'hour', ascending=False, inplace=True)\nsns.scatterplot(data=train_events, x='dow_str', y='hour_str', hue='event').set(title = \"Onset and Wake times plotted against day of the week\")\n","metadata":{"execution":{"iopub.status.busy":"2023-10-06T05:10:50.519814Z","iopub.execute_input":"2023-10-06T05:10:50.520769Z","iopub.status.idle":"2023-10-06T05:10:52.178621Z","shell.execute_reply.started":"2023-10-06T05:10:50.520722Z","shell.execute_reply":"2023-10-06T05:10:52.177480Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}