{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Imports"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Required for autocnet imports\n",
    "import os\n",
    "os.environ['ISISROOT'] = '/usgs/cpkgs/anaconda3_linux/envs/isis3.9.0'\n",
    "\n",
    "# Autocnet Imports\n",
    "from autocnet.graph.network import NetworkCandidateGraph\n",
    "from autocnet.io.db.model import Overlay\n",
    "\n",
    "# Helpful Python Modules\n",
    "import matplotlib.pyplot as plt  #     plotting package\n",
    "import numpy as np               #     numerical computing package"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Ingest Network"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You can either upload information from an isis control network to a database or use an existing database. Both methods require a configuration file (See from_cnet.ipynb for configuration file expectations)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "config_path = 'config.yml'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### If creating a database"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# lis_path = \"AS15_landingsite_apollolro.lis\"\n",
    "# cnet_path = \"AS15_landingsite_apollolro_jig1.net\"\n",
    "\n",
    "# ncg = NetworkCandidateGraph.from_filelist(lis_path, config_path, clear_db=True)\n",
    "# ncg.place_points_from_cnet(cnet_path) "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### If database exists"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/ladoramkershner/miniconda3/envs/autocnet_local/lib/python3.7/site-packages/sqlalchemy/orm/relationships.py:1917: SAWarning: Setting backref / back_populates on relationship Points.overlay to refer to viewonly relationship Overlay.points will be deprecated in SQLAlchemy 1.4, and will be disallowed in a future release.  viewonly relationships should not be mutated (this warning may be suppressed after 10 occurrences)\n",
      "  (self, other),\n",
      "/home/ladoramkershner/miniconda3/envs/autocnet_local/lib/python3.7/site-packages/sqlalchemy/orm/relationships.py:1917: SAWarning: Setting backref / back_populates on relationship Overlay.points to refer to viewonly relationship Points.overlay will be deprecated in SQLAlchemy 1.4, and will be disallowed in a future release.  viewonly relationships should not be mutated (this warning may be suppressed after 10 occurrences)\n",
      "  (self, other),\n"
     ]
    }
   ],
   "source": [
    "ncg = NetworkCandidateGraph();\n",
    "ncg.config_from_file(config_path);\n",
    "ncg.from_database();"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 37224 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "ncg.plot(); # check ncg is populated"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Check For Empty Overlaps"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Function Documentation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "ncg.empty_overlays?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "ncg.overlays?"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Run Command Visualize Results"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Review the function documentation (noticing the argument defaults) and run the cell below. Alter the valid point filters and/or the size_threshold.\n",
    "\n",
    "By default, a valid point is defined as Points.ignore==False, which converts to filters={'ignore':False}. This network contains a mix of constrained (pointtype=3) and free points (pointtype=2), corresponding with ground and relative control points. Try passing a 'filters' kwarg to find overlaps that do not contain non-ignored, relative control points."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 75452 characters]\n",
      "text/plain": [
       "<Figure size 1080x1080 with 2 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "overlays_wo_pts = ncg.empty_overlays(filters={'ignore': False}, size_threshold=0.05)\n",
    "overlays = ncg.overlays(size_threshold=0.001)\n",
    "\n",
    "#################### Plotting ####################\n",
    "fig, axs = plt.subplots(2, 1, figsize=(15, 15))\n",
    "\n",
    "# plot connected overlaps\n",
    "for o in overlays:\n",
    "    x, y = o.geom.exterior.xy\n",
    "    axs[0].plot(x, y, c='black')\n",
    "\n",
    "# plot empty overlaps\n",
    "for o in overlays_wo_pts:\n",
    "    x, y = o.geom.exterior.xy\n",
    "    axs[0].fill(x, y, c='red', alpha=0.5)\n",
    "    axs[1].fill(x, y, alpha=0.5)\n",
    "\n",
    "# plot properties\n",
    "axs[0].set_xlabel('Longitude', size=16);\n",
    "axs[0].set_ylabel('Latitude', size=16);\n",
    "axs[1].set_xlabel('Longitude', size=16);\n",
    "axs[1].set_ylabel('Latitude', size=16);\n",
    "axs[1].set_xlim(axs[0].get_xlim());\n",
    "axs[1].set_ylim(axs[0].get_ylim());"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Check Overlay Fully Connectivity"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The above function only looks at the number of points residing in an overlap. If you would like to evaluate to effective connectivity of an overlap, use the following function.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Function Documentation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "ncg.overlay_connection?"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Run function"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[(2, 3), (2, 4), (2, 5), (2, 6), (2, 8), (2, 11), (3, 4), (3, 5), (3, 6), (3, 8), (3, 11), (4, 5), (4, 6), (4, 8), (4, 11), (5, 6), (5, 8), (5, 11)]\n"
     ]
    }
   ],
   "source": [
    "oid = 5\n",
    "missing_edges = ncg.overlay_connection(oid)\n",
    "print(missing_edges)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Overlap 1 is empty\n",
      "[]\n",
      "\n",
      "Overlap 2 is empty\n",
      "[]\n",
      "\n",
      "Overlap 3 is empty\n",
      "[]\n",
      "\n",
      "Overlap 4 is empty\n",
      "[]\n",
      "\n",
      "Overlap 7 is empty\n",
      "[]\n",
      "\n",
      "[(2, 3), (2, 4), (2, 5), (2, 6), (2, 7), (2, 8), (2, 10), (3, 4), (3, 5), (3, 6), (3, 7), (3, 8), (3, 10), (4, 5), (4, 6), (4, 7), (4, 8), (4, 10), (5, 6), (5, 7), (5, 8), (5, 10)]\n",
      "\n",
      "Overlap 9 is empty\n",
      "[]\n",
      "\n",
      "Overlap 10 is empty\n",
      "[]\n",
      "\n",
      "Overlap 11 is fully connected\n",
      "[]\n",
      "\n",
      "Overlap 12 is empty\n",
      "[]\n",
      "\n",
      "Overlap 14 is empty\n",
      "[]\n",
      "\n",
      "[(2, 3), (2, 4), (2, 5), (2, 6), (2, 8), (2, 11), (3, 4), (3, 5), (3, 6), (3, 8), (3, 11), (4, 5), (4, 6), (4, 8), (4, 11), (5, 6), (5, 8), (5, 11)]\n",
      "\n",
      "Overlap 6 is empty\n",
      "[]\n",
      "\n",
      "Overlap 15 is empty\n",
      "[]\n",
      "\n",
      "[(2, 10), (3, 10), (4, 10), (5, 10)]\n",
      "\n",
      "Overlap 16 is empty\n",
      "[]\n",
      "\n",
      "Overlap 17 is empty\n",
      "[]\n",
      "\n",
      "Overlap 18 is empty\n",
      "[]\n",
      "\n",
      "Overlap 19 is empty\n",
      "[]\n",
      "\n",
      "Overlap 21 is empty\n",
      "[]\n",
      "\n",
      "Overlap 22 is empty\n",
      "[]\n",
      "\n",
      "Overlap 23 is empty\n",
      "[]\n",
      "\n",
      "[(2, 3), (2, 4), (2, 5), (2, 6), (2, 7), (2, 8), (2, 11), (3, 4), (3, 5), (3, 6), (3, 7), (3, 8), (3, 11), (4, 5), (4, 6), (4, 7), (4, 8), (4, 11), (5, 6), (5, 7), (5, 8), (5, 11)]\n",
      "\n",
      "[(2, 3), (2, 4), (2, 5), (2, 6), (2, 7), (2, 8), (3, 4), (3, 5), (3, 6), (3, 7), (3, 8), (4, 5), (4, 6), (4, 7), (4, 8), (5, 6), (5, 7), (5, 8), (7, 8)]\n",
      "\n",
      "Overlap 25 is empty\n",
      "[]\n",
      "\n",
      "Overlap 26 is empty\n",
      "[]\n",
      "\n",
      "[(2, 5)]\n",
      "\n",
      "[(2, 6), (2, 10), (3, 6), (3, 10), (4, 6), (4, 10), (5, 6), (5, 10)]\n",
      "\n",
      "Overlap 29 is fully connected\n",
      "[]\n",
      "\n",
      "Overlap 30 is fully connected\n",
      "[]\n",
      "\n",
      "Overlap 31 is empty\n",
      "[]\n",
      "\n",
      "Overlap 32 is empty\n",
      "[]\n",
      "\n",
      "[(2, 3), (2, 4), (2, 5), (2, 7), (2, 9), (3, 4), (3, 5), (3, 7), (3, 9), (4, 5), (4, 7), (4, 9), (5, 7), (5, 9)]\n",
      "\n",
      "Overlap 34 is empty\n",
      "[]\n",
      "\n",
      "Overlap 52 is empty\n",
      "[]\n",
      "\n",
      "Overlap 35 is empty\n",
      "[]\n",
      "\n",
      "Overlap 36 is empty\n",
      "[]\n",
      "\n",
      "Overlap 37 is empty\n",
      "[]\n",
      "\n",
      "Overlap 38 is fully connected\n",
      "[]\n",
      "\n",
      "Overlap 39 is fully connected\n",
      "[]\n",
      "\n",
      "Overlap 40 is fully connected\n",
      "[]\n",
      "\n",
      "Overlap 41 is fully connected\n",
      "[]\n",
      "\n",
      "Overlap 42 is fully connected\n",
      "[]\n",
      "\n",
      "Overlap 43 is fully connected\n",
      "[]\n",
      "\n",
      "Overlap 44 is empty\n",
      "[]\n",
      "\n",
      "Overlap 45 is empty\n",
      "[]\n",
      "\n",
      "[(2, 3), (2, 4), (2, 5), (2, 8), (3, 4), (3, 5), (3, 8), (4, 5), (4, 8), (5, 8)]\n",
      "\n",
      "[(2, 3), (2, 4), (2, 5), (2, 7), (2, 8), (2, 9), (2, 11), (3, 4), (3, 5), (3, 7), (3, 8), (3, 9), (3, 11), (4, 5), (4, 7), (4, 8), (4, 9), (4, 11), (5, 7), (5, 8), (5, 9), (5, 11)]\n",
      "\n",
      "[(2, 3), (2, 4), (2, 5), (2, 7), (2, 8), (2, 10), (2, 11), (3, 4), (3, 5), (3, 7), (3, 8), (3, 10), (3, 11), (4, 5), (4, 7), (4, 8), (4, 10), (4, 11), (5, 7), (5, 8), (5, 10), (5, 11), (7, 10), (8, 10), (10, 11)]\n",
      "\n",
      "Overlap 49 is empty\n",
      "[]\n",
      "\n",
      "Overlap 50 is empty\n",
      "[]\n",
      "\n",
      "Overlap 51 is empty\n",
      "[]\n",
      "\n",
      "Overlap 53 is empty\n",
      "[]\n",
      "\n",
      "Overlap 54 is empty\n",
      "[]\n",
      "\n",
      "Overlap 55 is fully connected\n",
      "[]\n",
      "\n",
      "Overlap 56 is empty\n",
      "[]\n",
      "\n",
      "[(2, 3), (2, 4), (2, 5), (2, 7), (2, 8), (2, 10), (3, 4), (3, 5), (3, 7), (3, 8), (3, 10), (4, 5), (4, 7), (4, 8), (4, 10), (5, 7), (5, 8), (5, 10)]\n",
      "\n",
      "Overlap 58 is empty\n",
      "[]\n",
      "\n",
      "[(2, 3), (2, 4), (2, 5), (2, 6), (2, 8), (3, 4), (3, 5), (3, 6), (3, 8), (4, 5), (4, 6), (4, 8), (5, 6), (5, 8)]\n",
      "\n",
      "Overlap 60 is empty\n",
      "[]\n",
      "\n",
      "Overlap 61 is fully connected\n",
      "[]\n",
      "\n",
      "Overlap 62 is empty\n",
      "[]\n",
      "\n",
      "[(2, 3), (2, 4), (2, 5), (2, 6), (3, 4), (3, 5), (3, 6), (4, 5), (4, 6), (5, 6)]\n",
      "\n",
      "Overlap 72 is empty\n",
      "[]\n",
      "\n",
      "[(2, 3), (2, 4), (2, 5), (2, 7), (2, 9), (3, 4), (3, 5), (3, 7), (3, 9), (4, 5), (4, 7), (4, 9), (5, 7), (5, 9)]\n",
      "\n",
      "Overlap 65 is empty\n",
      "[]\n",
      "\n",
      "Overlap 66 is empty\n",
      "[]\n",
      "\n",
      "Overlap 67 is empty\n",
      "[]\n",
      "\n",
      "Overlap 68 is empty\n",
      "[]\n",
      "\n",
      "Overlap 69 is fully connected\n",
      "[]\n",
      "\n",
      "[(2, 3), (2, 4), (2, 5), (2, 6), (2, 8), (2, 10), (2, 11), (3, 4), (3, 5), (3, 6), (3, 8), (3, 10), (3, 11), (4, 5), (4, 6), (4, 8), (4, 10), (4, 11), (5, 6), (5, 8), (5, 10), (5, 11)]\n",
      "\n",
      "Overlap 71 is empty\n",
      "[]\n",
      "\n"
     ]
    }
   ],
   "source": [
    "with ncg.session_scope() as session:\n",
    "    oids = session.query(Overlay.id).all()\n",
    "    for oid in oids:\n",
    "        oid = oid[0]\n",
    "        missing_edges = ncg.overlay_connection(oid)\n",
    "        print(missing_edges)\n",
    "        print()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "autocnet_local",
   "language": "python",
   "name": "autocnet_local"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
