{"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":"import pandas as pd\nimport numpy as np\nimport glob\nimport os\nfrom os import path\nfrom pathlib import Path\nfrom tqdm.auto import tqdm\nimport seaborn as sns\nimport plotly.express as px\nimport plotly.graph_objects as go\nfrom sklearn.preprocessing import RobustScaler","metadata":{"execution":{"iopub.status.busy":"2023-05-19T18:51:19.206115Z","iopub.execute_input":"2023-05-19T18:51:19.206886Z","iopub.status.idle":"2023-05-19T18:51:21.852061Z","shell.execute_reply.started":"2023-05-19T18:51:19.206847Z","shell.execute_reply":"2023-05-19T18:51:21.850510Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def butter_highpass(data, cutoff, fs, order=5):\n    nyq = 0.5 * fs\n    normal_cutoff = cutoff / nyq\n    b, a = signal.butter(order, normal_cutoff, btype='low', analog=False)\n    y = signal.filtfilt(b, a, data)\n    return y","metadata":{"execution":{"iopub.status.busy":"2023-05-19T18:51:21.854510Z","iopub.execute_input":"2023-05-19T18:51:21.854897Z","iopub.status.idle":"2023-05-19T18:51:21.866486Z","shell.execute_reply.started":"2023-05-19T18:51:21.854862Z","shell.execute_reply":"2023-05-19T18:51:21.865298Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def reader(file):\n\n    df = pd.read_csv(file, index_col='Time', usecols=['Time', 'AccV', 'AccML', 'AccAP', 'StartHesitation', 'Turn' , 'Walking'])\n\n    path_split = file.split('/')\n    df['Id'] = path_split[-1].split('.')[0]\n    dataset = Path(file).parts[-2]\n    df['Module'] = dataset\n\n    # this is done because the speeds are at different rates for the datasets\n    if dataset == 'tdcsfog':\n        df.AccV = df.AccV / 9.80665\n        df.AccML = df.AccML / 9.80665\n        df.AccAP = df.AccAP / 9.80665\n\n    df['Time_frac']=(df.index/df.index.max()).values\n    df['Time'] = (df.index).values\n\n    df = pd.merge(df, tasks[['Id']], how='left', on='Id').fillna(-1)\n\n    df = pd.merge(df, metadata_w_subjects[['Id','Subject', 'Visit','Test','Medication']], how='left', on='Id').fillna(-1)\n\n#         # stride\n#         df[\"Stride\"] = df[\"AccV\"] + df[\"AccML\"] + df[\"AccAP\"]\n\n#         # step\n#         df[\"Step\"] = np.sqrt(abs(df[\"Stride\"]))\n\n    df.fillna(method=\"ffill\", inplace=True)\n\n    return df\n","metadata":{"execution":{"iopub.status.busy":"2023-05-19T18:51:21.874713Z","iopub.execute_input":"2023-05-19T18:51:21.876381Z","iopub.status.idle":"2023-05-19T18:51:21.890636Z","shell.execute_reply.started":"2023-05-19T18:51:21.876339Z","shell.execute_reply":"2023-05-19T18:51:21.889746Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"root = '/kaggle/input/tlvmc-parkinsons-freezing-gait-prediction/'\n\ntrain = glob.glob(path.join(root, 'train/defog/**'))\ntrain.extend(glob.glob(path.join(root, 'train/tdcsfog/**')))\ntest = glob.glob(path.join(root, 'test/**/**'))\n\nsubjects = pd.read_csv(path.join(root, 'subjects.csv'))\ntasks = pd.read_csv(path.join(root, 'tasks.csv'))\nevents = pd.read_csv(path.join(root, 'events.csv'))\n\ntdcsfog_metadata = pd.read_csv(path.join(root, 'tdcsfog_metadata.csv'))\ndefog_metadata = pd.read_csv(path.join(root, 'defog_metadata.csv')) \n\ntdcsfog_metadata['Module'] = 'tdcsfog'\ndefog_metadata['Module'] = 'defog'\n\nfull_metadata = pd.concat([tdcsfog_metadata, defog_metadata])","metadata":{"execution":{"iopub.status.busy":"2023-05-19T18:51:21.892051Z","iopub.execute_input":"2023-05-19T18:51:21.892897Z","iopub.status.idle":"2023-05-19T18:51:22.064113Z","shell.execute_reply.started":"2023-05-19T18:51:21.892856Z","shell.execute_reply":"2023-05-19T18:51:22.063094Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"subjects.loc[subjects['Subject'] == 'fe5d84', 'Sex'] = 'F'","metadata":{"execution":{"iopub.status.busy":"2023-05-19T18:51:22.066809Z","iopub.execute_input":"2023-05-19T18:51:22.067275Z","iopub.status.idle":"2023-05-19T18:51:22.075866Z","shell.execute_reply.started":"2023-05-19T18:51:22.067235Z","shell.execute_reply":"2023-05-19T18:51:22.074621Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"subjects['Sex'] = subjects['Sex'].factorize()[0]\nsubjects = subjects.fillna(0).groupby('Subject').median()\nnew_names = {'Visit':'s_visit','Age':'s_age','YearsSinceDx':'s_years','UPDRSIII_On':'s_on','UPDRSIII_Off':'s_off','NFOGQ':'s_NFOGQ', 'Sex': 's_sex'}\nsubjects = subjects.rename(columns = new_names)","metadata":{"execution":{"iopub.status.busy":"2023-05-19T18:51:22.077337Z","iopub.execute_input":"2023-05-19T18:51:22.077661Z","iopub.status.idle":"2023-05-19T18:51:22.146905Z","shell.execute_reply.started":"2023-05-19T18:51:22.077620Z","shell.execute_reply":"2023-05-19T18:51:22.145802Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tasks['Duration'] = tasks['End'] - tasks['Begin']\ntasks = pd.pivot_table(tasks, values=['Duration'], index=['Id'], columns=['Task'], aggfunc='sum', fill_value=0)\ntasks.columns = [c[1] for c in tasks.columns]\ntasks = tasks.reset_index()","metadata":{"execution":{"iopub.status.busy":"2023-05-19T18:51:22.148931Z","iopub.execute_input":"2023-05-19T18:51:22.149553Z","iopub.status.idle":"2023-05-19T18:51:22.237540Z","shell.execute_reply.started":"2023-05-19T18:51:22.149521Z","shell.execute_reply":"2023-05-19T18:51:22.235068Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# merge the subjects with the metadata\nmetadata_w_subjects = full_metadata.merge(subjects, how='left', on='Subject').copy()\nfeatures = metadata_w_subjects.columns","metadata":{"execution":{"iopub.status.busy":"2023-05-19T18:51:22.238929Z","iopub.execute_input":"2023-05-19T18:51:22.239914Z","iopub.status.idle":"2023-05-19T18:51:22.251178Z","shell.execute_reply.started":"2023-05-19T18:51:22.239880Z","shell.execute_reply":"2023-05-19T18:51:22.250009Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"metadata_w_subjects['Medication'] = metadata_w_subjects['Medication'].factorize()[0]","metadata":{"execution":{"iopub.status.busy":"2023-05-19T18:51:22.255901Z","iopub.execute_input":"2023-05-19T18:51:22.256337Z","iopub.status.idle":"2023-05-19T18:51:22.271520Z","shell.execute_reply.started":"2023-05-19T18:51:22.256303Z","shell.execute_reply":"2023-05-19T18:51:22.270347Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.concat([reader(f) for f in tqdm(train)]).fillna(0); print(train.shape)\ncols = [c for c in train.columns if c not in ['Id','Subject','Module', 'Time', 'StartHesitation', 'Turn' , 'Walking', 'Valid', 'Task','Event']]\npcols = ['StartHesitation', 'Turn' , 'Walking']\nscols = ['Id', 'StartHesitation', 'Turn' , 'Walking']\ntrain=train.reset_index(drop=True)","metadata":{"execution":{"iopub.status.busy":"2023-05-19T18:51:22.276253Z","iopub.execute_input":"2023-05-19T18:51:22.277239Z","iopub.status.idle":"2023-05-19T18:53:59.651417Z","shell.execute_reply.started":"2023-05-19T18:51:22.277172Z","shell.execute_reply":"2023-05-19T18:53:59.650298Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# segment timeseries","metadata":{}},{"cell_type":"code","source":"segmentation = events[events.columns[events.columns.isin(['Id', 'Init', 'Completion', 'Type'])]]","metadata":{"execution":{"iopub.status.busy":"2023-05-19T18:53:59.674068Z","iopub.execute_input":"2023-05-19T18:53:59.674509Z","iopub.status.idle":"2023-05-19T18:53:59.694631Z","shell.execute_reply.started":"2023-05-19T18:53:59.674468Z","shell.execute_reply":"2023-05-19T18:53:59.693591Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"defog_time = np.where(train['Module'] == 'defog', train['Time'] / 100, train['Time'] / 128)\ntrain['time_s'] = defog_time","metadata":{"execution":{"iopub.status.busy":"2023-05-19T18:53:59.695806Z","iopub.execute_input":"2023-05-19T18:53:59.696145Z","iopub.status.idle":"2023-05-19T18:54:02.951853Z","shell.execute_reply.started":"2023-05-19T18:53:59.696105Z","shell.execute_reply":"2023-05-19T18:54:02.950639Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def mag_calc(ax, ay, az):\n    A=np.sqrt(ax**2+ay**2+az**2)\n    return A","metadata":{"execution":{"iopub.status.busy":"2023-05-19T18:54:02.953993Z","iopub.execute_input":"2023-05-19T18:54:02.954802Z","iopub.status.idle":"2023-05-19T18:54:02.960922Z","shell.execute_reply.started":"2023-05-19T18:54:02.954757Z","shell.execute_reply":"2023-05-19T18:54:02.959930Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['AccMag'] = mag_calc(train['AccV'], train['AccML'], train['AccAP'])\ntrain['Action'] = 'None'\ntrain['Action'] = np.where(train['StartHesitation'] == 1, 'StartHesitation', train['Action'])\ntrain['Action'] = np.where(train['Turn'] == 1, 'Turn', train['Action'])\ntrain['Action'] = np.where(train['Walking'] == 1, 'Walking', train['Action'])","metadata":{"execution":{"iopub.status.busy":"2023-05-19T18:54:02.962479Z","iopub.execute_input":"2023-05-19T18:54:02.962915Z","iopub.status.idle":"2023-05-19T18:54:06.575590Z","shell.execute_reply.started":"2023-05-19T18:54:02.962874Z","shell.execute_reply":"2023-05-19T18:54:06.574422Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ScaleTrain = train\nscale_columns = ['AccML', 'AccV', 'AccAP']\n\nscaler = RobustScaler()\n\nscaler = scaler.fit(ScaleTrain[scale_columns])\n\nScaleTrain.loc[:, scale_columns] = scaler.transform(ScaleTrain[scale_columns].to_numpy())","metadata":{"execution":{"iopub.status.busy":"2023-05-19T18:54:06.576973Z","iopub.execute_input":"2023-05-19T18:54:06.580851Z","iopub.status.idle":"2023-05-19T18:54:30.004283Z","shell.execute_reply.started":"2023-05-19T18:54:06.580808Z","shell.execute_reply":"2023-05-19T18:54:30.003106Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# load single csv\nd = ScaleTrain.loc[ScaleTrain['Id'] == '02ea782681']\n\n# restrict columns\ndwhole = d[d.columns[d.columns.isin(['AccV', 'AccML', 'AccAP', 'AccMag', 'Action', 'time_s'])]]\nd = d[d.columns[d.columns.isin(['AccV', 'AccML', 'AccAP', 'AccMag', 'Action', 'time_s'])]]\n\n# set color\nd['Color'] = d['Action']\nd['Color'] = np.where(d['Color'] == 'StartHesitation', 'PaleGreen', d['Color'])\nd['Color'] = np.where(d['Color'] == 'Turn', 'PaleTurquoise', d['Color'])\nd['Color'] = np.where(d['Color'] == 'Walking', 'Pink', d['Color'])\nd['Color'] = np.where(d['Color'] == 'None', 'Yellow', d['Color'])\n\n# set group of row in dataframe every time action changes\nd['group'] = d['Action'].ne(d['Action'].shift()).cumsum()\nd = d.groupby('group')\n\n# split df by group\ndsplit = []\ncolors = []\nacts = []\nfor name, data in d:\n    dsplit.append(data)\n    \n    color = data['Color'].unique()\n    colors.append(color[0])\n    \n    act = data['Action'].unique()\n    acts.append(act[0])\n\n# make new plotly figure\nfig = go.Figure()\n\n\n# add indiv acc vectors\nfig.add_trace(\n        go.Scatter(\n            x=dwhole['time_s'],\n            y=dwhole['AccV'], \n            mode='lines',\n            line={'color': 'DarkGreen'},\n            name = 'AccV'\n        )\n    )\nfig.add_trace(\n        go.Scatter(\n            x=dwhole['time_s'],\n            y=dwhole['AccML'], \n            mode='lines',\n            line={'color': 'Indigo'},\n            name = 'AccML'\n        )\n    )\nfig.add_trace(\n        go.Scatter(\n            x=dwhole['time_s'],\n            y=dwhole['AccAP'], \n            mode='lines',\n            line={'color': 'Crimson'},\n            name = 'AccAP'\n        )\n    )\n# plot every action segment\nfor n, dseg in enumerate(dsplit):\n    fig.add_trace(\n            go.Scatter(\n                x=dseg['time_s'],\n                y=dseg['AccMag'] - 1, \n                mode='lines',\n                line={'color': colors[n]},\n                name = acts[n]\n            )\n        )\n    \nnames = set()\nfig.for_each_trace(\n    lambda trace:\n        trace.update(showlegend=False)\n        if (trace.name in names) else names.add(trace.name))\n\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2023-05-19T18:54:30.005822Z","iopub.execute_input":"2023-05-19T18:54:30.006131Z","iopub.status.idle":"2023-05-19T18:54:34.385164Z","shell.execute_reply.started":"2023-05-19T18:54:30.006105Z","shell.execute_reply":"2023-05-19T18:54:34.384223Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from scipy import signal\n\n# Filter requirements.\nfs = 100.0       # sample rate, Hz\ncutoff = 5     # desired cutoff frequency of the filter, Hz ,      slightly higher than actual 1.2 Hznyq = 0.5 * fs  # Nyquist Frequencyorder = 2       # sin wave can be approx represented as quadratic\norder = 2\nnyq = 0.5 * fs  # Nyquist Frequency\n\n# load single csv\nd = ScaleTrain.loc[ScaleTrain['Id'] == '02ea782681']\n\n# restrict columns\nd = d[d.columns[d.columns.isin(['AccV', 'AccML', 'AccAP', 'AccMag','Action', 'time_s'])]]\n\nd['AccMag2'] = butter_highpass(d['AccMag'], cutoff, fs, order)\n\n# make new plotly figure\nfig = go.Figure()\n\nfig.add_trace(\n        go.Scatter(\n            x=d['time_s'],\n            y=d['AccV'], \n            mode='lines',\n            line={'color': 'MediumVioletRed'},\n            name = 'AccV'\n        )\n    )\nfig.add_trace(\n        go.Scatter(\n            x=d['time_s'],\n            y=d['AccML'], \n            mode='lines',\n            line={'color': 'Coral'},\n            name = 'AccML'\n        )\n    )\nfig.add_trace(\n        go.Scatter(\n            x=d['time_s'],\n            y=d['AccAP'], \n            mode='lines',\n            line={'color': 'BlueViolet'},\n            name = 'AccAP'\n        )\n    )\nfig.add_trace(\n        go.Scatter(\n            x=d['time_s'],\n            y=d['AccMag']-1, \n            mode='lines',\n            line={'color': 'Yellow'},\n            name = 'AccMag'\n        )\n    )\nfig.add_trace(\n        go.Scatter(\n            x=d['time_s'],\n            y=d['AccMag2'], \n            mode='lines',\n            line={'color': 'Green'},\n            name = 'AccMag2'\n        )\n    )\n\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2023-05-19T18:54:34.386616Z","iopub.execute_input":"2023-05-19T18:54:34.387129Z","iopub.status.idle":"2023-05-19T18:54:38.560521Z","shell.execute_reply.started":"2023-05-19T18:54:34.387097Z","shell.execute_reply":"2023-05-19T18:54:38.558297Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# load single csv\nt = pd.read_csv('/kaggle/input/tlvmc-parkinsons-freezing-gait-prediction/tasks.csv')\nt = t.loc[t['Id'] == '02ea782681']\nd = ScaleTrain.loc[ScaleTrain['Id'] == '02ea782681']\n\n# restrict columns\ndwhole = d[d.columns[d.columns.isin(['AccV', 'AccML', 'AccAP', 'AccMag', 'Action', 'time_s'])]]\nd = d[d.columns[d.columns.isin(['AccV', 'AccML', 'AccAP', 'AccMag', 'Action', 'time_s'])]]\n\n# set color\nd['Color'] = d['Action']\nd['Color'] = np.where(d['Color'] == 'StartHesitation', 'PaleGreen', d['Color'])\nd['Color'] = np.where(d['Color'] == 'Turn', 'PaleTurquoise', d['Color'])\nd['Color'] = np.where(d['Color'] == 'Walking', 'Pink', d['Color'])\nd['Color'] = np.where(d['Color'] == 'None', 'Yellow', d['Color'])\n\n# set color\nt['Color'] = t['Task']\nt['Color'] = np.where(t['Color'] == '4MW', 'MidnightBlue', t['Color'])\nt['Color'] = np.where(t['Color'] == '4MW-C', 'MediumBlue', t['Color'])\n\nt['Color'] = np.where(t['Color'] == 'TUG-ST', 'Cyan', t['Color'])\nt['Color'] = np.where(t['Color'] == 'TUG-DT', 'Aquamarine', t['Color'])\nt['Color'] = np.where(t['Color'] == 'TUG-C', 'DarkTurquoise', t['Color'])\n\nt['Color'] = np.where(t['Color'] == 'Turning-ST', 'DarkGreen', t['Color'])\nt['Color'] = np.where(t['Color'] == 'Turning-DT', 'Lime', t['Color'])\nt['Color'] = np.where(t['Color'] == 'Turning-C', 'YellowGreen', t['Color'])\n\nt['Color'] = np.where(t['Color'] == 'Hotspot1', 'Orange', t['Color'])\nt['Color'] = np.where(t['Color'] == 'Hotspot1-C', 'DarkOrange', t['Color'])\nt['Color'] = np.where(t['Color'] == 'Hotspot2', 'Tomato', t['Color'])\nt['Color'] = np.where(t['Color'] == 'Hotspot2-C', 'Coral', t['Color'])\n\nt['Color'] = np.where(t['Color'] == 'MB1', 'DarkMagenta', t['Color'])\nt['Color'] = np.where(t['Color'] == 'MB2b', 'Magenta', t['Color'])\nt['Color'] = np.where(t['Color'] == 'MB3-L', 'Violet', t['Color'])\nt['Color'] = np.where(t['Color'] == 'MB3-R', 'Violet', t['Color'])\nt['Color'] = np.where(t['Color'] == 'MB4', 'MediumPurple', t['Color'])\nt['Color'] = np.where(t['Color'] == 'MB5', 'Fuchsia', t['Color'])\nt['Color'] = np.where(t['Color'] == 'MB6-L', 'BlueViolet', t['Color'])\nt['Color'] = np.where(t['Color'] == 'MB6-R', 'BlueViolet', t['Color'])\nt['Color'] = np.where(t['Color'] == 'MB7', 'Orchid', t['Color'])\nt['Color'] = np.where(t['Color'] == 'MB8', 'Thistle', t['Color'])\nt['Color'] = np.where(t['Color'] == 'MB9', 'Plum', t['Color'])\nt['Color'] = np.where(t['Color'] == 'MB10', 'RebeccaPurple', t['Color'])\nt['Color'] = np.where(t['Color'] == 'MB11', 'Purple', t['Color'])\nt['Color'] = np.where(t['Color'] == 'MB12', 'Indigo', t['Color'])\nt['Color'] = np.where(t['Color'] == 'MB13', 'SlateBlue', t['Color'])\n\nt['Color'] = np.where(t['Color'] == 'Rest1', 'DarkKhaki', t['Color'])\nt['Color'] = np.where(t['Color'] == 'Rest2', 'Yellow', t['Color'])\n\nt['y'] = 12\n# set group of row in dataframe every time action changes\nd['group'] = d['Action'].ne(d['Action'].shift()).cumsum()\nd = d.groupby('group')\n\n# set group of row in dataframe every time action changes\nt['group'] = t['Task'].ne(t['Task'].shift()).cumsum()\nt = t.groupby('Begin')\n\n\n# split df by group\ndsplit = []\ncolors = []\nacts = []\nfor name, data in d:\n    dsplit.append(data)\n    \n    color = data['Color'].unique()\n    colors.append(color[0])\n    \n    act = data['Action'].unique()\n    acts.append(act[0])\n    \n# split t by group\ntsplit = []\ntcolors = []\ntacts = []\nfor name, data in t:\n    tsplit.append(data)\n    \n    color = data['Color'].unique()\n    tcolors.append(color[0])\n    \n    tact = data['Task'].unique()\n    tacts.append(tact[0])\n\n# make new plotly figure\nfig = go.Figure()\n\n\n# add indiv acc vectors\nfig.add_trace(\n        go.Scatter(\n            x=dwhole['time_s'],\n            y=dwhole['AccV'], \n            mode='lines',\n            line={'color': 'DarkGreen'},\n            name = 'AccV'\n        )\n    )\nfig.add_trace(\n        go.Scatter(\n            x=dwhole['time_s'],\n            y=dwhole['AccML'], \n            mode='lines',\n            line={'color': 'Indigo'},\n            name = 'AccML'\n        )\n    )\nfig.add_trace(\n        go.Scatter(\n            x=dwhole['time_s'],\n            y=dwhole['AccAP'], \n            mode='lines',\n            line={'color': 'Crimson'},\n            name = 'AccAP'\n        )\n    )\n# plot every action segment\nfor n, dseg in enumerate(dsplit):\n    fig.add_trace(\n            go.Scatter(\n                x=dseg['time_s'],\n                y=dseg['AccMag'] - 1, \n                mode='lines',\n                line={'color': colors[n]},\n                name = acts[n]\n            )\n        )\n    \nnames = set()\nfig.for_each_trace(\n    lambda trace:\n        trace.update(showlegend=False)\n        if (trace.name in names) else names.add(trace.name))\n\n# plot every action segment\nfor n, tseg in enumerate(tsplit):\n    \n    xseg = tseg['Begin']\n    xseg = pd.concat([xseg, tseg['End']], axis = 0)\n    xseg = xseg.sort_values()\n    xseg.name = \"x\"\n    xseg = pd.DataFrame(xseg).reset_index()\n    xseg['y'] = 12\n\n    fig.add_trace(\n            go.Scatter(\n                x= xseg['x'],\n                y=xseg['y'], \n                mode='lines+markers',\n                line={'color':  tcolors[n]},\n                name = tacts[n]\n            )\n        )\n\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2023-05-19T18:54:38.563996Z","iopub.execute_input":"2023-05-19T18:54:38.565342Z","iopub.status.idle":"2023-05-19T18:54:42.967251Z","shell.execute_reply.started":"2023-05-19T18:54:38.565259Z","shell.execute_reply":"2023-05-19T18:54:42.965007Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import warnings\nwarnings.filterwarnings('ignore')\n# load single csv\nt = pd.read_csv('/kaggle/input/tlvmc-parkinsons-freezing-gait-prediction/tasks.csv')\ndef addTracer(fig, f, t, num):\n    t = t.loc[t['Id'] == f]\n\n    # set color\n    t['Color'] = t['Task']\n    t['Color'] = np.where(t['Color'] == '4MW', 'MidnightBlue', t['Color'])\n    t['Color'] = np.where(t['Color'] == '4MW-C', 'MediumBlue', t['Color'])\n\n    t['Color'] = np.where(t['Color'] == 'TUG-ST', 'Cyan', t['Color'])\n    t['Color'] = np.where(t['Color'] == 'TUG-DT', 'Aquamarine', t['Color'])\n    t['Color'] = np.where(t['Color'] == 'TUG-C', 'DarkTurquoise', t['Color'])\n\n    t['Color'] = np.where(t['Color'] == 'Turning-ST', 'DarkGreen', t['Color'])\n    t['Color'] = np.where(t['Color'] == 'Turning-DT', 'Lime', t['Color'])\n    t['Color'] = np.where(t['Color'] == 'Turning-C', 'YellowGreen', t['Color'])\n\n    t['Color'] = np.where(t['Color'] == 'Hotspot1', 'Orange', t['Color'])\n    t['Color'] = np.where(t['Color'] == 'Hotspot1-C', 'DarkOrange', t['Color'])\n    t['Color'] = np.where(t['Color'] == 'Hotspot2', 'Tomato', t['Color'])\n    t['Color'] = np.where(t['Color'] == 'Hotspot2-C', 'Coral', t['Color'])\n\n    t['Color'] = np.where(t['Color'] == 'MB1', 'DarkMagenta', t['Color'])\n    t['Color'] = np.where(t['Color'] == 'MB2a', 'Magenta', t['Color'])\n    t['Color'] = np.where(t['Color'] == 'MB2b', 'Magenta', t['Color'])\n    t['Color'] = np.where(t['Color'] == 'MB3-L', 'Violet', t['Color'])\n    t['Color'] = np.where(t['Color'] == 'MB3-R', 'Violet', t['Color'])\n    t['Color'] = np.where(t['Color'] == 'MB4', 'MediumPurple', t['Color'])\n    t['Color'] = np.where(t['Color'] == 'MB5', 'Fuchsia', t['Color'])\n    t['Color'] = np.where(t['Color'] == 'MB6-L', 'BlueViolet', t['Color'])\n    t['Color'] = np.where(t['Color'] == 'MB6-R', 'BlueViolet', t['Color'])\n    t['Color'] = np.where(t['Color'] == 'MB7', 'Orchid', t['Color'])\n    t['Color'] = np.where(t['Color'] == 'MB8', 'Thistle', t['Color'])\n    t['Color'] = np.where(t['Color'] == 'MB9', 'Plum', t['Color'])\n    t['Color'] = np.where(t['Color'] == 'MB10', 'RebeccaPurple', t['Color'])\n    t['Color'] = np.where(t['Color'] == 'MB11', 'Purple', t['Color'])\n    t['Color'] = np.where(t['Color'] == 'MB12', 'Indigo', t['Color'])\n    t['Color'] = np.where(t['Color'] == 'MB13', 'SlateBlue', t['Color'])\n\n    t['Color'] = np.where(t['Color'] == 'Rest1', 'DarkKhaki', t['Color'])\n    t['Color'] = np.where(t['Color'] == 'Rest2', 'Yellow', t['Color'])\n\n    t['y'] = 12\n    # set group of row in dataframe every time action changes\n    t['group'] = t['Task'].ne(t['Task'].shift()).cumsum()\n    t = t.groupby('Begin')\n\n    # split t by group\n    tsplit = []\n    tcolors = []\n    tacts = []\n    for name, data in t:\n        tsplit.append(data)\n\n        color = data['Color'].unique()\n        tcolors.append(color[0])\n\n        tact = data['Task'].unique()\n        tacts.append(tact[0])\n\n\n\n    # plot every action segment\n    for n, tseg in enumerate(tsplit):\n\n        xseg = tseg['Begin']\n        xseg = pd.concat([xseg, tseg['End']], axis = 0)\n        xseg = xseg.sort_values()\n        xseg.name = \"x\"\n        xseg = pd.DataFrame(xseg).reset_index()\n        xseg['y'] = num\n\n        fig.add_trace(\n                go.Scatter(\n                    x= xseg['x'],\n                    y=xseg['y'], \n                    mode='lines+markers',\n                    line={'color':  tcolors[n]},\n                    name = tacts[n]\n                )\n            )\n    return(fig)\n\n# make new plotly figure\nfiles = ['2054f1d5df',\n         '2ea5e817f4',\n         '13a4fe5159',\n         '06414383cf',\n         '0c55be4384',\n         '0eaac04f17',\n         '32d03020a9',\n         '32843e32b6',\n         '139f60d29b',\n         '18e7abc37e',\n         '092b4c1819',\n         '02ea782681',\n         '2cc3c30645',\n         '2e75cf4507',\n         '0ec76d2d8e',\n         '1d99c2eecf',\n         '2c12284ed2',\n         '15508c7f41', \n         '2b6a1c294a',\n         '339c0cc15f', \n         '02ab235146', \n         '0a900ed8a2',\n         '296c84448e',\n         '2acdf5a450',\n         '285c2210b3',\n         '2a01c919c0',\n         '1b3bc93401',\n         '34b979fc28',\n         '1e8d55d48d',\n         '1ff78d55e9',\n         '28209b9006',\n         '36ad8ae06d',\n         '0d7ab3a9f9']\n\nfig = go.Figure()\nfor n, f in enumerate(files):\n    fig = addTracer(fig, f, t, n)\n    \nfig.for_each_trace(\n    lambda trace:\n        trace.update(showlegend=False)\n        if (trace.name in names) else names.add(trace.name))\n\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2023-05-19T18:54:42.970697Z","iopub.execute_input":"2023-05-19T18:54:42.972105Z","iopub.status.idle":"2023-05-19T18:54:45.317359Z","shell.execute_reply.started":"2023-05-19T18:54:42.972013Z","shell.execute_reply":"2023-05-19T18:54:45.316176Z"},"trusted":true},"execution_count":null,"outputs":[]}]}