{"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":"none","dataSources":[{"sourceId":59093,"databundleVersionId":7469972,"sourceType":"competition"}],"dockerImageVersionId":30673,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\n\n\nos.listdir(\"/kaggle/working\")\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-03-28T01:49:23.309712Z","iopub.execute_input":"2024-03-28T01:49:23.310126Z","iopub.status.idle":"2024-03-28T01:49:23.320814Z","shell.execute_reply.started":"2024-03-28T01:49:23.310096Z","shell.execute_reply":"2024-03-28T01:49:23.319369Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nimport pyarrow.parquet as pq\nimport numpy as np\n\neeg_list = os.listdir(\"/kaggle/input/hms-harmful-brain-activity-classification/train_eegs\")\nmeta = pd.read_csv(\"/kaggle/input/hms-harmful-brain-activity-classification/train.csv\")\n\na = 0\nb = 0\n\nfor eeg in eeg_list :\n    ID = int(eeg.split('.')[0])\n    Y = meta[\"expert_consensus\"].iloc[meta.index[(meta[\"eeg_id\"] == ID)]]\n    if Y.all():\n        a += 1\n    else:\n        b+= 1\n        print(ID)\n        print(c)\n    \n\n\nprint(\"same : \", a, \"\\ndifferent :\", b)","metadata":{"execution":{"iopub.status.busy":"2024-03-28T00:19:52.147559Z","iopub.execute_input":"2024-03-28T00:19:52.14834Z","iopub.status.idle":"2024-03-28T00:19:59.866478Z","shell.execute_reply.started":"2024-03-28T00:19:52.148291Z","shell.execute_reply":"2024-03-28T00:19:59.865218Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"!pip install torch-geometric\n","metadata":{"execution":{"iopub.status.busy":"2024-03-28T02:10:08.358506Z","iopub.execute_input":"2024-03-28T02:10:08.359223Z","iopub.status.idle":"2024-03-28T02:10:24.181955Z","shell.execute_reply.started":"2024-03-28T02:10:08.359185Z","shell.execute_reply":"2024-03-28T02:10:24.179963Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\nimport torch.nn as nn\nfrom torch_geometric.nn import GCNConv\nimport torch.nn.functional as F\n\nclass DynamicGCN(torch.nn.Module):\n    def __init__(self, num_node_features, hidden, num_classes, num_layers):\n        super().__init__()\n        self.num_layers = num_layers\n        self.Gconvs = nn.ModuleList([GCNConv(num_node_features, hidden) for _ in range(num_layers)])\n        self.fc_channel1 = nn.Linear(hidden, 3)\n        self.fc_channel2 = nn.Linear(3, hidden)\n        \n        self.fc_spatial = nn.Conv1d(num_layers, 1, kernel_size=1)\n        self.fc_class = nn.Linear(hidden, num_classes)\n        self.dropout = dropout\n        \n    def forward(self, data):\n        result = []\n        for conv in self.Gconvs:\n            x = conv(x, edge_index)\n            x = F.dropout(x, p=self.dropout, training=self.training)\n            result.append(x)\n        \n        result = torch.stack(result, dim=0)\n        \n        avg_pool = torch.mean(result, dim=2)\n        max_pool, _ = torch.max(result, dim=2)\n        \n        avg_pool = F.relu(self.fc_channel1(avg_pool))\n        max_pool = F.relu(self.fc_channel1(max_pool))\n        \n        fc_out1 = self.fc_channel2(avg_pool)\n        fc_out2 = self.fc_channel2(max_pool)\n\n        channel_attention = torch.sigmoid(fc_out1 + fc_out2)\n\n        avg_pool = torch.mean(result, dim=1, keepdim=True) \n        max_pool, _ = torch.max(result, dim=1, keepdim=True)\n\n        spatial_out = torch.cat([avg_pool, max_pool], dim=1) \n        spatial_attention = torch.sigmoid(self.fc_spatial(spatial_out)) \n        \n        channel_attention = channel_attention.unsqueeze(2)\n        spatial_attention = spatial_attention.unsqueeze(1)\n\n        result = channel_attention * spatial_attention * result\n\n        result = result.mean(dim=0)\n        result = F.dropout(result, p=self.dropout, training=self.training)\n        class_probs = F.softmax(self.fc_class(result), dim=1)\n\n        return class_probs","metadata":{"execution":{"iopub.status.busy":"2024-03-28T02:10:34.151981Z","iopub.execute_input":"2024-03-28T02:10:34.152374Z","iopub.status.idle":"2024-03-28T02:10:34.169526Z","shell.execute_reply.started":"2024-03-28T02:10:34.152344Z","shell.execute_reply":"2024-03-28T02:10:34.168024Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = GCN()\nmodel()","metadata":{"execution":{"iopub.status.busy":"2024-03-28T00:18:35.806919Z","iopub.execute_input":"2024-03-28T00:18:35.807913Z","iopub.status.idle":"2024-03-28T00:18:35.819191Z","shell.execute_reply.started":"2024-03-28T00:18:35.807873Z","shell.execute_reply":"2024-03-28T00:18:35.81783Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"m.iloc[m.index[(m[\"eeg_id\"] == 351917269)]]","metadata":{"execution":{"iopub.status.busy":"2024-03-27T15:43:44.87816Z","iopub.execute_input":"2024-03-27T15:43:44.878521Z","iopub.status.idle":"2024-03-27T15:43:44.898322Z","shell.execute_reply.started":"2024-03-27T15:43:44.878494Z","shell.execute_reply":"2024-03-27T15:43:44.897316Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"eeg = pq.read_table(\"/kaggle/input/hms-harmful-brain-activity-classification/train_spectrograms/2147388374.parquet\")\neeg.to_pandas()","metadata":{"execution":{"iopub.status.busy":"2024-03-27T15:43:56.521995Z","iopub.execute_input":"2024-03-27T15:43:56.52239Z","iopub.status.idle":"2024-03-27T15:43:56.595289Z","shell.execute_reply.started":"2024-03-27T15:43:56.522362Z","shell.execute_reply":"2024-03-27T15:43:56.594434Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}