{"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":"# Post process tool used to generate path overlays on top of floor map\n- used to visually compare predicted and ground truth path x,y, and floor values\n","metadata":{}},{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport matplotlib.pyplot as plt\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Choose site location to simplify and speed up the output\n- Floor mapping is used based on the input file\n- options have been set based on the input and output of the LSTM implementation and the LGBM implementation","metadata":{}},{"cell_type":"code","source":"from pathlib import Path\n\nfloor_map = {\"B2\":-2, \"B1\":-1, \"F1\":0, \"F2\": 1, \"F3\":2,\n             \"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,\n             \"7F\":6, \"8F\": 7, \"9F\":8}\n\ncounter_map = {-2:\"B2\", -1:\"B1\", 0:\"F1\", 1:\"F2\", 2:\"F3\",\n             3:\"F4\", 4:\"F5\", 5:\"F6\"}\n\ndef split_col(df):\n    df = pd.concat([\n        df['site_path_timestamp'].str.split('_', expand=True) \\\n        .rename(columns={0:'site',\n                         1:'path',\n                         2:'timestamp'}),\n        df\n    ], axis=1).copy()\n    return df\n\nsub_df = pd.read_csv('../input/light-gbm-indoorloc/submission.csv')\nsub_df = split_col(sub_df[['site_path_timestamp','floor','x','y']]).copy()\n\n#predTrue = pd.read_csv('../input/predictedvtruth/sample_output.csv')\npredTrue = pd.read_csv('../input/light-gbm-indoorloc/sample_output.csv')\n\n# lstm site = 5a0546857ecc773753327266\n# lgbm site = 5d27096c03f801723c31e5e0\n\n#def generate_target_sites(sub_df):\n#    return sorted(sub_df['site'].unique())\n\ndef generate_site_floors_dict(sub_df):\n    #sites = generate_target_sites(sub_df)\n    #site = '5a0546857ecc773753327266'\n    site='5d27096c03f801723c31e5e0'\n    #site = '5a0546857ecc773753327266'\n    site_floors_dict = {}\n    \n    #for site in sites:\n    site_path = Path('/kaggle/input/indoor-location-navigation/train') / site\n    site_floors_dict[site] = [path.name for path in site_path.glob('*')]\n    return site_floors_dict\n\n#all_sites = generate_target_sites(sub_df)\nsite_floors_dict = generate_site_floors_dict(sub_df)\nsite_floors_dict\n\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predTrue","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_sites(sites, sub1_df):#, sub2_df, sub3_df):\n    num_floors = 0\n    for site in sites:\n        num_floors += len(site_floors_dict[site])\n\n    fig, ax = plt.subplots(num_floors, 2, figsize=(8, 50))\n\n    idx = 0\n    for site in sites:\n        floors = site_floors_dict[site]\n\n        for floor in floors:\n            print(site, \" : \", floor, \" : \", )\n            #plot_preds(ax[idx], \"raw submission\", site, floor, sub1_df, show_preds=True)#, train_waypoints, show_preds=True)\n            plot_preds_truth(ax[idx][0], \"predictions\", site, floor, sub1_df, show_preds=True, show_train=False)#, train_waypoints, show_preds=True)\n            plot_preds_truth(ax[idx][1], \"ground truth\", site, floor, sub1_df, show_preds=False, show_train=True)\n            ##plot_preds(ax[idx][2], \"snap_to_grid\", site, floor, sub3_df, train_waypoints, show_preds=True)\n            idx += 1\n    fig.savefig('compare.png')\n    plt.show()    ","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import json\n\ndef plot_preds(\n    ax,\n    context_text,\n    site,\n    floorNo,\n    sub=None,\n    show_preds=True,\n    fix_labels=True,\n    true_locs=None,\n    base=\"../input/indoor-location-navigation\",\n    show_train=False,        \n    map_floor=None\n):\n    \"\"\"\n    Plots predictions on floorplan map.\n    \n    map_floor : use a different floor's map\n    \"\"\"\n    #map_floor = {-2:\"B2\", -1:\"B1\", 0:\"F1\", 1:\"F2\", 2:\"F3\",\n    #         3:\"F4\", 4:\"F5\", 5:\"F6\"}\n    \n    floor_map = {\"B2\":-2, \"B1\":-1, \"F1\":0, \"F2\": 1, \"F3\":2,\n             \"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,\n             \"7F\":6, \"8F\": 7, \"9F\":8}\n    \n    if map_floor is None:\n        map_floor = floorNo\n    # Prepare width_meter & height_meter (taken from the .json file)\n    floor_plan_filename = f\"{base}/metadata/{site}/{map_floor}/floor_image.png\"\n    json_plan_filename = f\"{base}/metadata/{site}/{map_floor}/floor_info.json\"\n    \n    with open(json_plan_filename) as json_file:\n        json_data = json.load(json_file)\n\n    width_meter = json_data[\"map_info\"][\"width\"]\n    height_meter = json_data[\"map_info\"][\"height\"]\n\n    floor_img = plt.imread(f\"{base}/metadata/{site}/{map_floor}/floor_image.png\")\n\n\n    ax.imshow(floor_img)\n\n    if show_train:\n        true_locs = true_locs.query('site == @site and floorNo == @map_floor').copy()\n        true_locs[\"x_\"] = true_locs[\"x\"] * floor_img.shape[0] / height_meter\n        true_locs[\"y_\"] = (\n            true_locs[\"y\"] * -1 * floor_img.shape[1] / width_meter\n        ) + floor_img.shape[0]\n        true_locs.query(\"site == @site and floorNo == @map_floor\").groupby(\"path\").plot(\n            x=\"x_\",\n            y=\"y_\",\n            style=\"+\",\n            ax=ax,\n            label=\"train waypoint location\",\n            color=\"grey\",\n            alpha=0.5,\n        )\n\n    if show_preds:\n        sub = sub[(sub['site']==site) & (sub['floor']==floor_map[floorNo])].copy()\n        #sub = sub[sub['site']==site]\n        #sub = sub[sub['floor']==floorNo]\n        \n        sub[\"x_\"] = sub[\"x\"] * floor_img.shape[0] / height_meter\n        sub[\"y_\"] = (sub[\"y\"] * -1 * floor_img.shape[1] / width_meter) + floor_img.shape[0]\n        \n        for path, path_data in sub.groupby(\"path\"):            \n            path_data.plot(\n                x=\"x_\",\n                y=\"y_\",\n                style=\".-\",\n                ax=ax,\n                title=\"context_text\",#f\"{context_text} - {site} - floor - {floorNo}\",\n                alpha=1,\n                label=path,\n            )\n    if fix_labels:\n        handles, labels = ax.get_legend_handles_labels()\n        by_label = dict(zip(labels, handles))\n        ax.legend(\n            by_label.values(), by_label.keys(), loc=\"center left\", bbox_to_anchor=(1, 0.5)\n        )\n    return","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import json\n\ndef plot_preds_truth(\n    ax,\n    context_text,\n    site,\n    floorNo,\n    sub=None,\n    show_preds=True,\n    fix_labels=True,\n    true_locs=None,\n    base=\"../input/indoor-location-navigation\",\n    show_train=False,        \n    map_floor=None\n):\n    \"\"\"\n    Plots predictions on floorplan map.\n    \n    map_floor : use a different floor's map\n    \"\"\"\n    #map_floor = {-2:\"B2\", -1:\"B1\", 0:\"F1\", 1:\"F2\", 2:\"F3\",\n    #         3:\"F4\", 4:\"F5\", 5:\"F6\"}\n    \n    floor_map = {\"B2\":-2, \"B1\":-1, \"F1\":0, \"F2\": 1, \"F3\":2,\n             \"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,\n             \"7F\":6, \"8F\": 7, \"9F\":8}\n    \n    if map_floor is None:\n        map_floor = floorNo\n    # Prepare width_meter & height_meter (taken from the .json file)\n    floor_plan_filename = f\"{base}/metadata/{site}/{map_floor}/floor_image.png\"\n    json_plan_filename = f\"{base}/metadata/{site}/{map_floor}/floor_info.json\"\n    \n    with open(json_plan_filename) as json_file:\n        json_data = json.load(json_file)\n\n    width_meter = json_data[\"map_info\"][\"width\"]\n    height_meter = json_data[\"map_info\"][\"height\"]\n\n    floor_img = plt.imread(f\"{base}/metadata/{site}/{map_floor}/floor_image.png\")\n\n\n    ax.imshow(floor_img)\n\n    if show_train:\n        #sub = sub[(sub['site']==site) & (sub['floor']==floor_map[floorNo])].copy()\n        sub = sub[(sub['fTrue']==floor_map[floorNo])].copy()\n        #sub = sub[sub['site']==site]\n        #sub = sub[sub['floor']==floorNo]\n        \n        sub[\"x_\"] = sub[\"xPred\"] * floor_img.shape[0] / height_meter\n        sub[\"y_\"] = (sub[\"yPred\"] * -1 * floor_img.shape[1] / width_meter) + floor_img.shape[0]\n        sub[\"x_t\"] = sub[\"xTrue\"] * floor_img.shape[0] / height_meter\n        sub[\"y_t\"] = (sub[\"yTrue\"] * -1 * floor_img.shape[1] / width_meter) + floor_img.shape[0]\n        \n        count = 0\n        for path, path_data in sub.groupby(\"path\"):            \n            if count < 10:\n                path_data.plot(\n                    x=\"x_t\",\n                    y=\"y_\",\n                    style=\".-\",\n                    ax=ax,\n                    title=f\"{context_text}\",#} - {site} - floor - {floorNo}\",\n                    alpha=1,\n                    #label=path,\n                )       \n            count+=1\n\n    if show_preds:\n        #sub = sub[(sub['site']==site) & (sub['floor']==floor_map[floorNo])].copy()\n        sub = sub[(sub['fTrue']==floor_map[floorNo])].copy()\n        #sub = sub[sub['site']==site]\n        #sub = sub[sub['floor']==floorNo]\n        \n        sub[\"x_\"] = sub[\"xPred\"] * floor_img.shape[0] / height_meter\n        sub[\"y_\"] = (sub[\"yPred\"] * -1 * floor_img.shape[1] / width_meter) + floor_img.shape[0]\n        sub[\"x_t\"] = sub[\"xTrue\"] * floor_img.shape[0] / height_meter\n        sub[\"y_t\"] = (sub[\"yTrue\"] * -1 * floor_img.shape[1] / width_meter) + floor_img.shape[0]\n        \n        count = 0\n        for path, path_data in sub.groupby(\"path\"):            \n            if count < 10:\n                path_data.plot(\n                    x=\"x_\",\n                    y=\"y_\",\n                    style=\".-\",\n                    ax=ax,\n                    title=f\"{context_text}\",#f\"{context_text} - {site} - floor - {floorNo}\",\n                    alpha=1,\n                    #label=path,\n                )       \n            count+=1\n        \n    if fix_labels:\n        handles, labels = ax.get_legend_handles_labels()\n        by_label = dict(zip(labels, handles))\n        ax.legend(\n            by_label.values(), by_label.keys(), loc=\"center left\", bbox_to_anchor=(1, 0.5)\n        )\n    return","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#sub_df[sub_df['site']=='5a0546857ecc773753327266'].head()\nfloorNo = 0\n#site = '5a0546857ecc773753327266'\nsite = '5d27096c03f801723c31e5e0'\n#sub = sub_df[(sub_df['site']==site) & (sub_df['floor']==floorNo)].copy()#'site == @site and floor == @floor').copy()\n#sub\n#sub = sub_df[sub_df['site']==site]\n#sub = sub_df[sub_df['floor']==floorNo]\n\n#sub[\"x_\"] = sub[\"x\"] * floor_img.shape[0] / height_meter\n#sub[\"y_\"] = (sub[\"y\"] * -1 * floor_img.shape[1] / width_meter) + floor_img.shape[0]\n\n#for path, path_data in sub.groupby(\"path\"):\n#    print(path_data)\n##sub[\"x_\"] = sub[\"x\"] * floor_img.shape[0] / height_meter\n##sub[\"y_\"] = (sub[\"y\"] * -1 * floor_img.shape[1] / width_meter) + floor_img.shape[0] \n##for path, path_data in sub.query(\"site == @site and floor == @floorNo\").groupby(\"path\"):\n##    path_data.plot(x=\"x_\",y=\"y_\",style=\".-\",ax=ax,title=f\"{context_text} - {site} - floor - {floorNo}\",alpha=1,label=path,)\n#sub_df.head()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Data is formated and plotted","metadata":{}},{"cell_type":"code","source":"#sites=['5a0546857ecc773753327266']\nsites = ['5d27096c03f801723c31e5e0']\n#plot_sites(sites, sub_df)#, processed_sub_df1, processed_sub_df2)\nplot_sites(sites, predTrue)\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}