Skip to content
Snippets Groups Projects
Commit 2cce57fe authored by jlaura's avatar jlaura Committed by GitHub
Browse files

Merge pull request #6 from jlaura/master

All tests passing locally
parents eb46fd9e affbc873
No related branches found
No related tags found
No related merge requests found
...@@ -20,7 +20,7 @@ ...@@ -20,7 +20,7 @@
"info_file_handler": { "info_file_handler": {
"formatter": "simple", "formatter": "simple",
"backupCount": 10, "backupCount": 10,
"level": "INFO", "level": "DEBUG",
"encoding": "utf8", "encoding": "utf8",
"class": "logging.handlers.RotatingFileHandler", "class": "logging.handlers.RotatingFileHandler",
"maxBytes": 10485760, "maxBytes": 10485760,
......
...@@ -145,7 +145,7 @@ class IsisStore(object): ...@@ -145,7 +145,7 @@ class IsisStore(object):
self._path = path self._path = path
if not mode: if not mode:
mode = 'a' # pragma: no cover mode = 'wb' # pragma: no cover
self._mode = mode self._mode = mode
self._handle = None self._handle = None
...@@ -170,13 +170,13 @@ class IsisStore(object): ...@@ -170,13 +170,13 @@ class IsisStore(object):
self._handle.seek(offset) self._handle.seek(offset)
self._handle.write(data) self._handle.write(data)
def create_points(self, obj, pointid_prefix, pointid_suffix): def create_points(self, graphs, pointid_prefix, pointid_suffix):
""" """
Step through a control network (C) and return protocol buffer point objects Step through a control network (C) and return protocol buffer point objects
Parameters Parameters
---------- ----------
obj : list graphs : list
of iterable objects (dataframes) with the appropriate of iterable objects (dataframes) with the appropriate
attributes: point_id, point_type, serial, measure_type, x, y required. attributes: point_id, point_type, serial, measure_type, x, y required.
The entries in the list must support grouping by the point_id attribute. The entries in the list must support grouping by the point_id attribute.
...@@ -197,20 +197,21 @@ class IsisStore(object): ...@@ -197,20 +197,21 @@ class IsisStore(object):
# TODO: Rewrite using apply syntax for performance # TODO: Rewrite using apply syntax for performance
point_sizes = [] point_sizes = []
point_messages = [] point_messages = []
for df in obj: for graph in graphs:
for i, g in df.groupby('point_id'): for i, control_point_id in enumerate(graph.point_nodes):
control_point = graph.node[control_point_id]
point_spec = cnf.ControlPointFileEntryV0002() point_spec = cnf.ControlPointFileEntryV0002()
point_spec.id = _set_pid(self.pointid) point_spec.id = _set_pid(control_point['pointid'])
for attr, attrtype in self.point_attrs: for attr, attrtype in self.point_attrs:
if attr in g.columns: if attr in control_point.keys():
# As per protobuf docs for assigning to a repeated field. # As per protobuf docs for assigning to a repeated field.
if attr == 'aprioriCovar': if attr == 'aprioriCovar':
arr = g.iloc[0]['aprioriCovar'] arr = control_point['aprioriCovar']
point_spec.aprioriCovar.extend(arr.ravel().tolist()) point_spec.aprioriCovar.extend(arr.ravel().tolist())
else: else:
setattr(point_spec, attr, attrtype(g.iloc[0][attr])) # The third argument casts the value to the correct type
point_spec.type = int(g.point_type.iat[0]) setattr(point_spec, attr, attrtype(control_point[attr]))
point_spec.type = int(control_point.get('point_type', 2))
# The reference index should always be the image with the lowest index # The reference index should always be the image with the lowest index
point_spec.referenceIndex = 0 point_spec.referenceIndex = 0
...@@ -218,16 +219,19 @@ class IsisStore(object): ...@@ -218,16 +219,19 @@ class IsisStore(object):
# A single extend call is cheaper than many add calls to pack points # A single extend call is cheaper than many add calls to pack points
measure_iterable = [] measure_iterable = []
for node_id, m in g.iterrows(): for j, control_measure_id in graph.edges(control_point_id):
control_measure = graph.node[control_measure_id]
if control_measure['node_type'] != 'correspondence':
continue
measure_spec = point_spec.Measure() measure_spec = point_spec.Measure()
# For all of the attributes, set if they are an dict accessible attr of the obj. # For all of the attributes, set if they are an dict accessible attr of the obj.
for attr, attrtype in self.measure_attrs: for attr, attrtype in self.measure_attrs:
if attr in control_measure.keys():
setattr(measure_spec, attr, attrtype(control_measure[attr]))
if attr in g.columns: measure_spec.type = int(control_measure['measure_type'])
setattr(measure_spec, attr, attrtype(m[attr])) measure_spec.line = float(control_measure['y'])
measure_spec.type = int(m.measure_type) measure_spec.sample = float(control_measure['x'])
measure_spec.line = m.y
measure_spec.sample = m.x
measure_iterable.append(measure_spec) measure_iterable.append(measure_spec)
self.nmeasures += 1 self.nmeasures += 1
point_spec.measures.extend(measure_iterable) point_spec.measures.extend(measure_iterable)
......
...@@ -198,6 +198,12 @@ class GeoDataset(object): ...@@ -198,6 +198,12 @@ class GeoDataset(object):
self._gcs = self._srs.CloneGeogCS() self._gcs = self._srs.CloneGeogCS()
return self._srs return self._srs
@property
def nbands(self):
if not getattr(self, '_nbands', None):
self._nbands = self.dataset.RasterCount
return self._nbands
@property @property
def geospatial_coordinate_system(self): def geospatial_coordinate_system(self):
if not getattr(self, '_gcs', None): if not getattr(self, '_gcs', None):
...@@ -321,10 +327,8 @@ class GeoDataset(object): ...@@ -321,10 +327,8 @@ class GeoDataset(object):
@property @property
def coordinate_transformation(self): def coordinate_transformation(self):
if not getattr(self, '_ct', None): if not getattr(self, '_ct', None):
print('Getting CT')
self._ct = osr.CoordinateTransformation(self.spatial_reference, self._ct = osr.CoordinateTransformation(self.spatial_reference,
self.geospatial_coordinate_system) self.geospatial_coordinate_system)
print('CT', self._ct)
return self._ct return self._ct
@property @property
...@@ -332,7 +336,6 @@ class GeoDataset(object): ...@@ -332,7 +336,6 @@ class GeoDataset(object):
if not getattr(self, '_ict', None): if not getattr(self, '_ict', None):
self._ict = osr.CoordinateTransformation(self.geospatial_coordinate_system, self._ict = osr.CoordinateTransformation(self.geospatial_coordinate_system,
self.spatial_reference) self.spatial_reference)
print(self._ict)
return self._ict return self._ict
@property @property
...@@ -558,6 +561,7 @@ def match_rasters(match_to, match_from, destination, ...@@ -558,6 +561,7 @@ def match_rasters(match_to, match_from, destination,
GRA_Cubic, GRA_CubicSpline, GRA_Lanczos, GRA_Average, GRA_Cubic, GRA_CubicSpline, GRA_Lanczos, GRA_Average,
GRA_Mode} GRA_Mode}
""" """
import gdalconst # import here so Sphinx can build the docos, mocking is not working import gdalconst # import here so Sphinx can build the docos, mocking is not working
# TODO: If a destination is not provided create an in-memory GeoDataSet object # TODO: If a destination is not provided create an in-memory GeoDataSet object
match_to_srs = match_to.dataset.GetProjection() match_to_srs = match_to.dataset.GetProjection()
...@@ -567,8 +571,8 @@ def match_rasters(match_to, match_from, destination, ...@@ -567,8 +571,8 @@ def match_rasters(match_to, match_from, destination,
match_from__srs = match_from.dataset.GetProjection() match_from__srs = match_from.dataset.GetProjection()
match_from__gt = match_from.geotransform match_from__gt = match_from.geotransform
dst = gdal.GetDriverByName('GTiff').Create(destination, width, height, match_from.RasterCount, dst = gdal.GetDriverByName('GTiff').Create(destination, width, height, 1,
gdalconst.GDT_Float32) gdalconst.GDT_Float64)
dst.SetGeoTransform(match_to_gt) dst.SetGeoTransform(match_to_gt)
dst.SetProjection(match_to_srs) dst.SetProjection(match_to_srs)
......
...@@ -364,9 +364,9 @@ class ReadBin52(object): ...@@ -364,9 +364,9 @@ class ReadBin52(object):
self.nseasons) self.nseasons)
caseidx = np.repeat(np.arange(self.ncases), self.nseasons) caseidx = np.repeat(np.arange(self.ncases), self.nseasons)
seasonidx = np.repeat(np.arange(self.nseasons), self.ncases) seasonidx = np.repeat(np.arange(self.nseasons), self.ncases)
flt_seasitems = seasitems.reshape(len(self.vlabels), flt_seasitems = seasitems.reshape(len(columns),
self.ncases * self.nseasons) self.ncases * self.nseasons)
self.seasons = pd.DataFrame(flt_seasitems, self.seasons = pd.DataFrame(flt_seasitems.T,
index=[caseidx,seasonidx], index=[caseidx,seasonidx],
columns=columns) columns=columns)
self.seasons.index.names = ['Case', 'Season'] self.seasons.index.names = ['Case', 'Season']
...@@ -432,6 +432,9 @@ class ReadBin52(object): ...@@ -432,6 +432,9 @@ class ReadBin52(object):
#Extract the hourly temperature data #Extract the hourly temperature data
hourly2dataframe() hourly2dataframe()
# Extract the seasons
season2dataframe()
# Extract by layer data from the data cube # Extract by layer data from the data cube
layeritems = self.bin52data[: , self.ndx: , : , 5: 7, : ] layeritems = self.bin52data[: , self.ndx: , : , 5: 7, : ]
latitems2dataframe() latitems2dataframe()
......
...@@ -3,6 +3,7 @@ import sys ...@@ -3,6 +3,7 @@ import sys
import unittest import unittest
from time import strftime, gmtime from time import strftime, gmtime
import networkx as nx
import pandas as pd import pandas as pd
import pvl import pvl
...@@ -23,38 +24,33 @@ class TestWriteIsisControlNetwork(unittest.TestCase): ...@@ -23,38 +24,33 @@ class TestWriteIsisControlNetwork(unittest.TestCase):
cls.serials = ['APOLLO15/METRIC/{}'.format(i) for i in serial_times.values()] cls.serials = ['APOLLO15/METRIC/{}'.format(i) for i in serial_times.values()]
columns = ['point_id', 'point_type', 'serialnumber', 'measure_type', 'x', 'y', 'node_id'] columns = ['point_id', 'point_type', 'serialnumber', 'measure_type', 'x', 'y', 'node_id']
data = [] G = nx.Graph()
G.add_node(0, node_type='image')
G.add_node(1, node_type='image')
G.add_edge(0,1)
G.point_nodes = []
for i in range(cls.npts): for i in range(cls.npts):
data.append((i, 2, cls.serials[0], 2, 0, 0, 0)) i += 1
data.append((i, 2, cls.serials[1], 2, 0, 0, 1)) G.point_nodes.append(i*100)
G.add_node(i * 100, node_type='point', pointid = i * 100, subpixel=False)
G.add_edge(i*100, 0)
G.add_edge(i*100, 1)
# Add a single correspondence
G.add_node(i * 1000, x=i*.5, y=i*.5, id=i*1000, node_type='correspondence', measure_type=2, serialnumber=cls.serials[0])
G.add_edge(i*1000, i*100)
# Add a second correspondence
G.add_node(i * 2000, x=i*.5, y=i*.5, id=i*2000, node_type='correspondence', measure_type=2, serialnumber=cls.serials[1])
G.add_edge(i * 2000, i*100)
dfs = [pd.DataFrame(data, columns=columns)]
cls.creation_date = strftime("%Y-%m-%d %H:%M:%S", gmtime()) cls.creation_date = strftime("%Y-%m-%d %H:%M:%S", gmtime())
cls.modified_date = strftime("%Y-%m-%d %H:%M:%S", gmtime()) cls.modified_date = strftime("%Y-%m-%d %H:%M:%S", gmtime())
io_controlnetwork.to_isis('test.net', dfs, mode='wb', targetname='Moon') io_controlnetwork.to_isis('test.net', [G], mode='wb', targetname='Moon')
cls.header_message_size = 78 cls.header_message_size = 78
cls.point_start_byte = 65609 # 66949 cls.point_start_byte = 65614 # 66949
def test_create_buffer_header(self): def test_create_buffer_header(self):
self.npts = 5
serial_times = {295: '1971-07-31T01:24:11.754',
296: '1971-07-31T01:24:36.970'}
self.serials = ['APOLLO15/METRIC/{}'.format(i) for i in serial_times.values()]
columns = ['point_id', 'point_type', 'serialnumber', 'measure_type', 'x', 'y', 'node_id']
data = []
for i in range(self.npts):
data.append((i, 2, self.serials[0], 2, 0, 0, 0))
data.append((i, 2, self.serials[1], 2, 0, 0, 1))
dfs = [pd.DataFrame(data, columns=columns)]
self.creation_date = strftime("%Y-%m-%d %H:%M:%S", gmtime())
self.modified_date = strftime("%Y-%m-%d %H:%M:%S", gmtime())
io_controlnetwork.to_isis('test.net', dfs, mode='wb', targetname='Moon')
self.header_message_size = 78 self.header_message_size = 78
self.point_start_byte = 66104 # 66949 self.point_start_byte = 66104 # 66949
...@@ -73,17 +69,16 @@ class TestWriteIsisControlNetwork(unittest.TestCase): ...@@ -73,17 +69,16 @@ class TestWriteIsisControlNetwork(unittest.TestCase):
self.assertEqual('None', header_protocol.description) self.assertEqual('None', header_protocol.description)
self.assertEqual(self.modified_date, header_protocol.lastModified) self.assertEqual(self.modified_date, header_protocol.lastModified)
#Repeating #Repeating
self.assertEqual([99] * self.npts, header_protocol.pointMessageSizes) self.assertEqual([137] * self.npts, header_protocol.pointMessageSizes)
def test_create_point(self): def test_create_point(self):
with open('test.net', 'rb') as f: with open('test.net', 'rb') as f:
f.seek(self.point_start_byte) f.seek(self.point_start_byte)
for i, length in enumerate([99] * self.npts): for i, length in enumerate(5*[137]):
point_protocol = cnf.ControlPointFileEntryV0002() point_protocol = cnf.ControlPointFileEntryV0002()
raw_point = f.read(length) raw_point = f.read(length)
point_protocol.ParseFromString(raw_point) point_protocol.ParseFromString(raw_point)
self.assertEqual(str(i), point_protocol.id) self.assertEqual(str((i + 1) * 100), point_protocol.id)
self.assertEqual(2, point_protocol.type) self.assertEqual(2, point_protocol.type)
for m in point_protocol.measures: for m in point_protocol.measures:
self.assertTrue(m.serialnumber in self.serials) self.assertTrue(m.serialnumber in self.serials)
...@@ -99,11 +94,12 @@ class TestWriteIsisControlNetwork(unittest.TestCase): ...@@ -99,11 +94,12 @@ class TestWriteIsisControlNetwork(unittest.TestCase):
self.assertEqual(10, mpoints) self.assertEqual(10, mpoints)
points_bytes = find_in_dict(pvl_header, 'PointsBytes') points_bytes = find_in_dict(pvl_header, 'PointsBytes')
self.assertEqual(495, points_bytes) self.assertEqual(685, points_bytes)
points_start_byte = find_in_dict(pvl_header, 'PointsStartByte') points_start_byte = find_in_dict(pvl_header, 'PointsStartByte')
self.assertEqual(self.point_start_byte, points_start_byte) self.assertEqual(self.point_start_byte, points_start_byte)
@classmethod @classmethod
def tearDownClass(cls): def tearDownClass(cls):
os.remove('test.net') #os.remove('test.net')
pass
...@@ -42,11 +42,10 @@ def setup_package(): ...@@ -42,11 +42,10 @@ def setup_package():
'pvl', 'pvl',
'protobuf==3.0.0b2', 'protobuf==3.0.0b2',
'h5py', 'h5py',
'icu',
'pandas', 'pandas',
'sqlalchemy', 'sqlalchemy',
'pyyaml', 'pyyaml',
'certifi'], 'networkx'],
classifiers=[ classifiers=[
"Development Status :: 3 - Alpha", "Development Status :: 3 - Alpha",
"Topic :: Utilities", "Topic :: Utilities",
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment