{"metadata":{"kernelspec":{"name":"python3","display_name":"Python 3","language":"python"},"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":30664,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import pandas as pd\nimport numpy as np","metadata":{"execution":{"iopub.status.busy":"2024-03-19T14:49:12.581760Z","iopub.execute_input":"2024-03-19T14:49:12.582856Z","iopub.status.idle":"2024-03-19T14:49:12.990058Z","shell.execute_reply.started":"2024-03-19T14:49:12.582797Z","shell.execute_reply":"2024-03-19T14:49:12.988936Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv('/kaggle/input/hms-harmful-brain-activity-classification/train.csv')\nTARGETS = df.columns[-6:]\ny = TARGETS\ny = df[TARGETS].agg('sum', axis=1)\ndf['sum_votes']=y","metadata":{"execution":{"iopub.status.busy":"2024-03-19T14:49:30.612762Z","iopub.execute_input":"2024-03-19T14:49:30.613159Z","iopub.status.idle":"2024-03-19T14:49:30.865378Z","shell.execute_reply.started":"2024-03-19T14:49:30.613126Z","shell.execute_reply":"2024-03-19T14:49:30.864517Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pop2 = df.loc[df['sum_votes']>8]\ntrain2 =  pop2.drop_duplicates(subset=['eeg_id']+list(TARGETS), keep='last')\ntrain2.shape","metadata":{"execution":{"iopub.status.busy":"2024-03-19T14:49:34.225027Z","iopub.execute_input":"2024-03-19T14:49:34.225381Z","iopub.status.idle":"2024-03-19T14:49:34.250325Z","shell.execute_reply.started":"2024-03-19T14:49:34.225353Z","shell.execute_reply":"2024-03-19T14:49:34.249532Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_unique = df.drop_duplicates(subset=['eeg_id', 'seizure_vote', 'lpd_vote', 'gpd_vote', 'lrda_vote', 'grda_vote', 'other_vote'], keep='first')\n#df_unique2 = df.drop_duplicates(subset=['eeg_id', 'seizure_vote', 'lpd_vote', 'gpd_vote', 'lrda_vote', 'grda_vote', 'other_vote'], keep='last')","metadata":{"execution":{"iopub.status.busy":"2024-03-19T14:49:43.389351Z","iopub.execute_input":"2024-03-19T14:49:43.390377Z","iopub.status.idle":"2024-03-19T14:49:43.410656Z","shell.execute_reply.started":"2024-03-19T14:49:43.390334Z","shell.execute_reply":"2024-03-19T14:49:43.409559Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_unique.shape","metadata":{"execution":{"iopub.status.busy":"2024-03-19T14:49:44.532816Z","iopub.execute_input":"2024-03-19T14:49:44.533217Z","iopub.status.idle":"2024-03-19T14:49:44.539735Z","shell.execute_reply.started":"2024-03-19T14:49:44.533183Z","shell.execute_reply":"2024-03-19T14:49:44.538639Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"FEATS = ['Fp1','T3','C3','O1','Fp2','C4','T4','O2']","metadata":{"execution":{"iopub.status.busy":"2024-03-19T14:49:46.254841Z","iopub.execute_input":"2024-03-19T14:49:46.255691Z","iopub.status.idle":"2024-03-19T14:49:46.260407Z","shell.execute_reply.started":"2024-03-19T14:49:46.255652Z","shell.execute_reply":"2024-03-19T14:49:46.259312Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def fifty_sec_eeg_from_parquet(parquet_path, offset):\n    \n    # EXTRACT MIDDLE 50 SECONDS\n    eeg = pd.read_parquet(parquet_path)\n    eeg = eeg.iloc[offset:offset+10_000]\n\n    \n    # CONVERT TO NUMPY\n    data = np.zeros((10_000,len(FEATS)))\n    for j,col in enumerate(FEATS):\n        \n        # FILL NAN\n        x = eeg[col].values.astype('float32')\n        m = np.nanmean(x)\n        if np.isnan(x).mean()<1: x = np.nan_to_num(x,nan=m)\n        else: x[:] = 0\n            \n        data[:,j] = x\n          \n    return data","metadata":{"execution":{"iopub.status.busy":"2024-03-19T14:50:23.594324Z","iopub.execute_input":"2024-03-19T14:50:23.595035Z","iopub.status.idle":"2024-03-19T14:50:23.601248Z","shell.execute_reply.started":"2024-03-19T14:50:23.594995Z","shell.execute_reply":"2024-03-19T14:50:23.600444Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\n\nall_eegs = {}\nPATH = '/kaggle/input/hms-harmful-brain-activity-classification/train_eegs/'\n\nfor i,(idx, row) in enumerate(df_unique.iterrows()):\n    if (i%100==0)&(i!=0): print(i,', ',end='')\n    \n    eeg_id = row['eeg_id']\n    label_id = row['label_id']\n    offset = int(row['eeg_label_offset_seconds']) * 200\n    # CREATE SPECTROGRAM FROM EEG PARQUET\n    eeg = fifty_sec_eeg_from_parquet(f'{PATH}{eeg_id}.parquet', offset=offset)\n    \n    # SAVE TO DISK\n    \n    all_eegs[label_id] = eeg\n   \n# SAVE EEG SPECTROGRAM DICTIONARY\nnp.save('eegs_pop1.npy',all_eegs)\n","metadata":{"execution":{"iopub.status.busy":"2024-03-19T14:50:42.424012Z","iopub.execute_input":"2024-03-19T14:50:42.424377Z","iopub.status.idle":"2024-03-19T14:51:00.866225Z","shell.execute_reply.started":"2024-03-19T14:50:42.424348Z","shell.execute_reply":"2024-03-19T14:51:00.865005Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\n\nall_eegs = {}\nPATH = '/kaggle/input/hms-harmful-brain-activity-classification/train_eegs/'\n\nfor i,(idx, row) in enumerate(train2.iterrows()):\n    if (i%100==0)&(i!=0): print(i,', ',end='')\n    \n    eeg_id = row['eeg_id']\n    label_id = row['label_id']\n    offset = int(row['eeg_label_offset_seconds']) * 200\n    # CREATE SPECTROGRAM FROM EEG PARQUET\n    eeg = fifty_sec_eeg_from_parquet(f'{PATH}{eeg_id}.parquet', offset=offset)\n    \n    # SAVE TO DISK\n    \n    all_eegs[label_id] = eeg\n   \n# SAVE EEG SPECTROGRAM DICTIONARY\nnp.save('eegs_pop2.npy',all_eegs)\n","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}