{"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":7457433,"sourceType":"competition"}],"dockerImageVersionId":30626,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"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)\nfrom glob import glob\nimport matplotlib.pyplot as plt\n\nfrom scipy import signal","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_kg_hide-output":true,"execution":{"iopub.status.busy":"2024-01-11T03:44:42.450834Z","iopub.execute_input":"2024-01-11T03:44:42.451147Z","iopub.status.idle":"2024-01-11T03:44:42.502042Z","shell.execute_reply.started":"2024-01-11T03:44:42.451123Z","shell.execute_reply":"2024-01-11T03:44:42.501124Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"BASE_PATH = '/kaggle/input/hms-harmful-brain-activity-classification/'","metadata":{"execution":{"iopub.status.busy":"2024-01-11T03:18:27.727484Z","iopub.execute_input":"2024-01-11T03:18:27.727934Z","iopub.status.idle":"2024-01-11T03:18:27.731823Z","shell.execute_reply.started":"2024-01-11T03:18:27.727912Z","shell.execute_reply":"2024-01-11T03:18:27.730979Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.DataFrame({'path': glob(BASE_PATH + '**/*.parquet')})\ndf['test_type'] = df['path'].str.split('/').str.get(-2).str.split('_').str.get(-1)\ndf['id'] = df['path'].str.split('/').str.get(-1).str.split('.').str.get(0)\ndf","metadata":{"execution":{"iopub.status.busy":"2024-01-11T03:18:27.733111Z","iopub.execute_input":"2024-01-11T03:18:27.733930Z","iopub.status.idle":"2024-01-11T03:18:28.750812Z","shell.execute_reply.started":"2024-01-11T03:18:27.733902Z","shell.execute_reply":"2024-01-11T03:18:28.750012Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"We have more eeg data than spectrogram data","metadata":{}},{"cell_type":"code","source":"df['test_type'].value_counts().plot(kind='bar', rot=0)","metadata":{"execution":{"iopub.status.busy":"2024-01-11T03:18:28.752591Z","iopub.execute_input":"2024-01-11T03:18:28.753485Z","iopub.status.idle":"2024-01-11T03:18:28.955260Z","shell.execute_reply.started":"2024-01-11T03:18:28.753457Z","shell.execute_reply":"2024-01-11T03:18:28.954685Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Electroencephalography (EEG) signals\n\nEEG is a no invasive method to record the macroscopic electrical (resting potential -70mV) activity in the brain by placing electrodes on the scalp. The electrodes are placed based on the 10-20 system, a system where the skull is split into increments of 10% or 20% to place the electrodes. Each electorde numbers and labes re based on location, \n\n - F for frontal region, T for temporal, P for parietal, and O for occipital\n - Odd numbers on the left, even on the right\n\n### Screening EEG studies\nThe most popular way to screen EEG is the banana montage shown in the figure below.","metadata":{}},{"cell_type":"markdown","source":"<img src= \"https://eegatlas-online.com/myapplications/images/MON/db.png\" alt =\"EEG\" height=\"100\" align='center'>","metadata":{"execution":{"iopub.status.busy":"2024-01-09T20:28:01.261886Z","iopub.execute_input":"2024-01-09T20:28:01.262303Z","iopub.status.idle":"2024-01-09T20:28:01.269803Z","shell.execute_reply.started":"2024-01-09T20:28:01.262258Z","shell.execute_reply":"2024-01-09T20:28:01.268274Z"}}},{"cell_type":"code","source":"df_eeg = pd.read_parquet(BASE_PATH + 'train_eegs/1000913311.parquet')\ndf_eeg.head()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T03:46:11.928306Z","iopub.execute_input":"2024-01-11T03:46:11.928617Z","iopub.status.idle":"2024-01-11T03:46:11.954646Z","shell.execute_reply.started":"2024-01-11T03:46:11.928596Z","shell.execute_reply":"2024-01-11T03:46:11.953839Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"right_temporal_chain = [('Fp2', 'F8'), ('F8', 'T4'), ('T4', 'T6'), ('T6', 'O2')]\nright_parasagittal_chain = [('Fp2', 'F4'), ('F4', 'C4'), ('C4', 'P4'), ('P4', 'O2')]\nleft_temporal_chain = [('Fp1', 'F7'), ('F7', 'T3'), ('T3', 'T5'), ('T5', 'O1')]\nleft_parasagittal_chain = [('Fp1', 'F3'), ('F3', 'C3'), ('C3', 'P3'), ('P3', 'O1')]\nz_electrodes = [('Fz', 'Cz'), ('Cz', 'Pz')]\nchains = right_temporal_chain + right_parasagittal_chain + left_temporal_chain + left_parasagittal_chain + z_electrodes\nfig, axes = plt.subplots(len(chains), 1, figsize=(12,40))\ntime = 2000\nfrequency = 200\nfor idx, ax in enumerate(axes.flatten()):\n    led1, led2 = chains[idx][0], chains[idx][1]\n    wave = df_eeg[led1] - df_eeg[led2]\n    ax.plot(df_eeg[:time].index/frequency, wave[:time])\n    ax.set_title(f'{led1} - {led2}')\nplt.tight_layout()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T03:34:22.113680Z","iopub.execute_input":"2024-01-11T03:34:22.113992Z","iopub.status.idle":"2024-01-11T03:34:25.409272Z","shell.execute_reply.started":"2024-01-11T03:34:22.113968Z","shell.execute_reply":"2024-01-11T03:34:25.408418Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"The resting negative potentials are upgoing waves, and positive potentials are downgoing waves.","metadata":{}},{"cell_type":"code","source":"# Referential Montages\ndf_eeg.mean(1).plot(kind='line')","metadata":{"execution":{"iopub.status.busy":"2024-01-11T03:30:03.785994Z","iopub.execute_input":"2024-01-11T03:30:03.786297Z","iopub.status.idle":"2024-01-11T03:30:04.355193Z","shell.execute_reply.started":"2024-01-11T03:30:03.786275Z","shell.execute_reply":"2024-01-11T03:30:04.353994Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Power Spectral Density\nWelch method averaging consecutive fourier transforms of small windows","metadata":{}},{"cell_type":"code","source":"fig, axes = plt.subplots(len(df_eeg.columns), 1, figsize=(12,40))\ncols = df_eeg.columns\nfor idx, ax in enumerate(axes.flatten()):\n    f, psd = signal.welch(df_eeg[cols[idx]], fs=200, nperseg=df_eeg.shape[0]//100)\n    ax.plot(f, psd)\n    ax.set_title(cols[idx])","metadata":{"execution":{"iopub.status.busy":"2024-01-11T03:56:15.347442Z","iopub.execute_input":"2024-01-11T03:56:15.347749Z","iopub.status.idle":"2024-01-11T03:56:18.107823Z","shell.execute_reply.started":"2024-01-11T03:56:15.347728Z","shell.execute_reply":"2024-01-11T03:56:18.106754Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"It's strange how they are all maked at the same point, this is all very new to me so I'm not sure if thats correct","metadata":{}},{"cell_type":"markdown","source":"## Spectrogram","metadata":{}},{"cell_type":"code","source":"df_spec = pd.read_parquet(BASE_PATH + 'train_spectrograms/1662527277.parquet')\ndf_spec","metadata":{"execution":{"iopub.status.busy":"2024-01-10T20:25:27.277367Z","iopub.execute_input":"2024-01-10T20:25:27.278013Z","iopub.status.idle":"2024-01-10T20:25:27.368139Z","shell.execute_reply.started":"2024-01-10T20:25:27.277975Z","shell.execute_reply":"2024-01-10T20:25:27.366906Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_spec_num(spectogram_label):\n    fig, axes = plt.subplots(2,2, figsize=(16,8))\n    cols = df_spec.filter(like=spectogram_label).columns\n    for idx, ax in enumerate(axes.flatten()):\n        ax.scatter(x=df_spec['time'], y=df_spec[cols[idx]])\n        ax.set_title(cols[idx])","metadata":{"execution":{"iopub.status.busy":"2024-01-10T20:29:55.791042Z","iopub.execute_input":"2024-01-10T20:29:55.791482Z","iopub.status.idle":"2024-01-10T20:29:55.798360Z","shell.execute_reply.started":"2024-01-10T20:29:55.791448Z","shell.execute_reply":"2024-01-10T20:29:55.797115Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_spectrograms(df):\n    fig, axes = plt.subplots(2,2, figsize=(16,8))\n    views = ['LL', 'RL', 'RP', 'LP']\n    \n    for i, ax in enumerate(axes.flatten()):\n        spec = df.filter(regex=f'^{views[i]}', axis=1)\n        spec = np.log(spec).T\n        ax.imshow(spec,  cmap='seismic', aspect='auto')\n        ax.set_title(views[i])\n        ax.set_ylabel(\"Frequency (Hz)\")\n        ax.set_xlabel(\"Time\")\n    plt.tight_layout()","metadata":{"execution":{"iopub.status.busy":"2024-01-10T20:35:54.948575Z","iopub.execute_input":"2024-01-10T20:35:54.949947Z","iopub.status.idle":"2024-01-10T20:35:54.956725Z","shell.execute_reply.started":"2024-01-10T20:35:54.949903Z","shell.execute_reply":"2024-01-10T20:35:54.955635Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_spectrograms(df_spec)","metadata":{"execution":{"iopub.status.busy":"2024-01-10T20:35:55.126309Z","iopub.execute_input":"2024-01-10T20:35:55.126702Z","iopub.status.idle":"2024-01-10T20:35:56.750420Z","shell.execute_reply.started":"2024-01-10T20:35:55.126672Z","shell.execute_reply":"2024-01-10T20:35:56.749073Z"},"trusted":true},"execution_count":null,"outputs":[]}]}