Commit 92887a5f authored by Jay's avatar Jay Committed by Jason R Laura
Browse files

ing imports in iPython notebooks.

parent 0fff803c
Loading
Loading
Loading
Loading
+1 −1
Changes for notebooks/ISIS Serial Numbers.ipynb: 1 added line, 1 removed line.
Original line number Diff line number Diff line
%% Cell type:code id: tags:

``` python
import os
import sys
print(os.path.abspath('../'))
sys.path.insert(0, os.path.abspath('../'))

from functools import reduce
import glob
import re

from autocnet.graph import network
from autocnet.fileio.sqlalchemy_json.alchemy import NestedJsonObject
from plio.sqlalchemy_json.alchemy import NestedJsonObject
from autocnet.utils.utils import find_in_dict

import sqlalchemy
from sqlalchemy.ext import declarative
from sqlalchemy import orm

import json
import warnings


import pvl
from pvl._collections import PVLModule
```

%% Output

    /Users/jlaura/github/autocnet

%% Cell type:markdown id: tags:

## Generate the data
The next three cells take a directory of cleaned `.trn` files (extra values like AUTO or OPTIONAL removed) and generate a `data.db` database of ISIS serial number translations.

%% Cell type:code id: tags:

``` python
# Database population and a declared class

Base = declarative.declarative_base()

class Translations(Base):
    __tablename__ = 'isis_translations'
    id = sqlalchemy.Column(sqlalchemy.INTEGER, primary_key=True)
    mission = sqlalchemy.Column(sqlalchemy.String)
    instrument = sqlalchemy.Column(sqlalchemy.String)
    translation = sqlalchemy.Column(NestedJsonObject)

    def __init__(self, mission, instrument, translation):
        self.mission = mission
        self.instrument = instrument
        self.translation = translation

class StringToMission(Base):
    __tablename__ = 'isis_mission_to_standard'
    id = sqlalchemy.Column(sqlalchemy.INTEGER, primary_key=True)
    key = sqlalchemy.Column(sqlalchemy.String)
    value = sqlalchemy.Column(sqlalchemy.String)

    def __init__(self, key, value):
        self.key = key
        self.value = value

engine = sqlalchemy.create_engine('sqlite:///data.db')
Base.metadata.bind = engine
Base.metadata.create_all()
session = orm.sessionmaker(bind=engine)()
```

%% Cell type:code id: tags:

``` python
files = glob.glob('../autocnet/examples/serial_number_translations/*.trn')
for f in files:
    p = pvl.load(f)
    name = os.path.basename(f[:-16])
    try:
        v = re.findall("([a-z, 0-9]*)([A-Z, a-z, 0-9]*)", name)[0]
        mission, instrument = v
    except:
        v = re.findall("[a-z, 0-9]*", name)
        mission = v[0]
        instrument = None
    r = Translations(mission, instrument, p)
    session.add(r)
session.commit()
```

%% Cell type:code id: tags:

``` python
# Build the mission names lookup table
v = """
Group = MissionName
  InputKey      = SpacecraftName
  InputGroup    = "IsisCube,Instrument"
  InputPosition = (IsisCube, Instrument)
  Translation   = (Aircraft, "Aircraft")
  Translation   = (Apollo15, "APOLLO 15")
  Translation   = (Apollo15, "APOLLO15")
  Translation   = (Apollo16, "APOLLO 16")
  Translation   = (Apollo16, "APOLLO16")
  Translation   = (Apollo17, "APOLLO 17")
  Translation   = (Apollo17, "APOLLO17")
  Translation   = (Cassini, Cassini-Huygens)
#  Translation   = (Chan1, "CHANDRAYAAN-1 ORBITER")
#  Translation   = (Chan1, CHANDRAYAAN1_ORBITER)
#  Translation   = (Chan1, CHANDRAYAAN-1)
  Translation   = (Chandrayaan1, "CHANDRAYAAN-1 ORBITER")
  Translation   = (Chandrayaan1, CHANDRAYAAN1_ORBITER)
  Translation   = (Chandrayaan1, CHANDRAYAAN-1)
  Translation   = (Clementine1, CLEMENTINE_1)
  Translation   = (Clementine1, "CLEMENTINE 1")
  Translation   = (Dawn, "DAWN")
  Translation   = (Galileo, "Galileo Orbiter")
  Translation   = (Hayabusa, HAYABUSA)
  Translation   = (Ideal, IdealSpacecraft)
  Translation   = (Kaguya, KAGUYA)
  Translation   = (Kaguya, SELENE-M)
  Translation   = (Lo, "Lunar Orbiter 3")
  Translation   = (Lo, "Lunar Orbiter 4")
  Translation   = (Lo, "Lunar Orbiter 5")
  Translation   = (Lro, "LUNAR RECONNAISSANCE ORBITER")
  Translation   = (Lro, "Lunar Reconnaissance Orbiter")
  Translation   = (Mariner10, Mariner_10)
  Translation   = (Mariner10, MARINER_10)
  Translation   = (Mer, "MARS EXPLORATION ROVER 1")
  Translation   = (Mer, MARS_EXPLORATION_ROVER_1)
  Translation   = (Mer, "MARS EXPLORATION ROVER 2")
  Translation   = (Mer, "SIMULATED MARS EXPLORATION ROVER 1")
  Translation   = (Mer, "SIMULATED MARS EXPLORATION ROVER 2")
  Translation   = (Messenger, MESSENGER)
  Translation   = (Messenger, Messenger)
  Translation   = (Mex, "MARS EXPRESS")
  Translation   = (Mex, "Mars Express")
    Translation   = (Mgs, MARSGLOBALSURVEYOR)
  Translation   = (Mgs, "MARS GLOBAL SURVEYOR")
  Translation   = (Mro, "MARS RECONNAISSANCE ORBITER")
  Translation   = (Mro, Mars_Reconnaissance_Orbiter)
  Translation   = (NewHorizons, "NEW HORIZONS")
  Translation   = (Near, NEAR)
  Translation   = (Near, "NEAR EARTH ASTEROID RENDEZVOUS")
  Translation   = (Odyssey, MARS_ODYSSEY)
  Translation   = (OsirisRex, OSIRIS-REX)
  Translation   = (Smart1, SMART1)
  Translation   = (Viking1, VIKING_ORBITER_1)
  Translation   = (Viking2, VIKING_ORBITER_2)
  Translation   = (Voyager1, VOYAGER_1)
  Translation   = (Voyager2, VOYAGER_2)
End_Group
End
"""
p = pvl.loads(v)

for k, v in p['MissionName'].items():
    if k == 'Translation':
        r = StringToMission(v[1], v[0])
        session.add(r)
session.commit()
```

%% Cell type:markdown id: tags:

## Sample Query

%% Cell type:code id: tags:

``` python
# Sample querying the database
for i, j, t in session.query(Translations.mission, Translations.instrument, Translations.translation):
    print(i,j)
    d = PVLModule(t)
    print(d)
    break
```

%% Cell type:markdown id: tags:

## Testing code to prototype the functionality now in autocnet

%% Cell type:code id: tags:

``` python
 class SerialNumberDecoder(pvl.decoder.PVLDecoder):
     """
     A PVL Decoder class to handle cube label parsing for the purpose of creating a valid ISIS
     serial number. Inherits from the PVLDecoder in planetarypy's pvl module.
     """
     def cast_unquoated_string(self, value):
         """
         Overrides the parent class's method so that any un-quoted string type value found in the
         parsed pvl will just return the original value. This is needed so that keyword values
         are not re-formatted from what is originally in the ISIS cube label.

         Note: This affects value types that are recognized as null, boolean, number, datetime,
         et at.
         """
         return value.decode('utf-8')


def get_isis_translation(label):
    """
    Compute the ISIS serial number for a given image using
    the input cube or the label extracted from the cube.

    Parameters
    ----------
    label : dict or str
            A PVL dict object or file name to extract
            the PVL object from

    Returns
    -------
    translation : dict
                  A PVLModule object containing the extracted
                  translation file
    """

    if not isinstance(label, PVLModule):
        label = pvl.load(label)

    cube_obj = find_in_dict(label, 'Instrument')

    # Grab the spacecraft name and run it through the ISIS lookup
    spacecraft_name = find_in_dict(cube_obj, 'SpacecraftName')
    for row in session.query(StringToMission).filter(StringToMission.key==spacecraft_name):
        spacecraft_name = row.value.lower()

    #Try and pull an instrument identifier
    try:
        instrumentid = find_in_dict(cube_obj, 'InstrumentId').capitalize()
    except:
        instrumentid = None

    # Grab the translation PVL object using the lookup
    for row in session.query(Translations).filter(Translations.mission==spacecraft_name,
                                                  Translations.instrument==instrumentid):
        # Convert the JSON back to a PVL object
        translation = PVLModule(row.translation)

    return translation

def extract_subgroup(data, key_list):
    return reduce(lambda d, k: d[k], key_list, data)

def generate_serial_number(label):

    if not isinstance(label, PVLModule):
        label = pvl.load(label, cls=SerialNumberDecoder)

    # Get the translation information
    translation = get_isis_translation(label)
    serial_number = []

    # Sort the keys to ensure proper iteration order
    keys = sorted(translation.keys())
    for k in keys:
        group = translation[k]
        search_key = group['InputKey']
        search_position = group['InputPosition']
        search_translation = {group['Translation'][1]:group['Translation'][0]}
        print(search_key, search_position, search_translation)

        sub_group = extract_subgroup(label, search_position)
        serial_entry = sub_group[search_key]
        if serial_entry in search_translation.keys():
            serial_entry = search_translation[serial_entry]
        elif '*' in search_translation.keys() and search_translation['*'] != '*':
            serial_entry = search_translation['*']

        serial_number.append(serial_entry)

    return '/'.join(serial_number)

```

%% Output

    SpacecraftName ['IsisCube', 'Instrument'] {'*': 'APOLLO15'}
    InstrumentId ['IsisCube', 'Instrument'] {'*': '*'}
    StartTime ['IsisCube', 'Instrument'] {'*': '*'}
    APOLLO15/METRIC/1971-07-31T01:24:36.970

%% Cell type:markdown id: tags:

Edit below code for the correct path to an Apollo cube and uncomment to test. Must remain commented out for a non-failing build server-side.

%% Cell type:code id: tags:

``` python
#serial = generate_serial_number('/Users/jlaura/Desktop/Apollo15/AS15-M-0296_sub4.cub')
#print(serial)
```
+2 −2
Changes for notebooks/Testing Slopes.ipynb: 2 added lines, 2 removed lines.
Original line number Diff line number Diff line
%% Cell type:markdown id: tags:

This notebook demonstrates a test of refining the fundamental matrix using the slopes between keypoints.

%% Cell type:code id: tags:

``` python
import os
import pandas as pd
import sys
sys.path.insert(0, os.path.abspath('..'))

import unittest

from autocnet.examples import get_path
from autocnet.fileio.io_controlnetwork import to_isis
from autocnet.fileio.io_controlnetwork import write_filelist
from plio.io.io_controlnetwork import to_isis
from plio.io.io_controlnetwork import write_filelist
from autocnet.graph.network import CandidateGraph
from autocnet.utils import utils

%pylab inline
```

%% Output

    Populating the interactive namespace from numpy and matplotlib

%% Cell type:code id: tags:

``` python
serial_numbers = {'AS15-M-0295_SML.png': '1971-07-31T01:24:11.754',
                'AS15-M-0296_SML.png': '1971-07-31T01:24:36.970',
                'AS15-M-0297_SML.png': '1971-07-31T01:25:02.243',
                'AS15-M-0298_SML.png': '1971-07-31T01:25:27.457',
                'AS15-M-0299_SML.png': '1971-07-31T01:25:52.669',
                'AS15-M-0300_SML.png': '1971-07-31T01:26:17.923'}

for k, v in serial_numbers.items():
    serial_numbers[k] = 'APOLLO15/METRIC/{}'.format(v)
```

%% Cell type:code id: tags:

``` python
adjacency = get_path('two_image_adjacency.json')
basepath = get_path('Apollo15')
cg = CandidateGraph.from_adjacency(adjacency, basepath=basepath)

cg.extract_features(method='sift', extractor_parameters={"nfeatures":500})
cg.match_features(k=2)
```

%% Cell type:code id: tags:

``` python
for source, destination, edge in cg.edges_iter(data=True):
    # Perform the symmetry check
    edge.symmetry_check()
    # Perform the ratio test
    edge.ratio_check(clean_keys=['symmetry'])
```

%% Output

    /Users/jlaura/anaconda3/envs/autocnet/lib/python3.5/site-packages/pandas/core/frame.py:1997: UserWarning: Boolean Series key will be reindexed to match DataFrame index.
      "DataFrame index.", UserWarning)

%% Cell type:code id: tags:

``` python
cg.apply_func_to_edges("compute_fundamental_matrix", clean_keys=['symmetry', 'ratio'])
```

%% Cell type:code id: tags:

``` python
f = cg.edge[0][1].fundamental_matrix
```

%% Cell type:code id: tags:

``` python
df=utils.calculate_slope(f.x1, f.x2)
len(np.asarray(df).ravel())
```

%% Output

    11

%% Cell type:code id: tags:

``` python
f.mask[f.mask==True].equals(f.mask[f.mask])
```

%% Output

    True

%% Cell type:code id: tags:

``` python
nbr_inliers = 20
fp = np.array(np.random.standard_normal((nbr_inliers, 2)))  # inliers
static_F = np.array([[4, 0.5, 10], [0.25, 1, 5], [0.2, 0.1, 1]])
fph = np.hstack((fp, np.ones((nbr_inliers, 1))))
tp = static_F.dot(fph.T)
# normalize hom. coordinates
tp /= tp[-1, :np.newaxis]
df1 = pd.DataFrame(fp, columns=['x', 'y'])
df2 = pd.DataFrame(tp.T[:, :2], columns=['x', 'y'])
```

%% Cell type:code id: tags:

``` python
df1
```

%% Output

               x         y
    0   1.633181  0.662958
    1  -0.652132  0.815878
    2  -0.438907  1.047533
    3  -0.339436  1.383948
    4  -0.958286  0.682298
    5   0.235437 -0.778398
    6  -0.456880 -1.248944
    7   0.235843 -0.454797
    8  -0.189979  2.118751
    9   0.944133  0.408296
    10 -0.337530 -1.455450
    11 -0.835177  0.469812
    12  0.764976  1.178681
    13  2.096332  0.580255
    14  0.780332 -1.046638
    15  0.325916  0.629410
    16 -0.780760  0.846021
    17  0.477673  0.395407
    18  1.575866  0.773803
    19 -0.036507  0.557871

%% Cell type:code id: tags:

``` python
df2
```

%% Output

                x         y
    0   12.106982  4.358614
    1    8.199881  5.943098
    2    8.621809  5.838713
    3    8.719442  5.884208
    4    7.424377  6.209099
    5   10.887362  4.416272
    6    9.630884  4.640421
    7   10.697907  4.596401
    8    8.773865  6.023836
    9   11.369585  4.590168
    10  10.066923  4.396940
    11   7.834798  5.978798
    12  10.740136  5.012282
    13  12.641683  4.132114
    14  11.982098  3.945629
    15  10.298838  5.062288
    16   7.862533  6.086306
    17  10.667482  4.858555
    18  11.985440  4.429107
    19   9.664326  5.292150

%% Cell type:code id: tags:

``` python
mask = df1.x > 0
mask
```

%% Output

    0      True
    1     False
    2     False
    3     False
    4     False
    5      True
    6     False
    7      True
    8     False
    9      True
    10    False
    11    False
    12     True
    13     True
    14     True
    15     True
    16    False
    17     True
    18     True
    19    False
    Name: x, dtype: bool

%% Cell type:code id: tags:

``` python
df1[mask]
```

%% Output

               x         y
    0   1.633181  0.662958
    5   0.235437 -0.778398
    7   0.235843 -0.454797
    9   0.944133  0.408296
    12  0.764976  1.178681
    13  2.096332  0.580255
    14  0.780332 -1.046638
    15  0.325916  0.629410
    17  0.477673  0.395407
    18  1.575866  0.773803

%% Cell type:code id: tags:

``` python
df2[mask]
```

%% Output

                x         y
    0   12.106982  4.358614
    5   10.887362  4.416272
    7   10.697907  4.596401
    9   11.369585  4.590168
    12  10.740136  5.012282
    13  12.641683  4.132114
    14  11.982098  3.945629
    15  10.298838  5.062288
    17  10.667482  4.858555
    18  11.985440  4.429107

%% Cell type:code id: tags:

``` python
df1[mask] = df2[mask]
```

%% Cell type:code id: tags:

``` python
df1
```

%% Output

                x         y
    0   12.106982  4.358614
    1   -0.652132  0.815878
    2   -0.438907  1.047533
    3   -0.339436  1.383948
    4   -0.958286  0.682298
    5   10.887362  4.416272
    6   -0.456880 -1.248944
    7   10.697907  4.596401
    8   -0.189979  2.118751
    9   11.369585  4.590168
    10  -0.337530 -1.455450
    11  -0.835177  0.469812
    12  10.740136  5.012282
    13  12.641683  4.132114
    14  11.982098  3.945629
    15  10.298838  5.062288
    16  -0.780760  0.846021
    17  10.667482  4.858555
    18  11.985440  4.429107
    19  -0.036507  0.557871
+1 −54
Changes for notebooks/Tutorial with Visualization.ipynb: 1 added line, 54 removed lines.
Original line number Diff line number Diff line
@@ -409,59 +409,6 @@
    "# Overlay, in red, the points that remain after suppression\n",
    "cg.edge[0][1].plot(clean_keys=['suppression'], line_kwargs={'linewidth':0}, scatter_kwargs={'color':'red'})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import sys\n",
    "import os\n",
    "sys.path.insert(0,'/Users/jlaura/github/plio/')\n",
    "from plio import io_controlnetwork as cn"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "cn.to_isis('test.net', cg, mode='wb', targetname='Moon')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "None\n"
     ]
    }
   ],
   "source": [
    "print(cnet)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
@@ -480,7 +427,7 @@
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.1"
   "version": "3.5.2"
  }
 },
 "nbformat": 4,