{"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":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"!pip3 install plotly --upgrade","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport plotly.subplots as sp\nimport plotly.graph_objects as go","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-01-14T18:44:46.924174Z","iopub.execute_input":"2024-01-14T18:44:46.924561Z","iopub.status.idle":"2024-01-14T18:44:47.371269Z","shell.execute_reply.started":"2024-01-14T18:44:46.924529Z","shell.execute_reply":"2024-01-14T18:44:47.370310Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"spect_path_prefix = \"/kaggle/input/hms-harmful-brain-activity-classification/train_spectrograms/\"\ntrain_df = pd.read_csv(\"/kaggle/input/hms-harmful-brain-activity-classification/train.csv\")\ntrain_df.info()","metadata":{"execution":{"iopub.status.busy":"2024-01-14T18:44:47.372533Z","iopub.execute_input":"2024-01-14T18:44:47.372980Z","iopub.status.idle":"2024-01-14T18:44:47.679116Z","shell.execute_reply.started":"2024-01-14T18:44:47.372949Z","shell.execute_reply":"2024-01-14T18:44:47.678031Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Encephalographic DSA is a three-dimensional method to display EEG signals consisting of the EEG frequency (y-axis), the power of the EEG signal (originally the z-axis, but colour-coded to be integrated into a two-dimensional plot) and the development of the EEG power spectrum over time (x-axis). The power spectrum is encoded in different colours; blue implies minimal power and red implies high or maximal power**\n\nRef: https://associationofanaesthetists-publications.onlinelibrary.wiley.com/doi/10.1111/anae.14458\n\nKnow more about the EEG spectography: https://www.kaggle.com/code/seshurajup/eegs-10-20-system\n","metadata":{}},{"cell_type":"code","source":"def plot_spectogram(spec_df, prefixes, title = \"Spectogram\"):\n    fig = sp.make_subplots(rows=len(prefixes), cols=1, subplot_titles=prefixes)\n    for i, prefix in enumerate(prefixes):\n        prefix_df = spec_df.filter(regex=f'^{prefix}', axis=1)\n        epsilon = 1e-10\n        fig.add_trace(go.Heatmap(z=np.log(prefix_df + epsilon).T,\n                                 y=pd.to_numeric(prefix_df.columns.str.replace(f\"{prefix}_\", '')),\n                                 coloraxis=\"coloraxis\"),\n                      row=i+1, col=1)\n         # Update x-axis and y-axis labels\n        fig.update_xaxes(title_text=\"Time(Seconds)\", row=i+1, col=1)\n        fig.update_yaxes(title_text=\"Frequency(Hz)\", row=i+1, col=1)\n        # update coloraxis\n        fig.update_layout(coloraxis = {'colorscale':'Jet'}, height=1500,title_text=title)\n    fig.show()","metadata":{"execution":{"iopub.status.busy":"2024-01-14T18:44:47.681736Z","iopub.execute_input":"2024-01-14T18:44:47.682176Z","iopub.status.idle":"2024-01-14T18:44:47.692519Z","shell.execute_reply.started":"2024-01-14T18:44:47.682121Z","shell.execute_reply":"2024-01-14T18:44:47.691361Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Plot 5 spectograms with maximum votes","metadata":{}},{"cell_type":"code","source":"# Sum all the votes\ntrain_df['total_votes'] = train_df[['seizure_vote', 'lpd_vote', 'gpd_vote', 'lrda_vote', 'grda_vote', 'other_vote']].sum(axis=1)\n# Find the maximum votes and corresponding index\nmax_votes_df = train_df.loc[train_df.groupby('spectrogram_id')['total_votes'].idxmax()]\n# Sort the DataFrame in descending order based on total_votes\nmax_votes_df_sorted = max_votes_df.sort_values(by='total_votes', ascending=False).head()\nmax_votes_df_sorted.head()","metadata":{"execution":{"iopub.status.busy":"2024-01-14T18:44:47.694411Z","iopub.execute_input":"2024-01-14T18:44:47.694842Z","iopub.status.idle":"2024-01-14T18:44:48.592740Z","shell.execute_reply.started":"2024-01-14T18:44:47.694802Z","shell.execute_reply":"2024-01-14T18:44:48.591517Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## Observing the spectograms with max votes\n# Iterate over the rows of the sorted DataFrame\nfor index, row in max_votes_df_sorted.iterrows():\n    print(row['spectrogram_id'])\n    spectrogram_id = row['spectrogram_id']\n    total_votes = row[\"total_votes\"]\n    spectogram_path = f\"{spect_path_prefix}{spectrogram_id}.parquet\"\n    spec_df = pd.read_parquet(spectogram_path)\n    plot_spectogram(spec_df,[\"LL\",\"RL\",\"RP\",\"LP\"], title=f\"Vote count = {total_votes}\")","metadata":{"execution":{"iopub.status.busy":"2024-01-14T18:44:48.593877Z","iopub.execute_input":"2024-01-14T18:44:48.594211Z","iopub.status.idle":"2024-01-14T18:44:49.798870Z","shell.execute_reply.started":"2024-01-14T18:44:48.594170Z","shell.execute_reply":"2024-01-14T18:44:49.797661Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Plot 5 spectograms with minimum votes","metadata":{}},{"cell_type":"code","source":"# Sum all the votes\ntrain_df['total_votes'] = train_df[['seizure_vote', 'lpd_vote', 'gpd_vote', 'lrda_vote', 'grda_vote', 'other_vote']].sum(axis=1)\n# Find the maximum votes and corresponding index\nmin_votes_df = train_df.loc[train_df.groupby('spectrogram_id')['total_votes'].idxmin()]\n# Sort the DataFrame in descending order based on total_votes\nmin_votes_df_sorted = min_votes_df.sort_values(by='total_votes').head()\nmin_votes_df_sorted.head()","metadata":{"execution":{"iopub.status.busy":"2024-01-14T18:44:49.800262Z","iopub.execute_input":"2024-01-14T18:44:49.800639Z","iopub.status.idle":"2024-01-14T18:44:50.727250Z","shell.execute_reply.started":"2024-01-14T18:44:49.800606Z","shell.execute_reply":"2024-01-14T18:44:50.726197Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## Observing the spectograms with min votes\n# Iterate over the rows of the sorted DataFrame\nfor index, row in min_votes_df_sorted.iterrows():\n    print(row['spectrogram_id'])\n    spectrogram_id = row['spectrogram_id']\n    total_votes = row[\"total_votes\"]\n    spectogram_path = f\"{spect_path_prefix}{spectrogram_id}.parquet\"\n    spec_df = pd.read_parquet(spectogram_path)\n    plot_spectogram(spec_df,[\"LL\",\"RL\",\"RP\",\"LP\"], title=f\"Vote count = {total_votes}\")","metadata":{"execution":{"iopub.status.busy":"2024-01-14T18:45:48.580077Z","iopub.execute_input":"2024-01-14T18:45:48.580530Z","iopub.status.idle":"2024-01-14T18:45:49.394794Z","shell.execute_reply.started":"2024-01-14T18:45:48.580492Z","shell.execute_reply":"2024-01-14T18:45:49.393322Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Acknowledgement\n- https://www.kaggle.com/code/mpwolke/seizures-classification-parquet\n- https://www.kaggle.com/code/clehmann10/plot-spectrograms/notebook","metadata":{}}]}