{"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":30698,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import mne\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport matplotlib\nfrom pathlib import Path\n\ndata_train_path = '/kaggle/input/hms-harmful-brain-activity-classification/train.csv'","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-05-14T20:29:28.339780Z","iopub.execute_input":"2024-05-14T20:29:28.340204Z","iopub.status.idle":"2024-05-14T20:29:28.347287Z","shell.execute_reply.started":"2024-05-14T20:29:28.340171Z","shell.execute_reply":"2024-05-14T20:29:28.345721Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = pd.read_csv(data_train_path)\n\ndf = data.copy() #бек лог что бы не перегружать \n\nprint(df.head())","metadata":{"execution":{"iopub.status.busy":"2024-05-14T20:29:28.362586Z","iopub.execute_input":"2024-05-14T20:29:28.363381Z","iopub.status.idle":"2024-05-14T20:29:28.578992Z","shell.execute_reply.started":"2024-05-14T20:29:28.363345Z","shell.execute_reply":"2024-05-14T20:29:28.576739Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"counts = df.groupby('eeg_id')['expert_consensus'].nunique()\n\neeg_id_with_multiple_consensus_5 = counts[counts > 5].index.tolist()\n\nprint(eeg_id_with_multiple_consensus_5)\n\neeg_id_with_multiple_consensus_4 = counts[counts > 4].index.tolist()\n\nprint(eeg_id_with_multiple_consensus_4)\n\neeg_id_with_multiple_consensus_3 = counts[counts > 3].index.tolist()\n\nprint(eeg_id_with_multiple_consensus_3)","metadata":{"execution":{"iopub.status.busy":"2024-05-14T20:29:28.581264Z","iopub.execute_input":"2024-05-14T20:29:28.581658Z","iopub.status.idle":"2024-05-14T20:29:28.609494Z","shell.execute_reply.started":"2024-05-14T20:29:28.581627Z","shell.execute_reply":"2024-05-14T20:29:28.608171Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# грузим нужныq (можем прсматривать любые файлы из категорий с 4 и 3 согласиями) updt! тройки какието криндовые\ndata_eeg_path = '/kaggle/input/hms-harmful-brain-activity-classification/train_eegs/1460778765.parquet'\ndata = pd.read_parquet(data_eeg_path).copy() #бек лог что бы не перегружать ","metadata":{"execution":{"iopub.status.busy":"2024-05-14T20:29:28.610762Z","iopub.execute_input":"2024-05-14T20:29:28.611107Z","iopub.status.idle":"2024-05-14T20:29:28.692742Z","shell.execute_reply.started":"2024-05-14T20:29:28.611077Z","shell.execute_reply":"2024-05-14T20:29:28.691395Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = data.drop(['EKG'], axis=1) # дропаем колонку тк ее нет в монтаже, если не сделать это сейчас то все дропнется\nch_names = data.columns.tolist()\n\nch_types = ['eeg']*len(ch_names) \n\ninfo = mne.create_info(ch_names, ch_types=ch_types, sfreq=200)\nprint(info)","metadata":{"execution":{"iopub.status.busy":"2024-05-14T20:29:28.694460Z","iopub.execute_input":"2024-05-14T20:29:28.695068Z","iopub.status.idle":"2024-05-14T20:29:28.719390Z","shell.execute_reply.started":"2024-05-14T20:29:28.695033Z","shell.execute_reply":"2024-05-14T20:29:28.717894Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_values = data.values.T\nraw = mne.io.RawArray(data_values, info)\nraw = raw.notch_filter(50).filter(0.1, 45)\nscalings = {'eeg': 300} \nduration = df_multiple['eeg_label_offset_seconds'].max()+50\nten_twenty_montage = mne.channels.make_standard_montage('standard_1020')\nraw.set_montage(ten_twenty_montage)\n\nraw.plot_sensors(show_names=True)\nplt.tight_layout()\n\n# график для простмотра и отчистки данных\nraw.plot(show_scrollbars=False, show_scalebars=False, duration= duration, scalings=scalings)\nplt.tight_layout()","metadata":{"execution":{"iopub.status.busy":"2024-05-14T20:29:28.722851Z","iopub.execute_input":"2024-05-14T20:29:28.723295Z","iopub.status.idle":"2024-05-14T20:29:33.316470Z","shell.execute_reply.started":"2024-05-14T20:29:28.723260Z","shell.execute_reply":"2024-05-14T20:29:33.315022Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Target = {'Seizure':0, 'LPD':1, 'GPD':2, 'LRDA':3, 'GRDA':4, 'Other':5}\nevent_ids = {'Seizure':0, 'LPD':1, 'LRDA':3, 'GRDA':4, 'Other':5}\nevents = df_multiple[['eeg_label_offset_seconds', 'expert_consensus']]\nevents.insert(1, 'New', 0)\nevents.loc[:,'expert_consensus'] = events['expert_consensus'].map(event_ids)\nevents.loc[:,'eeg_label_offset_seconds'] = (events['eeg_label_offset_seconds']+25)*200\nevents = events.values.astype(int)\nprint(events)","metadata":{"execution":{"iopub.status.busy":"2024-05-14T20:29:33.318632Z","iopub.execute_input":"2024-05-14T20:29:33.319472Z","iopub.status.idle":"2024-05-14T20:29:33.336706Z","shell.execute_reply.started":"2024-05-14T20:29:33.319437Z","shell.execute_reply":"2024-05-14T20:29:33.335353Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"events_old = events # для сравнения\nmne.viz.plot_events(events[:])\nplt.tight_layout()","metadata":{"execution":{"iopub.status.busy":"2024-05-14T20:29:33.338052Z","iopub.execute_input":"2024-05-14T20:29:33.338858Z","iopub.status.idle":"2024-05-14T20:29:33.650607Z","shell.execute_reply.started":"2024-05-14T20:29:33.338820Z","shell.execute_reply":"2024-05-14T20:29:33.649350Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"epochs = mne.Epochs(raw, events, event_id = event_ids, tmin=-5, tmax=5, preload=True, baseline=(None, 0))\nepochs.plot(n_epochs=10, events=True, picks = 'all', show_scrollbars=False, show_scalebars=False, scalings=scalings)\nplt.tight_layout() #эпохи пересекаются, надо пофиксить","metadata":{"execution":{"iopub.status.busy":"2024-05-14T20:29:33.652441Z","iopub.execute_input":"2024-05-14T20:29:33.653339Z","iopub.status.idle":"2024-05-14T20:29:39.287896Z","shell.execute_reply.started":"2024-05-14T20:29:33.653299Z","shell.execute_reply":"2024-05-14T20:29:39.286246Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"events = df_multiple[['eeg_label_offset_seconds', 'expert_consensus']]\nnon_overlapping_events = pd.DataFrame(columns=events.columns)\n\nlist_eeg_label_offset_seconds = list(df_multiple['eeg_label_offset_seconds'])\nnew_list = []\ncurrent_offset = 0\nmin_distance = 10\n\nwhile current_offset <= max(list_eeg_label_offset_seconds):\n    new_list.append(current_offset)\n    next_offset = next((x for x in list_eeg_label_offset_seconds if x >= current_offset + min_distance), None)\n    if next_offset is None:\n        break\n    current_offset = next_offset\n\nevents = df_multiple[['eeg_label_offset_seconds', 'expert_consensus']]\nnon_overlapping_events = pd.DataFrame(columns=events.columns)\n\nmask = events['eeg_label_offset_seconds'].isin(new_list)\nnon_overlapping_events = events[mask]\nnon_overlapping_events.insert(1, 'New', 0)\nnon_overlapping_events.loc[:,'expert_consensus'] = non_overlapping_events['expert_consensus'].map(event_ids)\nnon_overlapping_events.loc[:,'eeg_label_offset_seconds'] = (non_overlapping_events['eeg_label_offset_seconds']+25)*200\nnon_overlapping_events = non_overlapping_events.values.astype(int)","metadata":{"execution":{"iopub.status.busy":"2024-05-14T20:29:39.290074Z","iopub.execute_input":"2024-05-14T20:29:39.290810Z","iopub.status.idle":"2024-05-14T20:29:39.312177Z","shell.execute_reply.started":"2024-05-14T20:29:39.290764Z","shell.execute_reply":"2024-05-14T20:29:39.310909Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# старый \nmne.viz.plot_events(events_old[:])\nplt.tight_layout()\n\n# новый \nmne.viz.plot_events(non_overlapping_events[:])\nplt.tight_layout()","metadata":{"execution":{"iopub.status.busy":"2024-05-14T20:29:39.313482Z","iopub.execute_input":"2024-05-14T20:29:39.314359Z","iopub.status.idle":"2024-05-14T20:29:39.965485Z","shell.execute_reply.started":"2024-05-14T20:29:39.314324Z","shell.execute_reply":"2024-05-14T20:29:39.964236Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"epochs = mne.Epochs(raw, non_overlapping_events, event_id = event_ids, tmin=-5, tmax=5, preload=True, baseline=(None, 0))\nepochs.plot(n_epochs=10, events=True, picks = 'all', show_scrollbars=False, show_scalebars=False, scalings=scalings)\nplt.tight_layout()","metadata":{"execution":{"iopub.status.busy":"2024-05-14T20:29:39.967047Z","iopub.execute_input":"2024-05-14T20:29:39.967372Z","iopub.status.idle":"2024-05-14T20:29:42.893403Z","shell.execute_reply.started":"2024-05-14T20:29:39.967345Z","shell.execute_reply":"2024-05-14T20:29:42.892219Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"epochs['Seizure'].plot_psd(fmin=0.01, fmax=20)\nplt.tight_layout()","metadata":{"execution":{"iopub.status.busy":"2024-05-14T20:29:42.898473Z","iopub.execute_input":"2024-05-14T20:29:42.899674Z","iopub.status.idle":"2024-05-14T20:29:43.649445Z","shell.execute_reply.started":"2024-05-14T20:29:42.899635Z","shell.execute_reply":"2024-05-14T20:29:43.647970Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from mne.time_frequency import tfr_morlet, tfr_multitaper, tfr_stockwell\nfreq = np.arange(0.5, 20, 0.01)\n\n# Define the number of cycles\nn_cycles = freq/2\n\n#  Define the Morlet wavelet transform for the first epoch associated to the event seizure\npower_seizure = tfr_morlet(epochs['Seizure'][0], freq, n_cycles = n_cycles, return_itc = False)\nfor title in ch_names:\n    power_seizure.plot(picks=title, title=title)\n    plt.tight_layout()","metadata":{"execution":{"iopub.status.busy":"2024-05-14T20:29:43.650830Z","iopub.execute_input":"2024-05-14T20:29:43.651179Z","iopub.status.idle":"2024-05-14T20:30:11.994491Z","shell.execute_reply.started":"2024-05-14T20:29:43.651149Z","shell.execute_reply":"2024-05-14T20:30:11.993257Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"evoked = epochs.average()\nevoked","metadata":{"execution":{"iopub.status.busy":"2024-05-14T20:30:11.995892Z","iopub.execute_input":"2024-05-14T20:30:11.996238Z","iopub.status.idle":"2024-05-14T20:30:12.015317Z","shell.execute_reply.started":"2024-05-14T20:30:11.996209Z","shell.execute_reply":"2024-05-14T20:30:12.013509Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"epochs.event_id","metadata":{"execution":{"iopub.status.busy":"2024-05-14T20:30:12.017493Z","iopub.execute_input":"2024-05-14T20:30:12.017979Z","iopub.status.idle":"2024-05-14T20:30:12.026837Z","shell.execute_reply.started":"2024-05-14T20:30:12.017920Z","shell.execute_reply":"2024-05-14T20:30:12.025580Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"my_evokeds = {}\nfor condition in epochs.event_id:\n    my_evokeds[condition] = epochs[condition].average()\n    \nmy_evokeds","metadata":{"execution":{"iopub.status.busy":"2024-05-14T20:30:12.028692Z","iopub.execute_input":"2024-05-14T20:30:12.029795Z","iopub.status.idle":"2024-05-14T20:30:12.084729Z","shell.execute_reply.started":"2024-05-14T20:30:12.029746Z","shell.execute_reply":"2024-05-14T20:30:12.083216Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for condition in ['Seizure', 'LPD', 'LRDA', 'GRDA', 'Other']:\n    chan, lat, amp = my_evokeds[condition].copy().pick_channels(['Cz']).get_peak(tmin = -2, tmax = 0, mode = 'neg', return_amplitude = True)\n    print(condition)\n    print(lat, amp * 1e6)","metadata":{"execution":{"iopub.status.busy":"2024-05-14T20:30:12.087133Z","iopub.execute_input":"2024-05-14T20:30:12.087602Z","iopub.status.idle":"2024-05-14T20:30:12.121343Z","shell.execute_reply.started":"2024-05-14T20:30:12.087559Z","shell.execute_reply":"2024-05-14T20:30:12.119994Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for condition in ['Seizure', 'LPD', 'LRDA', 'GRDA', 'Other']:\n    amp = my_evokeds[condition].copy().pick_channels(['Cz']).crop(tmin = -2, tmax = 0).data.squeeze().mean()\n   # print(vector.shape)\n    print(condition)\n    print(amp * 1e6)","metadata":{"execution":{"iopub.status.busy":"2024-05-14T20:30:12.122554Z","iopub.execute_input":"2024-05-14T20:30:12.122887Z","iopub.status.idle":"2024-05-14T20:30:12.153024Z","shell.execute_reply.started":"2024-05-14T20:30:12.122857Z","shell.execute_reply":"2024-05-14T20:30:12.151589Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mne.viz.plot_compare_evokeds(my_evokeds, picks = ['Cz'], invert_y = True) # как то грязно ","metadata":{"execution":{"iopub.status.busy":"2024-05-14T20:30:12.154799Z","iopub.execute_input":"2024-05-14T20:30:12.155138Z","iopub.status.idle":"2024-05-14T20:30:12.828818Z","shell.execute_reply.started":"2024-05-14T20:30:12.155110Z","shell.execute_reply":"2024-05-14T20:30:12.827982Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"my_evokeds = {condition: evoked.copy().pick_channels(['Cz']) for condition, evoked in my_evokeds.items()}","metadata":{"execution":{"iopub.status.busy":"2024-05-14T20:30:12.830291Z","iopub.execute_input":"2024-05-14T20:30:12.830641Z","iopub.status.idle":"2024-05-14T20:30:12.855496Z","shell.execute_reply.started":"2024-05-14T20:30:12.830610Z","shell.execute_reply":"2024-05-14T20:30:12.854439Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"my_evokeds['Seizure'].ch_names","metadata":{"execution":{"iopub.status.busy":"2024-05-14T20:30:12.856890Z","iopub.execute_input":"2024-05-14T20:30:12.857918Z","iopub.status.idle":"2024-05-14T20:30:12.864577Z","shell.execute_reply.started":"2024-05-14T20:30:12.857881Z","shell.execute_reply":"2024-05-14T20:30:12.863060Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mne.viz.plot_compare_evokeds(my_evokeds['Seizure'], picks = ['Cz'], invert_y = True) ","metadata":{"execution":{"iopub.status.busy":"2024-05-14T20:30:12.866226Z","iopub.execute_input":"2024-05-14T20:30:12.867185Z","iopub.status.idle":"2024-05-14T20:30:13.453018Z","shell.execute_reply.started":"2024-05-14T20:30:12.867149Z","shell.execute_reply":"2024-05-14T20:30:13.451595Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mne.viz.plot_compare_evokeds(my_evokeds['LPD'], picks = ['Cz'], invert_y = True, colors = ['orange']) ","metadata":{"execution":{"iopub.status.busy":"2024-05-14T20:30:13.454530Z","iopub.execute_input":"2024-05-14T20:30:13.454865Z","iopub.status.idle":"2024-05-14T20:30:14.036554Z","shell.execute_reply.started":"2024-05-14T20:30:13.454836Z","shell.execute_reply":"2024-05-14T20:30:14.035378Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mne.viz.plot_compare_evokeds(my_evokeds['LRDA'], picks = ['Cz'], invert_y = True, colors = ['green']) ","metadata":{"execution":{"iopub.status.busy":"2024-05-14T20:30:14.038553Z","iopub.execute_input":"2024-05-14T20:30:14.039028Z","iopub.status.idle":"2024-05-14T20:30:14.628278Z","shell.execute_reply.started":"2024-05-14T20:30:14.038986Z","shell.execute_reply":"2024-05-14T20:30:14.626773Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mne.viz.plot_compare_evokeds(my_evokeds['GRDA'], picks = ['Cz'], invert_y = True, colors = ['red']) ","metadata":{"execution":{"iopub.status.busy":"2024-05-14T20:30:14.632483Z","iopub.execute_input":"2024-05-14T20:30:14.632852Z","iopub.status.idle":"2024-05-14T20:30:15.230841Z","shell.execute_reply.started":"2024-05-14T20:30:14.632822Z","shell.execute_reply":"2024-05-14T20:30:15.229380Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mne.viz.plot_compare_evokeds(my_evokeds['Other'], picks = ['Cz'], invert_y = True,  colors = ['purple']) ","metadata":{"execution":{"iopub.status.busy":"2024-05-14T20:30:15.232766Z","iopub.execute_input":"2024-05-14T20:30:15.233138Z","iopub.status.idle":"2024-05-14T20:30:15.841116Z","shell.execute_reply.started":"2024-05-14T20:30:15.233107Z","shell.execute_reply":"2024-05-14T20:30:15.839590Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data","metadata":{"execution":{"iopub.status.busy":"2024-05-14T20:30:15.842946Z","iopub.execute_input":"2024-05-14T20:30:15.844071Z","iopub.status.idle":"2024-05-14T20:30:15.885200Z","shell.execute_reply.started":"2024-05-14T20:30:15.844023Z","shell.execute_reply":"2024-05-14T20:30:15.883400Z"},"trusted":true},"execution_count":null,"outputs":[]}]}