{"cells":[{"metadata":{},"cell_type":"markdown","source":"## Library imports"},{"metadata":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","execution":{"iopub.execute_input":"2021-02-03T20:30:06.590945Z","iopub.status.busy":"2021-02-03T20:30:06.589984Z","iopub.status.idle":"2021-02-03T20:30:06.593594Z","shell.execute_reply":"2021-02-03T20:30:06.592887Z"},"papermill":{"duration":0.01623,"end_time":"2021-02-03T20:30:06.593847","exception":false,"start_time":"2021-02-03T20:30:06.577617","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"import os\nimport gc\nimport json \nimport glob\nimport numpy as np\nimport pandas as pd\npd.options.mode.chained_assignment = None  # default='warn'\n\n# plotting library\nfrom skimage import io\nfrom skimage.transform import resize\nimport matplotlib.pyplot as plt\n\nfrom PIL import Image\nimport cv2\n\n# interactive plots\nimport plotly.express as px\nimport plotly.graph_objects as go\nfrom plotly.subplots import make_subplots\n\nimport warnings  \nwarnings.filterwarnings('ignore')","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Helper functions"},{"metadata":{"trusted":true},"cell_type":"code","source":"def getFileData(file):    \n    wifi = list()\n    waypoint = list()\n    with open(file) as f:\n        txt = f.readlines()\n    for line in txt:\n        line = line.strip().split()\n        if line[1] == \"TYPE_WAYPOINT\":\n            waypoint.append(line)\n        if line[1] == \"TYPE_WIFI\":\n            wifi.append(line)\n    \n    # WiFi dataframe\n    wiFiDf = pd.DataFrame(np.array(wifi))\n    # sort by time\n    wiFiDf.sort_values(by=[0],inplace=True)\n\n    # waypoint df\n    waypointDf = pd.DataFrame(np.array(waypoint))\n    waypointDf.sort_values(by=[0], inplace=True)    \n\n    return wiFiDf, waypointDf","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def getPointData(wiFiDf, waypointDf):        \n    wayPtTimestamps = waypointDf[0].values.astype(np.int64)\n    wifiTimeStamps = wiFiDf[0].values.astype(np.int64)\n    \n    waypointx = waypointDf[2].values.astype(float)\n    waypointy = waypointDf[3].values.astype(float)\n    waypointData = np.column_stack((waypointx, waypointy))\n        \n    wifiAPx = np.interp(wifiTimeStamps, wayPtTimestamps, waypointx)\n    wifiAPy = np.interp(wifiTimeStamps, wayPtTimestamps, waypointy)\n    wifiAPData = np.column_stack((wifiAPx, wifiAPy))\n\n    return wayPtTimestamps, waypointData, wifiTimeStamps, wifiAPData","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"```python\ndef getGeoJsonInfo(jsonFilePath):\n\n    # Opening JSON file \n    f = open(inputFile)\n\n    # returns JSON object as  a dictionary \n    geoJsonData = json.load(f) \n    \n    boundaryPoints = []\n    for idx,feature in enumerate(geoJsonData['features']):\n        boundaryPoints.append(feature['geometry']['coordinates'])\n    print(np.squeeze(np.array(feature['geometry']['coordinates']), axis=0).shape)\n    boundaryPoints = np.squeeze(np.concatenate(boundaryPoints,axis=1), axis=0)\n    print(boundaryPoints.shape)\n    return boundaryPoints\n\n# inputFile = 'metadata/5a0546857ecc773753327266/B1/geojson_map.json'\n# pts = getGeoJsonInfo(inputFile)\n# plt.plot(pts[:,0], pts[:,1], 'ko')\n```"},{"metadata":{"execution":{"iopub.execute_input":"2021-02-03T20:30:06.639011Z","iopub.status.busy":"2021-02-03T20:30:06.638118Z","iopub.status.idle":"2021-02-03T20:30:09.333807Z","shell.execute_reply":"2021-02-03T20:30:09.334360Z"},"papermill":{"duration":2.711076,"end_time":"2021-02-03T20:30:09.334617","exception":false,"start_time":"2021-02-03T20:30:06.623541","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"base_path = '../input/indoor-location-navigation'\n\n# pull out all the buildings actually used in the test set, given current method we don't need the other ones\nssubm = pd.read_csv('../input/indoor-location-navigation/sample_submission.csv')\n\n# only 24 of the total buildings are used in the test set, \n# this allows us to greatly reduce the intial size of the dataset\n\nssubm_df = ssubm[\"site_path_timestamp\"].apply(lambda x: pd.Series(x.split(\"_\")))\nused_buildings = sorted(ssubm_df[0].value_counts().index.tolist())\n\n# dictionary used to map the floor codes to the values used in the submission file. \nfloor_map = {\"B2\":-2, \"B1\":-1, \"F1\":0, \"F2\": 1, \"F3\":2, \"F4\":3, \"F5\":4, \"F6\":5, \"F7\":6,\"F8\":7, \"F9\":8,\n             \"1F\":0, \"2F\":1, \"3F\":2, \"4F\":3, \"5F\":4, \"6F\":5, \"7F\":6, \"8F\": 7, \"9F\":8}","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Sample building site and floor"},{"metadata":{"trusted":true},"cell_type":"code","source":"building = used_buildings[0]\nfolders = sorted(glob.glob(os.path.join(base_path,'train', building +'/*')))\nfolder = folders[0]\nfloor = folder.split('/')[-1]\nfiles = glob.glob(os.path.join(folder, \"*.txt\"))\n\nprint(f\"building = {building}\")\nprint(f\"folder = {folder}\")\nprint(f\"floor = {floor}\")","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Get interpolated waypoints acoridng to wifi timestamps"},{"metadata":{"trusted":true},"cell_type":"code","source":"interpolatedInfo = {}\n\nfor i,file in enumerate(files):\n    fileName = file.split('/')[-1].rstrip('.txt')\n    interpolatedInfo[fileName] = {}\n    \n    # get wifi and waypoint data from each path file\n    wifiDf, waypointDf = getFileData(file)\n\n    # linear interpolate x,y coordinates based on wifi timestamp\n    wayPtTimestamps, waypointData, wifiTimeStamps, wifiAPData =  getPointData(wifiDf, waypointDf)\n\n    # store output\n    interpolatedInfo[fileName][\"wayPtTimestamps\"] = wayPtTimestamps\n    interpolatedInfo[fileName][\"waypointData\"] = waypointData\n    interpolatedInfo[fileName][\"wifiTimeStamps\"] = wifiTimeStamps\n    interpolatedInfo[fileName][\"wifiAPData\"] = wifiAPData","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"with open(f\"{base_path}/metadata/{building}/{floor}/floor_info.json\") as f:\n    content = f.read()\n    floor_info = json.loads(content)\n\nprint(f\"floor_info = {floor_info}\")","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Plot original data vs interpolated data"},{"metadata":{},"cell_type":"markdown","source":"```python\nimg = plt.imread(f\"{base_path}/metadata/{building}/{floor}/floor_image.png\")    \nplt.figure(figsize=(24,24))\nplt.imshow(img, extent=[0,floor_info[\"map_info\"][\"width\"],0,floor_info[\"map_info\"][\"height\"]])\n\nfor pathFile in list(interpolatedInfo.keys()):\n    pathData = interpolatedInfo[pathFile]\n    #plt.plot(pathData[\"waypointData\"][:,0], pathData[\"waypointData\"][:,1],  label = f'{pathFile}_waypt')\n    plt.plot(pathData[\"wifiAPData\"][:,0], pathData[\"wifiAPData\"][:,1], label = f'{pathFile}_interpolated',\n             marker ='o', markersize=1)\n#plt.legend(loc='best')\nplt.grid(True)\nplt.savefig(f\"{pathFile}.png\",dpi=300)\n```"},{"metadata":{"trusted":true},"cell_type":"code","source":"img = cv2.imread(f\"{base_path}/metadata/{building}/{floor}/floor_image.png\").astype(np.uint8)\nimage = Image.fromarray(img)\nlayout = go.Layout(images=[dict(\n                    source=image,\n                    xref = 'x', yref='y', x=0, y =floor_info[\"map_info\"][\"height\"],\n                    sizex=floor_info[\"map_info\"][\"width\"], sizey=floor_info[\"map_info\"][\"height\"],\n                    sizing= \"stretch\", opacity = 0.25, layer=\"below\")])\n\nfig = go.Figure(layout=layout)\nfor pathFile in list(interpolatedInfo.keys()):\n    pathData = interpolatedInfo[pathFile]\n    trace0 = go.Scatter(x=pathData[\"wifiAPData\"][:,0], y=pathData[\"wifiAPData\"][:,1],\n                    name=f'{pathFile}_interpolated')\n    trace1 = go.Scatter(x=pathData[\"waypointData\"][:,0], y=pathData[\"waypointData\"][:,1],\n                    name=f'{pathFile}_waypt')\n    fig.add_traces([trace0, trace1])\n    \nfig.show()   ","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}