Skip to content

Commit

Permalink
more comprehensive test plus other cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisnatali committed Apr 29, 2016
1 parent 6f3a5ba commit a26a42d
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 21 deletions.
2 changes: 2 additions & 0 deletions sequencer/Models.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@ def _strip_cols(self):
def sequence(self):
super(EnergyMaximizeReturn, self).sequence()
self._strip_cols()
# return to be consistent with parent
return self.output_frame
4 changes: 2 additions & 2 deletions sequencer/NetworkPlan.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ class NetworkPlan(object):
TOL = .5 # meters at the equator, tolerance is stricter towards the poles

def __init__(self, network, metrics, **kwargs):
self.priority_metric = kwargs['prioritize'] if 'prioritize' in kwargs else 'population'
self.proj = kwargs['proj'] if 'proj' in kwargs else 'utm'
self.priority_metric = kwargs.get('prioritize', 'population')
self.proj = kwargs.get('proj', 'utm')

# FIXME:
# Remove the dependency that sequencer has on the
Expand Down
8 changes: 6 additions & 2 deletions sequencer/Sequencer.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,12 @@ def upstream_distance(self, node):
return 0.0

def sequence(self):
"""
Compute the sequence (aka rank) of nodes and edges
This modifies the NetworkPlan member (so make a deep copy if you
need the original)
"""
self.results = pd.DataFrame(self._sequence(), dtype=object).set_index('Sequence..Far.sighted.sequence')
# Post process for output
self._build_node_wkt()
Expand Down Expand Up @@ -235,8 +241,6 @@ def _build_edge_wkt(self):
# Iterate through the nodes and their parent
for rank, fnode, tnode in zip(r.index, r['Sequence..Upstream.id'], r['Sequence..Vertex.id']):
if fnode is not None:
if np.any(np.mod([fnode, tnode], 1) != 0):
raise Exception('Non-integral node index in results.')
# Set the edge attributes with those found in sequencing
self.networkplan.network.edge[fnode][tnode]['rank'] = int(rank)
self.networkplan.network.edge[fnode][tnode]['distance'] = float(self.networkplan._distance(fnode, tnode))
Expand Down
60 changes: 43 additions & 17 deletions sequencer/Tests/Test_Suite.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,17 @@ def gen_data_with_fakes():
This should be sufficient for tests requiring fake nodes
network looks like
network looks like (fake node starred, demand in parens)
o *
| / \
* * *
/ \
* *
6*
|
|
0(100) 3(12)
/ \ / \
/ \ / \
1(50) 2(25) 4(6) 5(3)
where o is a fake node, * is not
Also returns edge_rank: dict of edge -> rank
"""

# create disjoint graph with 2 trees, one rooted by a fake node
Expand All @@ -73,35 +75,59 @@ def gen_data_with_fakes():
# now add fake root to tree at 3
network.add_edge(6, 3)

# set coordinates
# set coordinates (roughly match diagram above)
base_coord = np.array([10, 10])
coord_dict = {i: base_coord + [i*-1, i*-1] for i in range(6)}
nx.set_node_attributes(network, 'coords', coord_dict)
# and set fake node coordinates
nx.set_node_attributes(network, 'coords', {6: np.array([10, 11])})
fake_coord = np.array([20, 9])
coord_dict = {0: base_coord,
1: base_coord + [-1, 1],
2: base_coord + [1, 1],
3: fake_coord + [0, 1],
4: fake_coord + [-1, 2],
5: fake_coord + [1, 2],
6: fake_coord}

nx.set_node_attributes(network, 'coords', coord_dict)
# now set the metrics dataframe without the fake node
metrics_data = {'Demand...Projected.nodal.demand.per.year':
[100, 50, 25, 12, 6, 3],
'Population': [100, 50, 25, 12, 6, 3]}

metrics = DataFrame(metrics_data)
# Note, we skip fake node here
metrics['X'] = [ coord_dict[i][0] for i in range(6) ]
metrics['Y'] = [ coord_dict[i][1] for i in range(6) ]

return metrics, network

# assign expected ranks to nodes, edges (the sequence)
# note:
# - ranks are 1-based and originally assigned to nodes
# - edges are assigned rank based on the "to" node
# - fake nodes are skipped when assigning rank
# (See Sequencer.sequencer._sequence for details)
node_rank = {0: 1, 1: 2, 2: 3, 3: 4, 4: 5, 5: 6}
edge_rank = {(0, 1): 2, (0, 2): 3, (6, 3): 4, (3, 4): 5, (3, 5): 6}
return metrics, network, node_rank, edge_rank


def test_sequencer_with_fakes():
"""
Make sure we work with fake nodes
"""

# for now, just make sure it runs without exceptions
metrics, network = gen_data_with_fakes()
metrics, network, node_rank, edge_rank = gen_data_with_fakes()
nwp = NetworkPlan(network, metrics, prioritize='Population', proj='wgs4')
model = EnergyMaximizeReturn(nwp)
model.sequence()
#todo: check the result
results = model.sequence()

node_ids = results['Sequence..Vertex.id']
sequence_ids = results['Sequence..Far.sighted.sequence']
actual_node_rank = dict(zip(node_ids, sequence_ids))
actual_edge_rank = {k: v['rank'] for k, v in
model.networkplan.network.edge.iteritems()}
assert node_rank == actual_node_rank,\
"Node sequencing is not what was expected"
assert edge_rank == actual_edge_rank,\
"Edge sequencing is not what was expected"


class TestNetworkPlan(NetworkPlan):
Expand Down

0 comments on commit a26a42d

Please sign in to comment.