{"cells":[{"metadata":{},"cell_type":"markdown","source":"# Indoor Location & Navigation - Basic EDA - Paths On The Floor Map"},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import os\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\n\nimport json\nfrom pylab import imread\nfrom pprint import pprint","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Let's take a look into data directory."},{"metadata":{"trusted":true},"cell_type":"code","source":"data_path = '/kaggle/input/indoor-location-navigation'\nos.listdir(data_path)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"* **train** directory contains sites (shopping malls) directories that consists of floor direcories. And each floor directory contains txt files, that have information about paths (smarthones).\n\n* **metedata** directory contains floor map, its size and geo inforamation for each site and each floor.\n\nIn this competition we should predict smartphone location: floor number and x, y coordinates (TYPE_WAYPOINT). \n"},{"metadata":{"trusted":true},"cell_type":"code","source":"floor = '5a0546857ecc773753327266/F1'\nfloor_metadata_dir = os.path.join(data_path, 'metadata', floor)\nfloor_train_dir = os.path.join(data_path, 'train', floor)\n\nos.listdir(floor_metadata_dir)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"os.listdir(floor_train_dir)[:5]","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Loading floor map and its size"},{"metadata":{"trusted":true},"cell_type":"code","source":"image = imread(\n    os.path.join(floor_metadata_dir, 'floor_image.png')\n)\n\nwith open(os.path.join(floor_metadata_dir, 'floor_info.json')) as f:\n    content = f.read()\n    floor_info = json.loads(content)\n\nheight = float(floor_info['map_info']['height'])\nwidth = float(floor_info['map_info']['width'])\n    \npprint(floor_info)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Loading coordinates of paths points"},{"metadata":{"trusted":true},"cell_type":"code","source":"def load_points(filepath):\n    \n    with open(filepath) as f:\n        content = f.read()\n    \n    data = []\n    for row in content.split('\\n'):\n        if 'TYPE_WAYPOINT' in row:\n            data.append(row)\n\n    points = {\n        'timestamp': [],\n        'x': [],\n        'y': []\n\n    }\n\n    for row in data:\n        values = row.split('\\t')\n\n        points['timestamp'].append(int(values[0]))\n        points['x'].append(float(values[2]))\n        points['y'].append(float(values[3]))\n    \n    points_df = pd.DataFrame(points)\n    points_df.sort_values(by='timestamp', inplace=True)\n    \n    return points_df","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"paths = []\nfor filename in os.listdir(floor_train_dir):\n    if '.txt' not in filename:\n        continue\n    points = load_points(os.path.join(floor_train_dir, filename))\n    paths.append((len(points), points, filename))\n\npaths = sorted(paths, key=lambda path: path[0], reverse=True)\npaths = paths[:20]","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Paths Visualization"},{"metadata":{"trusted":true},"cell_type":"code","source":"fig = plt.figure(figsize=(15, 12))\nax = plt.subplot(111)\n\nplt.imshow(image, extent=[0, width, 0, height])\n\n\n\nfor _, points, filename in paths:\n    plt.scatter(points['x'], points['y'], label=filename)\n    plt.plot(points['x'], points['y'])\n    \n\nax.legend(loc='center left', bbox_to_anchor=(1, 0.5))\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Hope this notebook will be helpful for you. I wish you good fortune in the competition!"}],"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}