{"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":"## Goals: \n- Look at the accelerometer data in target events (hesitation, walk, and turn).\n- Visually identify some features that may help the preprocessin pipeline.","metadata":{}},{"cell_type":"code","source":"from pathlib import Path\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt","metadata":{"_uuid":"d5232335-da15-44dd-99d4-0f74f973b5c9","_cell_guid":"2367b827-242c-41ac-921a-226683e19c49","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-03-11T16:02:06.857232Z","iopub.execute_input":"2023-03-11T16:02:06.857926Z","iopub.status.idle":"2023-03-11T16:02:06.870080Z","shell.execute_reply.started":"2023-03-11T16:02:06.857877Z","shell.execute_reply":"2023-03-11T16:02:06.868983Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Data acquisition rate\ndata_rate = {\n    \"tdcsfog\": 128,\n    \"defog\": 100,\n    \"daily\": 100,\n    \"notype\": 100 # is defog\n}\n\n# Paths\ndata_path = Path('/kaggle/input/tlvmc-parkinsons-freezing-gait-prediction')\ntrain_path = data_path / 'train'\ntest_path = data_path / 'test'\n","metadata":{"execution":{"iopub.status.busy":"2023-03-11T16:02:06.871270Z","iopub.execute_input":"2023-03-11T16:02:06.871587Z","iopub.status.idle":"2023-03-11T16:02:06.881004Z","shell.execute_reply.started":"2023-03-11T16:02:06.871556Z","shell.execute_reply":"2023-03-11T16:02:06.879825Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Event file","metadata":{}},{"cell_type":"code","source":"# events.csv Metadata for each FoG event in all data series. \n# The event times agree with the labels \n\n#     Id The data series the event occured in.\n#     Init Time (s) the event began.\n#     Completion Time (s) the event ended.\n#     Type Whether StartHesitation, Turn, or Walking.\n#     Kinetic Whether the event was kinetic (1) and involved movement, or akinetic (0) and static.\n\ndf_event = pd.read_csv(str(data_path / \"events.csv\"))\ndf_event","metadata":{"execution":{"iopub.status.busy":"2023-03-11T16:02:33.738206Z","iopub.execute_input":"2023-03-11T16:02:33.738672Z","iopub.status.idle":"2023-03-11T16:02:33.770394Z","shell.execute_reply.started":"2023-03-11T16:02:33.738634Z","shell.execute_reply":"2023-03-11T16:02:33.769006Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_event.Type.unique()","metadata":{"execution":{"iopub.status.busy":"2023-03-11T16:02:34.456063Z","iopub.execute_input":"2023-03-11T16:02:34.456498Z","iopub.status.idle":"2023-03-11T16:02:34.465248Z","shell.execute_reply.started":"2023-03-11T16:02:34.456456Z","shell.execute_reply":"2023-03-11T16:02:34.463867Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"NOTE: What is akinetic walk? Walking without moving? Assuming `Kinetic` categorize Y type further, can we use `Kinetic` as an intermediate label?","metadata":{}},{"cell_type":"markdown","source":"### Plot events","metadata":{}},{"cell_type":"code","source":"# id to full path, data rate mapping\nid2path = {}\nid2rate = {}\n\nfor folder in ['defog', 'tdcsfog', 'notype']:\n    csv_files = (train_path / folder).glob('*.csv')\n    for path in csv_files:\n        id2path.update({path.stem: str(path)})\n        id2rate.update({path.stem: data_rate[folder]})","metadata":{"execution":{"iopub.status.busy":"2023-03-11T16:03:04.614559Z","iopub.execute_input":"2023-03-11T16:03:04.615043Z","iopub.status.idle":"2023-03-11T16:03:04.641547Z","shell.execute_reply.started":"2023-03-11T16:03:04.615001Z","shell.execute_reply":"2023-03-11T16:03:04.640263Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def load_data(id, start, end, padding=1) -> pd.DataFrame:\n    \"\"\"Load selected raw data.\"\"\"\n    \n    df = pd.read_csv(id2path[id])\n    df['t'] = df.Time / id2rate[id]\n    return df.query(\"t >= @start - @padding & t <= @end + @padding\")","metadata":{"execution":{"iopub.status.busy":"2023-03-11T16:04:08.853372Z","iopub.execute_input":"2023-03-11T16:04:08.854664Z","iopub.status.idle":"2023-03-11T16:04:08.860249Z","shell.execute_reply.started":"2023-03-11T16:04:08.854615Z","shell.execute_reply":"2023-03-11T16:04:08.858898Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_data(data: pd.DataFrame) -> None:\n    \"\"\"Plot event with some basic labels.\"\"\"\n    \n    fig, ax = plt.subplots()\n    \n    # Data from accelerometers\n    ax = data.plot(ax=ax, kind='line', x='t', y='AccV', label='v')\n    ax = data.plot(ax=ax, kind='line', x='t', y='AccML', label='ml')\n    ax = data.plot(ax=ax, kind='line', x='t', y='AccAP', label='ap')\n    \n    # Highlight actual target range\n    ax.fill_between(data.t, 0, 1, where=data.StartHesitation, color='red', alpha=0.5, transform=ax.get_xaxis_transform(), label='hesitation')\n    ax.fill_between(data.t, 0, 1, where=data.Turn, color='orange', alpha=0.5, transform=ax.get_xaxis_transform(), label='turn')\n    ax.fill_between(data.t, 0, 1, where=data.Walking, color='teal', alpha=0.5, transform=ax.get_xaxis_transform(), label='walk')\n    \n    ax.set_title(f\"Hesitation: {data.StartHesitation.mean()}, Turn: {data.Turn.mean()}, Walk: {data.Walking.mean()}\")\n    plt.legend(loc='best')","metadata":{"execution":{"iopub.status.busy":"2023-03-11T16:04:09.143561Z","iopub.execute_input":"2023-03-11T16:04:09.144567Z","iopub.status.idle":"2023-03-11T16:04:09.154612Z","shell.execute_reply.started":"2023-03-11T16:04:09.144514Z","shell.execute_reply":"2023-03-11T16:04:09.153122Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_event(df_event:pd.DataFrame, event: str, kinetic: bool = None) -> None:\n    \"\"\"Locate event, load data, and plot.\n    \n    Args: \n        event: `hesitation`, `turn`, or `walk`.\n        kinetic: TODO: understand what this is about...\n    \"\"\"\n    to_colname = {\"hesitation\": \"StartHesitation\", \"turn\":\"Turn\", \"walk\": \"Walking\"}\n    col_name = to_colname[event]\n    \n    # Get one matching event\n    if kinetic is None:\n        df_selected = df_event.query(f\"Type == @col_name\")\n    else:\n        df_selected = df_event.query(f\"Type == @col_name & Kinetic == @kinetic\")\n        \n    n = len(df_selected)\n    if n == 0:\n        raise ValueError('No matching case found.')\n    \n    x = df_selected.sample(1)\n    \n    print(f\"Selected ID: {x.Id.values[0]} from {n} matching cases\")\n    data = load_data(x.Id.values[0], x.Init.values[0], x.Completion.values[0])\n    plot_data(data)","metadata":{"execution":{"iopub.status.busy":"2023-03-11T16:04:12.185768Z","iopub.execute_input":"2023-03-11T16:04:12.186642Z","iopub.status.idle":"2023-03-11T16:04:12.197661Z","shell.execute_reply.started":"2023-03-11T16:04:12.186589Z","shell.execute_reply":"2023-03-11T16:04:12.196218Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":" plot_event(df_event, \"walk\")","metadata":{"execution":{"iopub.status.busy":"2023-03-11T16:07:29.656112Z","iopub.execute_input":"2023-03-11T16:07:29.656569Z","iopub.status.idle":"2023-03-11T16:07:30.128790Z","shell.execute_reply.started":"2023-03-11T16:07:29.656525Z","shell.execute_reply":"2023-03-11T16:07:30.127837Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"NOTE:\n- I thought walking should be easy, but it is difficult to tell... research how Garmin do it.","metadata":{}},{"cell_type":"code","source":" plot_event(df_event, \"turn\")","metadata":{"execution":{"iopub.status.busy":"2023-03-11T16:06:56.323095Z","iopub.execute_input":"2023-03-11T16:06:56.323542Z","iopub.status.idle":"2023-03-11T16:06:56.848433Z","shell.execute_reply.started":"2023-03-11T16:06:56.323501Z","shell.execute_reply":"2023-03-11T16:06:56.847013Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"NOTE: Hard to tell...? Help...","metadata":{}},{"cell_type":"code","source":" plot_event(df_event, \"hesitation\")","metadata":{"execution":{"iopub.status.busy":"2023-03-11T16:06:11.162061Z","iopub.execute_input":"2023-03-11T16:06:11.162476Z","iopub.status.idle":"2023-03-11T16:06:11.511932Z","shell.execute_reply.started":"2023-03-11T16:06:11.162441Z","shell.execute_reply":"2023-03-11T16:06:11.510987Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"NOTE:\n- Very few (n=107) cases of hesitation\n- Event during can be vary quite a bit\n- Hesitation doesn't means relatively still...","metadata":{}}]}