{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport os \nimport matplotlib.pyplot as plt\nfrom PIL import Image\nimport cv2\nimport json\nimport seaborn as sns","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def read_traces_file(fname):\n    df = pd.read_csv(fname,sep = '\\t',\n           comment = '#', names = [x for x in range(10)])\n    return df\n\ndef extract_metadata(fname):\n    with open(fname) as f:\n        dictio = json.load(f)\n        r_height = dictio['map_info']['height']\n        r_width = dictio['map_info']['width']\n        \n        return float(r_height), float(r_width)\n\ndef extract_traces(fname):\n    traces = []\n    full_df = read_traces_file(fname)\n    wayp = full_df[full_df[1]== 'TYPE_WAYPOINT']\n    coor = wayp.iloc[:,2:4]\n    for i in range(coor.shape[0]):\n        traces.append((float(coor.iloc[i][2]),float(coor.iloc[i][3])))\n    return traces\n        \ndef draw_map(map_image,metadata_fname, traces_fname, arrow = False):\n    \n    \"\"\"\n    This function can draw the locations with points of several colors from a list and one is chosen randomly.\n    This function can also draw an arrow from the previous to the current poiny if chosen (the arrow is green)\n    \"\"\"\n    \n    colors = [(255,0,0), (0,0,255), (255,0,255), (128,0,0),\n             (0,128,128), (0,255,255), (250,235,215),(205,133,63),\n             (112,128,144), (230,230,250),(240,255,255), (255,105,180),\n             (128,0,128), (30,144,255)] # this is the color pallet\n    color = colors[np.random.randint(len(colors), size = 1)[0]]\n    \n    img = map_image\n    traces = extract_traces(traces_fname)\n    real_height, real_width = extract_metadata(metadata_fname)\n    img_height = img.shape[0]\n    img_width = img.shape[1]\n    \n    for i in range(len(traces)):\n        \n        coor = traces[i]\n        \n        real_x = coor[0]\n        real_y = coor[1]\n        \n        corrected_x =  int((img_width/real_width) * real_x) # the points are adjusted by a factor to match the cv2 format\n        corrected_y =  int(img_height - (img_height/real_height) * real_y)\n    \n        \n        img = cv2.circle(img, (corrected_x, corrected_y), color = color, radius = 1, thickness = 4) #drawing the points\n        \n        if i != 0 and arrow:\n            \n            img = cv2.arrowedLine(img, previous, (corrected_x, corrected_y), color = (0,255,0), thickness = 1)\n        previous = (corrected_x, corrected_y)\n    \n    return img","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def extract_accelerometer_data(fname):\n    df = read_traces_file(fname)\n    acce_df = df[df[1]== \"TYPE_ACCELEROMETER\"]\n    acce_df = acce_df.iloc[:,:5]\n    time = acce_df[0].astype('int')\n    x_acc = acce_df[2].astype('float')\n    y_acc = acce_df[3].astype('float')\n    z_acc = acce_df[4].astype('float')\n    \n    return time, x_acc, y_acc, z_acc\n    \ndef plot_accelerometer(fname):\n    \n    t,x,y,z = extract_accelerometer_data(fname)\n    _ = plt.figure(figsize = (15,15))\n    plt.title(\"Acceleration Plot\")\n    plt.xlabel('Time')\n    plt.ylabel('Acceleration')\n    plt.plot(np.array(t), np.array(x), label = 'x-axis')\n    plt.plot(np.array(t), np.array(y), label = 'y-axis')\n    plt.plot(np.array(t), np.array(z), label = 'z-axis')\n    plt.grid()\n    plt.legend()\n    plt.show()\n    ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# showing examples \n\nimage = cv2.imread(\"../input/indoor-location-navigation/metadata/5cd56b5ae2acfd2d33b58544/F1/floor_image.png\")\nmeta_fname = \"../input/indoor-location-navigation/metadata/5cd56b5ae2acfd2d33b58544/F1/floor_info.json\"\ntrace_fname = \"../input/indoor-location-navigation/train/5cd56b5ae2acfd2d33b58544/F1/5cf23c227427840009011eda.txt\"\n\nimage = draw_map(image, meta_fname, trace_fname, arrow = True)\n_ = plt.figure(figsize = (15,15))\nplt.imshow(image)\nplt.show()\nplot_accelerometer(trace_fname)\n\nimage = cv2.imread(\"../input/indoor-location-navigation/metadata/5cd56b5ae2acfd2d33b58546/B1/floor_image.png\")\nmeta_fname = \"../input/indoor-location-navigation/metadata/5cd56b5ae2acfd2d33b58546/B1/floor_info.json\"\ntrace_fname = \"../input/indoor-location-navigation/train/5cd56b5ae2acfd2d33b58546/B1/5cf39cca5b96c60008d35add.txt\"\n\nimage = draw_map(image, meta_fname, trace_fname, arrow = True)\n_ = plt.figure(figsize = (15,15))\nplt.imshow(image)\nplt.show()\nplot_accelerometer(trace_fname)\n\nimage = cv2.imread(\"../input/indoor-location-navigation/metadata/5cd56b5ae2acfd2d33b58548/1F/floor_image.png\")\nmeta_fname = \"../input/indoor-location-navigation/metadata/5cd56b5ae2acfd2d33b58548/1F/floor_info.json\"\ntrace_fname = \"../input/indoor-location-navigation/train/5cd56b5ae2acfd2d33b58548/1F/5cf20b12718b08000848a9f6.txt\"\n\nimage = draw_map(image, meta_fname, trace_fname, arrow = True)\n_ = plt.figure(figsize = (15,15))\nplt.imshow(image)\nplt.show()\nplot_accelerometer(trace_fname)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Gyroscope data\n\ndef extract_gyroscope_data(fname):\n    df = read_traces_file(fname)\n    gyro_df = df[df[1]== \"TYPE_GYROSCOPE\"]\n    gyro_df = gyro_df.iloc[:,:5]\n    time = gyro_df[0].astype('int')\n    x_gyro = gyro_df[2].astype('float')\n    y_gyro = gyro_df[3].astype('float')\n    z_gyro = gyro_df[4].astype('float')\n    \n    return time, x_gyro, y_gyro, z_gyro\n    \ndef plot_accelerometer(fname):\n    \n    t,x,y,z = extract_gyroscope_data(fname)\n    axis_name = [\"x-axis\", 'y-axis', 'z-axis']\n    axis_data = [x,y,z]\n    _ = plt.figure(figsize = (40,10))\n    for i in range(3):\n        \n        plt.subplot(1,3, i+1)\n        plt.xlabel('Time')\n        plt.ylabel('Gyroscope Angle')\n        plt.plot(np.array(t), np.array(axis_data[i]), label = axis_name[i])\n        plt.grid()\n        plt.legend()\n\nplot_accelerometer(\"../input/indoor-location-navigation/train/5a0546857ecc773753327266/B1/5e15730aa280850006f3d005.txt\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","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}