{"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":"# Table of Contents\n* [List of files](#files)\n* [Select a specific file](#select)\n* [Pairwise Scatter Plots](#pairplot)\n* [Correlation](#correlation)\n* [Time Series Plots](#time_series)\n* [Derived Features](#derived)\n* [3D Plots](#3dplot)","metadata":{}},{"cell_type":"code","source":"# packages\n\n# standard\nimport numpy as np\nimport pandas as pd\nimport time\n\n# plots\nimport matplotlib.pyplot as plt\nfrom mpl_toolkits import mplot3d\nimport seaborn as sns\nimport plotly.express as px","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-03-12T09:11:51.650602Z","iopub.execute_input":"2023-03-12T09:11:51.651136Z","iopub.status.idle":"2023-03-12T09:11:56.393202Z","shell.execute_reply.started":"2023-03-12T09:11:51.651093Z","shell.execute_reply":"2023-03-12T09:11:56.391356Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# config\ndefault_col_1 = 'darkblue'\ndefault_col_2 = 'darkorange'","metadata":{"execution":{"iopub.status.busy":"2023-03-12T09:11:56.395766Z","iopub.execute_input":"2023-03-12T09:11:56.396743Z","iopub.status.idle":"2023-03-12T09:11:56.404111Z","shell.execute_reply.started":"2023-03-12T09:11:56.396699Z","shell.execute_reply":"2023-03-12T09:11:56.401152Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id='files'></a>\n# List of files","metadata":{}},{"cell_type":"code","source":"# show files - defog\n!ls -l '../input/tlvmc-parkinsons-freezing-gait-prediction/train/defog'","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2023-03-12T09:11:56.407552Z","iopub.execute_input":"2023-03-12T09:11:56.409098Z","iopub.status.idle":"2023-03-12T09:11:57.575820Z","shell.execute_reply.started":"2023-03-12T09:11:56.409028Z","shell.execute_reply":"2023-03-12T09:11:57.573996Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# show files - no type\n!ls -l '../input/tlvmc-parkinsons-freezing-gait-prediction/train/notype/'","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2023-03-12T09:11:57.579384Z","iopub.execute_input":"2023-03-12T09:11:57.580782Z","iopub.status.idle":"2023-03-12T09:11:58.706746Z","shell.execute_reply.started":"2023-03-12T09:11:57.580713Z","shell.execute_reply":"2023-03-12T09:11:58.704617Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# show files - tdcsfog\n!ls -l '../input/tlvmc-parkinsons-freezing-gait-prediction/train/tdcsfog/'","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2023-03-12T09:11:58.710177Z","iopub.execute_input":"2023-03-12T09:11:58.710646Z","iopub.status.idle":"2023-03-12T09:11:59.891713Z","shell.execute_reply.started":"2023-03-12T09:11:58.710602Z","shell.execute_reply":"2023-03-12T09:11:59.890376Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id='select'></a>\n# Select a specific file","metadata":{}},{"cell_type":"code","source":"my_file = 'defog/06414383cf.csv'","metadata":{"execution":{"iopub.status.busy":"2023-03-12T09:11:59.893532Z","iopub.execute_input":"2023-03-12T09:11:59.893902Z","iopub.status.idle":"2023-03-12T09:11:59.900287Z","shell.execute_reply.started":"2023-03-12T09:11:59.893865Z","shell.execute_reply":"2023-03-12T09:11:59.898960Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# import and preview\ndf = pd.read_csv('../input/tlvmc-parkinsons-freezing-gait-prediction/train/'+my_file)\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2023-03-12T09:11:59.902629Z","iopub.execute_input":"2023-03-12T09:11:59.903412Z","iopub.status.idle":"2023-03-12T09:12:00.212347Z","shell.execute_reply.started":"2023-03-12T09:11:59.903360Z","shell.execute_reply":"2023-03-12T09:12:00.210866Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# structure of data frame\ndf.info()","metadata":{"execution":{"iopub.status.busy":"2023-03-12T09:12:00.214089Z","iopub.execute_input":"2023-03-12T09:12:00.214548Z","iopub.status.idle":"2023-03-12T09:12:00.250117Z","shell.execute_reply.started":"2023-03-12T09:12:00.214508Z","shell.execute_reply":"2023-03-12T09:12:00.248741Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# basic stats\ndf.describe(percentiles=[0.01,0.05,0.1,0.25,0.5,0.75,0.9,0.95,0.99])","metadata":{"execution":{"iopub.status.busy":"2023-03-12T09:12:00.251889Z","iopub.execute_input":"2023-03-12T09:12:00.252344Z","iopub.status.idle":"2023-03-12T09:12:00.326782Z","shell.execute_reply.started":"2023-03-12T09:12:00.252304Z","shell.execute_reply":"2023-03-12T09:12:00.325508Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# acceleration features\naccs = ['AccV','AccML','AccAP']","metadata":{"execution":{"iopub.status.busy":"2023-03-12T09:12:00.330574Z","iopub.execute_input":"2023-03-12T09:12:00.330959Z","iopub.status.idle":"2023-03-12T09:12:00.337303Z","shell.execute_reply.started":"2023-03-12T09:12:00.330923Z","shell.execute_reply":"2023-03-12T09:12:00.335460Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id='pairplot'></a>\n# Pairwise Scatter Plots","metadata":{}},{"cell_type":"code","source":"color_var = 'Turn' # visualize additional feature (event) using color\nsns.pairplot(data=df[accs+[color_var]],\n             hue=color_var,\n             plot_kws = {'s': 15, \n                         'alpha' : 0.5})\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-03-12T09:12:00.339077Z","iopub.execute_input":"2023-03-12T09:12:00.339441Z","iopub.status.idle":"2023-03-12T09:12:41.244240Z","shell.execute_reply.started":"2023-03-12T09:12:00.339408Z","shell.execute_reply":"2023-03-12T09:12:41.242769Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id='correlation'></a>\n# Correlation","metadata":{}},{"cell_type":"code","source":"corr_pearson = df[accs].corr(method='pearson')\ncorr_spearman = df[accs].corr(method='spearman')\n\nplt.figure(figsize=(9,3))\nax1 = plt.subplot(1,2,1)\nsns.heatmap(corr_pearson, annot=True, cmap='RdYlGn', vmin=-1, vmax=+1)\nplt.title('Pearson Correlation')\n\nax2 = plt.subplot(1,2,2, sharex=ax1)\nsns.heatmap(corr_spearman, annot=True, cmap='RdYlGn', vmin=-1, vmax=+1)\nplt.title('Spearman Correlation')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-03-12T09:12:41.246260Z","iopub.execute_input":"2023-03-12T09:12:41.246984Z","iopub.status.idle":"2023-03-12T09:12:41.742659Z","shell.execute_reply.started":"2023-03-12T09:12:41.246946Z","shell.execute_reply":"2023-03-12T09:12:41.741428Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id='time_series'></a>\n# Time Series Plots","metadata":{}},{"cell_type":"code","source":"for f in accs:\n    plt.figure(figsize=(14,3))\n    plt.plot(df.Time, df[f], color=default_col_1)\n    plt.title(f)\n    plt.xlabel('Time')\n    plt.ylabel(f)\n    plt.grid()\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2023-03-12T09:12:41.744122Z","iopub.execute_input":"2023-03-12T09:12:41.744487Z","iopub.status.idle":"2023-03-12T09:12:42.418692Z","shell.execute_reply.started":"2023-03-12T09:12:41.744452Z","shell.execute_reply":"2023-03-12T09:12:42.417020Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# plot event\nplt.figure(figsize=(14,3))\nplt.plot(df.Time, df[color_var], color=default_col_2)\nplt.title(color_var)\nplt.xlabel('Time')\nplt.ylabel(color_var)\nplt.grid()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-03-12T09:12:42.420821Z","iopub.execute_input":"2023-03-12T09:12:42.421269Z","iopub.status.idle":"2023-03-12T09:12:42.610770Z","shell.execute_reply.started":"2023-03-12T09:12:42.421229Z","shell.execute_reply":"2023-03-12T09:12:42.609370Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Zoom in:","metadata":{}},{"cell_type":"code","source":"ta = 40000\ntb = 50000\n\nfor f in accs:\n    plt.figure(figsize=(14,3))\n    plt.plot(df.Time[ta:(tb+1)], df[f][ta:(tb+1)], color=default_col_1)\n    plt.title(f)\n    plt.xlabel('Time')\n    plt.ylabel(f)\n    plt.grid()\n    plt.show()\n    \n# plot event\nplt.figure(figsize=(14,3))\nplt.plot(df.Time[ta:(tb+1)], df[color_var][ta:(tb+1)], color=default_col_2)\nplt.title(color_var)\nplt.xlabel('Time')\nplt.ylabel(color_var)\nplt.grid()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-03-12T09:18:32.092273Z","iopub.execute_input":"2023-03-12T09:18:32.092781Z","iopub.status.idle":"2023-03-12T09:18:33.028099Z","shell.execute_reply.started":"2023-03-12T09:18:32.092736Z","shell.execute_reply":"2023-03-12T09:18:33.026513Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id='derived'></a>\n# Derived Features","metadata":{}},{"cell_type":"code","source":"# length of acceleration vector, adjust vertical component for gravity influence\ndf['acc_norm'] = np.sqrt(pow(df.AccV+1,2) + pow(df.AccML,2) + pow(df.AccAP,2))","metadata":{"execution":{"iopub.status.busy":"2023-03-12T09:12:43.349475Z","iopub.execute_input":"2023-03-12T09:12:43.349848Z","iopub.status.idle":"2023-03-12T09:12:43.361772Z","shell.execute_reply.started":"2023-03-12T09:12:43.349815Z","shell.execute_reply":"2023-03-12T09:12:43.360085Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# plot time series\nf = 'acc_norm'\nplt.figure(figsize=(14,3))\nplt.plot(df.Time, df[f], color=default_col_1)\nplt.title(f)\nplt.xlabel('Time')\nplt.ylabel(f)\nplt.grid()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-03-12T09:12:43.363995Z","iopub.execute_input":"2023-03-12T09:12:43.364423Z","iopub.status.idle":"2023-03-12T09:12:43.629026Z","shell.execute_reply.started":"2023-03-12T09:12:43.364380Z","shell.execute_reply":"2023-03-12T09:12:43.627521Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# plot histogram\nplt.figure(figsize=(8,3))\ndf['acc_norm'].plot(kind='hist', bins=100, \n                    color=default_col_1)\nplt.title('acc_norm - distribution')\nplt.grid()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-03-12T09:12:43.631105Z","iopub.execute_input":"2023-03-12T09:12:43.631487Z","iopub.status.idle":"2023-03-12T09:12:44.025352Z","shell.execute_reply.started":"2023-03-12T09:12:43.631443Z","shell.execute_reply":"2023-03-12T09:12:44.023471Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id='3dplot'></a>\n# 3D Plots","metadata":{}},{"cell_type":"code","source":"# pick a subset to keep it recognizable\nta = 43000\ntb = ta + 600\n\ndf_sub = df[ta:(tb+1)]","metadata":{"execution":{"iopub.status.busy":"2023-03-12T09:18:40.599888Z","iopub.execute_input":"2023-03-12T09:18:40.601762Z","iopub.status.idle":"2023-03-12T09:18:40.609257Z","shell.execute_reply.started":"2023-03-12T09:18:40.601648Z","shell.execute_reply":"2023-03-12T09:18:40.607487Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# plot as space curve\nfig = plt.figure(figsize=(7,7))\nax = plt.axes(projection='3d')\nax.plot3D(df_sub['AccV'], df_sub['AccML'], df_sub['AccAP'],\n          c=default_col_1)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-03-12T09:18:43.246045Z","iopub.execute_input":"2023-03-12T09:18:43.247226Z","iopub.status.idle":"2023-03-12T09:18:43.544525Z","shell.execute_reply.started":"2023-03-12T09:18:43.247137Z","shell.execute_reply":"2023-03-12T09:18:43.542235Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 3d scatter plot colored by time\nfig = plt.figure(figsize=(7,7))\nax = plt.axes(projection='3d')\nax.scatter3D(df_sub['AccV'], df_sub['AccML'], df_sub['AccAP'],\n             c=df_sub.Time, cmap='Blues')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-03-12T09:18:46.966713Z","iopub.execute_input":"2023-03-12T09:18:46.967383Z","iopub.status.idle":"2023-03-12T09:18:47.258022Z","shell.execute_reply.started":"2023-03-12T09:18:46.967327Z","shell.execute_reply":"2023-03-12T09:18:47.256188Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Interactive 3D Plot:","metadata":{}},{"cell_type":"code","source":"# interactive plot\nfig = px.scatter_3d(df_sub, x='AccV', y='AccML', z='AccAP',\n                    color='Time',\n                    size='acc_norm',\n                    hover_data=['Time','Turn'],\n                    opacity=0.5)\nfig.update_layout(title='Interactive 3D Plot - Display Time and Absolute Acceleration')\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2023-03-12T09:23:02.912080Z","iopub.execute_input":"2023-03-12T09:23:02.913402Z","iopub.status.idle":"2023-03-12T09:23:02.991787Z","shell.execute_reply.started":"2023-03-12T09:23:02.913358Z","shell.execute_reply":"2023-03-12T09:23:02.990540Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# interactive plot - display event via color\nfig = px.scatter_3d(df_sub, x='AccV', y='AccML', z='AccAP',\n                    color='Turn',\n                    size='Time',\n                    hover_data=['Time','acc_norm'],\n                    opacity=0.5)\nfig.update_layout(title='Interactive 3D Plot - Highlight Event')\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2023-03-12T09:23:21.588425Z","iopub.execute_input":"2023-03-12T09:23:21.588869Z","iopub.status.idle":"2023-03-12T09:23:22.019112Z","shell.execute_reply.started":"2023-03-12T09:23:21.588826Z","shell.execute_reply":"2023-03-12T09:23:22.017599Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Thanks for watching!","metadata":{}}]}