{"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":"!pip install --upgrade scipy","metadata":{"execution":{"iopub.status.busy":"2024-02-10T05:02:37.326802Z","iopub.execute_input":"2024-02-10T05:02:37.327582Z","iopub.status.idle":"2024-02-10T05:03:00.608614Z","shell.execute_reply.started":"2024-02-10T05:02:37.327528Z","shell.execute_reply":"2024-02-10T05:03:00.606797Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport numpy as np\nimport pandas as pd\nfrom scipy.signal import square, ShortTimeFFT\nfrom scipy.signal.windows import gaussian\n\nbase_dir = \"/kaggle/input/hms-harmful-brain-activity-classification\"\n\nfs = 200  # Sample rate.\n\ndf_traincsv = pd.read_csv(f'{base_dir}/train.csv')\nprint(df_traincsv.shape)\ndf_traincsv.head()\n","metadata":{"execution":{"iopub.status.busy":"2024-02-10T05:03:03.378233Z","iopub.execute_input":"2024-02-10T05:03:03.378733Z","iopub.status.idle":"2024-02-10T05:03:04.615047Z","shell.execute_reply.started":"2024-02-10T05:03:03.378685Z","shell.execute_reply":"2024-02-10T05:03:04.613582Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# eeg_id = 722738444\n# eeg_sub_id = 4\n\neeg_id = 1327593077\neeg_sub_id = 88\n\n# eeg_id = 3988090520\n# eeg_sub_id = 1\n\neeg = pd.read_parquet(f'{base_dir}/train_eegs/{eeg_id}.parquet')\nprint(eeg.shape)\neeg.head()","metadata":{"execution":{"iopub.status.busy":"2024-02-10T05:03:06.981303Z","iopub.execute_input":"2024-02-10T05:03:06.982217Z","iopub.status.idle":"2024-02-10T05:03:07.218893Z","shell.execute_reply.started":"2024-02-10T05:03:06.982166Z","shell.execute_reply":"2024-02-10T05:03:07.216870Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rec = df_traincsv.loc[(df_traincsv.eeg_id == eeg_id) & (df_traincsv.eeg_sub_id == eeg_sub_id)].iloc[0]\nrec","metadata":{"execution":{"iopub.status.busy":"2024-02-10T05:03:08.497140Z","iopub.execute_input":"2024-02-10T05:03:08.498701Z","iopub.status.idle":"2024-02-10T05:03:08.513932Z","shell.execute_reply.started":"2024-02-10T05:03:08.498640Z","shell.execute_reply":"2024-02-10T05:03:08.512959Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 50 second and center 10 second eeg sub samples \noffset = int(rec.eeg_label_offset_seconds)\nstart = offset * fs\nend = (offset + 50) * fs\neeg_sub_50 = eeg[start:end]\nstart = (offset + 20) * fs\nend = (offset + 30) * fs\neeg_sub_10 = eeg[start:end]","metadata":{"execution":{"iopub.status.busy":"2024-02-10T05:03:10.026064Z","iopub.execute_input":"2024-02-10T05:03:10.027212Z","iopub.status.idle":"2024-02-10T05:03:10.034511Z","shell.execute_reply.started":"2024-02-10T05:03:10.027159Z","shell.execute_reply":"2024-02-10T05:03:10.033281Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_eeg(ax, eeg, title, sep):\n    srate = 200 # Sample rate.\n    nx = eeg.shape[0]\n    totaltime = nx/srate\n    X, Y = np.linspace(0, totaltime, nx), np.zeros(nx)\n    yticklabels = eeg.columns[::-1]  # Reversed.\n\n    for i, label in enumerate(yticklabels):\n        Y = eeg[label]\n        ax.plot(X, Y + (i * sep), linewidth=0.5, color='black')\n\n    ax.set_title(title)\n    ax.set(ylim=(-0.5*sep, (len(yticklabels)-0.5)*sep),\n           yticks=np.arange(len(yticklabels))*sep,\n           yticklabels=yticklabels)\n    ax.set_xlabel('time [s]')\n\nfig, ax = plt.subplots(1, 1, figsize=(10, 15))\n\nplot_eeg(ax, eeg_sub_10, title='10 seconds sample - eeg: ' + str(rec.eeg_id)\n          + '/' + str(rec.eeg_sub_id) + ' ' + rec.expert_consensus, sep = 500)\n\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-02-10T05:03:11.918201Z","iopub.execute_input":"2024-02-10T05:03:11.918620Z","iopub.status.idle":"2024-02-10T05:03:12.593604Z","shell.execute_reply.started":"2024-02-10T05:03:11.918585Z","shell.execute_reply":"2024-02-10T05:03:12.592304Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Doing the spectrogram on a 50 s window.","metadata":{}},{"cell_type":"code","source":"N = eeg_sub_50.shape[0]\nt_x = np.arange(N) * 1/fs  # time indexes for signal\nprint(t_x[-1])\nt_x.shape","metadata":{"execution":{"iopub.status.busy":"2024-02-10T05:04:08.063468Z","iopub.execute_input":"2024-02-10T05:04:08.063910Z","iopub.status.idle":"2024-02-10T05:04:08.074763Z","shell.execute_reply.started":"2024-02-10T05:04:08.063879Z","shell.execute_reply":"2024-02-10T05:04:08.073200Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"g_std = 16  # standard deviation for Gaussian window in samples\nhop = 8  # 8\nwin_width = 49  # Pick an odd number.  49\nwin = gaussian(win_width, std=g_std, sym=True)  # symmetric Gaussian wind.\nSFT = ShortTimeFFT(win, hop=hop, fs=fs, mfft=800)\nelectrode = 0\nx = eeg_sub_50.iloc[:,electrode].values\nSx = SFT.spectrogram(x)  # calculate absolute square of STFT\n\n# dt = hop*1/fs\nt0 = win_width/(2*hop)\n# 10 second window\nti = int(20*fs/hop + t0)\ntf = int(30*fs/hop + t0)\n\nn = int(30/SFT.delta_f)  # Number of bins below 30 Hz.\nfig1, ax1 = plt.subplots(figsize=(6., 4.))  # enlarge plot a bit\n\nax1.imshow(Sx[1:(n+1), ti:tf], origin='lower', aspect='auto')\n\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2024-02-10T05:04:44.768204Z","iopub.execute_input":"2024-02-10T05:04:44.768612Z","iopub.status.idle":"2024-02-10T05:04:45.066515Z","shell.execute_reply.started":"2024-02-10T05:04:44.768583Z","shell.execute_reply":"2024-02-10T05:04:45.065236Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}