{"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":"none","dataSources":[{"sourceId":59093,"databundleVersionId":7469972,"sourceType":"competition"}],"dockerImageVersionId":30635,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"**In this notebook, we explore the EEG dataset available in the competition. We have used various other kaggle notebooks to draw inspiration and ideas.**\n\n1. https://www.kaggle.com/code/user0938479/baseline-improving?scriptVersionId=161904725\n2. https://www.kaggle.com/code/seshurajup/eda-train-csv","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nBASE_PATH = '/kaggle/input/hms-harmful-brain-activity-classification/'\ntrain = pd.read_csv(\"/kaggle/input/hms-harmful-brain-activity-classification/train.csv\")\n\ntrain.head()","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-03-07T20:58:34.012578Z","iopub.execute_input":"2024-03-07T20:58:34.013044Z","iopub.status.idle":"2024-03-07T20:58:34.241139Z","shell.execute_reply.started":"2024-03-07T20:58:34.013006Z","shell.execute_reply":"2024-03-07T20:58:34.239771Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rows, columns = train.shape\nrows, columns","metadata":{"execution":{"iopub.status.busy":"2024-03-07T20:59:12.423262Z","iopub.execute_input":"2024-03-07T20:59:12.423713Z","iopub.status.idle":"2024-03-07T20:59:12.431912Z","shell.execute_reply.started":"2024-03-07T20:59:12.423677Z","shell.execute_reply":"2024-03-07T20:59:12.430385Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_info = train.info()\ntrain_info","metadata":{"execution":{"iopub.status.busy":"2024-03-07T20:59:12.902610Z","iopub.execute_input":"2024-03-07T20:59:12.903097Z","iopub.status.idle":"2024-03-07T20:59:12.930989Z","shell.execute_reply.started":"2024-03-07T20:59:12.903059Z","shell.execute_reply":"2024-03-07T20:59:12.929190Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.describe()","metadata":{"execution":{"iopub.status.busy":"2024-03-07T20:59:13.138824Z","iopub.execute_input":"2024-03-07T20:59:13.139532Z","iopub.status.idle":"2024-03-07T20:59:13.245319Z","shell.execute_reply.started":"2024-03-07T20:59:13.139493Z","shell.execute_reply":"2024-03-07T20:59:13.244014Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"list(set(train['expert_consensus'].unique()))\n# This shows us the 6 unique labels given by the experts","metadata":{"execution":{"iopub.status.busy":"2024-03-07T20:59:47.615371Z","iopub.execute_input":"2024-03-07T20:59:47.615875Z","iopub.status.idle":"2024-03-07T20:59:47.634278Z","shell.execute_reply.started":"2024-03-07T20:59:47.615829Z","shell.execute_reply":"2024-03-07T20:59:47.632826Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(10, 6))\nsns.countplot(data=train, x='expert_consensus', palette='rocket')\nplt.title('Expert Consensus')\nplt.xlabel('Expert Consensus')\nplt.ylabel('Count')\nplt.xticks(rotation=45)\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2024-03-07T20:59:48.730740Z","iopub.execute_input":"2024-03-07T20:59:48.731194Z","iopub.status.idle":"2024-03-07T20:59:49.211701Z","shell.execute_reply.started":"2024-03-07T20:59:48.731157Z","shell.execute_reply":"2024-03-07T20:59:49.210165Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_eeg = pd.read_parquet(BASE_PATH + 'train_eegs/1628180742.parquet')\ndf_eeg.head()","metadata":{"execution":{"iopub.status.busy":"2024-03-07T20:59:50.257169Z","iopub.execute_input":"2024-03-07T20:59:50.257627Z","iopub.status.idle":"2024-03-07T20:59:50.298116Z","shell.execute_reply.started":"2024-03-07T20:59:50.257580Z","shell.execute_reply":"2024-03-07T20:59:50.296690Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"n_channels = df_eeg.shape\nn_channels\n\n# this is the no of channels ","metadata":{"execution":{"iopub.status.busy":"2024-03-07T21:00:21.645585Z","iopub.execute_input":"2024-03-07T21:00:21.646087Z","iopub.status.idle":"2024-03-07T21:00:21.654946Z","shell.execute_reply.started":"2024-03-07T21:00:21.646043Z","shell.execute_reply":"2024-03-07T21:00:21.653364Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"targets = ['seizure_vote', 'lpd_vote', 'gpd_vote', 'lrda_vote', 'grda_vote', 'other_vote']\n\nplt.figure(figsize=(15, 10))\nfor i, column in enumerate(targets, 1):\n    plt.subplot(2, 4, i)\n    sns.histplot(train[column], kde=False, bins=30)\n    plt.title(column)\nplt.tight_layout()","metadata":{"execution":{"iopub.status.busy":"2024-03-07T20:59:55.463995Z","iopub.execute_input":"2024-03-07T20:59:55.464429Z","iopub.status.idle":"2024-03-07T20:59:58.746015Z","shell.execute_reply.started":"2024-03-07T20:59:55.464394Z","shell.execute_reply":"2024-03-07T20:59:58.743843Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"offset_stats = train[['eeg_label_offset_seconds', 'spectrogram_label_offset_seconds']].describe()\noffset_stats","metadata":{"execution":{"iopub.status.busy":"2024-03-07T21:00:26.118044Z","iopub.execute_input":"2024-03-07T21:00:26.118540Z","iopub.status.idle":"2024-03-07T21:00:26.152137Z","shell.execute_reply.started":"2024-03-07T21:00:26.118500Z","shell.execute_reply":"2024-03-07T21:00:26.150864Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"total_eegs = len(train['eeg_id'].unique())\ntotal_eegs","metadata":{"execution":{"iopub.status.busy":"2024-03-07T21:00:28.632861Z","iopub.execute_input":"2024-03-07T21:00:28.633301Z","iopub.status.idle":"2024-03-07T21:00:28.646684Z","shell.execute_reply.started":"2024-03-07T21:00:28.633268Z","shell.execute_reply":"2024-03-07T21:00:28.645169Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"vote_counts_by_consensus = train.groupby('expert_consensus')[targets].sum()\npalette = sns.color_palette('rocket', n_colors=len(targets))\nplt.figure(figsize=(12, 8))\nvote_counts_by_consensus.plot(kind='bar', stacked=True, color=palette)\n\nplt.title('Overall Vote Counts')\nplt.xlabel('Expert Consensus')\nplt.ylabel('Total Votes')\nplt.xticks(rotation=45)\nplt.legend(title='Vote Types', bbox_to_anchor=(1.05, 1), loc=2)\n\nplt.show()\n\n\n\n","metadata":{"execution":{"iopub.status.busy":"2024-03-07T21:00:30.951805Z","iopub.execute_input":"2024-03-07T21:00:30.952585Z","iopub.status.idle":"2024-03-07T21:00:31.454511Z","shell.execute_reply.started":"2024-03-07T21:00:30.952544Z","shell.execute_reply":"2024-03-07T21:00:31.453052Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cumulative_votes = train.groupby('eeg_label_offset_seconds')[targets].sum().cumsum().reset_index()\n\nplt.figure(figsize=(12, 8))\nfor column in targets:\n    plt.plot(cumulative_votes['eeg_label_offset_seconds'], cumulative_votes[column], label=column)\n\nplt.title('Vote Counts Over EEG Label Offset Seconds')\nplt.xlabel('EEG Label Offset Seconds')\nplt.ylabel('Total Votes')\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-03-07T21:00:40.571802Z","iopub.execute_input":"2024-03-07T21:00:40.572923Z","iopub.status.idle":"2024-03-07T21:00:41.095543Z","shell.execute_reply.started":"2024-03-07T21:00:40.572875Z","shell.execute_reply":"2024-03-07T21:00:41.093966Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Chris's idea to calculate Mean Vote Ratio\ntotal_votes_per_pat = train.groupby('patient_id')[targets].sum().sum(axis=1)\nnormalized_votes = train.groupby('patient_id')[targets].sum().div(total_votes_per_pat, axis=0)\nmean_vote_ratio = normalized_votes.mean()\nprint( mean_vote_ratio )","metadata":{"execution":{"iopub.status.busy":"2024-03-07T21:05:56.104893Z","iopub.execute_input":"2024-03-07T21:05:56.105373Z","iopub.status.idle":"2024-03-07T21:05:56.134523Z","shell.execute_reply.started":"2024-03-07T21:05:56.105338Z","shell.execute_reply":"2024-03-07T21:05:56.133268Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"gap = 1 - sum([round(v,6) for _, v in mean_vote_ratio.items()])\nprint(gap)\nmean_vote_ratio['other_vote'] += gap\n\n\nsum([round(v,5) for _, v in mean_vote_ratio.items()])","metadata":{"execution":{"iopub.status.busy":"2024-03-07T21:05:57.939281Z","iopub.execute_input":"2024-03-07T21:05:57.939758Z","iopub.status.idle":"2024-03-07T21:05:57.951088Z","shell.execute_reply.started":"2024-03-07T21:05:57.939695Z","shell.execute_reply":"2024-03-07T21:05:57.949570Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mean_vote_ratio","metadata":{"execution":{"iopub.status.busy":"2024-03-07T21:05:59.938085Z","iopub.execute_input":"2024-03-07T21:05:59.938972Z","iopub.status.idle":"2024-03-07T21:05:59.966867Z","shell.execute_reply.started":"2024-03-07T21:05:59.938901Z","shell.execute_reply":"2024-03-07T21:05:59.964768Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub = pd.read_csv(\"/kaggle/input/hms-harmful-brain-activity-classification/sample_submission.csv\")\nfor target in targets:\n    sub[target] = mean_vote_ratio[target]\n\nsub.to_csv(\"/kaggle/working/submission.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2024-03-07T21:07:43.502542Z","iopub.execute_input":"2024-03-07T21:07:43.504051Z","iopub.status.idle":"2024-03-07T21:07:43.523431Z","shell.execute_reply.started":"2024-03-07T21:07:43.504002Z","shell.execute_reply":"2024-03-07T21:07:43.521777Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}