<commit_before>{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Jaccard index computation between shape files\n",
    "## Development version"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This notebook illustrates the calculation of the Jaccard index between slums shape file and slighntly modified one denoting the municipality ground truth for the slums in Kalyan, India."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "Python package imports"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import matplotlib.pyplot as plt\n",
    "%matplotlib inline\n",
    "\n",
    "#import fiona\n",
    "from fiona import collection as ficol\n",
    "from descartes import PolygonPatch"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Paths and filenames"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Full shape filename:  /home/elena/DynaSlum/Data/Kalyan/original/slums_municipality_ROI.shp\n"
     ]
    }
   ],
   "source": [
    "# data paths\n",
    "TRUTH_SHAPE_PATH = \"/home/elena/DynaSlum/Data/Kalyan/original/\"\n",
    "#TRUTH_SHAPE_PATH = \"/home/elena/DynaSlum/Data/misc/\"\n",
    "\n",
    "# masks filename\n",
    "slums_shape_fname = \"slums_municipality_ROI.shp\"\n",
    "#slums_shape_fname = \"coutwildrnp.shp\"\n",
    "#slums_shape_fname = \"test_uk.shp\"\n",
    "\n",
    "# masks full filenames\n",
    "truth_slums_shape_fullfname = os.path.join(TRUTH_SHAPE_PATH, slums_shape_fname)\n",
    "\n",
    "print(\"Full shape filename: \", truth_slums_shape_fullfname)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Load data and display in figure"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "image/png": "<KEY>EAV4IYQQ\nIkIU4IUQQogIUYAXQgghIkQBXgghhIiQu2zAuVBeDHvVAAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x7fb0f6fc9f28>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "BLUE = '#6699cc'\n",
    "fig = plt.figure(1, figsize=(6, 6), dpi=90)\n",
    "ax = fig.add_subplot(111)\n",
    "#with fiona.open(truth_slums_shape_fullfname, \"r\") as f:\n",
    "#    print(len(f))\n",
    "with ficol(truth_slums_shape_fullfname, \"r\") as input:\n",
    "    for f in input:\n",
    "        #print(f)\n",
    "        ax.add_patch(\n",
    "            PolygonPatch(\n",
    "                f['geometry'], fc=BLUE, ec=BLUE, alpha=0.5 ))\n",
    "ax.set_xlim(297329, 300252) \n",
    "ax.set_ylim(2123672, 2127405)        \n",
    "#ax.set_xlim(-114, -105) \n",
    "#ax.set_ylim(37.1, 42)        \n",
    "#ax.set_xlim(-8.65, 1.75) \n",
    "#ax.set_ylim(49.9, 60.85)        \n",
    "\n",
    "plt.show()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python [conda env:satelite]",
   "language": "python",
   "name": "conda-env-satelite-py"
  },
  "language_info": {<commit_msg>Loading and displaying shape files - works for Kalyan slums shape file<commit_after>{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Jaccard index computation between shape files\n",
    "## Development version"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This notebook illustrates the calculation of the Jaccard index between slums shape file and slighntly modified one denoting the municipality ground truth for the slums in Kalyan, India."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "Python package imports"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import os\n",
    "import matplotlib.pyplot as plt\n",
    "%matplotlib inline\n",
    "\n",
    "import fiona\n",
    "#import shapely.geometry as shgeom\n",
    "from fiona import collection as ficol\n",
    "from descartes import PolygonPatch"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Paths and filenames"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Full truth shape filename:  /home/elena/DynaSlum/Data/Kalyan/original/slums_municipality.shp\n",
      "Full test shape filename1:  /home/elena/DynaSlum/Data/Kalyan/test/slums_municipality_modified1.shp\n",
      "Full test shape filename2:  /home/elena/DynaSlum/Data/Kalyan/test/slums_municipality_modified2.shp\n"
     ]
    }
   ],
   "source": [
    "# data paths\n",
    "TRUTH_SHAPE_PATH = \"/home/elena/DynaSlum/Data/Kalyan/original/\"\n",
    "TEST_SHAPE_PATH = \"/home/elena/DynaSlum/Data/Kalyan/test/\"\n",
    "\n",
    "# masks filename\n",
    "slums_shape_fname = \"slums_municipality.shp\"\n",
    "test_shape_fname1 = \"slums_municipality_modified1.shp\" # does not display!?\n",
    "test_shape_fname2 = \"slums_municipality_modified2.shp\" # for now copy os the truth shape file\n",
    "\n",
    "# masks full filenames\n",
    "truth_slums_shape_fullfname = os.path.join(TRUTH_SHAPE_PATH, slums_shape_fname)\n",
    "test_slums_shape_fullfname1 = os.path.join(TEST_SHAPE_PATH, test_shape_fname1)\n",
    "test_slums_shape_fullfname2 = os.path.join(TEST_SHAPE_PATH, test_shape_fname2)\n",
    "\n",
    "print(\"Full truth shape filename: \", truth_slums_shape_fullfname)\n",
    "print(\"Full test shape filename1: \", test_slums_shape_fullfname1)\n",
    "print(\"Full test shape filename2: \", test_slums_shape_fullfname2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Load data and display the bounds"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(297328.6765506615, 2123671.144477916, 300732.35364115785, 2127839.1117448886)\n",
      "xmin:  297328.6765506615 xmax:  300732.35364115785 ymin:  2123671.144477916 ymax:  2127839.1117448886\n"
     ]
    }
   ],
   "source": [
    "fp = fiona.open(truth_slums_shape_fullfname)\n",
    "xmin, ymin, xmax, ymax = fp.bounds\n",
    "print(fp.bounds)\n",
    "print(\"xmin: \", xmin, \"xmax: \", xmax, \"ymin: \", ymin, \"ymax: \", ymax)\n",
    "offset= 100\n",
    "fp.close()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Load data and display in figure"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.figure.Figure at 0x7f1dc56132e8>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "<KEY>//<KEY>",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x7f1dc56132b0>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "ORANGE = '#FF4500'\n",
    "GREEN = '#008000'\n",
    "fig = plt.figure(1, figsize=(50, 50), dpi=120)\n",
    "_, (ax1, ax2) = plt.subplots(1, 2, sharey=True)\n",
    "\n",
    "with ficol(truth_slums_shape_fullfname, \"r\") as input:\n",
    "    for f in input:\n",
    "        ax1.add_patch(\n",
    "            PolygonPatch(\n",
    "                f['geometry'], fc=GREEN, ec=GREEN, alpha=0.5 ))\n",
    "ax1.set_xlim(xmin - offset, xmax + offset) \n",
    "ax1.set_ylim(ymin - offset, ymax + offset)              \n",
    "ax1.set_title('Slum shape file: truth')\n",
    "\n",
    "with ficol(test_slums_shape_fullfname2, \"r\") as input:\n",
    "    for g in input:\n",
    "        ax2.add_patch(\n",
    "            PolygonPatch(\n",
    "                g['geometry'], fc=ORANGE, ec=ORANGE, alpha=0.5 ))            \n",
    "\n",
    "ax2.set_xlim(xmin - offset, xmax + offset) \n",
    "ax2.set_ylim(ymin - offset, ymax + offset) \n",
    "\n",
    "ax2.set_title('Slum shape file: test')\n",
    "plt.show()\n"
   ]
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python [conda env:satelite]",
   "language": "python",
   "name": "conda-env-satelite-py"
  },
  "language_info": {
{'fraction_non_alphanumeric': 0.1886963416015404, 'fraction_numerical': 0.0326197757390418, 'mean_word_length': 2.6099754701553555, 'pattern_counts': {'":': 118, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1216982', 'n_tokens_mistral': 3694, 'n_tokens_neox': 3505, 'n_words': 604}