Skip to content

Commit

Permalink
better fix for #57, fake node test, refactored NetworkPlan init
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisnatali committed Apr 28, 2016
1 parent e305422 commit 9209ba7
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 22 deletions.
28 changes: 14 additions & 14 deletions sequencer/NetworkPlan.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
import fiona
import numpy as np
import networkx as nx
from scipy.sparse import csr_matrix
import scipy.sparse.csgraph as graph
import pandas as pd
import logging
import copy
Expand All @@ -26,13 +24,18 @@ 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'

# do the real init
# FIXME:
# Remove the dependency that sequencer has on the
# original metrics file (this is terrible coupling)
# see sequencer:_clean_results()
self._original_metrics = metrics

self._init_helper(network, metrics)


def _init_helper(self, network, metrics):
"""
re-usable init bypassing read shp,csv
All initialization (cleaning up metrics, network, etc)
"""

# Load in and align input data
Expand All @@ -41,7 +44,7 @@ def _init_helper(self, network, metrics):
metrics,
loc_tol = self.TOL)

self.coord_values = np.array(self.coords.values())
self.coord_values = self.coords.values()

# Set the edge weight to the distance between those nodes
self._weight_edges()
Expand Down Expand Up @@ -83,14 +86,7 @@ def from_files(cls, shp, csv, **kwargs):
# Pass along the projection
kwargs['proj'] = shapefile.crs['proj']

nwp = cls(nx.read_shp(shp), pd.read_csv(csv, header=1), **kwargs)

# FIXME:
# This is a really bad way to do this...the sequencer has a dependency
# on the csv_p attribute (i.e. the original csv) so leave it for now
nwp.csv_p = csv
return nwp

return cls(nx.read_shp(shp), pd.read_csv(csv, header=1), **kwargs)

@classmethod
def _assert_proj_match(self, shp, csv):
Expand Down Expand Up @@ -283,12 +279,16 @@ def network(self):
"""returns the DiGraph Object representation of the graph"""
return self._network

@property
def original_metrics(self):
"""returns the original (unprocessed) metrics data_frame"""
return self._original_metrics

@property
def metrics(self):
"""returns the nodal metrics Pandas DataFrame"""
return self._metrics


def download_scenario(scenario_number, directory_name=None, username=None, password=None,
np_url='http://networkplanner.modilabs.org/'):

Expand Down
12 changes: 4 additions & 8 deletions sequencer/Sequencer.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,7 @@ def upstream_distance(self, node):
return 0.0

def sequence(self):
self.results = pd.DataFrame(self._sequence()).set_index('Sequence..Far.sighted.sequence')

self.results = pd.DataFrame(self._sequence(), dtype=object).set_index('Sequence..Far.sighted.sequence')
# Post process for output
self._build_node_wkt()
self._build_edge_wkt()
Expand Down Expand Up @@ -235,7 +234,7 @@ def _build_edge_wkt(self):
r = self.results
# Iterate through the nodes and their parent
for rank, fnode, tnode in zip(r.index, r['Sequence..Upstream.id'], r['Sequence..Vertex.id']):
if not np.isnan(fnode):
if fnode:
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
Expand Down Expand Up @@ -291,11 +290,8 @@ def _clean_results(self):
"""This joins the sequenced results on the metrics dataframe and reappends the dropped rows"""

logger.info('Joining Sequencer Results on Input Metrics')
if not hasattr(self.networkplan, 'csv_p'):
logger.info('no original csv file found, skipping')
return

orig = pd.read_csv(self.networkplan.csv_p, header=1)
# FIXME: Remove this dependency on original_metrics
orig = self.networkplan.original_metrics
orig.columns = parse_cols(orig)
self.networkplan.metrics.index.name = 'Sequence..Vertex.id'
sequenced_metrics = pd.merge(self.networkplan.metrics.reset_index(), self.results.reset_index(), on='Sequence..Vertex.id')
Expand Down

0 comments on commit 9209ba7

Please sign in to comment.