{"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":"# I used https://www.kaggle.com/code/ahernandez1/american-epilepsy-society-seizure-a-playground as a guide\n# for making this notebook. \n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport matplotlib.pyplot as plt\nimport matplotlib as mpl\nfrom cycler import cycler\nimport scipy.io\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    print(dirname,' has ',len(filenames),' files.')\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames[:5]:\n        if 'Patient_1' in filename:\n            #print(os.path.join(dirname, filename))\n            pass\n        \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","execution":{"iopub.status.busy":"2022-04-02T19:16:01.039218Z","iopub.execute_input":"2022-04-02T19:16:01.039519Z","iopub.status.idle":"2022-04-02T19:16:02.535535Z","shell.execute_reply.started":"2022-04-02T19:16:01.039485Z","shell.execute_reply":"2022-04-02T19:16:02.534472Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dog4_interictal = scipy.io.loadmat('/kaggle/input/seizure-prediction/Dog_4/Dog_4/Dog_4_interictal_segment_0481.mat')\nprint(type(dog4_interictal))\nfor item in dog4_interictal.items():\n    print(item)\n    ","metadata":{"execution":{"iopub.status.busy":"2022-04-02T12:36:01.850922Z","iopub.execute_input":"2022-04-02T12:36:01.851288Z","iopub.status.idle":"2022-04-02T12:36:02.097372Z","shell.execute_reply.started":"2022-04-02T12:36:01.851247Z","shell.execute_reply":"2022-04-02T12:36:02.096792Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dog4_interictal.get('interictal_segment_481')","metadata":{"execution":{"iopub.status.busy":"2022-04-02T12:36:02.098492Z","iopub.execute_input":"2022-04-02T12:36:02.098862Z","iopub.status.idle":"2022-04-02T12:36:02.107718Z","shell.execute_reply.started":"2022-04-02T12:36:02.098821Z","shell.execute_reply":"2022-04-02T12:36:02.106922Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Figuring out how the filter() function works with lambda functions","metadata":{}},{"cell_type":"code","source":"# adapted from https://www.geeksforgeeks.org/lambda-filter-python-examples/\n# Python Program to find numbers divisible by 3 from a list using anonymous function\n  \n# Take a list of numbers. \nmy_list = [12, 65, 54, 39, 102, 339, 221, 50, 70, ]\n  \n# use anonymous function to filter and comparing \n# if divisible or not\nresult = list(filter(lambda x: x % 3 == 0, my_list)) \n  \nprint(result) \n\n# explaining the above:\n# the filter function takes a function and iterable as arguments: filter(function, iterable)\n# 'function' gets run for every item in 'iterable'\n# our lambda function returns True every time a number is divisible by 3\n# every item for which the lambda function returns True, gets returned by 'filter'\n","metadata":{"execution":{"iopub.status.busy":"2022-04-02T12:36:02.110120Z","iopub.execute_input":"2022-04-02T12:36:02.110572Z","iopub.status.idle":"2022-04-02T12:36:02.121334Z","shell.execute_reply.started":"2022-04-02T12:36:02.110533Z","shell.execute_reply":"2022-04-02T12:36:02.120503Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Trying the above with a dictionary","metadata":{}},{"cell_type":"code","source":"mydict={'num1':5,'num2':4,'num3':3,'num4':2,'num5':1}\nsearch_key='num1'\nselected_dict=dict(filter(lambda dict_item: search_key in dict_item,mydict.items()))\nprint(selected_dict)\nprint(selected_dict.values())","metadata":{"execution":{"iopub.status.busy":"2022-04-02T12:36:02.122539Z","iopub.execute_input":"2022-04-02T12:36:02.122781Z","iopub.status.idle":"2022-04-02T12:36:02.135438Z","shell.execute_reply.started":"2022-04-02T12:36:02.122752Z","shell.execute_reply":"2022-04-02T12:36:02.134510Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Plotting some EEG data","metadata":{}},{"cell_type":"code","source":"# Code in this cell was partially taken from:\n# https://matplotlib.org/3.3.1/gallery/specialty_plots/mri_with_eeg.html\n\n# I (lizelle niit) in turn modified this cell from https://www.kaggle.com/code/ahernandez1/american-epilepsy-society-seizure-a-playground\n\nfrom matplotlib.collections import LineCollection\n\n#file_to_inspect = scipy.io.loadmat('/kaggle/input/seizure-prediction/Patient_1/Patient_1/Patient_1_interictal_segment_0034.mat')\nfile_to_inspect = scipy.io.loadmat('/kaggle/input/seizure-prediction/Dog_4/Dog_4/Dog_4_interictal_segment_0481.mat')\n# retrieves only the item which contain the data of interest\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]\nprint('the dimensions of the data matrix', data.shape)\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\nprint(electrode_names)\nt = 10 * np.arange(n_samples) / n_samples # we know that n_samples comprise 10 minutes of measurements. so we can scale the numbers accordingly.\n\nmyfig, myaxs = plt.subplots(num_electrodes,figsize=(100,100))\nmyfig.suptitle('Vertically stacked subplots')\nfor plot_i in range(num_electrodes):\n    myaxs[plot_i].plot(t,data[plot_i,:])\n\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-04-02T13:25:19.476541Z","iopub.execute_input":"2022-04-02T13:25:19.476837Z","iopub.status.idle":"2022-04-02T13:25:27.814100Z","shell.execute_reply.started":"2022-04-02T13:25:19.476807Z","shell.execute_reply":"2022-04-02T13:25:27.813065Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}