{"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":7088190,"sourceType":"datasetVersion","datasetId":4084152},{"sourceId":7094190,"sourceType":"datasetVersion","datasetId":4088468}],"dockerImageVersionId":30588,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport gc\nimport time\nimport json\nfrom datetime import datetime\nimport matplotlib.pyplot as plt\nimport os, glob\nimport joblib\nimport random\nimport math\nfrom tqdm import tqdm \nfrom collections import OrderedDict\n\nfrom scipy.interpolate import interp1d\nfrom scipy import signal\nfrom scipy.signal import argrelmax\n\nfrom sklearn.metrics import mean_squared_error\n\nfrom math import pi, sqrt, exp\nimport sklearn,sklearn.model_selection\nimport torch\nfrom torch import nn,Tensor\nimport torch.nn.functional as F\nfrom torch.utils.data import DataLoader, Dataset, SubsetRandomSampler\nfrom torch.optim import AdamW\nfrom sklearn.metrics import average_precision_score\nfrom timm.scheduler import CosineLRScheduler\n\nfrom transformers import get_cosine_schedule_with_warmup\n\nplt.style.use(\"ggplot\")\n\nfrom pyarrow.parquet import ParquetFile\nimport pyarrow as pa \nimport ctypes","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-11-30T22:34:59.410184Z","iopub.execute_input":"2023-11-30T22:34:59.410664Z","iopub.status.idle":"2023-11-30T22:35:07.805865Z","shell.execute_reply.started":"2023-11-30T22:34:59.410619Z","shell.execute_reply":"2023-11-30T22:35:07.805034Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import warnings\nwarnings.filterwarnings('ignore')\n","metadata":{"execution":{"iopub.status.busy":"2023-11-30T22:35:07.807513Z","iopub.execute_input":"2023-11-30T22:35:07.809217Z","iopub.status.idle":"2023-11-30T22:35:07.814411Z","shell.execute_reply.started":"2023-11-30T22:35:07.809162Z","shell.execute_reply":"2023-11-30T22:35:07.813342Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Fundamental config\nNOTDEBUG = True # False -> DEBUG, True -> normally train\nWORKERS = os.cpu_count() // 2\nN_FOLDS = 5\nTRAIN_FOLD = 0\nMAX_LEN = 17280\nUSE_AMP = False\nSEED = 86\nSIGMA = 720 #average length of day is 24*60*12 = 17280 for comparison\nSAMPLE_FREQ = 1 # 1 obs per minute\nMAX_CHUNK_LEN = MAX_LEN // SAMPLE_FREQ # 1440pts = 1day\n","metadata":{"execution":{"iopub.status.busy":"2023-11-30T22:35:07.815709Z","iopub.execute_input":"2023-11-30T22:35:07.815985Z","iopub.status.idle":"2023-11-30T22:35:07.838403Z","shell.execute_reply.started":"2023-11-30T22:35:07.815959Z","shell.execute_reply":"2023-11-30T22:35:07.837519Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Model config\nHIDDEN = 256 if NOTDEBUG else 16\nEMB_DIM = 16\nKS = 31 if NOTDEBUG else 7\nN_BLKS = 5 if NOTDEBUG else 2\nDROPOUT = 0.2\nMODEL_PATH = '/kaggle/input/private/model_best_fold-0.pth'","metadata":{"execution":{"iopub.status.busy":"2023-11-30T22:35:07.839806Z","iopub.execute_input":"2023-11-30T22:35:07.840199Z","iopub.status.idle":"2023-11-30T22:35:07.848258Z","shell.execute_reply.started":"2023-11-30T22:35:07.840168Z","shell.execute_reply":"2023-11-30T22:35:07.847328Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def torch_fix_seed(seed=42):\n    # Python random\n    random.seed(seed)\n    # Numpy\n    np.random.seed(seed)\n    # Pytorch\n    torch.manual_seed(seed)\n    torch.cuda.manual_seed(seed)\n    # torch.backends.cudnn.deterministic = True\n    # torch.use_deterministic_algorithms = True\n    # torch.backends.cudnn.benchmark = True\n\ntorch_fix_seed(SEED)","metadata":{"execution":{"iopub.status.busy":"2023-11-30T22:35:07.850615Z","iopub.execute_input":"2023-11-30T22:35:07.850916Z","iopub.status.idle":"2023-11-30T22:35:07.863536Z","shell.execute_reply.started":"2023-11-30T22:35:07.850893Z","shell.execute_reply":"2023-11-30T22:35:07.862642Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"device = 'cuda' if torch.cuda.is_available() else 'cpu'\ndevice","metadata":{"execution":{"iopub.status.busy":"2023-11-30T22:35:07.864668Z","iopub.execute_input":"2023-11-30T22:35:07.865002Z","iopub.status.idle":"2023-11-30T22:35:07.948206Z","shell.execute_reply.started":"2023-11-30T22:35:07.864976Z","shell.execute_reply":"2023-11-30T22:35:07.947308Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class PATHS:\n    MAIN_DIR = \"/kaggle/input/child-mind-institute-detect-sleep-states/\"\n    # CSV FILES : \n    SUBMISSION = MAIN_DIR + \"sample_submission.csv\"\n    TRAIN_EVENTS = MAIN_DIR + \"train_events.csv\"\n    # PARQUET FILES:\n    TRAIN_SERIES = MAIN_DIR + \"train_series.parquet\"\n    TEST_SERIES = MAIN_DIR + \"test_series.parquet\"\nclass CFG:\n    DEMO_MODE = True\nclass data_reader:\n    def __init__(self, demo_mode):\n        super().__init__()\n        # MAPPING FOR DATA LOADING :\n        self.names_mapping = {\n            \"submission\" : {\"path\" : PATHS.SUBMISSION, \"is_parquet\" : False, \"has_timestamp\" : False}, \n            \"train_events\" : {\"path\" : PATHS.TRAIN_EVENTS, \"is_parquet\" : False, \"has_timestamp\" : True},\n            \"train_series\" : {\"path\" : PATHS.TRAIN_SERIES, \"is_parquet\" : True, \"has_timestamp\" : True},\n            \"test_series\" : {\"path\" : PATHS.TEST_SERIES, \"is_parquet\" : True, \"has_timestamp\" : True}\n        }\n        self.valid_names = [\"submission\", \"train_events\", \"train_series\", \"test_series\"]\n        self.demo_mode = demo_mode\n    \n    def verify(self, data_name):\n        \"function for data name verification\"\n        if data_name not in self.valid_names:\n            print(\"PLEASE ENTER A VALID DATASET NAME, VALID NAMES ARE : \", valid_names)\n        return\n    \n    def cleaning(self, data):\n        \"cleaning function : drop na values\"\n        before_cleaning = len(data)\n        print(\"Number of missing timestamps : \", len(data[data[\"timestamp\"].isna()]))\n        data = data.dropna(subset=[\"timestamp\"])\n        after_cleaning = len(data)\n        print(\"Percentage of removed rows : {:.1f}%\".format(100 * (before_cleaning - after_cleaning) / before_cleaning) )\n#         print(data.isna().any())\n#         data = data.bfill()\n        return data\n    \n    @staticmethod\n    def reduce_memory_usage(data):\n        \"iterate through all the columns of a dataframe and modify the data type to reduce memory usage.\"\n        start_mem = data.memory_usage().sum() / 1024**2\n        print('Memory usage of dataframe is {:.2f} MB'.format(start_mem))\n        for col in data.columns:\n            col_type = data[col].dtype    \n            if col_type != object:\n                c_min = data[col].min()\n                c_max = data[col].max()\n                if str(col_type)[:3] == 'int':\n                    if c_min > np.iinfo(np.int8).min and c_max < np.iinfo(np.int8).max:\n                        data[col] = data[col].astype(np.int8)\n                    elif c_min > np.iinfo(np.int16).min and c_max < np.iinfo(np.int16).max:\n                        data[col] = data[col].astype(np.int16)\n                    elif c_min > np.iinfo(np.int32).min and c_max < np.iinfo(np.int32).max:\n                        data[col] = data[col].astype(np.int32)\n                    elif c_min > np.iinfo(np.int64).min and c_max < np.iinfo(np.int64).max:\n                        data[col] = data[col].astype(np.int64)  \n                else:\n                    if c_min > np.finfo(np.float16).min and c_max < np.finfo(np.float16).max:\n                        data[col] = data[col].astype(np.float16)\n                    elif c_min > np.finfo(np.float32).min and c_max < np.finfo(np.float32).max:\n                        data[col] = data[col].astype(np.float32)\n                    else:\n                        data[col] = data[col].astype(np.float64)\n            else:\n                data[col] = data[col].astype('category')\n\n        end_mem = data.memory_usage().sum() / 1024**2\n        print('Memory usage after optimization is: {:.2f} MB'.format(end_mem))\n        print('Decreased by {:.1f}%'.format(100 * (start_mem - end_mem) / start_mem))\n        return data\n    \n    def load_data(self, data_name):\n        \"function for data loading\"\n        self.verify(data_name)\n        data_props = self.names_mapping[data_name]\n        if data_props[\"is_parquet\"]:\n            if self.demo_mode:\n                pf = ParquetFile(data_props[\"path\"]) \n                demo_rows = next(pf.iter_batches(batch_size=20_000)) \n                data = pa.Table.from_batches([demo_rows]).to_pandas()\n            else:\n                data = pd.read_parquet(data_props[\"path\"])\n        else:\n            if self.demo_mode:\n                data = pd.read_csv(data_props[\"path\"], nrows=20_000)\n            else:\n                data = pd.read_csv(data_props[\"path\"])\n                \n        gc.collect()\n        if data_props[\"has_timestamp\"]:\n            print('cleaning')\n            data = self.cleaning(data)\n            gc.collect()\n        #data = self.reduce_memory_usage(data)\n        return data","metadata":{"execution":{"iopub.status.busy":"2023-11-30T22:35:07.949800Z","iopub.execute_input":"2023-11-30T22:35:07.950161Z","iopub.status.idle":"2023-11-30T22:35:07.977343Z","shell.execute_reply.started":"2023-11-30T22:35:07.950133Z","shell.execute_reply":"2023-11-30T22:35:07.976522Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"reader = data_reader(demo_mode=False)\ntest_series = reader.load_data(data_name=\"test_series\")\nids = test_series.series_id.unique()\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2023-11-30T22:35:07.978344Z","iopub.execute_input":"2023-11-30T22:35:07.978634Z","iopub.status.idle":"2023-11-30T22:35:08.560511Z","shell.execute_reply.started":"2023-11-30T22:35:07.978609Z","shell.execute_reply":"2023-11-30T22:35:08.559553Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def seq_block(in_features, out_features, ks=3, drop_rate=0.2, dilation=1):\n    padding = ((ks-1)*dilation)//2\n    return nn.Sequential(\n        nn.Conv1d(in_features, out_features, ks, padding=padding, dilation=dilation),\n        nn.BatchNorm1d(out_features),\n        nn.LeakyReLU(0.2),\n        nn.Dropout(drop_rate)\n    )\n\nclass SimpleConvNet(nn.Module):\n    def __init__(self, in_c=2, out_c=2, hidden=128, emb_dim=8, ks=3, n_blks=3, dropout=0.2):\n        super(SimpleConvNet, self).__init__()\n        \n        self.hr_emb = nn.Embedding(24, emb_dim)\n        self.fc1_hr = nn.Linear(emb_dim, emb_dim)\n        self.fc2_hr = nn.Linear(emb_dim, emb_dim)\n        \n        self.fc_in = nn.Linear(in_c+emb_dim, hidden)\n        self.blks = nn.Sequential(\n            *[seq_block(hidden, hidden, ks, drop_rate=dropout, dilation=2**i) for i in range(n_blks)]\n            )\n        self.fc_out = nn.Linear(hidden, out_c)\n        \n        self.lrelu = nn.LeakyReLU(0.2)\n        \n    def forward(self, x, h):        \n        e = self.hr_emb(h)\n        e = self.fc1_hr(e)\n        e = self.lrelu(e)\n        e = self.fc2_hr(e)\n\n        x = torch.cat([x, e.squeeze(2)], dim=-1)\n        x = self.fc_in(x)\n        \n        x = x.permute(0,2,1)\n        \n        for b in self.blks:\n            x = b(x)\n\n        x = x.permute(0,2,1)\n        \n        x = self.fc_out(x)\n        return x","metadata":{"execution":{"iopub.status.busy":"2023-11-30T22:35:08.561954Z","iopub.execute_input":"2023-11-30T22:35:08.562241Z","iopub.status.idle":"2023-11-30T22:35:08.574597Z","shell.execute_reply.started":"2023-11-30T22:35:08.562217Z","shell.execute_reply":"2023-11-30T22:35:08.573548Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class SleepTestDataset(Dataset):\n    def __init__(\n        self,\n        test_series,\n        test_ids\n    ):\n        self.enmo_mean = np.load('/kaggle/input/private/enmo_mean.npy')\n        self.enmo_std = np.load('/kaggle/input/private/enmo_std.npy')\n\n        self.Xs = self.conv_dfs(test_series, test_ids)\n        \n        self.feat_list = np.load('/kaggle/input/private/feature_list.npy')\n        self.label_list = ['onset', 'wakeup']\n        \n        self.hour_feat= ['hour']\n        \n            \n    def conv_dfs(self, series, ids):\n        res = []\n        for j, viz_id in tqdm(enumerate(ids), total=len(ids)):\n            viz_series = series.loc[(series.series_id==viz_id)].copy().reset_index()\n            viz_series['dt'] = pd.to_datetime(viz_series.timestamp,format = '%Y-%m-%dT%H:%M:%S%z').astype(\"datetime64[ns, UTC-04:00]\")\n            viz_series['hour'] = viz_series['dt'].dt.hour\n            new_df = viz_series[['step', 'anglez', 'enmo', 'hour']]\n            res.append(new_df)\n\n        return res\n    \n\n    def norm_feat_eng(self, X, init=False):\n        X['anglez'] = X['anglez'] / 90.0\n        X['enmo'] = (X['enmo'] - self.enmo_mean)/ (self.enmo_std + 1e-12)\n        \n        for w in [1, 2, 4, 8, 16]:    \n            X['anglez_shift_pos_' + str(w)] = X['anglez'].shift(w).fillna(0)\n            X['anglez_shift_neg_' + str(w)] = X['anglez'].shift(-w).fillna(0)\n            \n            X['enmo_shift_pos_' + str(w)] = X['enmo'].shift(w).fillna(0)\n            X['enmo_shift_neg_' + str(w)] = X['enmo'].shift(-w).fillna(0)\n            \n            if init:\n                self.feat_list.append('anglez_shift_pos_' + str(w))\n                self.feat_list.append('anglez_shift_neg_' + str(w))\n                \n                self.feat_list.append('enmo_shift_pos_' + str(w))\n                self.feat_list.append('enmo_shift_neg_' + str(w))\n            \n        for r in [17, 33, 65]:\n            tmp_anglez = X['anglez'].rolling(r, center=True)\n            X[f'anglez_mean_{r}'] = tmp_anglez.mean()\n            X[f'anglez_std_{r}'] = tmp_anglez.std()            \n            \n            tmp_enmo = X['enmo'].rolling(r, center=True)\n            X[f'enmo_mean_{r}'] = tmp_enmo.mean()\n            X[f'enmo_std_{r}'] = tmp_enmo.std()\n            \n            if init:\n                self.feat_list.append(f'anglez_mean_{r}')\n                self.feat_list.append(f'anglez_std_{r}')\n\n                self.feat_list.append(f'enmo_mean_{r}')\n                self.feat_list.append(f'enmo_std_{r}')\n                \n        X = X.fillna(0)\n        \n        return X.astype(np.float32)\n\n    def gauss(self,n=SIGMA,sigma=SIGMA*0.15):\n        # guassian distribution function\n        r = range(-int(n/2),int(n/2)+1)\n        return [1 / (sigma * sqrt(2*pi)) * exp(-float(x)**2/(2*sigma**2)) for x in r]\n    \n    def __len__(self):\n        return len(self.Xs)\n    \n    def compress(self, xt, method='mean', sample_freq=SAMPLE_FREQ):\n        x, t = xt\n        seq_len = x.shape[0]\n        if method == 'mean':\n            x = x.reshape(seq_len//sample_freq, sample_freq, -1).mean(1)\n            t = t.reshape(seq_len//sample_freq, sample_freq, 1).mean(1)\n            \n        else:\n            raise NotImplementedError()\n\n        \n        return x.astype(np.float32), t.astype(np.int32)\n\n    def __getitem__(self, index):\n        X = self.Xs[index].copy()\n        X = self.norm_feat_eng(X, init=False)\n        x = X[self.feat_list].values.astype(np.float32)\n        t = X[self.hour_feat].values.astype(np.int32)\n        \n        if len(X)%SAMPLE_FREQ!=0:\n            res = SAMPLE_FREQ - (len(X)%SAMPLE_FREQ)\n            x = np.pad(x, ((0, res), (0, 0)))\n            t = np.pad(t, ((0, res), (0, 0)))\n            \n        x, t = self.compress((x, t), method='mean')\n                \n        return x, t\n\ntest_ds = SleepTestDataset(test_series, ids)","metadata":{"execution":{"iopub.status.busy":"2023-11-30T22:35:08.576142Z","iopub.execute_input":"2023-11-30T22:35:08.576445Z","iopub.status.idle":"2023-11-30T22:35:08.641951Z","shell.execute_reply.started":"2023-11-30T22:35:08.576419Z","shell.execute_reply":"2023-11-30T22:35:08.640989Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_dl = DataLoader(\n    test_ds,\n    batch_size=1,\n    pin_memory=True,\n    num_workers=WORKERS,\n    shuffle=False,\n    drop_last=False\n)","metadata":{"execution":{"iopub.status.busy":"2023-11-30T22:35:08.643098Z","iopub.execute_input":"2023-11-30T22:35:08.643530Z","iopub.status.idle":"2023-11-30T22:35:08.648193Z","shell.execute_reply.started":"2023-11-30T22:35:08.643497Z","shell.execute_reply":"2023-11-30T22:35:08.647227Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"autocast = torch.cuda.amp.autocast(enabled=True, dtype=torch.half)\nscaler = torch.cuda.amp.GradScaler(enabled=True, init_scale=4096)","metadata":{"execution":{"iopub.status.busy":"2023-11-30T22:35:08.649669Z","iopub.execute_input":"2023-11-30T22:35:08.650075Z","iopub.status.idle":"2023-11-30T22:35:08.658673Z","shell.execute_reply.started":"2023-11-30T22:35:08.650042Z","shell.execute_reply":"2023-11-30T22:35:08.657849Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def lpf(wave, fs=12*60*24, fe=60, n=3):\n    nyq = fs / 2.0\n    b, a = signal.butter(1, fe/nyq, btype='low')\n    for i in range(0, n):\n        wave = signal.filtfilt(b, a, wave)\n    return wave","metadata":{"execution":{"iopub.status.busy":"2023-11-30T22:35:08.659903Z","iopub.execute_input":"2023-11-30T22:35:08.660492Z","iopub.status.idle":"2023-11-30T22:35:08.671603Z","shell.execute_reply.started":"2023-11-30T22:35:08.660460Z","shell.execute_reply":"2023-11-30T22:35:08.670885Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def padding_(x, tgt_len=MAX_LEN):\n    res = tgt_len - (x.size(-2) % tgt_len)\n    x = F.pad(x, (0, 0, 0, res))\n    return x","metadata":{"execution":{"iopub.status.busy":"2023-11-30T22:35:08.675822Z","iopub.execute_input":"2023-11-30T22:35:08.676089Z","iopub.status.idle":"2023-11-30T22:35:08.681045Z","shell.execute_reply.started":"2023-11-30T22:35:08.676056Z","shell.execute_reply":"2023-11-30T22:35:08.680176Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = SimpleConvNet(in_c=len(test_ds.feat_list), out_c=2, hidden=HIDDEN, emb_dim=EMB_DIM, ks=KS, n_blks=N_BLKS, dropout=DROPOUT).to(device)\nmodel.load_state_dict(torch.load(MODEL_PATH, map_location=device))\nmodel = model.to(device)","metadata":{"execution":{"iopub.status.busy":"2023-11-30T22:35:08.682349Z","iopub.execute_input":"2023-11-30T22:35:08.682781Z","iopub.status.idle":"2023-11-30T22:35:12.788639Z","shell.execute_reply.started":"2023-11-30T22:35:08.682744Z","shell.execute_reply":"2023-11-30T22:35:12.787847Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_preds = []\nmodel.eval().half()\nfor fold in range(N_FOLDS):\n    \n    with torch.no_grad():\n        with tqdm(test_dl, leave=True) as pbar:\n            for j, (X_batch, hr_batch) in enumerate(pbar):\n                X_batch = X_batch.to(device)\n                hr_batch = hr_batch.to(device)\n                \n                x_seq_len = X_batch.shape[1]\n                y_pred = torch.zeros((1, x_seq_len*SAMPLE_FREQ, 2)).to(device)\n                y_seq_len = y_pred.shape[1]\n                \n                if x_seq_len%MAX_CHUNK_LEN != 0:\n                    X_batch = padding_(X_batch, MAX_CHUNK_LEN)\n                    hr_batch = padding_(hr_batch, MAX_CHUNK_LEN)\n\n                if y_seq_len%MAX_LEN != 0:\n                    y_pred = padding_(y_pred, MAX_LEN)\n\n                YPOS = 0\n                for i in range(0, x_seq_len, MAX_CHUNK_LEN):\n                    X_chunk = X_batch[:, i : i + MAX_CHUNK_LEN]\n                    hr_chunk = hr_batch[:, i : i + MAX_CHUNK_LEN]\n                    with autocast:\n                        p = model(X_chunk, hr_chunk)\n                        print(X_chunk.shape)\n                        print(p.shape, MAX_CHUNK_LEN)\n                        y_pred[:, YPOS : YPOS + MAX_CHUNK_LEN] = p\n\n                    YPOS += MAX_CHUNK_LEN*SAMPLE_FREQ\n\n                y_pred = y_pred[:, :y_seq_len]\n                    \n                if len(test_dl)==3:\n                    print(X_batch.sum(), X_batch.sum(), y_pred.sum())\n\n                if fold==0:\n                    y_preds.append(y_pred[0].sigmoid().cpu().numpy().astype(np.float32) / N_FOLDS)\n                else:\n                    y_preds[j] += y_pred[0].sigmoid().cpu().numpy().astype(np.float32) / N_FOLDS\n                        \n                \n                del X_batch, hr_batch\n                gc.collect()\n                torch.cuda.empty_cache()\n\n    gc.collect()\n    torch.cuda.empty_cache()","metadata":{"execution":{"iopub.status.busy":"2023-11-30T22:35:12.789750Z","iopub.execute_input":"2023-11-30T22:35:12.790032Z","iopub.status.idle":"2023-11-30T22:35:23.758318Z","shell.execute_reply.started":"2023-11-30T22:35:12.790007Z","shell.execute_reply":"2023-11-30T22:35:23.757155Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.DataFrame()","metadata":{"execution":{"iopub.status.busy":"2023-11-30T22:35:23.760038Z","iopub.execute_input":"2023-11-30T22:35:23.760746Z","iopub.status.idle":"2023-11-30T22:35:23.766645Z","shell.execute_reply.started":"2023-11-30T22:35:23.760690Z","shell.execute_reply":"2023-11-30T22:35:23.765744Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i, y_pred in tqdm(enumerate(y_preds), total=len(y_preds)):\n    before_RMSE = np.sqrt(mean_squared_error(y_pred, np.zeros_like(y_pred)))\n\n    if len(test_ds.Xs)!=3:\n        y_pred[:,0] = lpf(y_pred[:,0])\n        y_pred[:,1] = lpf(y_pred[:,1])\n        \n    else:\n        f, ax = plt.subplots(2)\n        ax[0].plot(y_pred[:,0], lw=0.5)\n        ax[1].plot(y_pred[:,1], lw=0.5)\n        plt.title(ids[i])\n        plt.show()\n\n    after_RMSE = np.sqrt(mean_squared_error(y_pred, np.zeros_like(y_pred)))\n\n    decay_ratio = before_RMSE/after_RMSE\n    y_pred *= decay_ratio\n\n    onset_candi = argrelmax(y_pred[:, 0], order=12*60*12) # 12*60*6 = 6hours\n    wakeup_candi = argrelmax(y_pred[:, 1], order=12*60*12) # 12*60*6 = 6hours\n    \n    series_id = ids[i]\n    try:                \n        onset_candi = onset_candi[0]\n        onset = pd.DataFrame({'series_id': [series_id]*len(onset_candi),\n                                'step': onset_candi,\n                                'score': y_pred[onset_candi, 0].reshape(-1),\n                                'event': ['onset']*len(onset_candi)})\n    except:\n        onset = pd.DataFrame()\n\n    try:\n        wakeup_candi = wakeup_candi[0]\n        wakeup = pd.DataFrame({'series_id': [series_id]*len(wakeup_candi),\n                                'step': wakeup_candi,\n                                'score': y_pred[wakeup_candi, 1].reshape(-1),\n                                'event': ['wakeup']*len(wakeup_candi)})\n    except:\n        wakeup = pd.DataFrame()\n\n    submission = pd.concat([submission, onset, wakeup], axis=0)\n\ngc.collect()\ntorch.cuda.empty_cache()","metadata":{"execution":{"iopub.status.busy":"2023-11-30T22:35:23.768206Z","iopub.execute_input":"2023-11-30T22:35:23.768904Z","iopub.status.idle":"2023-11-30T22:35:26.354450Z","shell.execute_reply.started":"2023-11-30T22:35:23.768875Z","shell.execute_reply":"2023-11-30T22:35:26.353440Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = submission.sort_values(['series_id','step']).reset_index(drop=True)\nsubmission['row_id'] = submission.index.astype(int)\nsubmission['score'] = submission['score'].fillna(submission['score'].mean())\nsubmission = submission[['row_id','series_id','step','event','score']]\nsubmission['step'] = submission['step']\nsubmission.to_csv('submission.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2023-11-30T22:35:26.355832Z","iopub.execute_input":"2023-11-30T22:35:26.356110Z","iopub.status.idle":"2023-11-30T22:35:26.371818Z","shell.execute_reply.started":"2023-11-30T22:35:26.356085Z","shell.execute_reply":"2023-11-30T22:35:26.370731Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission","metadata":{"execution":{"iopub.status.busy":"2023-11-30T22:35:26.373156Z","iopub.execute_input":"2023-11-30T22:35:26.373963Z","iopub.status.idle":"2023-11-30T22:35:26.391152Z","shell.execute_reply.started":"2023-11-30T22:35:26.373923Z","shell.execute_reply":"2023-11-30T22:35:26.390212Z"},"trusted":true},"execution_count":null,"outputs":[]}]}