{"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":"nvidiaTeslaT4","dataSources":[{"sourceId":59093,"databundleVersionId":7469972,"sourceType":"competition"},{"sourceId":37337,"sourceType":"modelInstanceVersion","isSourceIdPinned":true,"modelInstanceId":31415}],"dockerImageVersionId":30699,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"!pip install spkit\nfrom torch.optim.optimizer import Optimizer\nfrom tqdm import tqdm\nimport torch\nimport torch.nn as nn\nimport time\nimport torchinfo\nimport pandas as pd\nimport os\nfrom sklearn.decomposition import FastICA\nimport spkit as sp\nimport copy\nfrom sklearn.preprocessing import normalize\nimport numpy as np","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-04-24T10:12:11.934989Z","iopub.execute_input":"2024-04-24T10:12:11.935395Z","iopub.status.idle":"2024-04-24T10:12:24.80665Z","shell.execute_reply.started":"2024-04-24T10:12:11.935364Z","shell.execute_reply":"2024-04-24T10:12:24.805627Z"},"jupyter":{"source_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"COL_MAP = [\n    \"Fp1\",\n    \"F3\",\n    \"C3\",\n    \"P3\",\n    \"F7\",\n    \"T3\",\n    \"T5\",\n    \"O1\",\n    \"Fz\",\n    \"Cz\",\n    \"Pz\",\n    \"Fp2\",\n    \"F4\",\n    \"C4\",\n    \"P4\",\n    \"F8\",\n    \"T4\",\n    \"T6\",\n    \"O2\",\n    \"EKG\",\n]\ndef montage_from_eeg(eeg):\n\n    NAMES = [\"LL\", \"LP\", \"RP\", \"RR\"]\n\n    FEATS = [\n        [\"Fp1\", \"F7\", \"T3\", \"T5\", \"O1\"],\n        [\"Fp1\", \"F3\", \"C3\", \"P3\", \"O1\"],\n        [\"Fp2\", \"F8\", \"T4\", \"T6\", \"O2\"],\n        [\"Fp2\", \"F4\", \"C4\", \"P4\", \"O2\"],\n    ]\n\n    montage = np.array([])\n    for k in range(4):\n        signals = np.zeros(eeg.shape[0])\n        COLS = FEATS[k]\n        for kk in range(4):\n            x = eeg[:,COL_MAP.index(COLS[kk])] - eeg[:,COL_MAP.index(COLS[kk + 1])]\n            signals += x\n        if k == 0:\n            montage = signals\n        else:\n            montage = np.vstack((montage, signals))\n\n    return montage","metadata":{"execution":{"iopub.status.busy":"2024-04-24T10:22:09.288091Z","iopub.execute_input":"2024-04-24T10:22:09.288907Z","iopub.status.idle":"2024-04-24T10:22:09.298779Z","shell.execute_reply.started":"2024-04-24T10:22:09.288875Z","shell.execute_reply":"2024-04-24T10:22:09.297601Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def remove_artifacts(eeg_subsample, frequency=200, n_components=19):\n    try:\n        Xf = sp.filter_X(eeg_subsample, band=[0.70], btype=\"highpass\", fs=frequency, verbose=0)\n        ica = FastICA(n_components=n_components, random_state=0, max_iter=1000, tol=1e-2)\n        XR = ica.fit_transform(Xf)\n        return Xf - XR\n    except Exception as e:    \n        print(\"ICA NOT DONE\")\n        print(e)\n        return eeg_subsample ","metadata":{"execution":{"iopub.status.busy":"2024-04-24T10:22:11.060828Z","iopub.execute_input":"2024-04-24T10:22:11.061673Z","iopub.status.idle":"2024-04-24T10:22:11.068247Z","shell.execute_reply.started":"2024-04-24T10:22:11.061636Z","shell.execute_reply":"2024-04-24T10:22:11.067217Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def clean_and_normalize_montage_sample(subsample_data):\n    X = subsample_data.values[:, :-1]\n    removed = remove_artifacts(X)\n    normalized_removed = normalize(removed, norm=\"l2\", axis=0)\n    montages=montage_from_eeg(normalized_removed)\n    return montages","metadata":{"execution":{"iopub.status.busy":"2024-04-24T10:12:24.833841Z","iopub.execute_input":"2024-04-24T10:12:24.83459Z","iopub.status.idle":"2024-04-24T10:12:24.84123Z","shell.execute_reply.started":"2024-04-24T10:12:24.834561Z","shell.execute_reply":"2024-04-24T10:12:24.840272Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class DenseLayer(nn.Module):\n    def __init__(self,in_channels,middle_channels=128,out_channels=32): # should we put middle_channels here?\n        \n        super(DenseLayer, self).__init__()\n        self.layer = nn.Sequential(\n            nn.BatchNorm1d(in_channels),\n            nn.ReLU(inplace=True),\n            nn.Conv1d(in_channels,middle_channels,1),\n            nn.BatchNorm1d(middle_channels),\n            nn.ReLU(inplace=True),\n            nn.Conv1d(middle_channels,out_channels,3,padding=1)\n        )\n    \n    def forward(self,x):\n        return torch.cat([x,self.layer(x)],dim=1)\n\nclass DenseBlock(nn.Sequential):\n    def __init__(self,layer_num,growth_rate,in_channels,middle_channels=128):\n        super(DenseBlock, self).__init__()\n        for i in range(layer_num):\n            layer = DenseLayer(in_channels+i*growth_rate,middle_channels,growth_rate)\n            self.add_module('denselayer%d'%(i),layer)\n\nclass Transition(nn.Sequential):\n    def __init__(self,channels):\n        super(Transition, self).__init__()\n        self.add_module('norm',nn.BatchNorm1d(channels))\n        self.add_module('relu',nn.ReLU(inplace=True))\n        self.add_module('conv',nn.Conv1d(channels,channels//2,3,padding=1))\n        self.add_module('Avgpool',nn.AvgPool1d(2))\n\nclass ResidualBlock(nn.Module):\n    def __init__(self,in_channels,out_channels,stride=1):\n        super(ResidualBlock, self).__init__()\n        self.conv1 = nn.Sequential(\n                                nn.Conv1d(in_channels, out_channels, kernel_size = 3, stride = stride, padding = 1),\n                                nn.BatchNorm1d(out_channels),\n                                nn.ReLU())\n        self.conv2 = nn.Sequential(\n                        nn.Conv1d(out_channels, out_channels, kernel_size = 3, stride = 1, padding = 1),\n                        nn.BatchNorm1d(out_channels))\n        self.relu = nn.ReLU(inplace=True)\n        self.relu = nn.ReLU()\n        self.out_channels = out_channels\n        \n    def forward(self, x):\n        residual = x\n        out = self.conv1(x)\n        out = self.conv2(out)\n        out += residual\n        out = self.relu(out)\n        return out\n\nclass DenseNet(nn.Module):\n    def __init__(self,layer_num=(6,12,24,16),growth_rate=32,init_features=64,in_channels=1,middle_channels=128,classes=5):\n        super(DenseNet, self).__init__()\n        self.feature_channel_num=init_features\n        self.conv=nn.Conv1d(in_channels,self.feature_channel_num,7,2,3)\n        self.norm=nn.BatchNorm1d(self.feature_channel_num)\n        self.relu=nn.ReLU()\n        self.maxpool=nn.MaxPool1d(3,2,1)\n\n        self.DenseBlock1=DenseBlock(layer_num[0],growth_rate,self.feature_channel_num,middle_channels)\n        self.feature_channel_num=self.feature_channel_num+layer_num[0]*growth_rate\n        self.Transition1=Transition(self.feature_channel_num)\n\n        self.DenseBlock2=DenseBlock(layer_num[1],growth_rate,self.feature_channel_num//2,middle_channels)\n        self.feature_channel_num=self.feature_channel_num//2+layer_num[1]*growth_rate\n        self.Transition2 = Transition(self.feature_channel_num)\n\n        self.DenseBlock3 = DenseBlock(layer_num[2],growth_rate,self.feature_channel_num//2,middle_channels)\n        self.feature_channel_num=self.feature_channel_num//2+layer_num[2]*growth_rate\n        self.Transition3 = Transition(self.feature_channel_num)\n\n        self.DenseBlock4 = DenseBlock(layer_num[3],growth_rate,self.feature_channel_num//2,middle_channels)\n        self.feature_channel_num=self.feature_channel_num//2+layer_num[3]*growth_rate\n\n        self.avgpool=nn.AdaptiveAvgPool1d(1)\n\n        self.classifer = nn.Sequential(\n            nn.Linear(self.feature_channel_num, self.feature_channel_num//2),\n            nn.ReLU(),\n            nn.Dropout(0.5),\n            nn.Linear(self.feature_channel_num//2, classes),\n            nn.LogSoftmax(dim=1)\n        )\n\n\n    def forward(self,x):\n        x = self.conv(x)\n        x = self.norm(x)\n        x = self.relu(x)\n        x = self.maxpool(x)\n\n        x = self.DenseBlock1(x)\n        x = self.Transition1(x)\n\n        x = self.DenseBlock2(x)\n        x = self.Transition2(x)\n\n        x = self.DenseBlock3(x)\n        x = self.Transition3(x)\n\n        x = self.DenseBlock4(x)\n        x = self.avgpool(x)\n        x = x.view(-1,self.feature_channel_num)\n        x = self.classifer(x)\n\n        return x\n","metadata":{"execution":{"iopub.status.busy":"2024-04-24T10:12:24.854913Z","iopub.execute_input":"2024-04-24T10:12:24.855301Z","iopub.status.idle":"2024-04-24T10:12:24.883239Z","shell.execute_reply.started":"2024-04-24T10:12:24.855269Z","shell.execute_reply":"2024-04-24T10:12:24.882239Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def load_and_preprocess(\n    input_master=\"/kaggle/input/hms-harmful-brain-activity-classification\",\n    input_type: str = \"test\"\n):\n\n    test_df = pd.read_csv(f\"{input_master}/{input_type}.csv\")\n    test_eeg_path = f\"{input_master}/{input_type}_eegs\"\n    # eeg_file = os.listdir(test_eeg_path) # depends on how we want to go about it\n    X = np.array([])\n    ids = test_df[\"eeg_id\"].values # this I need to change bro\n    totaleegs=len(y)\n    with tqdm(total = totaleegs) as pbar:\n        for eeg_id in test_df[\"eeg_id\"][0:10]:\n            eeg_data_sample = pd.read_parquet(f'{test_eeg_path}/{eeg_id}.parquet')\n            preprocessed = clean_and_normalize_montage_sample(eeg_data_sample.head(10000))\n            preprocessed = np.expand_dims(preprocessed, 0)\n            if len(X)==0:\n                X = preprocessed\n            else:     \n                X = np.vstack((X, preprocessed))\n            pbar.update(1)\n    return X, ids\n","metadata":{"execution":{"iopub.status.busy":"2024-04-24T11:41:48.925137Z","iopub.execute_input":"2024-04-24T11:41:48.925899Z","iopub.status.idle":"2024-04-24T11:41:48.934087Z","shell.execute_reply.started":"2024-04-24T11:41:48.925867Z","shell.execute_reply":"2024-04-24T11:41:48.932811Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"device  = 'cuda' if torch.cuda.is_available() else 'cpu'\nmodel = DenseNet(layer_num=(6,12,24,16),growth_rate=32,in_channels=4,classes=6)\nmodel = model.to(device)\n","metadata":{"execution":{"iopub.status.busy":"2024-04-24T10:12:24.884848Z","iopub.execute_input":"2024-04-24T10:12:24.885217Z","iopub.status.idle":"2024-04-24T10:12:25.044376Z","shell.execute_reply.started":"2024-04-24T10:12:24.885172Z","shell.execute_reply":"2024-04-24T10:12:25.043639Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"checkpoint = torch.load(\"/kaggle/input/densenet_basic/pytorch/epic/1/all_data_10.pth\",map_location=\"cpu\")\nmodel.load_state_dict(checkpoint['model_state_dict'])\nloss_fn = nn.KLDivLoss(reduction=\"batchmean\")\nmodel.eval()","metadata":{"execution":{"iopub.status.busy":"2024-04-24T10:56:22.252655Z","iopub.execute_input":"2024-04-24T10:56:22.253519Z","iopub.status.idle":"2024-04-24T10:56:22.51207Z","shell.execute_reply.started":"2024-04-24T10:56:22.253482Z","shell.execute_reply":"2024-04-24T10:56:22.511092Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X,ids =load_and_preprocess()\nX = torch.from_numpy(X).to(device,dtype=torch.float)","metadata":{"execution":{"iopub.status.busy":"2024-04-24T11:42:08.053443Z","iopub.execute_input":"2024-04-24T11:42:08.053803Z","iopub.status.idle":"2024-04-24T11:42:08.67134Z","shell.execute_reply.started":"2024-04-24T11:42:08.053777Z","shell.execute_reply":"2024-04-24T11:42:08.670166Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with torch.inference_mode():\n    test_pred = model(X)","metadata":{"execution":{"iopub.status.busy":"2024-04-24T11:42:17.817119Z","iopub.execute_input":"2024-04-24T11:42:17.818024Z","iopub.status.idle":"2024-04-24T11:42:17.997764Z","shell.execute_reply.started":"2024-04-24T11:42:17.817991Z","shell.execute_reply":"2024-04-24T11:42:17.996966Z"},"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(test_pred.cpu().numpy(),columns=TARGETS)\nsub[\"eeg_id\"] = ids\nsub = sub[['eeg_id', *TARGETS]]\nsub.to_csv('submission.csv',index=False)\nsub.head()","metadata":{"execution":{"iopub.status.busy":"2024-04-24T11:42:20.003639Z","iopub.execute_input":"2024-04-24T11:42:20.004582Z","iopub.status.idle":"2024-04-24T11:42:20.022707Z","shell.execute_reply.started":"2024-04-24T11:42:20.004546Z","shell.execute_reply":"2024-04-24T11:42:20.021682Z"},"trusted":true},"execution_count":null,"outputs":[]}]}