{"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":7808072,"sourceType":"datasetVersion","datasetId":4572866}],"dockerImageVersionId":30646,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# The Objective of this competition is to classify seizures and other patterns of harmful brain activity in critically ill patients","metadata":{}},{"cell_type":"markdown","source":"## There are six patterns of interest for this competition: \n- seizure (SZ)\n- generalized periodic discharges (GPD)\n- lateralized periodic discharges (LPD)\n- lateralized rhythmic delta activity (LRDA)\n- generalized rhythmic delta activity (GRDA)\n- \"other\"","metadata":{}},{"cell_type":"markdown","source":"## Experts Classification\n- **Idealized patterns**: Segments where there is a high level of agreement among experts regarding the correct label.\n- **Proto-patterns**: Segments where approximately half of the experts assign the label \"other\" while the other half assign one of the remaining five labels.\n- **Edge cases**: Segments where experts are roughly split between two of the five named patterns, making classification challenging and ambiguous.\n","metadata":{}},{"cell_type":"markdown","source":"![Experts Classification](https://storage.googleapis.com/kaggle-media/competitions/Harvard%20Medical%20School/eFig2.png)","metadata":{}},{"cell_type":"code","source":"!pip install pyarrow","metadata":{"execution":{"iopub.status.busy":"2024-03-03T09:09:05.638470Z","iopub.execute_input":"2024-03-03T09:09:05.639115Z","iopub.status.idle":"2024-03-03T09:09:17.579975Z","shell.execute_reply.started":"2024-03-03T09:09:05.639084Z","shell.execute_reply":"2024-03-03T09:09:17.578786Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport pyarrow.parquet as pq\nimport math","metadata":{"execution":{"iopub.status.busy":"2024-03-10T14:22:12.463759Z","iopub.execute_input":"2024-03-10T14:22:12.464701Z","iopub.status.idle":"2024-03-10T14:22:12.918924Z","shell.execute_reply.started":"2024-03-10T14:22:12.464655Z","shell.execute_reply":"2024-03-10T14:22:12.917893Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"BASE_DIR = \"/kaggle/input/hms-harmful-brain-activity-classification/\"","metadata":{"execution":{"iopub.status.busy":"2024-03-10T14:22:14.676192Z","iopub.execute_input":"2024-03-10T14:22:14.677143Z","iopub.status.idle":"2024-03-10T14:22:14.680929Z","shell.execute_reply.started":"2024-03-10T14:22:14.677109Z","shell.execute_reply":"2024-03-10T14:22:14.680006Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.read_csv(BASE_DIR + \"train.csv\")\ntrain_df","metadata":{"execution":{"iopub.status.busy":"2024-03-10T14:22:15.011266Z","iopub.execute_input":"2024-03-10T14:22:15.011869Z","iopub.status.idle":"2024-03-10T14:22:15.301706Z","shell.execute_reply.started":"2024-03-10T14:22:15.011834Z","shell.execute_reply":"2024-03-10T14:22:15.300801Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\n\ntrain_eegs = BASE_DIR + 'train_eegs'\ntrain_spectrograms = BASE_DIR + 'train_spectrograms'\n\nprint(\"Number of files in train_eegs:\", len(os.listdir(train_eegs)))\nprint(\"Number of files in train_spectrograms:\", len(os.listdir(train_spectrograms)))","metadata":{"execution":{"iopub.status.busy":"2024-03-10T14:22:15.660383Z","iopub.execute_input":"2024-03-10T14:22:15.660739Z","iopub.status.idle":"2024-03-10T14:22:16.183599Z","shell.execute_reply.started":"2024-03-10T14:22:15.660711Z","shell.execute_reply":"2024-03-10T14:22:16.182665Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# EDA","metadata":{}},{"cell_type":"markdown","source":"## What is Parquet?\nApache Parquet is an open source, column-oriented data file format designed for efficient data storage and retrieval. It provides efficient data compression and encoding schemes with enhanced performance to handle complex data in bulk.","metadata":{}},{"cell_type":"markdown","source":"### testing parquet","metadata":{}},{"cell_type":"code","source":"file_path = BASE_DIR + 'test_eegs/3911565283.parquet'\n\ntable = pq.read_table(file_path)\n\n# Convert the table to a pandas DataFrame\ndf = table.to_pandas()\n\ndf","metadata":{"execution":{"iopub.status.busy":"2024-03-10T14:22:16.992835Z","iopub.execute_input":"2024-03-10T14:22:16.993188Z","iopub.status.idle":"2024-03-10T14:22:17.138342Z","shell.execute_reply.started":"2024-03-10T14:22:16.993159Z","shell.execute_reply":"2024-03-10T14:22:17.137458Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.columns","metadata":{"execution":{"iopub.status.busy":"2024-03-10T14:22:17.215991Z","iopub.execute_input":"2024-03-10T14:22:17.216324Z","iopub.status.idle":"2024-03-10T14:22:17.222758Z","shell.execute_reply.started":"2024-03-10T14:22:17.216296Z","shell.execute_reply":"2024-03-10T14:22:17.221658Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"![Regions](https://upload.wikimedia.org/wikipedia/commons/thumb/7/70/21_electrodes_of_International_10-20_system_for_EEG.svg/1024px-21_electrodes_of_International_10-20_system_for_EEG.svg.png)","metadata":{}},{"cell_type":"markdown","source":"![image.png](https://upload.wikimedia.org/wikipedia/commons/thumb/f/fb/EEG_10-10_system_with_additional_information.svg/1280px-EEG_10-10_system_with_additional_information.svg.png)","metadata":{}},{"cell_type":"markdown","source":"![Regions](https://uploads-ssl.webflow.com/5e2b590ca27de6be81c64780/5e8ad2dca6b2c1434cac8025_Double_Banana_Setup_(flat)-17-p-1080.png)","metadata":{}},{"cell_type":"code","source":"file_path = BASE_DIR + 'test_spectrograms/853520.parquet'\n\ntable = pq.read_table(file_path)\n# Convert the table to a pandas DataFrame\ntest_file = table.to_pandas()\n\ntest_file","metadata":{"execution":{"iopub.status.busy":"2024-03-10T14:22:18.076837Z","iopub.execute_input":"2024-03-10T14:22:18.077195Z","iopub.status.idle":"2024-03-10T14:22:18.168487Z","shell.execute_reply.started":"2024-03-10T14:22:18.077165Z","shell.execute_reply":"2024-03-10T14:22:18.167629Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**train_spectrograms/** Spectrograms assembled EEG data. Use the metadata in train.csv to select specific annotated subsets. The column names indicate the frequency in hertz and the recording regions of the EEG electrodes. The latter are abbreviated as LL = left lateral; RL = right lateral; LP = left parasagittal; RP = right parasagittal.","metadata":{}},{"cell_type":"code","source":"category_counts = train_df['expert_consensus'].value_counts()\n\nplt.figure(figsize=(8, 6))\nplt.bar(category_counts.index, category_counts.values)\nplt.xlabel('expert_consensus')\nplt.ylabel('Counts')\nplt.title('Bar Plot of expert_consensus Counts')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-03-10T14:22:19.948911Z","iopub.execute_input":"2024-03-10T14:22:19.949265Z","iopub.status.idle":"2024-03-10T14:22:20.191240Z","shell.execute_reply.started":"2024-03-10T14:22:19.949236Z","shell.execute_reply":"2024-03-10T14:22:20.190367Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(8, 6))\nplt.hist(train_df['seizure_vote'], bins=20, color='skyblue', edgecolor='black')\nplt.xlabel('seizure_vote')\nplt.ylabel('Frequency')\nplt.title('Histogram of seizure_vote')\nplt.grid(True)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-03-10T14:22:20.322355Z","iopub.execute_input":"2024-03-10T14:22:20.322686Z","iopub.status.idle":"2024-03-10T14:22:20.568724Z","shell.execute_reply.started":"2024-03-10T14:22:20.322659Z","shell.execute_reply":"2024-03-10T14:22:20.567895Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(8, 6))\nplt.hist(train_df['lpd_vote'], bins=20, color='skyblue', edgecolor='black')\nplt.xlabel('lpd_vote')\nplt.ylabel('Frequency')\nplt.title('Histogram of lpd_vote')\nplt.grid(True)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-03-10T14:22:21.374709Z","iopub.execute_input":"2024-03-10T14:22:21.375449Z","iopub.status.idle":"2024-03-10T14:22:21.614929Z","shell.execute_reply.started":"2024-03-10T14:22:21.375409Z","shell.execute_reply":"2024-03-10T14:22:21.614010Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(8, 6))\nplt.hist(train_df['gpd_vote'], bins=20, color='skyblue', edgecolor='black')\nplt.xlabel('gpd_vote')\nplt.ylabel('Frequency')\nplt.title('Histogram of gpd_vote')\nplt.grid(True)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-03-10T14:22:21.617399Z","iopub.execute_input":"2024-03-10T14:22:21.617769Z","iopub.status.idle":"2024-03-10T14:22:21.866389Z","shell.execute_reply.started":"2024-03-10T14:22:21.617737Z","shell.execute_reply":"2024-03-10T14:22:21.865518Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(8, 6))\nplt.hist(train_df['lrda_vote'], bins=20, color='skyblue', edgecolor='black')\nplt.xlabel('lrda_vote')\nplt.ylabel('Frequency')\nplt.title('Histogram of lrda_vote')\nplt.grid(True)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-03-10T14:22:21.892117Z","iopub.execute_input":"2024-03-10T14:22:21.892408Z","iopub.status.idle":"2024-03-10T14:22:22.136426Z","shell.execute_reply.started":"2024-03-10T14:22:21.892383Z","shell.execute_reply":"2024-03-10T14:22:22.135532Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(8, 6))\nplt.hist(train_df['grda_vote'], bins=20, color='skyblue', edgecolor='black')\nplt.xlabel('grda_vote')\nplt.ylabel('Frequency')\nplt.title('Histogram of grda_vote')\nplt.grid(True)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-03-10T14:22:22.164159Z","iopub.execute_input":"2024-03-10T14:22:22.164806Z","iopub.status.idle":"2024-03-10T14:22:22.401335Z","shell.execute_reply.started":"2024-03-10T14:22:22.164778Z","shell.execute_reply":"2024-03-10T14:22:22.400458Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(8, 6))\nplt.hist(train_df['other_vote'], bins=20, color='skyblue', edgecolor='black')\nplt.xlabel('other_vote')\nplt.ylabel('Frequency')\nplt.title('Histogram of other_vote')\nplt.grid(True)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-03-10T14:22:22.516132Z","iopub.execute_input":"2024-03-10T14:22:22.516398Z","iopub.status.idle":"2024-03-10T14:22:22.740632Z","shell.execute_reply.started":"2024-03-10T14:22:22.516376Z","shell.execute_reply":"2024-03-10T14:22:22.739725Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.describe()","metadata":{"execution":{"iopub.status.busy":"2024-03-10T14:22:22.995550Z","iopub.execute_input":"2024-03-10T14:22:22.995882Z","iopub.status.idle":"2024-03-10T14:22:23.068014Z","shell.execute_reply.started":"2024-03-10T14:22:22.995855Z","shell.execute_reply":"2024-03-10T14:22:23.067140Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df['total_evaluators'] = train_df[['seizure_vote', 'lpd_vote', 'gpd_vote', 'lrda_vote', 'grda_vote', 'other_vote']].sum(axis = 1)\ntrain_df['consensus'] = train_df[['seizure_vote', 'lpd_vote', 'gpd_vote', 'lrda_vote', 'grda_vote', 'other_vote']].max(axis = 1)\ntrain_df['consensus_col'] = train_df[['seizure_vote', 'lpd_vote', 'gpd_vote', 'lrda_vote', 'grda_vote', 'other_vote']].idxmax(axis = 1)\ntrain_df['percentage_agreement'] = train_df['consensus'] / train_df['total_evaluators']","metadata":{"execution":{"iopub.status.busy":"2024-03-10T14:22:23.983928Z","iopub.execute_input":"2024-03-10T14:22:23.984301Z","iopub.status.idle":"2024-03-10T14:22:24.062624Z","shell.execute_reply.started":"2024-03-10T14:22:23.984270Z","shell.execute_reply":"2024-03-10T14:22:24.061833Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.columns","metadata":{"execution":{"iopub.status.busy":"2024-03-10T14:22:25.091565Z","iopub.execute_input":"2024-03-10T14:22:25.091917Z","iopub.status.idle":"2024-03-10T14:22:25.099358Z","shell.execute_reply.started":"2024-03-10T14:22:25.091890Z","shell.execute_reply":"2024-03-10T14:22:25.098444Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Dataset Class","metadata":{}},{"cell_type":"code","source":"import torch\nimport torch.nn as nn\nimport torch.nn.functional as F\nfrom torch.utils.data import DataLoader, Dataset\nimport torchvision.transforms as transforms\n\nclass EEGDataset(Dataset):\n    def __init__(self, data_dir, metadata_file, transform=None):\n        self.data_dir = data_dir\n        self.metadata = pd.read_csv(metadata_file)\n        self.transform = transform\n        \n        self.vote_columns = self.metadata['expert_consensus'].unique()\n        self.label_mapping = {col: i for i, col in enumerate(self.vote_columns)}\n        self.metadata['consensus_col'] = self.metadata['expert_consensus'].map(self.label_mapping)\n\n    def __len__(self):\n        return len(self.metadata)\n\n    def __getitem__(self, idx):\n        eeg_info = self.metadata.iloc[idx]\n        eeg_id = eeg_info['eeg_id']\n        eeg_sub_id = eeg_info['eeg_sub_id']\n        eeg_label_offset_seconds = int(eeg_info['eeg_label_offset_seconds'])\n        patient_id = eeg_info['patient_id']\n        label_id = eeg_info['label_id']\n        expert_consensus = eeg_info['expert_consensus']\n        \n        \n        eeg_file_path = os.path.join(self.data_dir, 'train_eegs', f'{eeg_id}.parquet')\n        eeg_data = pd.read_parquet(eeg_file_path)\n        eeg_segment = eeg_data.iloc[(eeg_label_offset_seconds * 200) : ((eeg_label_offset_seconds + 10) * 200)]\n        eeg_segment = eeg_segment.to_numpy()\n        \n        spectrogram_id = eeg_info['spectrogram_id']\n        spectrogram_sub_id = eeg_info['spectrogram_sub_id']\n        spectrogram_label_offset_seconds = int(eeg_info['spectrogram_label_offset_seconds'])\n        \n        spectrogram_file_path = os.path.join(self.data_dir, 'train_spectrograms', f'{spectrogram_id}.parquet')\n        spectrogram_data = pd.read_parquet(spectrogram_file_path)\n        spectrogram_segment = spectrogram_data.loc[(spectrogram_data.time >= spectrogram_label_offset_seconds) & (spectrogram_data.time < (spectrogram_label_offset_seconds + 600))]\n        spectrogram_segment = spectrogram_segment.to_numpy()\n        \n        \n        if self.transform:\n            eeg_segment = self.transform(eeg_segment)\n            spectrogram_segment = self.transform(spectrogram_segment)\n            \n        y = np.zeros(len(self.vote_columns), 'float32')\n        label = self.metadata.iloc[idx]['consensus_col']\n        y[label] = 1\n        y = torch.tensor(y, dtype=torch.float32)\n        \n\n        return torch.tensor(eeg_segment.astype(np.float32)), torch.tensor(spectrogram_segment.astype(np.float32)), y\n\ndata_dir = BASE_DIR\nmetadata_file = '/kaggle/input/hms-harmful-brain-activity-classification/train.csv'\ntransform = transforms.Compose([transforms.ToTensor()])\n\ndataset = EEGDataset(data_dir, metadata_file)\n","metadata":{"execution":{"iopub.status.busy":"2024-03-10T14:22:32.020013Z","iopub.execute_input":"2024-03-10T14:22:32.020725Z","iopub.status.idle":"2024-03-10T14:22:38.328844Z","shell.execute_reply.started":"2024-03-10T14:22:32.020690Z","shell.execute_reply":"2024-03-10T14:22:38.327989Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset[0]","metadata":{"execution":{"iopub.status.busy":"2024-03-10T14:22:38.330521Z","iopub.execute_input":"2024-03-10T14:22:38.330883Z","iopub.status.idle":"2024-03-10T14:22:38.515273Z","shell.execute_reply.started":"2024-03-10T14:22:38.330856Z","shell.execute_reply":"2024-03-10T14:22:38.514375Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset[0][0].shape","metadata":{"execution":{"iopub.status.busy":"2024-03-10T14:22:38.516538Z","iopub.execute_input":"2024-03-10T14:22:38.516882Z","iopub.status.idle":"2024-03-10T14:22:38.558564Z","shell.execute_reply.started":"2024-03-10T14:22:38.516851Z","shell.execute_reply":"2024-03-10T14:22:38.557686Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset[0][1].shape","metadata":{"execution":{"iopub.status.busy":"2024-03-10T14:22:38.560302Z","iopub.execute_input":"2024-03-10T14:22:38.560583Z","iopub.status.idle":"2024-03-10T14:22:38.601539Z","shell.execute_reply.started":"2024-03-10T14:22:38.560560Z","shell.execute_reply":"2024-03-10T14:22:38.600611Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset[0][2].shape","metadata":{"execution":{"iopub.status.busy":"2024-03-10T14:22:38.602710Z","iopub.execute_input":"2024-03-10T14:22:38.602985Z","iopub.status.idle":"2024-03-10T14:22:38.639860Z","shell.execute_reply.started":"2024-03-10T14:22:38.602961Z","shell.execute_reply":"2024-03-10T14:22:38.638949Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Data Loader","metadata":{}},{"cell_type":"code","source":"batch_size = 8\ndataloader = DataLoader(dataset, batch_size=batch_size, shuffle=True,drop_last=True)","metadata":{"execution":{"iopub.status.busy":"2024-03-10T14:22:44.742586Z","iopub.execute_input":"2024-03-10T14:22:44.743333Z","iopub.status.idle":"2024-03-10T14:22:44.748095Z","shell.execute_reply.started":"2024-03-10T14:22:44.743300Z","shell.execute_reply":"2024-03-10T14:22:44.747133Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Models","metadata":{}},{"cell_type":"markdown","source":"## DNN","metadata":{}},{"cell_type":"code","source":"class DNN(nn.Module):\n    def __init__(self):\n        super(DNN, self).__init__()\n        self.fc1 = nn.Linear(20, 128)\n        self.fc2 = nn.Linear(128, 64)\n        self.fc3 = nn.Linear(64, 6)\n\n    def forward(self, x):\n        x = torch.relu(self.fc1(x))\n        x = torch.relu(self.fc2(x))\n        x = self.fc3(x)\n        return torch.softmax(x, dim=1)","metadata":{"execution":{"iopub.status.busy":"2024-03-03T09:09:54.195530Z","iopub.execute_input":"2024-03-03T09:09:54.195882Z","iopub.status.idle":"2024-03-03T09:09:54.202495Z","shell.execute_reply.started":"2024-03-03T09:09:54.195857Z","shell.execute_reply":"2024-03-03T09:09:54.201510Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 1D CNN","metadata":{}},{"cell_type":"code","source":"# out = (in - d(k - 1) + 2*p - 1)/s + 1\n# out = (in - k + 2*p)/s + 1\nclass EEG_1D_CNN(nn.Module):\n    def __init__(self):\n        super(EEG_1D_CNN, self).__init__()\n        # input shape:          8, 20, 2000\n        self.conv1 = nn.Conv1d(20, 40, kernel_size=15, stride =5) # 8, 20,398\n        self.conv2 = nn.Conv1d(40, 60, kernel_size=5, stride=3) # 8, 20,\n        self.fc1 = nn.Linear(60*132, 128)\n        \n    def forward(self, x):\n        x = x.permute(0,2,1)\n        x = self.conv1(x)\n        x = torch.relu(x)\n        x = self.conv2(x)\n        x = torch.relu(x)\n\n        x = x.view(x.size(0),-1)\n        x = self.fc1(x)\n        return x\n\n# class Spectrogram_CNN(nn.Module):\n#     def __init__(self):\n#         super(Spectrogram_CNN, self).__init__()\n#         self.fc0 = nn.Linear(401,512) # 1, 150, 512\n#         self.conv1 = nn.Conv2d(1, 20, kernel_size=(2, 7), stride =(1, 5)) # 20, 149, 102\n#         self.conv2 = nn.Conv2d(20, 40, kernel_size=(3,2), stride=(1,1)) # 40, 147,101\n#         self.fc1 = nn.Linear(1, 128)\n        \n#     def forward(self, x):\n\n#         x = x.view(-1,x.shape[-1])\n#         x = self.fc0(x)\n\n#         x = x.view(batch_size,1,-1,x.shape[1])\n\n#         x = self.conv1(x)\n#         x = torch.relu(x)\n#         x = self.conv2(x)\n#         x = torch.relu(x)\n\n#         x = x.view(x.size(0),-1)\n#         x = self.fc1(x)\n#         return x\n\n\nclass Combined_1D_CNNModel(nn.Module):\n    def __init__(self):\n        super(Combined_1D_CNNModel, self).__init__()\n        self.eeg_cnn = EEG_1D_CNN()\n#         self.spec_cnn = Spectrogram_CNN()\n        \n        self.fc1 = nn.Linear(128, 6)\n#         self.fc2 = nn.Linear(128, 6)\n        \n    def forward(self, eeg_input):\n        eeg_output = self.eeg_cnn(eeg_input)\n#         spec_output = self.spec_cnn(spec_input)\n\n        eeg_output = self.fc1(eeg_output)\n#         spec_output = self.fc2(spec_output)\n        eeg_output = torch.softmax(eeg_output, dim=1)\n        return eeg_output","metadata":{"execution":{"iopub.status.busy":"2024-03-10T14:24:41.478661Z","iopub.execute_input":"2024-03-10T14:24:41.479018Z","iopub.status.idle":"2024-03-10T14:24:41.490569Z","shell.execute_reply.started":"2024-03-10T14:24:41.478988Z","shell.execute_reply":"2024-03-10T14:24:41.489686Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 2D CNN","metadata":{}},{"cell_type":"code","source":"# out = (in - d(k - 1) + 2*p - 1)/s + 1\n# out = (in - k + 2*p)/s + 1\nclass EEG_CNN(nn.Module):\n    def __init__(self):\n        super(EEG_CNN, self).__init__()\n        # input shape:          8, 2000, 20\n        self.fc0 = nn.Linear(20,512) # 8,1,2000,512\n        self.conv1 = nn.Conv2d(1, 20, kernel_size=(10, 4), stride =(5, 1)) # 20, 399, 509\n        self.conv2 = nn.Conv2d(20, 40, kernel_size=(5,5), stride=(2,3)) # 40, 198,169\n        self.fc1 = nn.Linear(40*198*169, 128)\n        \n    def forward(self, x):\n        x = x.view(-1,x.shape[-1])\n        x = self.fc0(x)\n        x = x.view(batch_size,1,-1,x.shape[1])\n        x = self.conv1(x)\n        x = torch.relu(x)\n        x = self.conv2(x)\n        x = torch.relu(x)\n\n        x = x.view(x.size(0),-1)\n        x = self.fc1(x)\n        return x\n\n# class Spectrogram_CNN(nn.Module):\n#     def __init__(self):\n#         super(Spectrogram_CNN, self).__init__()\n#         self.fc0 = nn.Linear(401,512) # 1, 150, 512\n#         self.conv1 = nn.Conv2d(1, 20, kernel_size=(2, 7), stride =(1, 5)) # 20, 149, 102\n#         self.conv2 = nn.Conv2d(20, 40, kernel_size=(3,2), stride=(1,1)) # 40, 147,101\n#         self.fc1 = nn.Linear(1, 128)\n        \n#     def forward(self, x):\n\n#         x = x.view(-1,x.shape[-1])\n#         x = self.fc0(x)\n\n#         x = x.view(batch_size,1,-1,x.shape[1])\n\n#         x = self.conv1(x)\n#         x = torch.relu(x)\n#         x = self.conv2(x)\n#         x = torch.relu(x)\n\n#         x = x.view(x.size(0),-1)\n#         x = self.fc1(x)\n#         return x\n\n\nclass CombinedCNNModel(nn.Module):\n    def __init__(self):\n        super(CombinedCNNModel, self).__init__()\n        self.eeg_cnn = EEG_CNN()\n#         self.spec_cnn = Spectrogram_CNN()\n        \n        self.fc1 = nn.Linear(128, 6)\n#         self.fc2 = nn.Linear(128, 6)\n        \n    def forward(self, eeg_input):\n        eeg_output = self.eeg_cnn(eeg_input)\n#         spec_output = self.spec_cnn(spec_input)\n\n        eeg_output = self.fc1(eeg_output)\n#         spec_output = self.fc2(spec_output)\n        eeg_output = torch.softmax(eeg_output, dim=1)\n        return eeg_output","metadata":{"execution":{"iopub.status.busy":"2024-03-03T09:09:59.171041Z","iopub.execute_input":"2024-03-03T09:09:59.171871Z","iopub.status.idle":"2024-03-03T09:09:59.183108Z","shell.execute_reply.started":"2024-03-03T09:09:59.171839Z","shell.execute_reply":"2024-03-03T09:09:59.182086Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# class EEG_LSTM(nn.Module):\n#     def __init__(self, input_size, hidden_size, num_layers, num_classes):\n#         super(EEG_LSTM, self).__init__()\n#         self.hidden_size = hidden_size\n#         self.num_layers = num_layers\n#         self.lstm = nn.LSTM(input_size, hidden_size, num_layers, batch_first=True)\n#         self.fc = nn.Linear(hidden_size, num_classes)\n        \n#     def forward(self, x):\n#         h0 = torch.zeros(self.num_layers, x.size(0), self.hidden_size).to(x.device)\n#         c0 = torch.zeros(self.num_layers, x.size(0), self.hidden_size).to(x.device)\n\n#         out, _ = self.lstm(x, (h0, c0))\n#         out = self.fc(out[:, -1, :])\n        \n#         return out\n\n# class Spectrogram_LSTM(nn.Module):\n#     def __init__(self, input_size, hidden_size, num_layers, num_classes):\n#         super(Spectrogram_LSTM, self).__init__()\n#         self.hidden_size = hidden_size\n#         self.num_layers = num_layers\n#         self.lstm = nn.LSTM(input_size, hidden_size, num_layers, batch_first=True)\n#         self.fc = nn.Linear(hidden_size, num_classes)\n        \n#     def forward(self, x):\n#         h0 = torch.zeros(self.num_layers, x.size(0), self.hidden_size).to(x.device)\n#         c0 = torch.zeros(self.num_layers, x.size(0), self.hidden_size).to(x.device)\n\n#         out, _ = self.lstm(x, (h0, c0))\n#         out = self.fc(out[:, -1, :])\n        \n#         return out\n\n# class CombinedLSTMModel(nn.Module):\n#     def __init__(self, eeg_input_size, spec_input_size, eeg_hidden_size, spec_hidden_size, num_layers, num_classes):\n#         super(CombinedLSTMModel, self).__init__()\n#         self.eeg_lstm = EEG_LSTM(eeg_input_size, eeg_hidden_size, num_layers, num_classes)\n#         self.spec_lstm = Spectrogram_LSTM(spec_input_size, spec_hidden_size, num_layers, num_classes)\n#         self.fc = nn.Linear(num_classes * 2, num_classes)\n        \n#     def forward(self, eeg_input, spec_input):\n#         eeg_output = self.eeg_lstm(eeg_input)\n#         spec_output = self.spec_lstm(spec_input)\n        \n#         combined_output = torch.cat((eeg_output, spec_output), dim=1)\n#         output = self.fc(combined_output)\n#         output = torch.softmax(output, dim=1)\n        \n#         return output","metadata":{"execution":{"iopub.status.busy":"2024-03-03T09:10:05.716773Z","iopub.execute_input":"2024-03-03T09:10:05.717127Z","iopub.status.idle":"2024-03-03T09:10:05.722885Z","shell.execute_reply.started":"2024-03-03T09:10:05.717098Z","shell.execute_reply":"2024-03-03T09:10:05.722008Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dnn_model = DNN()\ncnn_model = CombinedCNNModel()\ncnn_1d_model = Combined_1D_CNNModel()\n# lstm_model = CombinedLSTMModel(eeg_input_size=20, spec_input_size=401, eeg_hidden_size=32, spec_hidden_size=64, num_layers=2, num_classes=6)","metadata":{"execution":{"iopub.status.busy":"2024-03-03T09:10:06.000049Z","iopub.execute_input":"2024-03-03T09:10:06.000966Z","iopub.status.idle":"2024-03-03T09:10:07.757443Z","shell.execute_reply.started":"2024-03-03T09:10:06.000929Z","shell.execute_reply":"2024-03-03T09:10:07.756501Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"if torch.cuda.is_available():\n    print('CUDA is available! You can use the GPU.')\nelse:\n    print('CUDA is not available. Using CPU...')\ndevice = torch.device('cuda' if torch.cuda.is_available() else 'cpu')\ndnn_model.to(device)\ncnn_model.to(device)\ncnn_1d_model.to(device)\n# lstm_model.to(device)","metadata":{"execution":{"iopub.status.busy":"2024-03-03T09:10:10.810894Z","iopub.execute_input":"2024-03-03T09:10:10.811277Z","iopub.status.idle":"2024-03-03T09:10:11.157270Z","shell.execute_reply.started":"2024-03-03T09:10:10.811248Z","shell.execute_reply":"2024-03-03T09:10:11.156317Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# cnn_model = nn.DataParallel(cnn_model)","metadata":{"execution":{"iopub.status.busy":"2024-03-03T09:10:14.055583Z","iopub.execute_input":"2024-03-03T09:10:14.055955Z","iopub.status.idle":"2024-03-03T09:10:14.060274Z","shell.execute_reply.started":"2024-03-03T09:10:14.055926Z","shell.execute_reply":"2024-03-03T09:10:14.059292Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"criterion = nn.CrossEntropyLoss()\ndnn_optimizer = torch.optim.Adam(dnn_model.parameters(), lr=0.001)\ncnn_optimizer = torch.optim.Adam(cnn_model.parameters(), lr=0.001)\ncnn_1d_optimizer = torch.optim.Adam(cnn_1d_model.parameters(), lr=0.001)\n# lstm_optimizer = torch.optim.Adam(lstm_model.parameters(), lr=0.001)","metadata":{"execution":{"iopub.status.busy":"2024-03-03T09:10:14.406198Z","iopub.execute_input":"2024-03-03T09:10:14.406635Z","iopub.status.idle":"2024-03-03T09:10:14.412568Z","shell.execute_reply.started":"2024-03-03T09:10:14.406607Z","shell.execute_reply":"2024-03-03T09:10:14.411611Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Training","metadata":{}},{"cell_type":"code","source":"num_epochs = 1","metadata":{"execution":{"iopub.status.busy":"2024-03-03T09:10:15.406634Z","iopub.execute_input":"2024-03-03T09:10:15.407428Z","iopub.status.idle":"2024-03-03T09:10:15.411878Z","shell.execute_reply.started":"2024-03-03T09:10:15.407383Z","shell.execute_reply":"2024-03-03T09:10:15.410891Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# torch.cuda.empty_cache()","metadata":{"execution":{"iopub.status.busy":"2024-03-03T09:10:16.689460Z","iopub.execute_input":"2024-03-03T09:10:16.689848Z","iopub.status.idle":"2024-03-03T09:10:16.693979Z","shell.execute_reply.started":"2024-03-03T09:10:16.689819Z","shell.execute_reply":"2024-03-03T09:10:16.693050Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## DNN ","metadata":{}},{"cell_type":"code","source":"# for epoch in range(num_epochs):\n#     print(f\"DNN Epoch {epoch+1}/{num_epochs}\")\n    \n    \n#     train_loss = 0\n#     dnn_model.train()\n#     for eeg, spectrogram, labels in dataloader:\n#         eeg, labels = eeg.to(device), labels.to(device)\n        \n\n#         dnn_optimizer.zero_grad()\n#         outputs = dnn_model(eeg)\n#         loss = criterion(outputs, labels)\n#         loss.backward()\n#         dnn_optimizer.step()\n#         train_loss += loss.item()\n\n        \n      \n#     # step the scheduler\n#     train_loss /= len(train_loader)\n#     # append all the losses for each epoch\n#     train_loss_values.append(train_loss)\n#     print(f\"Epoch: {epoch + 1} | training loss: {train_loss:.4f}\")\n\n# print(\"DNN Training completed\")","metadata":{"execution":{"iopub.status.busy":"2024-03-03T07:39:18.173629Z","iopub.execute_input":"2024-03-03T07:39:18.173923Z","iopub.status.idle":"2024-03-03T07:39:18.182556Z","shell.execute_reply.started":"2024-03-03T07:39:18.173899Z","shell.execute_reply":"2024-03-03T07:39:18.181820Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## CNN","metadata":{}},{"cell_type":"markdown","source":"### 1D","metadata":{}},{"cell_type":"code","source":"torch.cuda.empty_cache()\ntrain_loss_values = []\nnan_counts = 0\nfor epoch in range(num_epochs):\n    print(f\"CNN Epoch {epoch+1}/{num_epochs}\")\n    \n    \n    train_loss = 0\n    cnn_1d_model.train()\n    for eeg, spectrogram, labels in dataloader:\n        eeg,spectrogram, labels = eeg.to(device),spectrogram.to(device) , labels.to(device)\n        cnn_1d_optimizer.zero_grad()\n        outputs = cnn_1d_model(eeg)\n        \n        loss = criterion(outputs, labels)\n        \n        if math.isnan(loss.item()):\n            nan_counts = nan_counts +1\n            continue\n        \n        loss.backward()\n        cnn_1d_optimizer.step()\n        train_loss += loss.item()\n        \n\n    train_loss /= len(dataloader)\n    train_loss_values.append(train_loss)\n    print(f\"Epoch: {epoch + 1} | training loss: {train_loss:.4f}\")\n\nprint(\"1D CNN Training completed\")","metadata":{"execution":{"iopub.status.busy":"2024-03-03T07:39:18.183760Z","iopub.execute_input":"2024-03-03T07:39:18.184033Z","iopub.status.idle":"2024-03-03T08:57:52.866725Z","shell.execute_reply.started":"2024-03-03T07:39:18.183992Z","shell.execute_reply":"2024-03-03T08:57:52.865615Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(nan_counts)\nprint(nan_counts/len(dataloader))\nprint((nan_counts/len(dataloader))*100)","metadata":{"execution":{"iopub.status.busy":"2024-03-03T08:57:52.868255Z","iopub.execute_input":"2024-03-03T08:57:52.868841Z","iopub.status.idle":"2024-03-03T08:57:52.873946Z","shell.execute_reply.started":"2024-03-03T08:57:52.868805Z","shell.execute_reply":"2024-03-03T08:57:52.873154Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"torch.save(cnn_1d_model.state_dict(), 'cnn_1d_model.pth')","metadata":{"execution":{"iopub.status.busy":"2024-03-03T08:57:52.874938Z","iopub.execute_input":"2024-03-03T08:57:52.875204Z","iopub.status.idle":"2024-03-03T08:57:52.905668Z","shell.execute_reply.started":"2024-03-03T08:57:52.875181Z","shell.execute_reply":"2024-03-03T08:57:52.904827Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### 2D","metadata":{}},{"cell_type":"code","source":"torch.cuda.empty_cache()\ntrain_loss_values = []\nnan_counts = 0\nfor epoch in range(num_epochs):\n    print(f\"CNN Epoch {epoch+1}/{num_epochs}\")\n    \n    \n    train_loss = 0\n    cnn_model.train()\n    for eeg, spectrogram, labels in dataloader:\n        eeg,spectrogram, labels = eeg.to(device),spectrogram.to(device) , labels.to(device)\n        cnn_optimizer.zero_grad()\n        outputs = cnn_model(eeg)\n        \n        loss = criterion(outputs, labels)\n        \n        if math.isnan(loss.item()):\n            nan_counts = nan_counts +1\n            continue\n        \n        loss.backward()\n        cnn_optimizer.step()\n        train_loss += loss.item()\n        \n\n    train_loss /= len(dataloader)\n    train_loss_values.append(train_loss)\n    print(f\"Epoch: {epoch + 1} | training loss: {train_loss:.4f}\")\n\nprint(\"2D CNN Training completed\")","metadata":{"execution":{"iopub.status.busy":"2024-03-03T09:10:25.852329Z","iopub.execute_input":"2024-03-03T09:10:25.852692Z","iopub.status.idle":"2024-03-03T10:52:30.845971Z","shell.execute_reply.started":"2024-03-03T09:10:25.852665Z","shell.execute_reply":"2024-03-03T10:52:30.845015Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(nan_counts)\nprint(nan_counts/len(dataloader))\nprint((nan_counts/len(dataloader))*100)","metadata":{"execution":{"iopub.status.busy":"2024-03-03T10:52:30.847713Z","iopub.execute_input":"2024-03-03T10:52:30.848054Z","iopub.status.idle":"2024-03-03T10:52:30.853312Z","shell.execute_reply.started":"2024-03-03T10:52:30.848026Z","shell.execute_reply":"2024-03-03T10:52:30.852420Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"torch.save(cnn_model.state_dict(), 'cnn_model.pth')","metadata":{"execution":{"iopub.status.busy":"2024-03-03T10:52:30.854347Z","iopub.execute_input":"2024-03-03T10:52:30.854597Z","iopub.status.idle":"2024-03-03T10:52:32.284785Z","shell.execute_reply.started":"2024-03-03T10:52:30.854575Z","shell.execute_reply":"2024-03-03T10:52:32.283753Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## LSTM","metadata":{}},{"cell_type":"code","source":"model = Combined_1D_CNNModel()\nmodel.load_state_dict(torch.load(\"/kaggle/input/conv1d-hbac/cnn_1d_model.pth\"))\nmodel.eval()","metadata":{"execution":{"iopub.status.busy":"2024-03-10T14:24:52.068813Z","iopub.execute_input":"2024-03-10T14:24:52.069180Z","iopub.status.idle":"2024-03-10T14:24:52.294304Z","shell.execute_reply.started":"2024-03-10T14:24:52.069148Z","shell.execute_reply":"2024-03-10T14:24:52.293400Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = pd.read_parquet('/kaggle/input/hms-harmful-brain-activity-classification/test_eegs/3911565283.parquet')\ntest = test.iloc[4000:6000]\ntest = test.to_numpy()\ntest = torch.tensor(test.astype(np.float32))\ntest = test.unsqueeze(0)\ntest.shape","metadata":{"execution":{"iopub.status.busy":"2024-03-10T14:31:32.000339Z","iopub.execute_input":"2024-03-10T14:31:32.001025Z","iopub.status.idle":"2024-03-10T14:31:32.019310Z","shell.execute_reply.started":"2024-03-10T14:31:32.000990Z","shell.execute_reply":"2024-03-10T14:31:32.018390Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"output = model(test)\noutput","metadata":{"execution":{"iopub.status.busy":"2024-03-10T14:31:34.774663Z","iopub.execute_input":"2024-03-10T14:31:34.775308Z","iopub.status.idle":"2024-03-10T14:31:34.858615Z","shell.execute_reply.started":"2024-03-10T14:31:34.775275Z","shell.execute_reply":"2024-03-10T14:31:34.857740Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# next(dnn_model.parameters()).device","metadata":{"execution":{"iopub.status.busy":"2024-03-03T09:08:18.436567Z","iopub.status.idle":"2024-03-03T09:08:18.436870Z","shell.execute_reply.started":"2024-03-03T09:08:18.436716Z","shell.execute_reply":"2024-03-03T09:08:18.436729Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}