{"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":"\nimport os\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\n# for 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","execution":{"iopub.status.busy":"2023-04-27T19:55:35.732695Z","iopub.execute_input":"2023-04-27T19:55:35.733142Z","iopub.status.idle":"2023-04-27T19:55:35.739623Z","shell.execute_reply.started":"2023-04-27T19:55:35.733103Z","shell.execute_reply":"2023-04-27T19:55:35.737857Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Main directory\ndir_fog = '/kaggle/input/tlvmc-parkinsons-freezing-gait-prediction/'","metadata":{"execution":{"iopub.status.busy":"2023-04-27T19:55:36.198332Z","iopub.execute_input":"2023-04-27T19:55:36.198762Z","iopub.status.idle":"2023-04-27T19:55:36.204843Z","shell.execute_reply.started":"2023-04-27T19:55:36.198727Z","shell.execute_reply":"2023-04-27T19:55:36.203308Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"subjects = pd.read_csv(os.path.join(dir_fog, 'subjects.csv'))\nevents = pd.read_csv(os.path.join(dir_fog, 'events.csv'))\n\nprint(f\"number of subjects: {len(subjects)}\")\n","metadata":{"execution":{"iopub.status.busy":"2023-04-27T19:55:36.662609Z","iopub.execute_input":"2023-04-27T19:55:36.663013Z","iopub.status.idle":"2023-04-27T19:55:36.682981Z","shell.execute_reply.started":"2023-04-27T19:55:36.662980Z","shell.execute_reply":"2023-04-27T19:55:36.681479Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Read","metadata":{}},{"cell_type":"code","source":"def get_contents(parent_dir, data_dir:str):\n    \"\"\"Outputs the new directory (j_dir) and its contents\"\"\"\n    \n    j_dir = os.path.join(parent_dir, data_dir)\n    contents = os.listdir(j_dir)\n    print(f\"number of collected datasets ({data_dir}): {len(contents)}\")\n    return j_dir, contents\n\ndef get_data(data_dir, fname:str):\n    \"\"\"Gets the data in a pd.DataFrame format, given the w directory and file name\"\"\"\n    \n    full_path = data_dir+'/'+fname\n    return pd.read_csv(full_path)","metadata":{"execution":{"iopub.status.busy":"2023-04-27T19:55:37.704760Z","iopub.execute_input":"2023-04-27T19:55:37.705197Z","iopub.status.idle":"2023-04-27T19:55:37.713001Z","shell.execute_reply.started":"2023-04-27T19:55:37.705150Z","shell.execute_reply":"2023-04-27T19:55:37.711473Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dir_lab, lab_data = get_contents(dir_fog, 'train/tdcsfog')\ndir_hom, hom_data = get_contents(dir_fog, 'train/defog')","metadata":{"execution":{"iopub.status.busy":"2023-04-27T19:55:38.203821Z","iopub.execute_input":"2023-04-27T19:55:38.204276Z","iopub.status.idle":"2023-04-27T19:55:38.212731Z","shell.execute_reply.started":"2023-04-27T19:55:38.204237Z","shell.execute_reply":"2023-04-27T19:55:38.211351Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Check Metadata\ntdcs_fog_meta = pd.read_csv(os.path.join(dir_fog, 'tdcsfog_metadata.csv'))\ntdcs_fog_meta[tdcs_fog_meta.Subject=='13abfd']\n\ntdcs_fog_meta['file'] = tdcs_fog_meta.Id +'.csv'\nprint(tdcs_fog_meta.info())\ntdcs_fog_meta.head()","metadata":{"execution":{"iopub.status.busy":"2023-04-27T19:55:38.730025Z","iopub.execute_input":"2023-04-27T19:55:38.730487Z","iopub.status.idle":"2023-04-27T19:55:38.760895Z","shell.execute_reply.started":"2023-04-27T19:55:38.730451Z","shell.execute_reply":"2023-04-27T19:55:38.759047Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Pre-processing","metadata":{}},{"cell_type":"code","source":"from scipy import signal, integrate\n\ndef filt(series, **kwargs):\n    \"\"\"\n    Time series filtering\n\n    Args:\n        series: the time series to be filtered\n    **kwargs:\n        fs: the sampling frequency\n        Wn: the cut-off frequency(ies)\n        N: the filter order\n        btype: the filter type (\"bandpass\", \"low\" or \"high\")\n\n    Returns:\n        The filtered signal\n    \"\"\"\n\n    butter = signal.butter(**kwargs, output='sos')\n    filtered = signal.sosfilt(butter, series)\n\n    return filtered\n\ndef filter_col(df, **kwargs):\n    \"\"\"filter each column separately.\"\"\"\n    \n    table = []\n    for col in df.columns:\n        table.append((filt(df[col], fs=100, N=4, Wn=[0.5, 6], btype='bandpass')))  # ensure conversion only for accelerations\n\n    filtered_df = list(map(list, zip(*table)))\n\n    return pd.DataFrame(filtered_df, columns=df.columns)","metadata":{"execution":{"iopub.status.busy":"2023-04-27T20:00:25.253715Z","iopub.execute_input":"2023-04-27T20:00:25.254223Z","iopub.status.idle":"2023-04-27T20:00:25.265980Z","shell.execute_reply.started":"2023-04-27T20:00:25.254158Z","shell.execute_reply":"2023-04-27T20:00:25.263928Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Visualise\n(How does FoG look like?\nThis is fully unfiltered data)","metadata":{}},{"cell_type":"code","source":"stop = 0\nfor i in range(0,len(lab_data)):\n    data_example = get_data(dir_lab, tdcs_fog_meta.loc[i].file)\n    data_example.loc[:, ['AccV', 'AccAP', 'AccML']] = filter_col(data_example.loc[:, ['AccV', 'AccAP', 'AccML']])\n    if any(data_example.StartHesitation > 0): # visualise any trial where FoG was found\n        stop += 1\n        print(f\"filename: {tdcs_fog_meta.loc[i].file}\")\n        data_example.loc[:,'StartHesitation'] *= 12\n        data_example.loc[:,'Turn'] *= 10\n\n        data_example.iloc[:,1:].plot()\n        \n        if stop == 2: # stop ploting after n examples\n            break","metadata":{"execution":{"iopub.status.busy":"2023-04-27T20:12:29.936201Z","iopub.execute_input":"2023-04-27T20:12:29.937653Z","iopub.status.idle":"2023-04-27T20:12:38.620074Z","shell.execute_reply.started":"2023-04-27T20:12:29.937600Z","shell.execute_reply":"2023-04-27T20:12:38.618361Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Explore Events for labeled trials","metadata":{}},{"cell_type":"code","source":"fog_event = data_example.iloc[:,1:][data_example.StartHesitation>1]\nturn_event = data_example.iloc[:,1:][data_example.Turn>1]\nwalk_event = data_example.iloc[:,1:][data_example.Walking>0.5]","metadata":{"execution":{"iopub.status.busy":"2023-04-27T20:12:44.794120Z","iopub.execute_input":"2023-04-27T20:12:44.795571Z","iopub.status.idle":"2023-04-27T20:12:44.811153Z","shell.execute_reply.started":"2023-04-27T20:12:44.795504Z","shell.execute_reply":"2023-04-27T20:12:44.809548Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# fft features\n\nfrom scipy.fft import fft, fftfreq\nimport numpy as np\n\n# Calculate fft\ndef calc_fft(sign, samp_f):\n    N = len(sign)\n    T = 1.0 / samp_f\n    yf = fft(np.array(sign))\n    xf = np.linspace(0.0, 1.0/(2.0*T), N//2)\n    plt.plot(xf, 2.0/N * np.abs(yf[:N//2]))\n    plt.xlim([0,10])\n    \ncalc_fft(sign=filt(series=np.array(fog_event.AccAP), fs=100, N=4, Wn=[0.1, 6], btype='bandpass'), samp_f=100.0)\ncalc_fft(sign=filt(series=np.array(walk_event.AccAP), fs=100, N=4, Wn=[0.1, 6], btype='bandpass'), samp_f=100.0)\ncalc_fft(sign=filt(series=np.array(turn_event.AccAP), fs=100, N=4, Wn=[0.1, 6], btype='bandpass'), samp_f=100.0)\nplt.xlabel('frequency (Hz)')\nplt.legend(['fog', 'walk', 'turn'])","metadata":{"execution":{"iopub.status.busy":"2023-04-27T20:12:45.291338Z","iopub.execute_input":"2023-04-27T20:12:45.291778Z","iopub.status.idle":"2023-04-27T20:12:45.705773Z","shell.execute_reply.started":"2023-04-27T20:12:45.291740Z","shell.execute_reply":"2023-04-27T20:12:45.704445Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"calc_fft(sign=filt(series=np.array(fog_event.AccML), fs=100, N=4, Wn=[0.1, 6], btype='bandpass'), samp_f=100.0)\ncalc_fft(sign=filt(series=np.array(walk_event.AccML), fs=100, N=4, Wn=[0.1, 6], btype='bandpass'), samp_f=100.0)\ncalc_fft(sign=filt(series=np.array(turn_event.AccML), fs=100, N=4, Wn=[0.1, 6], btype='bandpass'), samp_f=100.0)\nplt.xlabel('frequency (Hz)')\nplt.legend(['fog', 'walk', 'turn'])","metadata":{"execution":{"iopub.status.busy":"2023-04-27T20:12:45.879280Z","iopub.execute_input":"2023-04-27T20:12:45.880023Z","iopub.status.idle":"2023-04-27T20:12:46.250672Z","shell.execute_reply.started":"2023-04-27T20:12:45.879966Z","shell.execute_reply":"2023-04-27T20:12:46.249094Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"calc_fft(sign=filt(series=np.array(fog_event.AccV), fs=100, N=4, Wn=[0.1, 6], btype='bandpass'), samp_f=100.0)\ncalc_fft(sign=filt(series=np.array(walk_event.AccV), fs=100, N=4, Wn=[0.1, 6], btype='bandpass'), samp_f=100.0)\ncalc_fft(sign=filt(series=np.array(turn_event.AccV), fs=100, N=4, Wn=[0.1, 6], btype='bandpass'), samp_f=100.0)\nplt.xlabel('frequency (Hz)')\nplt.legend(['fog', 'walk', 'turn'])","metadata":{"execution":{"iopub.status.busy":"2023-04-27T20:12:47.368136Z","iopub.execute_input":"2023-04-27T20:12:47.368604Z","iopub.status.idle":"2023-04-27T20:12:47.718871Z","shell.execute_reply.started":"2023-04-27T20:12:47.368564Z","shell.execute_reply":"2023-04-27T20:12:47.717399Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}