{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","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":30635,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# EEG signal denosing using Wavelet transform","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport pywt","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_kg_hide-input":true,"execution":{"iopub.status.busy":"2024-06-13T15:21:58.192040Z","iopub.execute_input":"2024-06-13T15:21:58.193457Z","iopub.status.idle":"2024-06-13T15:21:58.198162Z","shell.execute_reply.started":"2024-06-13T15:21:58.193404Z","shell.execute_reply":"2024-06-13T15:21:58.197116Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# from this notebook: \"Harmful Brain Activity Plotting eeg/spec(https://www.kaggle.com/code/ybbbat/harmful-brain-activity-plotting-eeg-spec)\"\ndef plot_eeg(df, moving_avg=1):\n    fig, axs = plt.subplots(20, 1, figsize=(30, 15), sharex=True)\n    for i, ax in enumerate(axs):\n        ax.plot(df.iloc[:,i], color=\"black\")\n        ax.set_ylabel(df.columns[i], rotation=0)\n        ax.set_yticklabels([])\n        ax.set_yticks([])\n        ax.set_xticks([])\n        ax.spines[[\"top\", \"bottom\", \"left\", \"right\"]].set_visible(False)","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2024-06-13T15:22:01.020556Z","iopub.execute_input":"2024-06-13T15:22:01.020963Z","iopub.status.idle":"2024-06-13T15:22:01.028826Z","shell.execute_reply.started":"2024-06-13T15:22:01.020927Z","shell.execute_reply":"2024-06-13T15:22:01.027545Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Denoising function","metadata":{}},{"cell_type":"code","source":"def maddest(d, axis=None):\n    return np.mean(np.absolute(d - np.mean(d, axis)), axis)\n\ndef denoise(x, wavelet='haar', level=1):\n    ret = {key:[] for key in x.columns}\n    \n    for pos in x.columns:\n        coeff = pywt.wavedec(x[pos], wavelet, mode=\"per\")\n        sigma = (1/0.6745) * maddest(coeff[-level])\n\n        uthresh = sigma * np.sqrt(2*np.log(len(x)))\n        coeff[1:] = (pywt.threshold(i, value=uthresh, mode='hard') for i in coeff[1:])\n\n        ret[pos]=pywt.waverec(coeff, wavelet, mode='per')\n    \n    return pd.DataFrame(ret)","metadata":{"execution":{"iopub.status.busy":"2024-06-13T15:22:07.767103Z","iopub.execute_input":"2024-06-13T15:22:07.767502Z","iopub.status.idle":"2024-06-13T15:22:07.775275Z","shell.execute_reply.started":"2024-06-13T15:22:07.767472Z","shell.execute_reply":"2024-06-13T15:22:07.774075Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# List of mother Function you can use for wavelet transform","metadata":{}},{"cell_type":"code","source":"print(\"The mother function you can use\")\nprint(pywt.wavelist())","metadata":{"execution":{"iopub.status.busy":"2024-06-13T15:22:10.480476Z","iopub.execute_input":"2024-06-13T15:22:10.480858Z","iopub.status.idle":"2024-06-13T15:22:10.487238Z","shell.execute_reply.started":"2024-06-13T15:22:10.480826Z","shell.execute_reply":"2024-06-13T15:22:10.485982Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# get eeg signal\ndf = pd.read_csv(\"/kaggle/input/hms-harmful-brain-activity-classification/train.csv\")\nrow = df.sample(n=1, random_state=60).iloc[0,]","metadata":{"execution":{"iopub.status.busy":"2024-06-13T15:22:12.980683Z","iopub.execute_input":"2024-06-13T15:22:12.981089Z","iopub.status.idle":"2024-06-13T15:22:13.148589Z","shell.execute_reply.started":"2024-06-13T15:22:12.981053Z","shell.execute_reply":"2024-06-13T15:22:13.147525Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Befor Denoising","metadata":{}},{"cell_type":"code","source":"eeg = pd.read_parquet(\"/kaggle/input/hms-harmful-brain-activity-classification/train_eegs/\"+str(row[\"eeg_id\"])+\".parquet\")\neeg = eeg[int(row[\"eeg_label_offset_seconds\"])*200:int(row[\"eeg_label_offset_seconds\"])*200+50*200]\nplot_eeg(eeg)\nplt.savefig(\"bef.png\")","metadata":{"execution":{"iopub.status.busy":"2024-06-13T15:22:15.310914Z","iopub.execute_input":"2024-06-13T15:22:15.311335Z","iopub.status.idle":"2024-06-13T15:22:18.259630Z","shell.execute_reply.started":"2024-06-13T15:22:15.311287Z","shell.execute_reply":"2024-06-13T15:22:18.258404Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# After Denoising","metadata":{}},{"cell_type":"code","source":"eeg_denoised = denoise(eeg, wavelet=\"db8\")\nplot_eeg(eeg_denoised)\nplt.savefig(\"aft.png\")","metadata":{"execution":{"iopub.status.busy":"2024-06-13T15:22:24.573529Z","iopub.execute_input":"2024-06-13T15:22:24.573935Z","iopub.status.idle":"2024-06-13T15:22:26.822503Z","shell.execute_reply.started":"2024-06-13T15:22:24.573901Z","shell.execute_reply":"2024-06-13T15:22:26.821074Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}