{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceId":53666,"databundleVersionId":6589269,"sourceType":"competition"},{"sourceId":6879418,"sourceType":"datasetVersion","datasetId":3785250},{"sourceId":6955368,"sourceType":"datasetVersion","datasetId":3993054},{"sourceId":7027622,"sourceType":"datasetVersion","datasetId":4041874},{"sourceId":7058618,"sourceType":"datasetVersion","datasetId":4063315},{"sourceId":7072803,"sourceType":"datasetVersion","datasetId":3983955,"isSourceIdPinned":false},{"sourceId":7076676,"sourceType":"datasetVersion","datasetId":4076187},{"sourceId":7083477,"sourceType":"datasetVersion","datasetId":4080830},{"sourceId":7110296,"sourceType":"datasetVersion","datasetId":4099650},{"sourceId":7116882,"sourceType":"datasetVersion","datasetId":4104401},{"sourceId":7124391,"sourceType":"datasetVersion","datasetId":4109703},{"sourceId":7130911,"sourceType":"datasetVersion","datasetId":4108642},{"sourceId":144358840,"sourceType":"kernelVersion"},{"sourceId":149254949,"sourceType":"kernelVersion"}],"dockerImageVersionId":30559,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Install additional libraries","metadata":{}},{"cell_type":"code","source":"!pip install hydra-core segmentation_models_pytorch==0.3.3 --no-index --find-links=/kaggle/input/ex-library","metadata":{"execution":{"iopub.status.busy":"2023-12-04T22:11:12.853046Z","iopub.execute_input":"2023-12-04T22:11:12.853382Z","iopub.status.idle":"2023-12-04T22:11:25.086842Z","shell.execute_reply.started":"2023-12-04T22:11:12.853346Z","shell.execute_reply":"2023-12-04T22:11:25.085633Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%cd /kaggle/input/prectime-inference","metadata":{"execution":{"iopub.status.busy":"2023-12-04T22:11:25.092368Z","iopub.execute_input":"2023-12-04T22:11:25.09268Z","iopub.status.idle":"2023-12-04T22:11:25.100087Z","shell.execute_reply.started":"2023-12-04T22:11:25.092649Z","shell.execute_reply":"2023-12-04T22:11:25.099109Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Preprocess","metadata":{}},{"cell_type":"code","source":"!python -m run.prepare_data_prectime dir=kaggle phase=test","metadata":{"execution":{"iopub.status.busy":"2023-12-04T22:11:25.101157Z","iopub.execute_input":"2023-12-04T22:11:25.101425Z","iopub.status.idle":"2023-12-04T22:11:27.368249Z","shell.execute_reply.started":"2023-12-04T22:11:25.101401Z","shell.execute_reply":"2023-12-04T22:11:27.367219Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Inference","metadata":{}},{"cell_type":"code","source":"%cd /kaggle/input/inference-prectime-transformers","metadata":{"execution":{"iopub.status.busy":"2023-12-04T22:11:27.371886Z","iopub.execute_input":"2023-12-04T22:11:27.372258Z","iopub.status.idle":"2023-12-04T22:11:27.379473Z","shell.execute_reply.started":"2023-12-04T22:11:27.372228Z","shell.execute_reply":"2023-12-04T22:11:27.378422Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"file_list = [\"/kaggle/input/prectime-kfold2/kfold2/fold0\",\n             \"/kaggle/input/prectime-kfold2/kfold2/fold1\",\n             \"/kaggle/input/prectime-kfold2/kfold2/fold2\",\n             \"/kaggle/input/prectime-kfold2/kfold2/fold3\",\n             \"/kaggle/input/prectime-kfold2/kfold2/fold4\"]","metadata":{"execution":{"iopub.status.busy":"2023-12-04T22:11:27.406016Z","iopub.execute_input":"2023-12-04T22:11:27.406343Z","iopub.status.idle":"2023-12-04T22:11:27.414897Z","shell.execute_reply.started":"2023-12-04T22:11:27.406318Z","shell.execute_reply":"2023-12-04T22:11:27.41402Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nfor file in file_list:\n    mp = os.path.join(file, \"best_model_weights.pth\")\n    cp = os.path.join(file, \"best_cfg.pkl\")\n    c = f\"python -m run.inference_prectime dir=kaggle phase=test weight_dir={mp} config_dir={cp} post_process.gap=120 post_process.quantile=0.7 overlap_interval=720 chunks=480\"\n    os.system(c)","metadata":{"execution":{"iopub.status.busy":"2023-12-04T22:11:27.416052Z","iopub.execute_input":"2023-12-04T22:11:27.416319Z","iopub.status.idle":"2023-12-04T22:12:56.979778Z","shell.execute_reply.started":"2023-12-04T22:11:27.416295Z","shell.execute_reply":"2023-12-04T22:12:56.97878Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%cd /kaggle/input/inference-code-sleep/inference-code-sleep","metadata":{"execution":{"iopub.status.busy":"2023-12-04T22:12:56.981043Z","iopub.execute_input":"2023-12-04T22:12:56.981333Z","iopub.status.idle":"2023-12-04T22:12:56.989861Z","shell.execute_reply.started":"2023-12-04T22:12:56.981308Z","shell.execute_reply":"2023-12-04T22:12:56.988904Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# file_list = [\"/kaggle/input/lstm-cv-5-unet-101-1126/kaggle-sleep-sweep/00i4hw45\",\n#              \"/kaggle/input/lstm-cv-5-unet-101-1126/kaggle-sleep-sweep/018yy18b\",\n#             \"/kaggle/input/lstm-cv-5-unet-101-1126/kaggle-sleep-sweep/0ewchaaa\",\n#             \"/kaggle/input/lstm-cv-5-unet-101-1126/kaggle-sleep-sweep/263gwqp5\",\n#             \"/kaggle/input/lstm-cv-5-unet-101-1126/kaggle-sleep-sweep/qxzgheul\"]\n\nfile_list = [\"/kaggle/input/lstm-cv-5-unet-101-1126/kaggle-sleep-sweep/00i4hw45\"]","metadata":{"execution":{"iopub.status.busy":"2023-12-04T22:12:56.9911Z","iopub.execute_input":"2023-12-04T22:12:56.991389Z","iopub.status.idle":"2023-12-04T22:12:57.001666Z","shell.execute_reply.started":"2023-12-04T22:12:56.991364Z","shell.execute_reply":"2023-12-04T22:12:57.00089Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nfor file in file_list:\n    mp = os.path.join(file, \"model_weights.pth\")\n    cp = os.path.join(file, \"cfg.pkl\")\n    c = f\"python -m run.inference dir=kaggle phase=test weight_dir={mp} config_dir={cp} post_process.gap=120 post_process.quantile=0.7\"\n    os.system(c)","metadata":{"execution":{"iopub.status.busy":"2023-12-04T22:12:57.002799Z","iopub.execute_input":"2023-12-04T22:12:57.003047Z","iopub.status.idle":"2023-12-04T22:14:33.660317Z","shell.execute_reply.started":"2023-12-04T22:12:57.003024Z","shell.execute_reply":"2023-12-04T22:14:33.659257Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%cd /kaggle/input/add-pp-code","metadata":{"execution":{"iopub.status.busy":"2023-12-04T22:14:33.661662Z","iopub.execute_input":"2023-12-04T22:14:33.661972Z","iopub.status.idle":"2023-12-04T22:14:33.668012Z","shell.execute_reply.started":"2023-12-04T22:14:33.661945Z","shell.execute_reply":"2023-12-04T22:14:33.667048Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!python -m run.prepare_data dir=kaggle phase=test","metadata":{"execution":{"iopub.status.busy":"2023-12-04T22:14:33.672624Z","iopub.execute_input":"2023-12-04T22:14:33.672874Z","iopub.status.idle":"2023-12-04T22:14:35.854677Z","shell.execute_reply.started":"2023-12-04T22:14:33.672852Z","shell.execute_reply":"2023-12-04T22:14:35.853583Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%cd /kaggle/input/inference-code-for-old-models","metadata":{"execution":{"iopub.status.busy":"2023-12-04T22:14:35.856014Z","iopub.execute_input":"2023-12-04T22:14:35.856307Z","iopub.status.idle":"2023-12-04T22:14:35.862822Z","shell.execute_reply.started":"2023-12-04T22:14:35.856281Z","shell.execute_reply":"2023-12-04T22:14:35.861896Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!python -m run.inference\\\n    dir=kaggle\\\n    model.encoder_name=resnet34\\\n    model.encoder_weights=null\\\n    phase=test\\\n    weight_dir=/kaggle/input/sweep-11-22/2023_11_22/trans/kaggle-sleep-sweep/5gg69gnz/model_weights.pth\\\n    config_dir=/kaggle/input/sweep-11-22/2023_11_22/trans/kaggle-sleep-sweep/5gg69gnz/cfg.pkl\\\n    batch_size=8\\\n    post_process.gap=120\\\n    post_process.quantile=0.7\\\n    suffix=siri1\\\n    duration=11520\\\n    overlap_interval=768","metadata":{"execution":{"iopub.status.busy":"2023-12-04T22:14:35.864232Z","iopub.execute_input":"2023-12-04T22:14:35.864604Z","iopub.status.idle":"2023-12-04T22:14:55.107936Z","shell.execute_reply.started":"2023-12-04T22:14:35.864549Z","shell.execute_reply":"2023-12-04T22:14:55.106828Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!python -m run.inference\\\n    dir=kaggle\\\n    model.encoder_name=resnet34\\\n    model.encoder_weights=null\\\n    phase=test\\\n    weight_dir=/kaggle/input/sweep-11-22/2023_11_22/trans/kaggle-sleep-sweep/wmttopsg/model_weights.pth\\\n    config_dir=/kaggle/input/sweep-11-22/2023_11_22/trans/kaggle-sleep-sweep/wmttopsg/cfg.pkl\\\n    batch_size=8\\\n    post_process.gap=120\\\n    post_process.quantile=0.7\\\n    suffix=siri2\\\n    duration=11520\\\n    overlap_interval=768","metadata":{"execution":{"iopub.status.busy":"2023-12-04T22:14:55.109608Z","iopub.execute_input":"2023-12-04T22:14:55.109972Z","iopub.status.idle":"2023-12-04T22:15:16.13224Z","shell.execute_reply.started":"2023-12-04T22:14:55.109933Z","shell.execute_reply":"2023-12-04T22:15:16.131129Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!python -m run.inference\\\n    dir=kaggle\\\n    model.encoder_name=resnet34\\\n    model.encoder_weights=null\\\n    phase=test\\\n    weight_dir=/kaggle/input/sweep-11-22/2023_11_22/fe/kaggle-sleep-sweep/mv4p23yd/model_weights.pth\\\n    config_dir=/kaggle/input/sweep-11-22/2023_11_22/fe/kaggle-sleep-sweep/mv4p23yd/cfg.pkl\\\n    batch_size=8\\\n    post_process.gap=120\\\n    post_process.quantile=0.7\\\n    suffix=siri3\\\n    duration=11520\\\n    overlap_interval=768","metadata":{"execution":{"iopub.status.busy":"2023-12-04T22:15:16.133625Z","iopub.execute_input":"2023-12-04T22:15:16.133914Z","iopub.status.idle":"2023-12-04T22:15:37.668755Z","shell.execute_reply.started":"2023-12-04T22:15:16.133888Z","shell.execute_reply":"2023-12-04T22:15:37.667632Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!python -m run.inference\\\n    dir=kaggle\\\n    model.encoder_name=resnet34\\\n    model.encoder_weights=null\\\n    phase=test\\\n    weight_dir=/kaggle/input/sweep-11-22/2023_11_22/fe/kaggle-sleep-sweep/w4kbnbo7/model_weights.pth\\\n    config_dir=/kaggle/input/sweep-11-22/2023_11_22/fe/kaggle-sleep-sweep/w4kbnbo7/cfg.pkl\\\n    batch_size=8\\\n    post_process.gap=120\\\n    post_process.quantile=0.7\\\n    suffix=siri4\\\n    duration=11520\\\n    overlap_interval=768","metadata":{"execution":{"iopub.status.busy":"2023-12-04T22:15:37.670183Z","iopub.execute_input":"2023-12-04T22:15:37.670463Z","iopub.status.idle":"2023-12-04T22:15:59.168869Z","shell.execute_reply.started":"2023-12-04T22:15:37.670439Z","shell.execute_reply":"2023-12-04T22:15:59.167622Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!python -m run.inference\\\n    dir=kaggle\\\n    model.encoder_name=resnet34\\\n    model.encoder_weights=null\\\n    phase=test\\\n    weight_dir=/kaggle/input/sweep-11-22/2023_11_22/decoder/kaggle-sleep-sweep/e8i84w46/model_weights.pth\\\n    config_dir=/kaggle/input/sweep-11-22/2023_11_22/decoder/kaggle-sleep-sweep/e8i84w46/cfg.pkl\\\n    batch_size=8\\\n    post_process.gap=120\\\n    post_process.quantile=0.7\\\n    suffix=siri5\\\n    duration=11520\\\n    overlap_interval=768","metadata":{"execution":{"iopub.status.busy":"2023-12-04T22:15:59.170373Z","iopub.execute_input":"2023-12-04T22:15:59.170707Z","iopub.status.idle":"2023-12-04T22:16:20.636215Z","shell.execute_reply.started":"2023-12-04T22:15:59.170678Z","shell.execute_reply":"2023-12-04T22:16:20.635153Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!python -m run.inference\\\n    dir=kaggle\\\n    model.encoder_name=resnet34\\\n    model.encoder_weights=null\\\n    phase=test\\\n    weight_dir=/kaggle/input/sweep-11-22/2023_11_22/duration/kaggle-sleep-sweep/0u5fm2zs/model_weights.pth\\\n    config_dir=/kaggle/input/sweep-11-22/2023_11_22/duration/kaggle-sleep-sweep/0u5fm2zs/cfg.pkl\\\n    batch_size=8\\\n    post_process.gap=120\\\n    post_process.quantile=0.7\\\n    suffix=siri6\\\n    duration=11520\\\n    overlap_interval=768","metadata":{"execution":{"iopub.status.busy":"2023-12-04T22:16:20.637632Z","iopub.execute_input":"2023-12-04T22:16:20.637946Z","iopub.status.idle":"2023-12-04T22:16:41.57952Z","shell.execute_reply.started":"2023-12-04T22:16:20.637917Z","shell.execute_reply":"2023-12-04T22:16:41.578416Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!python -m run.inference\\\n    dir=kaggle\\\n    model.encoder_name=resnet34\\\n    model.encoder_weights=null\\\n    phase=test\\\n    weight_dir=/kaggle/input/sweep-11-22/2023_11_22/decoder/kaggle-sleep-sweep/8ovh9ysb/model_weights.pth\\\n    config_dir=/kaggle/input/sweep-11-22/2023_11_22/decoder/kaggle-sleep-sweep/8ovh9ysb/cfg.pkl\\\n    batch_size=8\\\n    post_process.gap=120\\\n    post_process.quantile=0.7\\\n    suffix=siri7\\\n    duration=11520\\\n    overlap_interval=768","metadata":{"execution":{"iopub.status.busy":"2023-12-04T22:16:41.581208Z","iopub.execute_input":"2023-12-04T22:16:41.581549Z","iopub.status.idle":"2023-12-04T22:17:02.496847Z","shell.execute_reply.started":"2023-12-04T22:16:41.581519Z","shell.execute_reply":"2023-12-04T22:17:02.495829Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!python -m run.inference\\\n    dir=kaggle\\\n    model.encoder_name=resnet34\\\n    model.encoder_weights=null\\\n    phase=test\\\n    weight_dir=/kaggle/input/sweep-11-22/2023_11_22/duration/kaggle-sleep-sweep/ihfpp9ec/model_weights.pth\\\n    config_dir=/kaggle/input/sweep-11-22/2023_11_22/duration/kaggle-sleep-sweep/ihfpp9ec/cfg.pkl\\\n    batch_size=8\\\n    post_process.gap=120\\\n    post_process.quantile=0.7\\\n    suffix=siri8\\\n    duration=17280\\\n    overlap_interval=768","metadata":{"execution":{"iopub.status.busy":"2023-12-04T22:17:02.498175Z","iopub.execute_input":"2023-12-04T22:17:02.498478Z","iopub.status.idle":"2023-12-04T22:17:23.645749Z","shell.execute_reply.started":"2023-12-04T22:17:02.498433Z","shell.execute_reply":"2023-12-04T22:17:23.644755Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!python -m run.inference\\\n    dir=kaggle\\\n    model.encoder_name=resnet34\\\n    model.encoder_weights=null\\\n    phase=test\\\n    weight_dir=/kaggle/input/sweep-11-22/2023_11_22/trans/kaggle-sleep-sweep/qg2tc49r/model_weights.pth\\\n    config_dir=/kaggle/input/sweep-11-22/2023_11_22/trans/kaggle-sleep-sweep/qg2tc49r/cfg.pkl\\\n    batch_size=8\\\n    post_process.gap=120\\\n    post_process.quantile=0.7\\\n    suffix=siri9\\\n    duration=11520\\\n    overlap_interval=768","metadata":{"execution":{"iopub.status.busy":"2023-12-04T22:17:23.647136Z","iopub.execute_input":"2023-12-04T22:17:23.647441Z","iopub.status.idle":"2023-12-04T22:17:45.064176Z","shell.execute_reply.started":"2023-12-04T22:17:23.647413Z","shell.execute_reply":"2023-12-04T22:17:45.063226Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!python -m run.inference\\\n    dir=kaggle\\\n    model.encoder_name=resnet34\\\n    model.encoder_weights=null\\\n    phase=test\\\n    weight_dir=/kaggle/input/sweep-11-22/2023_11_22/trans/kaggle-sleep-sweep/0x1ndifs/model_weights.pth\\\n    config_dir=/kaggle/input/sweep-11-22/2023_11_22/trans/kaggle-sleep-sweep/0x1ndifs/cfg.pkl\\\n    batch_size=8\\\n    post_process.gap=120\\\n    post_process.quantile=0.7\\\n    suffix=siri10\\\n    duration=11520\\\n    overlap_interval=768","metadata":{"execution":{"iopub.status.busy":"2023-12-04T22:17:45.06562Z","iopub.execute_input":"2023-12-04T22:17:45.06591Z","iopub.status.idle":"2023-12-04T22:18:06.752988Z","shell.execute_reply.started":"2023-12-04T22:17:45.065883Z","shell.execute_reply":"2023-12-04T22:18:06.751909Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Merge","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport polars as pl\nimport glob\nfrom tqdm import tqdm","metadata":{"execution":{"iopub.status.busy":"2023-12-04T22:18:06.754433Z","iopub.execute_input":"2023-12-04T22:18:06.754775Z","iopub.status.idle":"2023-12-04T22:18:06.82254Z","shell.execute_reply.started":"2023-12-04T22:18:06.754741Z","shell.execute_reply":"2023-12-04T22:18:06.821827Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%cd /kaggle/working/","metadata":{"execution":{"iopub.status.busy":"2023-12-04T22:18:06.823704Z","iopub.execute_input":"2023-12-04T22:18:06.823957Z","iopub.status.idle":"2023-12-04T22:18:06.829958Z","shell.execute_reply.started":"2023-12-04T22:18:06.823933Z","shell.execute_reply":"2023-12-04T22:18:06.828964Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"keys = glob.glob(\"keys*.npy\")\npreds = glob.glob(\"preds*.npy\")\nkeys.sort()\npreds.sort()","metadata":{"execution":{"iopub.status.busy":"2023-12-04T22:18:06.831117Z","iopub.execute_input":"2023-12-04T22:18:06.831405Z","iopub.status.idle":"2023-12-04T22:18:06.840277Z","shell.execute_reply.started":"2023-12-04T22:18:06.83138Z","shell.execute_reply":"2023-12-04T22:18:06.839316Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"keys","metadata":{"execution":{"iopub.status.busy":"2023-12-04T22:18:06.84138Z","iopub.execute_input":"2023-12-04T22:18:06.841655Z","iopub.status.idle":"2023-12-04T22:18:06.852Z","shell.execute_reply.started":"2023-12-04T22:18:06.84163Z","shell.execute_reply":"2023-12-04T22:18:06.850986Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_pred_df(\n    keys: list[str], preds: np.ndarray\n)-> pl.DataFrame:\n    \"\"\"make submission dataframe for segmentation task\n\n    Args:\n        keys (list[str]): list of keys. key is \"{series_id}_{chunk_id}\"\n        preds (np.ndarray): (num_series * num_chunks, duration, 2)\n\n    Returns:\n        pl.DataFrame: submission dataframe\n    \"\"\"\n    series_ids = np.array(list(map(lambda x: x.split(\"_\")[0], keys)))\n    unique_series_ids = np.unique(series_ids)\n    \n    hm = {}\n    for series_id in unique_series_ids:\n        series_idx = np.where(series_ids == series_id)[0]\n        this_series_preds = preds[series_idx].reshape(-1, 2)\n\n        for i, event_name in enumerate([\"onset\", \"wakeup\"]):\n            tmp_df = []\n            this_event_preds = this_series_preds[:, i]\n            if i == 0:\n                hm[series_id] = {}\n            hm[series_id][event_name] = this_event_preds \n    return hm","metadata":{"execution":{"iopub.status.busy":"2023-12-04T22:18:06.853165Z","iopub.execute_input":"2023-12-04T22:18:06.853884Z","iopub.status.idle":"2023-12-04T22:18:06.862582Z","shell.execute_reply.started":"2023-12-04T22:18:06.853848Z","shell.execute_reply":"2023-12-04T22:18:06.861829Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_results_slide_window(pred, gap):\n    scores = list(pred)\n    stack = [0]\n    dp = [-1] * len(scores)\n    dp[0] = 0\n    for i in range(1,len(scores)):\n        if i - stack[-1] < gap:\n            if scores[i] >= scores[stack[-1]]:\n                stack.pop()\n                if i - gap >= 0:\n                    if stack:\n                        if dp[i - gap] != stack[-1]:\n                            while stack and dp[i - gap] - stack[-1] < gap:\n                                stack.pop()\n                            stack.append(dp[i - gap])\n                    else:\n                        stack.append(dp[i - gap])\n                stack.append(i)\n        else:\n            stack.append(i)\n        dp[i] = stack[-1]\n    return stack\n\ndef get_submission(preds,gap,quantile):\n    records = []\n    for series_id in preds[0].keys():\n        for i, event_name in enumerate([\"onset\", \"wakeup\"]):\n            mx_dim = 0\n            for p in preds:\n                tmp_p = p[series_id][event_name]\n                if tmp_p.shape[0] > mx_dim:\n                    mx_dim = tmp_p.shape[0]\n            tmp_df = 0\n            for p in preds:\n                tmp_p = p[series_id][event_name]\n                tmp_df += np.pad(tmp_p, (0, mx_dim - tmp_p.shape[0]), \"constant\")\n\n            steps = get_results_slide_window(tmp_df, gap)\n            scores = tmp_df[steps]\n            tmp_df = []\n            \n            for step, score in zip(steps, scores):\n                tmp_df.append(\n                    {\n                        \"series_id\": series_id,\n                        \"step\": step,\n                        \"event\": event_name,\n                        \"score\": score,\n                    }\n                )\n            tmp_df = pl.DataFrame(tmp_df)\n            tmp_df = tmp_df.filter(pl.col(\"score\") > pl.col(\"score\").quantile(quantile))\n            records.append(tmp_df)\n    if len(records) == 0:\n        records.append(\n            pl.DataFrame({\n                \"series_id\": series_id,\n                \"step\": 0,\n                \"event\": \"onset\",\n                \"score\": 0,\n            })\n        )\n    sub_df = pl.concat(records)\n    row_ids = pl.Series(name=\"row_id\", values=np.arange(len(sub_df)))\n    sub_df = sub_df.with_columns(row_ids).select([\"row_id\", \"series_id\", \"step\", \"event\", \"score\"])\n    return sub_df","metadata":{"execution":{"iopub.status.busy":"2023-12-04T22:18:06.863999Z","iopub.execute_input":"2023-12-04T22:18:06.864337Z","iopub.status.idle":"2023-12-04T22:18:06.881208Z","shell.execute_reply.started":"2023-12-04T22:18:06.864305Z","shell.execute_reply":"2023-12-04T22:18:06.880404Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = []\nfor k, p in tqdm(zip(keys, preds), total = len(keys)):\n    k = np.load(k)\n    p = np.load(p)\n    predictions.append(get_pred_df(k, p[:, :, [1, 2]]))\nsub_df = get_submission(predictions, 120, 0.7)","metadata":{"execution":{"iopub.status.busy":"2023-12-04T22:18:06.882313Z","iopub.execute_input":"2023-12-04T22:18:06.882631Z","iopub.status.idle":"2023-12-04T22:18:07.099426Z","shell.execute_reply.started":"2023-12-04T22:18:06.882595Z","shell.execute_reply":"2023-12-04T22:18:07.098459Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%cd /kaggle/working/","metadata":{"execution":{"iopub.status.busy":"2023-12-04T22:18:07.100477Z","iopub.execute_input":"2023-12-04T22:18:07.10079Z","iopub.status.idle":"2023-12-04T22:18:07.106777Z","shell.execute_reply.started":"2023-12-04T22:18:07.100763Z","shell.execute_reply":"2023-12-04T22:18:07.105635Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub_df.write_csv(\"./submission.csv\")","metadata":{"execution":{"iopub.status.busy":"2023-12-04T22:18:07.107862Z","iopub.execute_input":"2023-12-04T22:18:07.10818Z","iopub.status.idle":"2023-12-04T22:18:07.118244Z","shell.execute_reply.started":"2023-12-04T22:18:07.108133Z","shell.execute_reply":"2023-12-04T22:18:07.117341Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!rm -rf /kaggle/working/processed_data\n!rm -rf /kaggle/working/output","metadata":{"execution":{"iopub.status.busy":"2023-12-04T22:18:07.121142Z","iopub.execute_input":"2023-12-04T22:18:07.121438Z","iopub.status.idle":"2023-12-04T22:18:09.095771Z","shell.execute_reply.started":"2023-12-04T22:18:07.121412Z","shell.execute_reply":"2023-12-04T22:18:09.094321Z"},"trusted":true},"execution_count":null,"outputs":[]}]}