{"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":30646,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Code to retrieve EEG and Spectrogram\n\n### Source: https://www.kaggle.com/competitions/hms-harmful-brain-activity-classification/discussion/468010\n\nFor a specific row of train.csv, here is code to retrieve the corresponding EEG and Spectrogram.","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\n\nEEG_PATH = '/kaggle/input/hms-harmful-brain-activity-classification/train_eegs/'\nSPEC_PATH = '/kaggle/input/hms-harmful-brain-activity-classification/train_spectrograms/'\ntrain_dataframe = pd.read_csv('/kaggle/input/hms-harmful-brain-activity-classification/train.csv')\n\nfor i in range (0, 50):\n    # ROW INDEX\n    GET_ROW = i\n    row = train_dataframe.iloc[i]\n    \n    # EEG\n    eeg = pd.read_parquet(f'{EEG_PATH}{row.eeg_id}.parquet')\n    eeg_offset = int( row.eeg_label_offset_seconds )\n    eeg = eeg.iloc[eeg_offset*200:(eeg_offset+50)*200]\n    eeg = eeg.iloc[4000:6000]\n    eeg.to_parquet(f'/kaggle/working/{row.eeg_id}_subsample_{row.eeg_sub_id}.parquet', index=False)\"\"\"\n    \n    \"\"\"\n    # SPECTROGRAM\n    spectrogram = pd.read_parquet(f'{SPEC_PATH}{row.spectrogram_id}.parquet')\n    spec_offset = int( row.spectrogram_label_offset_seconds )\n    spectrogram = spectrogram.loc[(spectrogram.time>=spec_offset)&(spectrogram.time<spec_offset+600)]\n    spectrogram = spectrogram[(spectrogram['time'] >= (300 + spec_offset - 5)) & (spectrogram['time'] <= (300 + spec_offset + 5))]\n    spectrogram.to_parquet(f'/kaggle/working/{row.spectrogram_id}_subsample_{row.spectrogram_sub_id}.parquet', index=False)\n    \"\"\"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Finish","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"row = train.iloc[GET_ROW]\nprint(row)","metadata":{"execution":{"iopub.status.busy":"2024-02-17T09:35:41.450688Z","iopub.execute_input":"2024-02-17T09:35:41.451189Z","iopub.status.idle":"2024-02-17T09:35:41.793771Z","shell.execute_reply.started":"2024-02-17T09:35:41.451158Z","shell.execute_reply":"2024-02-17T09:35:41.791799Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(f'{EEG_PATH}{row.eeg_id}.parquet')\neeg = pd.read_parquet(f'{EEG_PATH}{row.eeg_id}.parquet')","metadata":{"execution":{"iopub.status.busy":"2024-02-17T09:35:43.797066Z","iopub.execute_input":"2024-02-17T09:35:43.797520Z","iopub.status.idle":"2024-02-17T09:35:44.016348Z","shell.execute_reply.started":"2024-02-17T09:35:43.797469Z","shell.execute_reply":"2024-02-17T09:35:44.015035Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print ( int(row.eeg_label_offset_seconds) )\neeg_offset = int( row.eeg_label_offset_seconds )\nprint(eeg_offset)\neeg = eeg.iloc[eeg_offset*200:(eeg_offset+50)*200]","metadata":{"execution":{"iopub.status.busy":"2024-02-17T09:35:45.615079Z","iopub.execute_input":"2024-02-17T09:35:45.616387Z","iopub.status.idle":"2024-02-17T09:35:45.625344Z","shell.execute_reply.started":"2024-02-17T09:35:45.616338Z","shell.execute_reply":"2024-02-17T09:35:45.623495Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"eeg","metadata":{"execution":{"iopub.status.busy":"2024-02-17T09:35:50.659684Z","iopub.execute_input":"2024-02-17T09:35:50.660059Z","iopub.status.idle":"2024-02-17T09:35:50.701428Z","shell.execute_reply.started":"2024-02-17T09:35:50.660029Z","shell.execute_reply":"2024-02-17T09:35:50.700145Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"eeg = eeg.iloc[4000:6000]","metadata":{"execution":{"iopub.status.busy":"2024-02-17T09:35:54.762536Z","iopub.execute_input":"2024-02-17T09:35:54.763005Z","iopub.status.idle":"2024-02-17T09:35:54.769905Z","shell.execute_reply.started":"2024-02-17T09:35:54.762972Z","shell.execute_reply":"2024-02-17T09:35:54.768146Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"eeg","metadata":{"execution":{"iopub.status.busy":"2024-02-17T09:37:36.394619Z","iopub.execute_input":"2024-02-17T09:37:36.395236Z","iopub.status.idle":"2024-02-17T09:37:36.438751Z","shell.execute_reply.started":"2024-02-17T09:37:36.395196Z","shell.execute_reply":"2024-02-17T09:37:36.437516Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"eeg.to_parquet(f'/kaggle/working/{row.eeg_id}_subsample_{row.eeg_sub_id}.parquet', index=False)","metadata":{"execution":{"iopub.status.busy":"2024-02-17T09:36:02.432924Z","iopub.execute_input":"2024-02-17T09:36:02.433357Z","iopub.status.idle":"2024-02-17T09:36:02.454167Z","shell.execute_reply.started":"2024-02-17T09:36:02.433324Z","shell.execute_reply":"2024-02-17T09:36:02.453174Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(f'{SPEC_PATH}{row.spectrogram_id}.parquet')\nspectrogram = pd.read_parquet(f'{SPEC_PATH}{row.spectrogram_id}.parquet')","metadata":{"execution":{"iopub.status.busy":"2024-02-17T09:10:23.363835Z","iopub.execute_input":"2024-02-17T09:10:23.364299Z","iopub.status.idle":"2024-02-17T09:10:23.443490Z","shell.execute_reply.started":"2024-02-17T09:10:23.364253Z","shell.execute_reply":"2024-02-17T09:10:23.442483Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sampling_frequency = 200","metadata":{"execution":{"iopub.status.busy":"2024-02-17T09:10:21.487922Z","iopub.execute_input":"2024-02-17T09:10:21.490570Z","iopub.status.idle":"2024-02-17T09:10:21.498049Z","shell.execute_reply.started":"2024-02-17T09:10:21.490518Z","shell.execute_reply":"2024-02-17T09:10:21.496613Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(int( row.spectrogram_label_offset_seconds ))\nspec_offset = int( row.spectrogram_label_offset_seconds )\nprint(spec_offset)\nspectrogram = spectrogram.loc[(spectrogram.time>=spec_offset)&(spectrogram.time<spec_offset+600)]","metadata":{"execution":{"iopub.status.busy":"2024-02-17T09:10:24.688373Z","iopub.execute_input":"2024-02-17T09:10:24.688809Z","iopub.status.idle":"2024-02-17T09:10:24.698856Z","shell.execute_reply.started":"2024-02-17T09:10:24.688775Z","shell.execute_reply":"2024-02-17T09:10:24.697330Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"spectrogram","metadata":{"execution":{"iopub.status.busy":"2024-02-17T09:10:26.684264Z","iopub.execute_input":"2024-02-17T09:10:26.685164Z","iopub.status.idle":"2024-02-17T09:10:26.731600Z","shell.execute_reply.started":"2024-02-17T09:10:26.685113Z","shell.execute_reply":"2024-02-17T09:10:26.730320Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"spectrogram.iloc[0]","metadata":{"execution":{"iopub.status.busy":"2024-02-17T09:10:31.715910Z","iopub.execute_input":"2024-02-17T09:10:31.716787Z","iopub.status.idle":"2024-02-17T09:10:31.729063Z","shell.execute_reply.started":"2024-02-17T09:10:31.716742Z","shell.execute_reply":"2024-02-17T09:10:31.727646Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"spectrogram = spectrogram[(spectrogram['time'] >= 335) & (spectrogram['time'] <= 345)]","metadata":{"execution":{"iopub.status.busy":"2024-02-17T09:10:33.631719Z","iopub.execute_input":"2024-02-17T09:10:33.632112Z","iopub.status.idle":"2024-02-17T09:10:33.639780Z","shell.execute_reply.started":"2024-02-17T09:10:33.632082Z","shell.execute_reply":"2024-02-17T09:10:33.638348Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"spectrogram","metadata":{"execution":{"iopub.status.busy":"2024-02-17T09:10:34.327643Z","iopub.execute_input":"2024-02-17T09:10:34.328026Z","iopub.status.idle":"2024-02-17T09:10:34.361526Z","shell.execute_reply.started":"2024-02-17T09:10:34.327998Z","shell.execute_reply":"2024-02-17T09:10:34.360540Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}