{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.12.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[],"dockerImageVersionId":28755,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import os, sys, shutil\n\nWORK_CODE_DIR = \"/kaggle/working/sleep-mamba-unet\"\n\nif os.path.exists(WORK_CODE_DIR):\n    shutil.rmtree(WORK_CODE_DIR)","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2026-06-15T17:10:56.398475Z","iopub.execute_input":"2026-06-15T17:10:56.398743Z","iopub.status.idle":"2026-06-15T17:10:56.409281Z","shell.execute_reply.started":"2026-06-15T17:10:56.398710Z","shell.execute_reply":"2026-06-15T17:10:56.408560Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!git clone https://github.com/syqwq-OMG/sleep.git {WORK_CODE_DIR}","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2026-06-15T17:10:56.411117Z","iopub.execute_input":"2026-06-15T17:10:56.411631Z","iopub.status.idle":"2026-06-15T17:10:57.011083Z","shell.execute_reply.started":"2026-06-15T17:10:56.411606Z","shell.execute_reply":"2026-06-15T17:10:57.010077Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"os.chdir(WORK_CODE_DIR + \"/sleep-mamba-unet\")\nsys.path.insert(0, WORK_CODE_DIR)\n\nprint(\"cwd:\", os.getcwd())","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2026-06-15T17:10:57.012558Z","iopub.execute_input":"2026-06-15T17:10:57.012781Z","iopub.status.idle":"2026-06-15T17:10:57.017865Z","shell.execute_reply.started":"2026-06-15T17:10:57.012757Z","shell.execute_reply":"2026-06-15T17:10:57.017049Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os\nfrom pathlib import Path\n\nprint(\"code files:\")\nprint(os.listdir(\".\"))\n\nprint(\"\\nconfigs:\")\nprint(sorted(os.listdir(\"configs\")))\n\nprint(\"\\nkaggle input:\")\nprint(os.listdir(\"/kaggle/input\"))\n\nDATA_DIR = \"/kaggle/input/competitions/child-mind-institute-detect-sleep-states\"\nprint(\"\\ndata files:\")\nprint(os.listdir(DATA_DIR))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-06-15T17:10:57.019173Z","iopub.execute_input":"2026-06-15T17:10:57.019456Z","iopub.status.idle":"2026-06-15T17:10:57.033890Z","shell.execute_reply.started":"2026-06-15T17:10:57.019426Z","shell.execute_reply":"2026-06-15T17:10:57.033001Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# train","metadata":{}},{"cell_type":"code","source":"PRESET = \"medium\"\nFOLD = 0\n\ntrain_cfg = f\"configs/kaggle_{PRESET}.yaml\"\nsubmit_cfg = f\"configs/kaggle_{PRESET}_submit.yaml\"\nckpt = f\"/kaggle/working/outputs/models/fold{FOLD}.pt\"\n\nprint(\"train_cfg:\", train_cfg)\nprint(\"submit_cfg:\", submit_cfg)\nprint(\"ckpt:\", ckpt)\n\n!python -m src.train --config {train_cfg} --fold {FOLD}","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-06-15T17:10:57.035101Z","iopub.execute_input":"2026-06-15T17:10:57.036070Z","iopub.status.idle":"2026-06-15T17:20:00.252105Z","shell.execute_reply.started":"2026-06-15T17:10:57.036032Z","shell.execute_reply":"2026-06-15T17:20:00.251388Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# score","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nfrom src.postprocess import load_config, predictions_to_submission\nfrom src.metric import score_events\n\ncfg = load_config(train_cfg)\n\npred = pd.read_parquet(\"/kaggle/working/outputs/oof_pred.parquet\")\nevents = pd.read_csv(\"/kaggle/input/competitions/child-mind-institute-detect-sleep-states/train_events.csv\")\nevents = events.dropna(subset=[\"step\"])\nevents_val = events[events[\"series_id\"].isin(pred[\"series_id\"].unique())]\n\nval_sub = predictions_to_submission(pred, cfg)\nlocal_score = score_events(events_val, val_sub, cfg[\"postprocess\"][\"tolerances_steps\"])\n\nprint(\"local score:\", local_score)\nprint(val_sub.head())\nprint(val_sub.shape)\nprint(val_sub[\"event\"].value_counts())\nprint(val_sub[\"score\"].describe())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-06-15T17:20:00.253482Z","iopub.execute_input":"2026-06-15T17:20:00.253759Z","iopub.status.idle":"2026-06-15T17:21:41.210143Z","shell.execute_reply.started":"2026-06-15T17:20:00.253732Z","shell.execute_reply":"2026-06-15T17:21:41.209385Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# infer","metadata":{}},{"cell_type":"code","source":"!python -m src.infer --config {submit_cfg} --checkpoints {ckpt} --phase test\n\n!python -m src.postprocess \\\n  --pred /kaggle/working/outputs/test_pred.parquet \\\n  --out /kaggle/working/submission.csv \\\n  --config {submit_cfg}","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-06-15T17:21:41.212133Z","iopub.execute_input":"2026-06-15T17:21:41.212445Z","iopub.status.idle":"2026-06-15T17:21:47.771296Z","shell.execute_reply.started":"2026-06-15T17:21:41.212415Z","shell.execute_reply":"2026-06-15T17:21:47.770539Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sub = pd.read_csv(\"/kaggle/working/submission.csv\")\n\nprint(sub.head())\nprint(sub.shape)\nprint(sub[\"event\"].value_counts())\nprint(sub[\"score\"].describe())\n\nassert list(sub.columns) == [\"row_id\", \"series_id\", \"step\", \"event\", \"score\"]\nassert set(sub[\"event\"]).issubset({\"onset\", \"wakeup\"})\nassert sub[\"score\"].between(0, 1).all()\n\nprint(\"submission ready:\", \"/kaggle/working/submission.csv\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-06-15T17:21:47.772549Z","iopub.execute_input":"2026-06-15T17:21:47.772870Z","iopub.status.idle":"2026-06-15T17:21:47.790303Z","shell.execute_reply.started":"2026-06-15T17:21:47.772841Z","shell.execute_reply":"2026-06-15T17:21:47.789544Z"}},"outputs":[],"execution_count":null}]}