{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_kg_hide-input":true,"_kg_hide-output":true,"execution":{"iopub.status.busy":"2022-05-01T14:52:32.451482Z","iopub.execute_input":"2022-05-01T14:52:32.451950Z","iopub.status.idle":"2022-05-01T14:52:42.290481Z","shell.execute_reply.started":"2022-05-01T14:52:32.451850Z","shell.execute_reply":"2022-05-01T14:52:42.287919Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<center style=\"font-family:verdana;\"><h1 style=\"font-size:200%; padding: 10px; background: #191970;\"><b style=\"color:white;\">MAT files</b></h1></center>","metadata":{}},{"cell_type":"markdown","source":"\"MAT-files are binary MATLAB® files that store workspace variables. Starting with MAT-file Version 4, there are several subsequent versions of MAT-files that support an increasing set of features. MATLAB releases R2006b and later all support all MAT-file versions.\"\n\nhttps://www.mathworks.com/help/matlab/import_export/mat-file-versions.html#:~:text=MAT%2Dfiles%20are%20binary%20MATLAB,support%20all%20MAT%2Dfile%20versions.","metadata":{}},{"cell_type":"markdown","source":"![](https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQNMtDfmgyRn-Kq8hBkUIMJG0M1aSvuVUH3IA&usqp=CAU)akspython.com","metadata":{}},{"cell_type":"markdown","source":"#Special thanks to Monika Suresh and AHernandez1 for showing how to work with .Mat files\n\nMonika Suresh https://www.kaggle.com/code/m0nika/seizurecnn\n\nahernandez1  https://www.kaggle.com/code/ahernandez1/american-epilepsy-society-seizure-a-playground","metadata":{}},{"cell_type":"code","source":"#Code by Monika Suresh https://www.kaggle.com/code/m0nika/seizurecnn\n\nfrom __future__ import print_function\nimport tensorflow as tf\n\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Dense, Dropout, Flatten\nfrom tensorflow.keras.layers import Conv2D, MaxPooling2D\n#from keras import backend as K\n\nimport random\nimport numpy as np\nimport pandas as pd\n\nimport scipy.io\nfrom scipy.signal import spectrogram\nimport matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2022-05-01T14:52:50.928167Z","iopub.execute_input":"2022-05-01T14:52:50.928464Z","iopub.status.idle":"2022-05-01T14:52:58.228291Z","shell.execute_reply.started":"2022-05-01T14:52:50.928431Z","shell.execute_reply":"2022-05-01T14:52:58.227409Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#Interictal (the time between seizures)\n\n\"A long-standing bias suggests that temporal lobe epilepsy (TLE) is the epilepsy syndrome most often complicated by interictal (the time between seizures) cognitive and behavioral problems. This may be true, but the available evidence does not clearly support this view.\"\n\n\"The interictal period comprises more than 99% of most patients’ lives. Interictal cognitive and behavioral disorders profoundly impair the quality of life. These problems are continuous, unlike the seizures, which are intermittent.\"\n\n\"Encompassing a wide spectrum, these disorders often fit awkwardly into neuropsychiatric categories. Even when patients fit into Diagnostic and Statistical Manual of Mental Disorders (4) categories, they often remain untreated because some physicians believe that using medications to treat other problems might lower the seizure threshold.\"\n\nhttps://www.epilepsy.com/complications-risks/moods-behavior/interictal-problems","metadata":{}},{"cell_type":"code","source":"#Code by Monika Suresh https://www.kaggle.com/code/m0nika/seizurecnn\n\ninterictal_tst = '/kaggle/input/seizure-prediction/Patient_1/Patient_1/Patient_1_interictal_segment_0001.mat'\npreictal_tst = '/kaggle/input/seizure-prediction/Patient_1/Patient_1/Patient_1_preictal_segment_0001.mat'\ninterictal_data = scipy.io.loadmat(interictal_tst)\npreictal_data = scipy.io.loadmat(preictal_tst)","metadata":{"execution":{"iopub.status.busy":"2022-05-01T14:55:39.681938Z","iopub.execute_input":"2022-05-01T14:55:39.682228Z","iopub.status.idle":"2022-05-01T14:55:45.111292Z","shell.execute_reply.started":"2022-05-01T14:55:39.682198Z","shell.execute_reply":"2022-05-01T14:55:45.110270Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Code by Monika Suresh https://www.kaggle.com/code/m0nika/seizurecnn\n\ninterictal_array = interictal_data['interictal_segment_1'][0][0][0]\npreictal_array = preictal_data['preictal_segment_1'][0][0][0]","metadata":{"execution":{"iopub.status.busy":"2022-05-01T14:56:01.827562Z","iopub.execute_input":"2022-05-01T14:56:01.828144Z","iopub.status.idle":"2022-05-01T14:56:01.834024Z","shell.execute_reply.started":"2022-05-01T14:56:01.828095Z","shell.execute_reply":"2022-05-01T14:56:01.832894Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Code by Monika Suresh https://www.kaggle.com/code/m0nika/seizurecnn\n\nl = list(range(10000))\nfor i in l[::5000]:\n    print('Interictal')\n    i_secs = interictal_array[0][i:i+5000]\n    i_f, i_t, i_Sxx = spectrogram(i_secs, fs=5000, return_onesided=False)\n    i_SS = np.log1p(i_Sxx)\n    plt.imshow(i_SS[:] / np.max(i_SS), cmap='Greens')\n    plt.show()\n    print('Preictal')\n    p_secs = preictal_array[0][i:i+5000]\n    p_f, p_t, p_Sxx = spectrogram(p_secs, fs=5000, return_onesided=False)\n    p_SS = np.log1p(p_Sxx)\n    plt.imshow(p_SS[:] / np.max(p_SS), cmap='Greens')\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2022-05-01T14:57:40.177743Z","iopub.execute_input":"2022-05-01T14:57:40.178023Z","iopub.status.idle":"2022-05-01T14:57:40.824483Z","shell.execute_reply.started":"2022-05-01T14:57:40.177993Z","shell.execute_reply":"2022-05-01T14:57:40.823411Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Code by Monika Suresh https://www.kaggle.com/code/m0nika/seizurecnn\nall_X = []\nall_Y = []\n\ntypes = ['Patient_1_interictal_segment', 'Patient_1_preictal_segment']\n\nfor i,typ in enumerate(types):\n    # Looking at 18 files for each event for a balanced dataset\n    for j in range(18):\n        fl = '/kaggle/input/seizure-prediction/Patient_1/Patient_1/{}_{}.mat'.format(typ, str(j + 1).zfill(4))\n        data = scipy.io.loadmat(fl)\n        k = typ.replace('Patient_1_', '') + '_'\n        d_array = data[k + str(j + 1)][0][0][0]\n        lst = list(range(3000000))  # 10 minutes\n        for m in lst[::5000]:\n            # Create a spectrogram every 1 second\n            p_secs = d_array[0][m:m+5000]\n            p_f, p_t, p_Sxx = spectrogram(p_secs, fs=5000, return_onesided=False)\n            p_SS = np.log1p(p_Sxx)\n            arr = p_SS[:] / np.max(p_SS)\n            all_X.append(arr)\n            all_Y.append(i)","metadata":{"execution":{"iopub.status.busy":"2022-05-01T14:59:13.223047Z","iopub.execute_input":"2022-05-01T14:59:13.223354Z","iopub.status.idle":"2022-05-01T15:00:24.571860Z","shell.execute_reply.started":"2022-05-01T14:59:13.223321Z","shell.execute_reply":"2022-05-01T15:00:24.570436Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Code by Monika Suresh https://www.kaggle.com/code/m0nika/seizurecnn\n\n# Shuffling the data\ndataset = list(zip(all_X, all_Y))\nrandom.shuffle(dataset)\nall_X,all_Y = zip(*dataset)\nprint(len(all_X))","metadata":{"execution":{"iopub.status.busy":"2022-05-01T15:00:46.091974Z","iopub.execute_input":"2022-05-01T15:00:46.092350Z","iopub.status.idle":"2022-05-01T15:00:46.194055Z","shell.execute_reply.started":"2022-05-01T15:00:46.092302Z","shell.execute_reply":"2022-05-01T15:00:46.192740Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Code by Monika Suresh https://www.kaggle.com/code/m0nika/seizurecnn\n\n# Splitting data into train/test, leaving only 600 samples for testing\nx_train = np.array(all_X[:21000])\ny_train = np.array(all_Y[:21000])\nx_test = np.array(all_X[21000:])\ny_test = np.array(all_Y[21000:])","metadata":{"execution":{"iopub.status.busy":"2022-05-01T15:00:50.245389Z","iopub.execute_input":"2022-05-01T15:00:50.245907Z","iopub.status.idle":"2022-05-01T15:00:51.111004Z","shell.execute_reply.started":"2022-05-01T15:00:50.245850Z","shell.execute_reply":"2022-05-01T15:00:51.110064Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Code by Monika Suresh https://www.kaggle.com/code/m0nika/seizurecnn\n\nbatch_size = 128\nnum_classes = 2\nepochs = 30\nimg_rows, img_cols = 256, 22","metadata":{"execution":{"iopub.status.busy":"2022-05-01T15:00:55.017618Z","iopub.execute_input":"2022-05-01T15:00:55.017948Z","iopub.status.idle":"2022-05-01T15:00:55.022262Z","shell.execute_reply.started":"2022-05-01T15:00:55.017912Z","shell.execute_reply":"2022-05-01T15:00:55.021159Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Code by Monika Suresh https://www.kaggle.com/code/m0nika/seizurecnn\n\nx_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1)\nx_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1)\ninput_shape = (img_rows, img_cols, 1)\nx_train = x_train.astype('float32')\nx_test = x_test.astype('float32')","metadata":{"execution":{"iopub.status.busy":"2022-05-01T15:01:02.099225Z","iopub.execute_input":"2022-05-01T15:01:02.099530Z","iopub.status.idle":"2022-05-01T15:01:02.397096Z","shell.execute_reply.started":"2022-05-01T15:01:02.099497Z","shell.execute_reply":"2022-05-01T15:01:02.396183Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Code by Monika Suresh https://www.kaggle.com/code/m0nika/seizurecnn\n\ny_train = tf.keras.utils.to_categorical(y_train, num_classes) \ny_test = tf.keras.utils.to_categorical(y_test, num_classes)","metadata":{"execution":{"iopub.status.busy":"2022-05-01T15:01:07.223529Z","iopub.execute_input":"2022-05-01T15:01:07.223844Z","iopub.status.idle":"2022-05-01T15:01:07.229418Z","shell.execute_reply.started":"2022-05-01T15:01:07.223813Z","shell.execute_reply":"2022-05-01T15:01:07.228710Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Code by Monika Suresh https://www.kaggle.com/code/m0nika/seizurecnn\n\nmodel = Sequential()\n\nmodel.add(Conv2D(32, kernel_size=(5, 5),\n                 activation='relu',\n                 input_shape=input_shape))\nmodel.add(Conv2D(32, (3, 3), activation='relu'))\nmodel.add(MaxPooling2D(pool_size=(2, 2)))\nmodel.add(Dropout(0.25))\nmodel.add(Flatten())\nmodel.add(Dense(32, activation='relu'))\nmodel.add(Dropout(0.5))\nmodel.add(Dense(num_classes, activation='sigmoid'))\n\nmodel.compile(loss=tf.keras.losses.binary_crossentropy,\n              optimizer=tf.keras.optimizers.RMSprop(),\n              metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2022-05-01T15:01:12.726042Z","iopub.execute_input":"2022-05-01T15:01:12.726448Z","iopub.status.idle":"2022-05-01T15:01:12.955341Z","shell.execute_reply.started":"2022-05-01T15:01:12.726419Z","shell.execute_reply":"2022-05-01T15:01:12.954194Z"},"_kg_hide-output":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Code by Monika Suresh https://www.kaggle.com/code/m0nika/seizurecnn\n\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2022-05-01T15:01:52.521072Z","iopub.execute_input":"2022-05-01T15:01:52.521765Z","iopub.status.idle":"2022-05-01T15:01:52.532378Z","shell.execute_reply.started":"2022-05-01T15:01:52.521728Z","shell.execute_reply":"2022-05-01T15:01:52.531462Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Code by Monika Suresh https://www.kaggle.com/code/m0nika/seizurecnn\n\nmodel.fit(x_train, y_train,\n          batch_size=batch_size,\n          epochs=epochs,\n          verbose=1,\n          validation_data=(x_test, y_test))\nscore = model.evaluate(x_test, y_test, verbose=0)\nprint('Test loss:', score[0])\nprint('Test accuracy:', score[1])","metadata":{"execution":{"iopub.status.busy":"2022-05-01T15:02:24.618674Z","iopub.execute_input":"2022-05-01T15:02:24.619532Z","iopub.status.idle":"2022-05-01T15:34:07.107593Z","shell.execute_reply.started":"2022-05-01T15:02:24.619484Z","shell.execute_reply":"2022-05-01T15:34:07.106831Z"},"_kg_hide-output":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#np.float is deprecated. Just use float (snippet below)\n\nnp.float` is a deprecated alias for the builtin `float`. To silence this warning, use `float` by itself. Doing this will not modify any behavior and is safe. If you specifically wanted the numpy scalar type, use `np.float64` here.\nDeprecated in NumPy 1.20; for more details and guidance: https://numpy.org/devdocs/release/1.20.0-notes.html#d","metadata":{}},{"cell_type":"code","source":"#Code by ahernandez1  https://www.kaggle.com/code/ahernandez1/american-epilepsy-society-seizure-a-playground\n#ARMHZJZ user ahernadez\n\n#How does an espectogram of an eeg electrode looks like? Could we use it to make a prediction?\n\ndef plotSpectograms(file, electrode=0):\n    search_key = \"_segment_\"       # search key string\n    matFile = scipy.io.loadmat(os.path.join(file))\n    segment = dict(filter(lambda item: search_key in item[0], matFile.items()))\n    segment_list_values = list(segment.values())\n\n    x = np.array(segment_list_values[0][0][0][0][electrode], dtype=float) #Original np.float was deprecated \n    secs = np.array(segment_list_values[0][0][0][1], dtype=float) #Original np.float was deprecated\n    Fs = np.array(segment_list_values[0][0][0][2], dtype=float)[0][0] #Original np.float was deprecated\n    NFFT = 1024  # the length of the windowing segments\n    dt = 1/Fs\n    t = np.arange(0.0, 600.0, dt)\n\n    fig, (ax1, ax2) = plt.subplots(nrows=2, figsize=(15,10))\n    ax1.plot(t, x)\n    Pxx, freqs, bins, im = ax2.specgram(x, NFFT=NFFT, Fs=Fs, noverlap=100, sides='twosided')\n    # The `specgram` method returns 4 objects. They are:\n    # - Pxx: the periodogram\n    # - freqs: the frequency vector\n    # - bins: the centers of the time bins\n    # - im: the .image.AxesImage instance representing the data in the plot\n    plt.show()\n    \nfile = [\n    '/kaggle/input/seizure-prediction/Patient_1/Patient_1/Patient_1_interictal_segment_0001.mat',\n    '/kaggle/input/seizure-prediction/Patient_1/Patient_1/Patient_1_preictal_segment_0001.mat',\n    '/kaggle/input/seizure-prediction/Dog_3/Dog_3/Dog_3_interictal_segment_0005.mat',\n    '/kaggle/input/seizure-prediction/Dog_3/Dog_3/Dog_3_preictal_segment_0005.mat'\n]\nfor f in file:\n    plotSpectograms(f, electrode=5)","metadata":{"execution":{"iopub.status.busy":"2022-05-01T15:58:42.723632Z","iopub.execute_input":"2022-05-01T15:58:42.724035Z","iopub.status.idle":"2022-05-01T15:58:49.201517Z","shell.execute_reply.started":"2022-05-01T15:58:42.723999Z","shell.execute_reply":"2022-05-01T15:58:49.200422Z"},"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#Declaring time of Death of this Kaggle Notebook: 13:26 - 01/05/2022","metadata":{}},{"cell_type":"code","source":"#Code by ahernandez1  https://www.kaggle.com/code/ahernandez1/american-epilepsy-society-seizure-a-playground\n\n#how an eeg looks like.\n\n#Code by https://www.kaggle.com/code/ahernandez1/american-epilepsy-society-seizure-a-playground\n\n# Code in this cell was partially taken from:\n# https://matplotlib.org/3.3.1/gallery/specialty_plots/mri_with_eeg.html\n\nimport matplotlib.pyplot as plt\nfrom matplotlib.collections import LineCollection\nimport numpy as np\n\nfile_to_inspect = scipy.io.loadmat('/kaggle/input/seizure-prediction/Patient_1/Patient_1/Patient_1_interictal_segment_0001.mat')\n# retrieves only the item which contain the data of interese\nsearch_key = '_segment_'\nsegment = dict(filter(lambda item: search_key in item[0], file_to_inspect.items()))\nsegment = list(segment.values())\ndata = segment[0][0][0][0]\nnum_electrodes = data.shape[0]             # rows (i.e. electrodes) in the data matrix\nn_samples = data.shape[1]                  # number of samples on each row (i.e. electrode's samples)\nelectrode_names = segment[0][0][0][3][0]   # name or labels of the electrodes\nt = 10 * np.arange(n_samples) / n_samples\n\n# create the 'figure'\nfig = plt.figure(\"EEG samples\",figsize=(30,15))\n\nticklocs = []\nax = fig.add_subplot(1, 1, 1)\nax.set_xlim(0, 10)\nax.set_xticks(np.arange(10))\ndata_min = data.min()\ndata_max = data.max()\ndr = (data_max - data_min) * 0.7   #crowd it a bit\ny0 = data_min\ny1 = (num_electrodes -1) * dr + data_max\nax.set_ylim(y0, y1)\n\nsegs = []\nfor electrode in range(num_electrodes):\n    segs.append(np.column_stack((t, data[electrode, :])))\n    ticklocs.append(electrode * dr)\n    \noffsets = np.zeros((num_electrodes, 2), dtype=float)\noffsets[:, 1] = ticklocs\n\nlines = LineCollection(segs, offsets=offsets, transOffset=None)\nax.add_collection(lines)\n\n# Set the yticks to use axes coordinates on the y axis\nax.set_yticks(ticklocs)\nax.set_yticklabels(electrode_names)\n\nax.set_xlabel('Time (minutes)')\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-05-01T15:40:20.680125Z","iopub.execute_input":"2022-05-01T15:40:20.681880Z","iopub.status.idle":"2022-05-01T15:40:24.913756Z","shell.execute_reply.started":"2022-05-01T15:40:20.681786Z","shell.execute_reply":"2022-05-01T15:40:24.912859Z"},"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#I think I killed my \"Patient\" above. Time of death of this Notebook: 13:26, 01/05/2022 ","metadata":{}},{"cell_type":"markdown","source":"#Acknowledgement:\n\nMonika Suresh https://www.kaggle.com/code/m0nika/seizurecnn\n\nARMHZJZ ahernandez1  https://www.kaggle.com/code/ahernandez1/american-epilepsy-society-seizure-a-playground","metadata":{}}]}