{"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 os\nimport pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nplt.tight_layout()\nplt.rcParams['figure.figsize'] = (6, 3)\n%matplotlib inline\nimport warnings\n# warnings.filterwarnings('ignore')\n\npd.set_option(\"display.max_columns\", None)","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-10-22T11:32:21.014361Z","iopub.execute_input":"2023-10-22T11:32:21.014904Z","iopub.status.idle":"2023-10-22T11:32:21.413138Z","shell.execute_reply.started":"2023-10-22T11:32:21.014852Z","shell.execute_reply":"2023-10-22T11:32:21.410454Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\nfrom torch import nn\nfrom torch.utils.data import DataLoader, TensorDataset, random_split\n\nfrom fastai.tabular.all import *","metadata":{"execution":{"iopub.status.busy":"2023-10-22T11:32:21.418099Z","iopub.execute_input":"2023-10-22T11:32:21.418702Z","iopub.status.idle":"2023-10-22T11:32:25.643416Z","shell.execute_reply.started":"2023-10-22T11:32:21.418668Z","shell.execute_reply":"2023-10-22T11:32:25.641472Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"device = torch.device(\"cuda:0\" if torch.cuda.is_available() else \"cpu\")\ndevice","metadata":{"execution":{"iopub.status.busy":"2023-10-22T11:32:25.645070Z","iopub.execute_input":"2023-10-22T11:32:25.645658Z","iopub.status.idle":"2023-10-22T11:32:25.654028Z","shell.execute_reply.started":"2023-10-22T11:32:25.645622Z","shell.execute_reply":"2023-10-22T11:32:25.652357Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Data","metadata":{}},{"cell_type":"code","source":"train_events = pd.read_csv('/kaggle/input/child-mind-institute-detect-sleep-states/train_events.csv')\n# train_raw = pd.read_parquet('/kaggle/input/child-mind-institute-detect-sleep-states/train_series.parquet')\n# test_raw = pd.read_parquet('/kaggle/input/child-mind-institute-detect-sleep-states/test_series.parquet')\n# sample_submission = pd.read_csv(\"/kaggle/input/child-mind-institute-detect-sleep-states/sample_submission.csv\")","metadata":{"execution":{"iopub.status.busy":"2023-10-22T11:33:10.652737Z","iopub.execute_input":"2023-10-22T11:33:10.653179Z","iopub.status.idle":"2023-10-22T11:33:10.673644Z","shell.execute_reply.started":"2023-10-22T11:33:10.653145Z","shell.execute_reply":"2023-10-22T11:33:10.671790Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_events","metadata":{"execution":{"iopub.status.busy":"2023-10-22T11:34:21.896607Z","iopub.execute_input":"2023-10-22T11:34:21.897085Z","iopub.status.idle":"2023-10-22T11:34:21.916636Z","shell.execute_reply.started":"2023-10-22T11:34:21.897048Z","shell.execute_reply":"2023-10-22T11:34:21.915086Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_events.loc[:,\"timestamp_no_tz\"] = [s[:-5] for s in train_events.timestamp]\ntrain_events","metadata":{"execution":{"iopub.status.busy":"2023-10-22T11:34:24.425342Z","iopub.execute_input":"2023-10-22T11:34:24.425830Z","iopub.status.idle":"2023-10-22T11:34:24.450284Z","shell.execute_reply.started":"2023-10-22T11:34:24.425795Z","shell.execute_reply":"2023-10-22T11:34:24.448765Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_events[\"datetime\"] = pd.to_datetime(train_events[\"timestamp_no_tz\"], errors=\"raise\", format=\"%Y-%m-%dT%H:%M:%S\", utc=False)\ntrain_events","metadata":{"execution":{"iopub.status.busy":"2023-10-22T11:34:26.936470Z","iopub.execute_input":"2023-10-22T11:34:26.936923Z","iopub.status.idle":"2023-10-22T11:34:26.966211Z","shell.execute_reply.started":"2023-10-22T11:34:26.936897Z","shell.execute_reply":"2023-10-22T11:34:26.964847Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"nights = train_events.night.value_counts()\nplt.bar(nights.index, nights)","metadata":{"execution":{"iopub.status.busy":"2023-10-22T11:44:56.790729Z","iopub.execute_input":"2023-10-22T11:44:56.791577Z","iopub.status.idle":"2023-10-22T11:44:57.013391Z","shell.execute_reply.started":"2023-10-22T11:44:56.791520Z","shell.execute_reply":"2023-10-22T11:44:57.012414Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"events = train_events.pivot(index=['series_id', 'night'], columns='event', values='datetime')\nevents.reset_index(inplace=True)\nevents[\"duration\"] = ((events.wakeup - events.onset).dt.seconds / 3600).astype(float)\nevents[\"onset_time\"] = (events.onset.dt.hour + events.onset.dt.minute / 60).astype(float)\nevents\n\nevents.loc[events.onset_time < 12,\"onset_time\"] += 24\nevents","metadata":{"execution":{"iopub.status.busy":"2023-10-22T11:45:03.995071Z","iopub.execute_input":"2023-10-22T11:45:03.996388Z","iopub.status.idle":"2023-10-22T11:45:04.028047Z","shell.execute_reply.started":"2023-10-22T11:45:03.996329Z","shell.execute_reply":"2023-10-22T11:45:04.026634Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"events.sort_values(\"onset_time\")","metadata":{"execution":{"iopub.status.busy":"2023-10-22T11:45:07.758802Z","iopub.execute_input":"2023-10-22T11:45:07.760275Z","iopub.status.idle":"2023-10-22T11:45:07.781102Z","shell.execute_reply.started":"2023-10-22T11:45:07.760203Z","shell.execute_reply":"2023-10-22T11:45:07.779923Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"unique_ids = events.series_id.unique()\n# print(unique_ids)\nfor series in unique_ids[:5]:\n    events_na = events[events.series_id == series].dropna()\n    print(f\"Found {len(events_na)} logged nights for {series}\")\n    plt.bar(events_na.night, events_na.duration, bottom=events_na.onset_time, alpha=0.5)","metadata":{"execution":{"iopub.status.busy":"2023-10-22T11:45:09.666656Z","iopub.execute_input":"2023-10-22T11:45:09.667061Z","iopub.status.idle":"2023-10-22T11:45:09.960102Z","shell.execute_reply.started":"2023-10-22T11:45:09.667036Z","shell.execute_reply":"2023-10-22T11:45:09.958320Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.hist(events.onset_time, bins=100)\nplt.axvline(24, color=\"gray\")","metadata":{"execution":{"iopub.status.busy":"2023-10-22T11:47:15.831566Z","iopub.execute_input":"2023-10-22T11:47:15.831991Z","iopub.status.idle":"2023-10-22T11:47:16.202157Z","shell.execute_reply.started":"2023-10-22T11:47:15.831960Z","shell.execute_reply":"2023-10-22T11:47:16.200685Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.hist(events.duration, bins=100)\nplt.axvline(events.duration.mean(), color=\"gray\")","metadata":{"execution":{"iopub.status.busy":"2023-10-22T11:47:34.809649Z","iopub.execute_input":"2023-10-22T11:47:34.810176Z","iopub.status.idle":"2023-10-22T11:47:35.186178Z","shell.execute_reply.started":"2023-10-22T11:47:34.810137Z","shell.execute_reply":"2023-10-22T11:47:35.184595Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.scatter(events.onset_time, events.duration, marker=\"x\", alpha=0.5)","metadata":{"execution":{"iopub.status.busy":"2023-10-22T11:45:12.597271Z","iopub.execute_input":"2023-10-22T11:45:12.598480Z","iopub.status.idle":"2023-10-22T11:45:12.796276Z","shell.execute_reply.started":"2023-10-22T11:45:12.598431Z","shell.execute_reply":"2023-10-22T11:45:12.795337Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def scatter_hist(x, y, ax, ax_histx, ax_histy):\n    # no labels\n    ax_histx.tick_params(axis=\"x\", labelbottom=False)\n    ax_histy.tick_params(axis=\"y\", labelleft=False)\n\n    # the scatter plot:\n    ax.scatter(x, y)\n\n    # now determine nice limits by hand:\n    binwidth = 0.25\n    xymax = max(np.max(np.abs(x)), np.max(np.abs(y)))\n    lim = (int(xymax/binwidth) + 1) * binwidth\n\n    ax_histx.hist(x, bins=50)\n    ax_histy.hist(y, bins=50, orientation='horizontal')","metadata":{"execution":{"iopub.status.busy":"2023-10-22T11:57:43.076060Z","iopub.execute_input":"2023-10-22T11:57:43.076577Z","iopub.status.idle":"2023-10-22T11:57:43.085605Z","shell.execute_reply.started":"2023-10-22T11:57:43.076524Z","shell.execute_reply":"2023-10-22T11:57:43.083932Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig = plt.figure(figsize=(6, 6))\ngs = fig.add_gridspec(2, 2,  width_ratios=(4, 1), height_ratios=(1, 4),\n                      left=0.1, right=0.9, bottom=0.1, top=0.9,\n                      wspace=0.05, hspace=0.05)\nax = fig.add_subplot(gs[1, 0])\nax_histx = fig.add_subplot(gs[0, 0], sharex=ax)\nax_histy = fig.add_subplot(gs[1, 1], sharey=ax)\nscatter_hist(events.onset_time, events.duration, ax, ax_histx, ax_histy)","metadata":{"execution":{"iopub.status.busy":"2023-10-22T11:57:43.400812Z","iopub.execute_input":"2023-10-22T11:57:43.401314Z","iopub.status.idle":"2023-10-22T11:57:44.003341Z","shell.execute_reply.started":"2023-10-22T11:57:43.401272Z","shell.execute_reply":"2023-10-22T11:57:44.002314Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}