{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":59093,"databundleVersionId":7469972,"sourceType":"competition"},{"sourceId":33291,"sourceType":"modelInstanceVersion","modelInstanceId":27871}],"dockerImageVersionId":30683,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"#import\n\nimport random\nimport cv2\nimport json\nimport copy\nimport torch\nimport gc\nimport os\nimport librosa\nimport pickle\nimport timm\nimport mne\n\nimport numpy as np\nimport albumentations as A\nimport pandas as pd\nfrom tqdm import tqdm\nfrom scipy.signal import butter, lfilter\n\n\nimport torch\nimport torchaudio\nimport torch.nn as nn\nfrom torch.utils.data import DataLoader\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-04-18T09:08:44.457380Z","iopub.execute_input":"2024-04-18T09:08:44.458308Z","iopub.status.idle":"2024-04-18T09:08:44.464408Z","shell.execute_reply.started":"2024-04-18T09:08:44.458274Z","shell.execute_reply":"2024-04-18T09:08:44.463366Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"CFG={\n    'batch_size':32,\n    'num_worker':4,\n    'data':'/kaggle/input/hms-harmful-brain-activity-classification/test.csv',\n    'weights_eeg_raw':'/kaggle/input/hms-2nd-place-solution/pytorch/hms-eeg_raw/1',\n}","metadata":{"execution":{"iopub.status.busy":"2024-04-18T09:08:53.271183Z","iopub.execute_input":"2024-04-18T09:08:53.271922Z","iopub.status.idle":"2024-04-18T09:08:53.276701Z","shell.execute_reply.started":"2024-04-18T09:08:53.271887Z","shell.execute_reply":"2024-04-18T09:08:53.275694Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"CFG['weights_eeg_raw']=[os.path.join(CFG['weights_eeg_raw'],x) for x in sorted(os.listdir(CFG['weights_eeg_raw']))]\nCFG","metadata":{"execution":{"iopub.status.busy":"2024-04-18T09:08:53.565663Z","iopub.execute_input":"2024-04-18T09:08:53.566275Z","iopub.status.idle":"2024-04-18T09:08:53.576285Z","shell.execute_reply.started":"2024-04-18T09:08:53.566245Z","shell.execute_reply":"2024-04-18T09:08:53.575394Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#dataiter\nclass AlaskaDataIter():\n    def __init__(self, df,\n                 training_flag=False,shuffle=False,\n                 use_spec=False,\n                 use_eeg=False,\n                 use_mix=False,\n                 ll=0,rr=20,\n                 flip=False,\n                 use_mne_filter=True):\n        \n        self.flip_eeg=flip\n        self.ll=ll\n        self.rr=rr\n        \n        print(self.ll,self.rr, 'with mne filter:', use_mne_filter)\n        \n        \n        self.training_flag = training_flag\n        self.shuffle = shuffle\n\n        self.raw_data_set_size = None     ##decided by self.parse_file\n\n\n        self.df=df\n        \n\n        TARS = {'Seizure': 0, 'LPD': 1, 'GPD': 2, 'LRDA': 3, 'GRDA': 4, 'Other': 5}\n        self.TARS2 = {x: y for y, x in TARS.items()}\n\n\n        self.eeg_nms=['Fp1', 'F3', 'C3', 'P3', 'F7', 'T3', 'T5', 'O1', 'Fz', 'Cz', 'Pz',\n       'Fp2', 'F4', 'C4', 'P4', 'F8', 'T4', 'T6', 'O2', 'EKG']\n\n        self.LL = ['Fp1', 'F7', 'T3', 'T5', 'O1']\n\n        self.RL = ['Fp2', 'F8', 'T4', 'T6', 'O2']\n\n        self.LP = ['Fp1', 'F3', 'C3', 'P3', 'O1']\n\n        self.RP = ['Fp2', 'F4', 'C4', 'P4', 'O2']\n\n        self.mid = ['Fz', 'Cz', 'Pz']\n        self.leads_dict = {value: index for index, value in enumerate(self.eeg_nms)}\n\n        self.use_eeg = use_eeg\n        self.use_spec = use_spec\n        self.use_mix = use_mix\n        self.use_mne_filter=use_mne_filter\n        \n        \n        \n    def __getitem__(self, item):\n\n        return self.single_map_func(self.df.iloc[item], self.training_flag)\n\n    def __len__(self):\n\n        return len(self.df)\n\n    def brain_lead(self, waves):\n        waves = copy.deepcopy(waves)\n        brain_leads = [self.LL, self.RL, self.LP, self.RP]\n\n        leads = []\n\n        for chain in brain_leads:\n            for i in range(len(chain) - 1):\n                tmp_lead = waves[self.leads_dict[chain[i]]] - waves[self.leads_dict[chain[i + 1]]]\n                leads.append(tmp_lead)\n\n        data = np.concatenate([leads], axis=0)\n        \n        return data\n    def mirror_spec(self, data):\n\n        # index_choice = [[0, 1, 3, 2], [0, 1, 2, 3], [1, 0, 2, 3], [1, 0, 3, 2]]\n        indx = [1, 0, 3, 2]\n        return data[..., indx]\n\n    def mirror_eeg(self, data):\n\n        indx1 = [0, 1, 2, 3, 4, 5, 6, 7]\n        indx2 = [11, 12, 13, 14, 15, 16, 17, 18]\n\n        data[indx1, ...], data[indx2, ...] = data[indx2, ...], data[indx1, ...]\n\n        return data\n    def butter_bandpass(self,lowcut, highcut, fs, order=5):\n        return butter(order, [lowcut, highcut], fs=fs, btype=\"band\")\n\n    def butter_bandpass_filter(self,data, lowcut, highcut, fs, order=5):\n        b, a = self.butter_bandpass(lowcut, highcut, fs, order=order)\n        y = lfilter(b, a, data)\n        return y\n    def get_eeg(self, dp, is_training,flip=False):\n        eeg_path = '/kaggle/input/hms-harmful-brain-activity-classification/test_eegs/%s.parquet' % (dp['eeg_id'])\n        eeg = pd.read_parquet(eeg_path)\n\n        \n        offset = 0\n        eeg = eeg.iloc[int(offset * 200):int(offset * 200) + 10000]\n\n        waves = eeg.values\n\n        waves = np.transpose(waves, axes=[1, 0])\n\n        for i in range(waves.shape[0]):\n            m = np.nanmean(waves[i])\n            if np.isnan(waves[i]).mean() < 1:\n                waves[i] = np.nan_to_num(waves[i], nan=m)\n            else:\n                waves[i] = 0\n        \n        if flip:\n            waves=self.mirror_eeg(waves)\n        waves = self.brain_lead(waves)\n        waves = np.array(waves, dtype=np.float64)\n\n        waves = np.clip(waves, -1024, 1024)\n        if self.use_mne_filter:\n            waves = mne.filter.filter_data(waves, 200, self.ll, self.rr, verbose=False)\n        else:\n            waves = self.butter_bandpass_filter(waves,0.5,20,200,2)\n        #\n\n\n        return waves\n          \n    \n    def single_map_func(self, dp, is_training):\n        \n        ####customed here\n        data=self.get_eeg(dp,is_training,self.flip_eeg)\n        return data.astype(np.float32)","metadata":{"execution":{"iopub.status.busy":"2024-04-18T09:08:54.473179Z","iopub.execute_input":"2024-04-18T09:08:54.473749Z","iopub.status.idle":"2024-04-18T09:08:54.499059Z","shell.execute_reply.started":"2024-04-18T09:08:54.473716Z","shell.execute_reply":"2024-04-18T09:08:54.498084Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class Net1d(nn.Module):\n    def __init__(self,):\n        super(Net1d, self).__init__()\n        self.model=timm.create_model('efficientnet_b5', pretrained=False, in_chans=3)\n        self.pool=nn.AdaptiveAvgPool2d(1)\n        self.fc=nn.Linear(2048,out_features=6,bias=True)\n        self.dropout=nn.Dropout(p=0.5)\n\n    def extract_features(self, x):\n        feature1=self.model.forward_features(x)\n        return feature1\n    def forward(self, x):\n        \n        bs = x.size(0)\n        reshaped_tensor = x.view(bs,16,1000, 10)\n        reshaped_and_permuted_tensor = reshaped_tensor.permute(0,1,3,2)\n        reshaped_and_permuted_tensor= reshaped_and_permuted_tensor.reshape(bs,16*10,1000)\n        x=torch.unsqueeze(reshaped_and_permuted_tensor,dim=1)\n        x=torch.cat([x,x,x],dim=1)\n        bs=x.size(0)\n\n        x = self.extract_features(x)\n\n        # print(x.size())\n        x = self.pool(x)\n        x = x.view(bs, -1)\n        \n        x =self.dropout(x)\n        x = self.fc(x)\n        x =torch.softmax(x,-1)\n        \n        return x","metadata":{"execution":{"iopub.status.busy":"2024-04-18T09:08:55.968688Z","iopub.execute_input":"2024-04-18T09:08:55.969507Z","iopub.status.idle":"2024-04-18T09:08:55.978791Z","shell.execute_reply.started":"2024-04-18T09:08:55.969474Z","shell.execute_reply":"2024-04-18T09:08:55.977788Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df=pd.read_csv(CFG['data'])\n\ntest_df.head(5)","metadata":{"execution":{"iopub.status.busy":"2024-04-18T09:09:22.551969Z","iopub.execute_input":"2024-04-18T09:09:22.552828Z","iopub.status.idle":"2024-04-18T09:09:22.580079Z","shell.execute_reply.started":"2024-04-18T09:09:22.552786Z","shell.execute_reply":"2024-04-18T09:09:22.579012Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def inference_function(test_loader, model, device,double_input=False):\n    model.eval()\n    \n    prediction_dict = {}\n    preds = []\n    with tqdm(test_loader, unit=\"test_batch\", desc='Inference') as tqdm_test_loader:\n        for step, X in enumerate(tqdm_test_loader):\n            \n            X = X.to(device)\n\n            batch_size = X.size(0)\n            with torch.no_grad():\n                y_preds = model(X)\n\n            preds.append(y_preds.to('cpu').numpy()) \n                \n    prediction_dict[\"predictions\"] = np.concatenate(preds) \n    return prediction_dict","metadata":{"execution":{"iopub.status.busy":"2024-04-18T09:26:32.408599Z","iopub.execute_input":"2024-04-18T09:26:32.409057Z","iopub.status.idle":"2024-04-18T09:26:32.417576Z","shell.execute_reply.started":"2024-04-18T09:26:32.409017Z","shell.execute_reply":"2024-04-18T09:26:32.416570Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def run_weight_eeg_raw():\n    device = torch.device(\"cuda:0\" if torch.cuda.is_available() else \"cpu\")\n\n    print('infer with run_weight_eeg_raw')\n    predictions=[]\n    for model_weight in CFG['weights_eeg_raw']:\n        test_dataset = AlaskaDataIter(test_df, training_flag=False, shuffle=False,use_eeg=True,ll=0.5,rr=20)\n        test_loader = DataLoader(test_dataset,\n                         CFG['batch_size'],\n                         num_workers=CFG['num_worker'],\n                         shuffle=False)\n\n        model = Net1d()\n        state_dict = torch.load(model_weight, map_location=device)\n\n        model.load_state_dict(state_dict,strict=True)\n        model.to(device)\n        prediction_dict = inference_function(test_loader, model, device)\n        predictions.append(prediction_dict[\"predictions\"])\n\n\n        test_dataset = AlaskaDataIter(test_df, training_flag=False, shuffle=False,use_eeg=True,flip=True,ll=0.5,rr=20)\n        test_loader = DataLoader(test_dataset,\n                         CFG['batch_size'],\n                         num_workers=CFG['num_worker'],\n                         shuffle=False)\n\n        model = Net1d()\n        state_dict = torch.load(model_weight, map_location=device)\n\n        model.load_state_dict(state_dict,strict=True)\n        model.to(device)\n        prediction_dict = inference_function(test_loader, model, device)\n        predictions.append(prediction_dict[\"predictions\"])\n\n        torch.cuda.empty_cache()\n        gc.collect()\n    predictions = np.array(predictions)\n    predictions = np.mean(predictions, axis=0)\n    \n    return predictions","metadata":{"execution":{"iopub.status.busy":"2024-04-18T09:26:32.855747Z","iopub.execute_input":"2024-04-18T09:26:32.856518Z","iopub.status.idle":"2024-04-18T09:26:32.867109Z","shell.execute_reply.started":"2024-04-18T09:26:32.856486Z","shell.execute_reply":"2024-04-18T09:26:32.866010Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = []\n\n# #***0.23 best version29, add 1000 val data\nans=run_weight_eeg_raw()\npredictions.append(ans) \n\n\npredictions=np.array(predictions)\npredictions=np.mean(predictions,axis=0)\n","metadata":{"execution":{"iopub.status.busy":"2024-04-18T09:26:33.452953Z","iopub.execute_input":"2024-04-18T09:26:33.453945Z","iopub.status.idle":"2024-04-18T09:27:05.329345Z","shell.execute_reply.started":"2024-04-18T09:26:33.453909Z","shell.execute_reply":"2024-04-18T09:27:05.328200Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"TARGETS = ['seizure_vote', 'lpd_vote', 'gpd_vote', 'lrda_vote', 'grda_vote', 'other_vote']\nsub = pd.DataFrame({'eeg_id': test_df.eeg_id.values})\nsub[TARGETS] = predictions\nsub.to_csv('submission.csv',index=False)\nprint(f'Submissionn shape: {sub.shape}')\nsub.head()","metadata":{"execution":{"iopub.status.busy":"2024-04-18T09:27:16.842768Z","iopub.execute_input":"2024-04-18T09:27:16.843831Z","iopub.status.idle":"2024-04-18T09:27:16.863446Z","shell.execute_reply.started":"2024-04-18T09:27:16.843778Z","shell.execute_reply":"2024-04-18T09:27:16.862344Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}