{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"from functools import reduce\n\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\n\n%config InlineBackend.figure_formats = ['svg']","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def read_all(file):\n    names = ['Time', 'Type'] + ['col'+str(x) for x in range(1,10)]\n    df = pd.read_csv(file, sep='\\t', comment='#', header=None, names=names)\n    return df","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def read_trace(path_trace_file):\n    df = read_all(path_trace_file)\n    df.Time = (df.Time - df.Time[0]) / 1000\n\n    acc = df[df.Type == 'TYPE_ACCELEROMETER'].dropna(axis=1).rename(columns={'col1': 'a_x', 'col2': 'a_y', 'col3': 'a_z'}).drop('Type', axis=1).reset_index(drop=True)\n    gyro = df[df.Type == 'TYPE_GYROSCOPE'].dropna(axis=1).rename(columns={'col1': 'w_x', 'col2': 'w_y', 'col3': 'w_z'}).drop('Type', axis=1).reset_index(drop=True)\n    rot = df[df.Type == 'TYPE_ROTATION_VECTOR'].dropna(axis=1).rename(columns={'col1': 'rot_x', 'col2': 'rot_y', 'col3': 'rot_z'}).drop('Type', axis=1).reset_index(drop=True)\n    mag = df[df.Type == 'TYPE_MAGNETIC_FIELD'].dropna(axis=1).rename(columns={'col1': 'B_x', 'col2': 'B_y', 'col3': 'B_z'}).drop('Type', axis=1).reset_index(drop=True)\n    waypoint =  df.loc[df.Type == 'TYPE_WAYPOINT', ['Time', 'col1', 'col2']].rename(columns={'col1': 'd_x', 'col2': 'd_y'}).reset_index(drop=True)\n    # There is some ambiguity in these guys.\n    # wifi = ...\n    # beacon = ...\n\n    dfs = [acc, gyro, rot, mag]\n    df_final = reduce(lambda left, right: pd.merge(left, right, on='Time', how='left'), dfs)\n    \n    df_final = df_final.astype('float32')\n    waypoint = waypoint.astype('float32')\n    return df_final, waypoint","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"path_trace_file = \"../input/indoor-location-navigation/train/5a0546857ecc773753327266/F2/5d1340ade5d8ea0008e38ff8.txt\"\ndf1, df2 = read_trace(path_trace_file)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Trace Features"},{"metadata":{"trusted":true},"cell_type":"code","source":"df1.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"yl = {'a': 'acceleration (m/s$^2$)', 'w': 'Angular Velocity (rad/s)', 'rot': 'Rotation Vector', 'B': 'B-field ($\\mu$T)'}\nfor feature in ['a', 'w', 'rot', 'B']:\n    sub_feature = ['Time'] + [x for x in df1.columns if x.startswith(feature)]\n    df1[sub_feature].plot(x='Time', figsize=(12, 3), xlabel='Time (s)', ylabel=yl[feature])\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Waypoints"},{"metadata":{"trusted":true},"cell_type":"code","source":"df2","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(12, 3))\ndf2.plot(x='Time', y=['d_x', 'd_y'], marker='o', title='Time Evolution', ax=ax1, ylabel='Position (m)')\ndf2.plot(x='d_x', y='d_y', marker='o', xlabel='d_x (m)', ylabel='d_y (m)', title='2D Position', ax=ax2)\nplt.show()","execution_count":null,"outputs":[]}],"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":4,"nbformat_minor":4}