From 3307cc8dbb6bead4845282c3ca6e16f98d6bc04b Mon Sep 17 00:00:00 2001 From: Jay <jlaura@asu.edu> Date: Wed, 8 Feb 2017 07:53:48 -0700 Subject: [PATCH] Rollsback the move from dataframe to graph cnet --- plio/io/io_controlnetwork.py | 38 ++++++++--------- plio/io/tests/test_io_controlnetwork.py | 54 +++++++++++++------------ 2 files changed, 46 insertions(+), 46 deletions(-) diff --git a/plio/io/io_controlnetwork.py b/plio/io/io_controlnetwork.py index 280b9ff..b120bd6 100644 --- a/plio/io/io_controlnetwork.py +++ b/plio/io/io_controlnetwork.py @@ -145,7 +145,7 @@ class IsisStore(object): self._path = path if not mode: - mode = 'wb' # pragma: no cover + mode = 'a' # pragma: no cover self._mode = mode self._handle = None @@ -170,13 +170,13 @@ class IsisStore(object): self._handle.seek(offset) self._handle.write(data) - def create_points(self, graphs, pointid_prefix, pointid_suffix): + def create_points(self, obj, pointid_prefix, pointid_suffix): """ Step through a control network (C) and return protocol buffer point objects Parameters ---------- - graphs : list + obj : list of iterable objects (dataframes) with the appropriate attributes: point_id, point_type, serial, measure_type, x, y required. The entries in the list must support grouping by the point_id attribute. @@ -197,21 +197,20 @@ class IsisStore(object): # TODO: Rewrite using apply syntax for performance point_sizes = [] point_messages = [] - for graph in graphs: - for i, control_point_id in enumerate(graph.point_nodes): - control_point = graph.node[control_point_id] + for df in obj: + for i, g in df.groupby('point_id'): point_spec = cnf.ControlPointFileEntryV0002() - point_spec.id = _set_pid(control_point['pointid']) + point_spec.id = _set_pid(self.pointid) + for attr, attrtype in self.point_attrs: - if attr in control_point.keys(): + if attr in g.columns: # As per protobuf docs for assigning to a repeated field. if attr == 'aprioriCovar': - arr = control_point['aprioriCovar'] + arr = g.iloc[0]['aprioriCovar'] point_spec.aprioriCovar.extend(arr.ravel().tolist()) else: - # The third argument casts the value to the correct type - setattr(point_spec, attr, attrtype(control_point[attr])) - point_spec.type = int(control_point.get('point_type', 2)) + setattr(point_spec, attr, attrtype(g.iloc[0][attr])) + point_spec.type = int(g.point_type.iat[0]) # The reference index should always be the image with the lowest index point_spec.referenceIndex = 0 @@ -219,19 +218,16 @@ class IsisStore(object): # A single extend call is cheaper than many add calls to pack points measure_iterable = [] - 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 + for node_id, m in g.iterrows(): measure_spec = point_spec.Measure() # For all of the attributes, set if they are an dict accessible attr of the obj. for attr, attrtype in self.measure_attrs: - if attr in control_measure.keys(): - setattr(measure_spec, attr, attrtype(control_measure[attr])) - measure_spec.type = int(control_measure['measure_type']) - measure_spec.line = float(control_measure['y']) - measure_spec.sample = float(control_measure['x']) + if attr in g.columns: + setattr(measure_spec, attr, attrtype(m[attr])) + measure_spec.type = int(m.measure_type) + measure_spec.line = m.y + measure_spec.sample = m.x measure_iterable.append(measure_spec) self.nmeasures += 1 point_spec.measures.extend(measure_iterable) diff --git a/plio/io/tests/test_io_controlnetwork.py b/plio/io/tests/test_io_controlnetwork.py index 6efe026..bf77fb4 100644 --- a/plio/io/tests/test_io_controlnetwork.py +++ b/plio/io/tests/test_io_controlnetwork.py @@ -3,7 +3,6 @@ import sys import unittest from time import strftime, gmtime -import networkx as nx import pandas as pd import pvl @@ -24,35 +23,40 @@ class TestWriteIsisControlNetwork(unittest.TestCase): cls.serials = ['APOLLO15/METRIC/{}'.format(i) for i in serial_times.values()] columns = ['point_id', 'point_type', 'serialnumber', 'measure_type', 'x', 'y', 'node_id'] - 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 = [] + data = [] for i in range(cls.npts): - i += 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) + data.append((i, 2, cls.serials[0], 2, 0, 0, 0)) + data.append((i, 2, cls.serials[1], 2, 0, 0, 1)) + dfs = [pd.DataFrame(data, columns=columns)] cls.creation_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', [G], mode='wb', targetname='Moon') + io_controlnetwork.to_isis('test.net', dfs, mode='wb', targetname='Moon') cls.header_message_size = 78 cls.point_start_byte = 65614 # 66949 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.point_start_byte = 66104 # 66949 + self.point_start_byte = 65614 # 66949 with open('test.net', 'rb') as f: f.seek(io_controlnetwork.HEADERSTARTBYTE) @@ -69,16 +73,17 @@ class TestWriteIsisControlNetwork(unittest.TestCase): self.assertEqual('None', header_protocol.description) self.assertEqual(self.modified_date, header_protocol.lastModified) #Repeating - self.assertEqual([137] * self.npts, header_protocol.pointMessageSizes) + self.assertEqual([135] * self.npts, header_protocol.pointMessageSizes) def test_create_point(self): + with open('test.net', 'rb') as f: f.seek(self.point_start_byte) - for i, length in enumerate(5*[137]): + for i, length in enumerate([135] * self.npts): point_protocol = cnf.ControlPointFileEntryV0002() raw_point = f.read(length) point_protocol.ParseFromString(raw_point) - self.assertEqual(str((i + 1) * 100), point_protocol.id) + self.assertEqual(str(i), point_protocol.id) self.assertEqual(2, point_protocol.type) for m in point_protocol.measures: self.assertTrue(m.serialnumber in self.serials) @@ -94,12 +99,11 @@ class TestWriteIsisControlNetwork(unittest.TestCase): self.assertEqual(10, mpoints) points_bytes = find_in_dict(pvl_header, 'PointsBytes') - self.assertEqual(685, points_bytes) + self.assertEqual(675, points_bytes) points_start_byte = find_in_dict(pvl_header, 'PointsStartByte') self.assertEqual(self.point_start_byte, points_start_byte) @classmethod def tearDownClass(cls): - #os.remove('test.net') - pass + os.remove('test.net') -- GitLab