{"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":"markdown","source":"# Import Modules","metadata":{}},{"cell_type":"code","source":"import os\nimport tqdm\nimport pandas as pd\nimport matplotlib.pyplot as plt","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-04-21T07:34:30.248935Z","iopub.execute_input":"2023-04-21T07:34:30.249449Z","iopub.status.idle":"2023-04-21T07:34:30.255517Z","shell.execute_reply.started":"2023-04-21T07:34:30.249405Z","shell.execute_reply":"2023-04-21T07:34:30.254064Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Load data\n\n## tasks, meta & subjects","metadata":{}},{"cell_type":"code","source":"# parent directory\npdir = '/kaggle/input/tlvmc-parkinsons-freezing-gait-prediction'","metadata":{"execution":{"iopub.status.busy":"2023-04-21T06:50:13.702953Z","iopub.execute_input":"2023-04-21T06:50:13.703391Z","iopub.status.idle":"2023-04-21T06:50:13.709385Z","shell.execute_reply.started":"2023-04-21T06:50:13.703352Z","shell.execute_reply":"2023-04-21T06:50:13.708046Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# load tasks data.\ndf_tasks = pd.read_csv(os.path.join(pdir, 'tasks.csv'))\ndf_tasks.head()","metadata":{"execution":{"iopub.status.busy":"2023-04-21T06:50:14.243212Z","iopub.execute_input":"2023-04-21T06:50:14.243628Z","iopub.status.idle":"2023-04-21T06:50:14.288413Z","shell.execute_reply.started":"2023-04-21T06:50:14.243590Z","shell.execute_reply":"2023-04-21T06:50:14.286818Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# load metadata.\ndf_defog_meta = pd.read_csv(os.path.join(pdir, 'defog_metadata.csv'))\ndf_defog_meta.head()","metadata":{"execution":{"iopub.status.busy":"2023-04-21T06:50:14.691519Z","iopub.execute_input":"2023-04-21T06:50:14.691961Z","iopub.status.idle":"2023-04-21T06:50:14.713159Z","shell.execute_reply.started":"2023-04-21T06:50:14.691921Z","shell.execute_reply":"2023-04-21T06:50:14.711672Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# load subjects data.\ndf_subjects = pd.read_csv(os.path.join(pdir, 'subjects.csv'))\ndf_subjects.head()","metadata":{"execution":{"iopub.status.busy":"2023-04-21T06:50:15.188128Z","iopub.execute_input":"2023-04-21T06:50:15.188945Z","iopub.status.idle":"2023-04-21T06:50:15.214334Z","shell.execute_reply.started":"2023-04-21T06:50:15.188881Z","shell.execute_reply":"2023-04-21T06:50:15.212814Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## defog","metadata":{"execution":{"iopub.status.busy":"2023-04-20T01:37:04.824406Z","iopub.execute_input":"2023-04-20T01:37:04.825008Z","iopub.status.idle":"2023-04-20T01:37:04.833519Z","shell.execute_reply.started":"2023-04-20T01:37:04.824958Z","shell.execute_reply":"2023-04-20T01:37:04.831656Z"}}},{"cell_type":"code","source":"# check the number of training defog files.\ndefog_files_name = os.listdir(os.path.join(pdir, 'train', 'defog'))\nprint(f'The number of training defog files: {len(defog_files_name)}')","metadata":{"execution":{"iopub.status.busy":"2023-04-21T06:50:25.184730Z","iopub.execute_input":"2023-04-21T06:50:25.185218Z","iopub.status.idle":"2023-04-21T06:50:25.193493Z","shell.execute_reply.started":"2023-04-21T06:50:25.185175Z","shell.execute_reply":"2023-04-21T06:50:25.192060Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# load all defog data in a variable 'data_dict'\ndata_dict = {}\nfor fname in tqdm.tqdm(defog_files_name):\n    Id = fname.replace(\".csv\", \"\")\n    data_dict[Id] = pd.read_csv(os.path.join(pdir, 'train', 'defog', fname))","metadata":{"execution":{"iopub.status.busy":"2023-04-21T06:50:27.549085Z","iopub.execute_input":"2023-04-21T06:50:27.549538Z","iopub.status.idle":"2023-04-21T06:50:54.399041Z","shell.execute_reply.started":"2023-04-21T06:50:27.549497Z","shell.execute_reply":"2023-04-21T06:50:54.397470Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Visualyze by task","metadata":{}},{"cell_type":"code","source":"# get unique task\nunique_tasks = df_tasks['Task'].unique().tolist()\nprint('Type of tasks:', unique_tasks)","metadata":{"execution":{"iopub.status.busy":"2023-04-21T07:19:23.507700Z","iopub.execute_input":"2023-04-21T07:19:23.508152Z","iopub.status.idle":"2023-04-21T07:19:23.515604Z","shell.execute_reply.started":"2023-04-21T07:19:23.508114Z","shell.execute_reply":"2023-04-21T07:19:23.513955Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Select Id to show plot\ntarget_Id = []\nfor Id in data_dict.keys():\n    # calculate the number of unique tasks per Id\n    num_unique_per_Id = len(df_tasks.loc[df_tasks['Id'] == Id, 'Task'].unique())\n    \n    if num_unique_per_Id > 25:\n        target_Id.append(Id)","metadata":{"execution":{"iopub.status.busy":"2023-04-21T07:13:13.012649Z","iopub.execute_input":"2023-04-21T07:13:13.013120Z","iopub.status.idle":"2023-04-21T07:13:13.086699Z","shell.execute_reply.started":"2023-04-21T07:13:13.013079Z","shell.execute_reply":"2023-04-21T07:13:13.085290Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"frequency = 100  # defog data's recording frequency\n\nnum_rows = len(unique_tasks)\nnum_columns = 3\n\nfig, ax = plt.subplots(num_rows, num_columns, figsize=(15, 45))\nfor i, task in enumerate(unique_tasks):\n    \n    for Id in target_Id:\n        # get specific Id's defog data\n        df_defog = data_dict[Id]\n        \n        try:\n            # get the bigining and ending time of one task\n            tmp = df_tasks[(df_tasks['Task'] == task) & (df_tasks['Id'] == Id)]\n            begin, end = int(tmp['Begin'] * frequency), int(tmp['End'] * frequency)\n            \n            # plot accelaration \n            ax[i, 0].plot(df_defog.loc[begin:end, 'AccV'].reset_index(drop=True), alpha=0.25)\n            ax[i, 1].plot(df_defog.loc[begin:end, 'AccML'].reset_index(drop=True), alpha=0.25)\n            ax[i, 2].plot(df_defog.loc[begin:end, 'AccAP'].reset_index(drop=True), alpha=0.25)\n\n        except:pass\n    \n    ax[i, 0].set_title(f'AccV @ {task}')\n    ax[i, 1].set_title(f'AccML @ {task}')\n    ax[i, 2].set_title(f'AccAP @ {task}')\n    \nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-04-21T07:19:39.279265Z","iopub.execute_input":"2023-04-21T07:19:39.279671Z","iopub.status.idle":"2023-04-21T07:19:52.863956Z","shell.execute_reply.started":"2023-04-21T07:19:39.279635Z","shell.execute_reply":"2023-04-21T07:19:52.862537Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Visualize by medication","metadata":{"execution":{"iopub.status.busy":"2023-04-21T07:20:52.670447Z","iopub.execute_input":"2023-04-21T07:20:52.670914Z","iopub.status.idle":"2023-04-21T07:20:52.680888Z","shell.execute_reply.started":"2023-04-21T07:20:52.670876Z","shell.execute_reply":"2023-04-21T07:20:52.678732Z"}}},{"cell_type":"code","source":"num_rows = 3\nnum_columns = 2\n\nfig, ax = plt.subplots(num_rows, num_columns, figsize=(15, 8))\nfor Id in data_dict.keys():\n    # get specific Id's defog data\n    df_defog = data_dict[Id]\n    df_defog = df_defog[(df_defog['Valid']==True) & (df_defog['Task']==True)].reset_index(drop=True)\n    \n    # get medication info\n    medication = df_defog_meta.loc[df_defog_meta['Id'] == Id, 'Medication'].iloc[0]\n    \n    if medication == 'off':\n        # plot accelaraton value of medication off at first row of the figure\n        ax[0, 0].plot(df_defog.loc[:, 'AccV'].reset_index(drop=True), alpha=0.25)\n        ax[1, 0].plot(df_defog.loc[:, 'AccML'].reset_index(drop=True), alpha=0.25)\n        ax[2, 0].plot(df_defog.loc[:, 'AccAP'].reset_index(drop=True), alpha=0.25)        \n        ax[0, 0].set_title(f'AccV @ Medication = {medication}')\n        ax[1, 0].set_title(f'AccML @ Medication = {medication}')\n        ax[2, 0].set_title(f'AccAP @ Medication = {medication}')\n    else:\n        # plot accelaraton value of medication on at second row of the figure\n        ax[0, 1].plot(df_defog.loc[:, 'AccV'].reset_index(drop=True), alpha=0.25)\n        ax[1, 1].plot(df_defog.loc[:, 'AccML'].reset_index(drop=True), alpha=0.25)\n        ax[2, 1].plot(df_defog.loc[:, 'AccAP'].reset_index(drop=True), alpha=0.25)        \n        ax[0, 1].set_title(f'AccV @ Medication = {medication}')\n        ax[1, 1].set_title(f'AccML @ Medication = {medication}')\n        ax[2, 1].set_title(f'AccAP @ Medication = {medication}')\n    \n    ax[0, 0].set_ylim(-6, 4)\n    ax[1, 0].set_ylim(-6, 4)\n    ax[2, 0].set_ylim(-6, 4)\n    ax[0, 1].set_ylim(-6, 4)\n    ax[1, 1].set_ylim(-6, 4)\n    ax[2, 1].set_ylim(-6, 4)\n          \nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-04-21T07:44:25.572286Z","iopub.execute_input":"2023-04-21T07:44:25.572748Z","iopub.status.idle":"2023-04-21T07:44:31.008467Z","shell.execute_reply.started":"2023-04-21T07:44:25.572709Z","shell.execute_reply":"2023-04-21T07:44:31.006934Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Visualize by sex","metadata":{"execution":{"iopub.status.busy":"2023-04-21T07:20:52.670447Z","iopub.execute_input":"2023-04-21T07:20:52.670914Z","iopub.status.idle":"2023-04-21T07:20:52.680888Z","shell.execute_reply.started":"2023-04-21T07:20:52.670876Z","shell.execute_reply":"2023-04-21T07:20:52.678732Z"}}},{"cell_type":"code","source":"num_rows = 3\nnum_columns = 2\n\nfig, ax = plt.subplots(num_rows, num_columns, figsize=(15, 8))\nfor Id in data_dict.keys():\n    # get specific Id's defog data\n    df_defog = data_dict[Id]\n    df_defog = df_defog[(df_defog['Valid']==True) & (df_defog['Task']==True)].reset_index(drop=True)\n    \n    # get sex info\n    subject_num = df_defog_meta.loc[df_defog_meta['Id'] == Id, 'Subject'].iloc[0]\n    sex = df_subjects.loc[df_subjects['Subject'] == subject_num, 'Sex'].iloc[0]\n    \n    if sex == 'F':\n        # plot accelaraton value of medication off at first row of the figure\n        ax[0, 0].plot(df_defog.loc[:, 'AccV'].reset_index(drop=True), alpha=0.25)\n        ax[1, 0].plot(df_defog.loc[:, 'AccML'].reset_index(drop=True), alpha=0.25)\n        ax[2, 0].plot(df_defog.loc[:, 'AccAP'].reset_index(drop=True), alpha=0.25)        \n        ax[0, 0].set_title(f'AccV @ Sex = {sex}')\n        ax[1, 0].set_title(f'AccML @ Sex = {sex}')\n        ax[2, 0].set_title(f'AccAP @ Sex = {sex}')\n    else:\n        # plot accelaraton value of medication on at second row of the figure\n        ax[0, 1].plot(df_defog.loc[:, 'AccV'].reset_index(drop=True), alpha=0.25)\n        ax[1, 1].plot(df_defog.loc[:, 'AccML'].reset_index(drop=True), alpha=0.25)\n        ax[2, 1].plot(df_defog.loc[:, 'AccAP'].reset_index(drop=True), alpha=0.25)        \n        ax[0, 1].set_title(f'AccV @ Sex = {sex}')\n        ax[1, 1].set_title(f'AccML @ Sex = {sex}')\n        ax[2, 1].set_title(f'AccAP @ Sex = {sex}')\n    \n    ax[0, 0].set_ylim(-6, 4)\n    ax[1, 0].set_ylim(-6, 4)\n    ax[2, 0].set_ylim(-6, 4)\n    ax[0, 1].set_ylim(-6, 4)\n    ax[1, 1].set_ylim(-6, 4)\n    ax[2, 1].set_ylim(-6, 4)\n          \nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-04-21T07:44:31.010750Z","iopub.execute_input":"2023-04-21T07:44:31.011170Z","iopub.status.idle":"2023-04-21T07:44:36.183040Z","shell.execute_reply.started":"2023-04-21T07:44:31.011129Z","shell.execute_reply":"2023-04-21T07:44:36.181677Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}