{"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"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":53666,"databundleVersionId":6589269,"sourceType":"competition"},{"sourceId":6507308,"sourceType":"datasetVersion","datasetId":3706667}],"dockerImageVersionId":30558,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"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 seaborn as sns\nimport matplotlib.pyplot as plt\nimport polars as pl\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","_kg_hide-output":true,"_kg_hide-input":true,"execution":{"iopub.status.busy":"2023-11-13T10:43:51.065708Z","iopub.execute_input":"2023-11-13T10:43:51.06611Z","iopub.status.idle":"2023-11-13T10:43:51.077977Z","shell.execute_reply.started":"2023-11-13T10:43:51.066078Z","shell.execute_reply":"2023-11-13T10:43:51.076813Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# df.loc[df['column_name'] == some_value]\ntrain_series = pl.read_parquet('/kaggle/input/zzzs-lightweight-training-dataset-target/Zzzs_train.parquet')\n\n# train_series\nids = train_series['series_id'].unique(maintain_order = True)\nprint (list(ids))\nids = list(ids)\n","metadata":{"execution":{"iopub.status.busy":"2023-11-13T10:43:51.080133Z","iopub.execute_input":"2023-11-13T10:43:51.080794Z","iopub.status.idle":"2023-11-13T10:43:52.346548Z","shell.execute_reply.started":"2023-11-13T10:43:51.080752Z","shell.execute_reply":"2023-11-13T10:43:52.345429Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = pd.read_csv('/kaggle/input/child-mind-institute-detect-sleep-states/train_events.csv')\ndata = data.loc[data['series_id'].isin(ids) ]\n\ndata.shape","metadata":{"execution":{"iopub.status.busy":"2023-11-13T10:43:52.348048Z","iopub.execute_input":"2023-11-13T10:43:52.348402Z","iopub.status.idle":"2023-11-13T10:43:52.378166Z","shell.execute_reply.started":"2023-11-13T10:43:52.34837Z","shell.execute_reply":"2023-11-13T10:43:52.376975Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Distribution of Sleep Durations","metadata":{}},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\n# Pivot the data to get onset and wakeup times side by side for each night\n# Calculate sleep duration for each night\nsleep_durations = data.pivot_table(index=['series_id', 'night'], columns='event', values='step').reset_index()\nsleep_durations['duration'] = sleep_durations['wakeup'] - sleep_durations['onset']\n\n# Remove any negative durations (might arise from data inconsistencies)\nsleep_durations = sleep_durations[sleep_durations['duration'] > 0]\n\n# Initialize the figure\nplt.figure(figsize=(15, 6))\n\n# Plot the distribution of sleep durations\nsns.histplot(sleep_durations['duration'], bins=50, kde=True, color='purple')\nsns.despine()\n# Set title and labels\nplt.title('Distribution of Sleep Durations')\nplt.xlabel('Sleep Duration (in steps)')\nplt.ylabel('Number of Nights')\n\nplt.show()\n","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2023-11-13T10:43:52.380799Z","iopub.execute_input":"2023-11-13T10:43:52.381153Z","iopub.status.idle":"2023-11-13T10:43:52.885908Z","shell.execute_reply.started":"2023-11-13T10:43:52.381124Z","shell.execute_reply":"2023-11-13T10:43:52.884785Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Here's a histogram displaying sleep durations. The x-axis represents the sleep duration in hours, and the y-axis represents the frequency of occurrences. From this, we can see the distribution of sleep durations among the participants.","metadata":{}},{"cell_type":"markdown","source":"# Heatmap of Sleep Durations","metadata":{}},{"cell_type":"code","source":"# Convert the timestamp column to datetime format with timezone handling\ndata['datetime'] = pd.to_datetime(data['timestamp'], errors='coerce', utc=True).dt.tz_convert('US/Eastern')\n\n# Drop rows where datetime couldn't be parsed\ndata.dropna(subset=['datetime'], inplace=True)\n\n# Extract day of week and hour from the datetime\ndata['day_of_week'] = data['datetime'].dt.day_name()\ndata['hour'] = data['datetime'].dt.hour\n\n# Filter for sleep onset events only, as we want to use onset times for the heatmap\nonset_data = data[data['event'] == 'onset']\n\n# Create a pivot table for the heatmap\nheatmap_data = onset_data.pivot_table(index='hour', columns='day_of_week', values='step', aggfunc='count').fillna(0)\n\n# Order days of the week\nordered_days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']\nheatmap_data = heatmap_data[ordered_days]\n\nplt.figure(figsize=(12, 7))\nsns.heatmap(heatmap_data, cmap='YlGnBu', linewidths=0.5)\nplt.title('Heatmap of Sleep Onset by Day and Hour')\nplt.ylabel('Hour of Day')\nplt.xlabel('Day of the Week')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-11-13T10:43:52.887693Z","iopub.execute_input":"2023-11-13T10:43:52.888063Z","iopub.status.idle":"2023-11-13T10:43:53.332167Z","shell.execute_reply.started":"2023-11-13T10:43:52.888033Z","shell.execute_reply":"2023-11-13T10:43:53.330952Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Here's the heatmap displaying sleep onset times by day and hour:\n\nThe x-axis represents the day of the week.\nThe y-axis represents the hour of the day.\nThe color intensity indicates the frequency of sleep onset times.","metadata":{}},{"cell_type":"markdown","source":"# Sleep Onset and Wakeup Timeline","metadata":{}},{"cell_type":"code","source":"# Merge onset and wakeup data on dates to ensure alignment\ntimeline_data = pd.merge(\n    data[data['event'] == 'onset'][['datetime']],\n    data[data['event'] == 'wakeup'][['datetime']],\n    left_on=data[data['event'] == 'onset']['datetime'].dt.date,\n    right_on=data[data['event'] == 'wakeup']['datetime'].dt.date,\n    suffixes=('_onset', '_wakeup')\n)\n\n# Convert time to minutes after midnight for both onset and wakeup\ntimeline_data['onset_minutes'] = timeline_data['datetime_onset'].dt.time.apply(lambda x: x.hour * 60 + x.minute)\ntimeline_data['wakeup_minutes'] = timeline_data['datetime_wakeup'].dt.time.apply(lambda x: x.hour * 60 + x.minute)\n\nplt.figure(figsize=(14, 8))\nplt.scatter(timeline_data['datetime_onset'].dt.date, timeline_data['onset_minutes'], label='Sleep Onset', color='blue',marker='x', alpha=0.2)\nplt.scatter(timeline_data['datetime_wakeup'].dt.date, timeline_data['wakeup_minutes'], label='Wakeup', color='red',marker='x', alpha=0.2)\n\nplt.title('Sleep Onset and Wakeup Timeline')\nplt.xlabel('Date')\nplt.ylabel('Time of Day (minutes after midnight)')\nplt.legend()\nsns.despine()\nplt.grid(True, which='both', linestyle='--', linewidth=0.5)\nplt.yticks(ticks=range(0, 24*60, 60), labels=[f'{h:02d}:00' for h in range(24)])\nplt.xticks(rotation=45)\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-11-13T10:43:53.33436Z","iopub.execute_input":"2023-11-13T10:43:53.334985Z","iopub.status.idle":"2023-11-13T10:43:54.24961Z","shell.execute_reply.started":"2023-11-13T10:43:53.334954Z","shell.execute_reply":"2023-11-13T10:43:54.248666Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Here's the Sleep Onset and Wakeup Timeline:\n\nThe x-axis represents the date.\nThe y-axis represents the time of day in minutes after midnight. For instance, 300 minutes after midnight corresponds to 5:00 AM.\nThe blue dots represent sleep onset times, while the red dots represent wakeup times.","metadata":{}},{"cell_type":"markdown","source":"# Average Sleep Duration by Hour of Onset","metadata":{}},{"cell_type":"code","source":"# Pivot the data to get onset and wakeup times side by side for each night\npivot_data = data.pivot_table(index=['series_id', 'night'], columns='event', values='step').reset_index()\n\n# Calculate sleep duration for each night\npivot_data['duration'] = pivot_data['wakeup'] - pivot_data['onset']\n\n# Calculate sleep duration in minutes and extract hour of onset\npivot_data['duration_minutes'] = pivot_data['duration'] / 60\npivot_data['onset_hour'] = pd.to_datetime(pivot_data['onset'], unit='s').dt.hour\n\n# Group by onset hour and calculate average sleep duration\naverage_duration_by_hour = pivot_data.groupby('onset_hour')['duration_minutes'].mean()\n\nplt.figure(figsize=(12, 7))\naverage_duration_by_hour.plot(kind='bar', color='teal', edgecolor='black')\nplt.title('Average Sleep Duration by Hour of Onset')\nsns.despine()\nplt.xlabel('Hour of Sleep Onset')\nplt.ylabel('Average Sleep Duration (minutes)')\nplt.grid(True, which='both', linestyle='--', linewidth=0.5, axis='y')\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-11-13T10:43:54.250879Z","iopub.execute_input":"2023-11-13T10:43:54.251982Z","iopub.status.idle":"2023-11-13T10:43:54.833667Z","shell.execute_reply.started":"2023-11-13T10:43:54.251947Z","shell.execute_reply":"2023-11-13T10:43:54.83252Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Box Plot of Sleep Duration by Day of Week","metadata":{}},{"cell_type":"code","source":"# Extract day of week from onset timestamp for the box plot\npivot_data['day_of_week'] = pd.to_datetime(pivot_data['onset'], unit='s').dt.day_name()\n\n# Order days of the week\nordered_days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']\n\nplt.figure(figsize=(12, 7))\nsns.boxplot(x='day_of_week', y='duration_minutes', data=pivot_data, order=ordered_days, palette='pastel')\nsns.despine()\nplt.title('Box Plot of Sleep Duration by Day of Week')\nplt.xlabel('Day of the Week')\nplt.ylabel('Sleep Duration (minutes)')\nplt.grid(True, which='both', linestyle='--', linewidth=0.5, axis='y')\nplt.tight_layout()\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2023-11-13T10:43:54.835121Z","iopub.execute_input":"2023-11-13T10:43:54.835475Z","iopub.status.idle":"2023-11-13T10:43:55.327776Z","shell.execute_reply.started":"2023-11-13T10:43:54.835446Z","shell.execute_reply":"2023-11-13T10:43:55.32683Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Circadian Rhythm Scatter Plot","metadata":{}},{"cell_type":"code","source":"# Convert onset and wakeup times to minutes after midnight for the scatter plot using existing columns\nonset_minutes = pivot_data['onset_hour'] * 60\nwakeup_minutes = pd.to_datetime(pivot_data['wakeup'], unit='s').dt.time.apply(lambda x: x.hour * 60 + x.minute)\n\nplt.figure(figsize=(12, 7))\nplt.scatter(wakeup_minutes, onset_minutes, color='green', alpha=0.5)\nsns.despine()\nplt.title('Circadian Rhythm Scatter Plot')\nplt.xlabel('Wakeup Time (minutes after midnight)')\nplt.ylabel('Sleep Onset Time (minutes after midnight)')\nplt.grid(True, which='both', linestyle='--', linewidth=0.5)\nplt.xticks(ticks=range(0, 24*60, 60), labels=[f'{h:02d}:00' for h in range(24)])\nplt.yticks(ticks=range(0, 24*60, 60), labels=[f'{h:02d}:00' for h in range(24)])\nplt.xticks(rotation=45)\nplt.tight_layout()\nplt.show()\n","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2023-11-13T10:43:55.330497Z","iopub.execute_input":"2023-11-13T10:43:55.330833Z","iopub.status.idle":"2023-11-13T10:43:56.015199Z","shell.execute_reply.started":"2023-11-13T10:43:55.330805Z","shell.execute_reply":"2023-11-13T10:43:56.013881Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Density Plot of Sleep Onset and Wakeup Times","metadata":{}},{"cell_type":"code","source":"# Plotting density plots for sleep onset and wakeup times\nplt.figure(figsize=(14, 8))\n\nsns.kdeplot(onset_minutes, fill=True, color='blue', label='Sleep Onset', bw_adjust=0.5)\nsns.kdeplot(wakeup_minutes, fill=True, color='red', label='Wakeup', bw_adjust=0.5)\nsns.despine()\nplt.title('Density Plot of Sleep Onset and Wakeup Times')\nplt.xlabel('Time (minutes after midnight)')\nplt.ylabel('Density')\nplt.grid(True, which='both', linestyle='--', linewidth=0.5)\nplt.xticks(ticks=range(0, 24*60, 60), labels=[f'{h:02d}:00' for h in range(24)])\nplt.legend()\nplt.xticks(rotation=45)\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-11-13T10:43:56.01733Z","iopub.execute_input":"2023-11-13T10:43:56.017977Z","iopub.status.idle":"2023-11-13T10:43:56.80814Z","shell.execute_reply.started":"2023-11-13T10:43:56.017931Z","shell.execute_reply":"2023-11-13T10:43:56.806931Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Distribution of Sleep Onset and Wakeup Times","metadata":{}},{"cell_type":"code","source":"train_events = data.copy()\n# Set the style for the plots\nsns.set_style(\"whitegrid\")\n\n# Initialize the figure\nplt.figure(figsize=(15, 7))\n\n# Plot the distribution of 'step' for both onset and wakeup events\nsns.kdeplot(train_events[train_events['event'] == 'onset']['step'], fill=True, label='Sleep Onset', color='blue')\nsns.kdeplot(train_events[train_events['event'] == 'wakeup']['step'], fill=True, label='Wakeup', color='orange')\n\n# Set title and labels\nplt.title('Distribution of Sleep Onset and Wakeup Times')\nplt.xlabel('Step')\nplt.ylabel('Density')\nplt.legend()\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-11-13T10:43:56.809613Z","iopub.execute_input":"2023-11-13T10:43:56.810457Z","iopub.status.idle":"2023-11-13T10:43:57.391716Z","shell.execute_reply.started":"2023-11-13T10:43:56.810426Z","shell.execute_reply":"2023-11-13T10:43:57.390582Z"},"jupyter":{"source_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Distribution of Number of Events per Series","metadata":{}},{"cell_type":"code","source":"# Calculate the number of events per series\nevents_per_series = train_events['series_id'].value_counts()\n\n# Initialize the figure\nplt.figure(figsize=(15, 6))\n\n# Plot the distribution of number of events per series\nsns.histplot(events_per_series, bins=30, kde=True, color='green')\n\n# Set title and labels\nplt.title('Distribution of Number of Events per Series')\nplt.xlabel('Number of Events')\nplt.ylabel('Number of Series')\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2023-11-13T10:43:57.393452Z","iopub.execute_input":"2023-11-13T10:43:57.394332Z","iopub.status.idle":"2023-11-13T10:43:57.945484Z","shell.execute_reply.started":"2023-11-13T10:43:57.3943Z","shell.execute_reply":"2023-11-13T10:43:57.94435Z"},"jupyter":{"source_hidden":true},"trusted":true},"execution_count":null,"outputs":[]}]}