{"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":"code","source":"import numpy as np \nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport librosa\nimport os","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-02-18T05:44:24.949561Z","iopub.execute_input":"2024-02-18T05:44:24.949987Z","iopub.status.idle":"2024-02-18T05:44:25.436388Z","shell.execute_reply.started":"2024-02-18T05:44:24.949945Z","shell.execute_reply":"2024-02-18T05:44:25.435329Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv('/kaggle/input/hms-harmful-brain-activity-classification/train.csv')\ntrain_eeg_path = '/kaggle/input/hms-harmful-brain-activity-classification/train_eegs/'","metadata":{"execution":{"iopub.status.busy":"2024-02-18T05:44:25.438181Z","iopub.execute_input":"2024-02-18T05:44:25.438649Z","iopub.status.idle":"2024-02-18T05:44:25.734664Z","shell.execute_reply.started":"2024-02-18T05:44:25.438618Z","shell.execute_reply":"2024-02-18T05:44:25.733472Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2024-02-18T05:44:25.736069Z","iopub.execute_input":"2024-02-18T05:44:25.736532Z","iopub.status.idle":"2024-02-18T05:44:25.764712Z","shell.execute_reply.started":"2024-02-18T05:44:25.736492Z","shell.execute_reply":"2024-02-18T05:44:25.763495Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# MEL_SPECTROGRAM Function","metadata":{}},{"cell_type":"code","source":"import librosa\n\ndef spectrogram_from_eeg(parquet_path, display=True):\n    \n    # LOAD MIDDLE 50 SECONDS OF EEG SERIES\n    eeg = pd.read_parquet(parquet_path)\n    middle = (len(eeg)-10_000)//2\n    eeg = eeg.iloc[middle:middle+10_000]\n    \n    x = np.array(eeg['EKG'])\n    x = np.nan_to_num(x, copy=False)\n    # RAW SPECTROGRAM\n    mel_spec = librosa.feature.melspectrogram(y=x, sr=200, hop_length=len(x)//256, \n          n_fft=1024, n_mels=128, fmin=0, fmax=20, win_length=128)\n\n    # LOG TRANSFORM\n    width = (mel_spec.shape[1]//32)*32\n    mel_spec_db = librosa.power_to_db(mel_spec, ref=np.max).astype(np.float32)[:,:width]\n\n    # STANDARDIZE TO -1 TO 1\n    mel_spec_db = (mel_spec_db+40)/40 \n        \n    return mel_spec_db","metadata":{"execution":{"iopub.status.busy":"2024-02-18T05:44:25.767682Z","iopub.execute_input":"2024-02-18T05:44:25.768464Z","iopub.status.idle":"2024-02-18T05:44:25.779618Z","shell.execute_reply.started":"2024-02-18T05:44:25.768419Z","shell.execute_reply":"2024-02-18T05:44:25.778362Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Visualization","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(20,8))\nROWS = 2\nCOLS = 3\nfor i in range(1,7):\n    sample_id = train.eeg_id.unique()[i]\n    tars = train[train.eeg_id == train.eeg_id.unique()[i]]['expert_consensus'].unique()\n    plt.subplot(ROWS, COLS, i)\n    sample_path = f'/kaggle/input/hms-harmful-brain-activity-classification/train_eegs/{sample_id}.parquet'\n    spec = spectrogram_from_eeg(sample_path)\n    plt.imshow(spec)\n    \n    \n    plt.title(f'EEG = {sample_id}\\nTarget = {tars}',size=12)\n    plt.yticks([])\n    plt.ylabel('Frequencies (Hz)',size=14)\n    plt.xlabel('Time (sec)',size=16)\n","metadata":{"execution":{"iopub.status.busy":"2024-02-18T05:44:25.780984Z","iopub.execute_input":"2024-02-18T05:44:25.781377Z","iopub.status.idle":"2024-02-18T05:44:42.692236Z","shell.execute_reply.started":"2024-02-18T05:44:25.781343Z","shell.execute_reply":"2024-02-18T05:44:42.690672Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Saving Files","metadata":{}},{"cell_type":"code","source":"%%time\nPATH = '/kaggle/input/hms-harmful-brain-activity-classification/train_eegs/'\ndirectory_path = 'EKG_Spectrograms/'\nif not os.path.exists(directory_path):\n    os.makedirs(directory_path)\n    \nDISPLAY = 4\nEEG_IDS = train.eeg_id.unique()\nall_ekgs = {}\n\nfor i,eeg_id in enumerate(EEG_IDS):\n    if (i%100==0)&(i!=0): print(i,', ',end='')\n        \n    # CREATE SPECTROGRAM FROM EEG PARQUET\n    img = spectrogram_from_eeg(f'{PATH}{eeg_id}.parquet', i<DISPLAY)\n    \n    # SAVE TO DISK\n    if i==DISPLAY:\n        print(f'Creating and writing {len(EEG_IDS)} spectrograms to disk... ',end='')\n    np.save(f'{directory_path}{eeg_id}',img)\n    all_ekgs[eeg_id] = img\n    \n   #SAVE EEG SPECTROGRAM DICTIONARY\nnp.save('/kaggle/working/ekg_specs',all_ekgs)","metadata":{"execution":{"iopub.status.busy":"2024-02-18T05:45:25.378894Z","iopub.execute_input":"2024-02-18T05:45:25.379345Z","iopub.status.idle":"2024-02-18T05:45:25.783596Z","shell.execute_reply.started":"2024-02-18T05:45:25.379301Z","shell.execute_reply":"2024-02-18T05:45:25.782377Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}