Skip to content

Commit

Permalink
Node aggregation and naming fix (#32)
Browse files Browse the repository at this point in the history
* string stop id name update

* use running tally instead

* save the sid lookup

* save orig short stop id

* dont need the renaming of the sid col in the first place?
  • Loading branch information
kuanb committed Mar 6, 2018
1 parent 5ab0b89 commit eaaecc2
Showing 1 changed file with 22 additions and 11 deletions.
33 changes: 22 additions & 11 deletions peartree/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,13 @@ def _add_cross_feed_edges(G: nx.MultiDiGraph,

def _add_nodes_and_edges(G: nx.MultiDiGraph,
name: str,
sid_lookup: Dict[str, str],
stops_df: pd.DataFrame,
summary_edge_costs: pd.DataFrame) -> nx.MultiDiGraph:
summary_edge_costs: pd.DataFrame) -> Dict[str, str]:
# As we convert stop ids to actual nodes, let's keep track of those names
# here so that we can reference them when we add connector edges across
# the various feeds loaded into the graph
sid_lookup = {}

for i, row in stops_df.iterrows():
sid = str(row.stop_id)
full_sid = nameify_stop_id(name, sid)
Expand All @@ -158,6 +162,8 @@ def _add_nodes_and_edges(G: nx.MultiDiGraph,
sid_to,
length=row.edge_cost)

return sid_lookup


def populate_graph(G: nx.MultiDiGraph,
name: str,
Expand All @@ -166,19 +172,14 @@ def populate_graph(G: nx.MultiDiGraph,
summary_edge_costs: pd.DataFrame,
connection_threshold: Union[int, float],
walk_speed_kmph: float=4.5):
# As we convert stop ids to actual nodes, let's keep track of those names
# here so that we can reference them when we add connector edges across
# the various feeds loaded into the graph
sid_lookup = {}

# Generate a merge of the wait time data and the feed stops data that will
# be used for both the addition of new stop nodes and the addition of
# cross feed edges later on (that join one feeds stops to the other if
# they are within the connection threshold).
stops_df = _merge_stop_waits_and_attributes(wait_times_by_stop, feed.stops)

# Mutates the G network object
_add_nodes_and_edges(G, name, sid_lookup, stops_df, summary_edge_costs)
sid_lookup = _add_nodes_and_edges(G, name, stops_df, summary_edge_costs)

# Generate cross feed edge values
exempt_nodes = sid_lookup.values()
Expand All @@ -201,6 +202,7 @@ def make_synthetic_system_network(
# Same as populate_graph, we use this dict to monitor the stop ids
# that are created
sid_lookup = {}
all_nodes = None
for feat in reference_geojson['features']:
ref_shape = shape(feat['geometry'])

Expand All @@ -222,14 +224,23 @@ def make_synthetic_system_network(
edges = generate_edges_df(stop_ids, all_pts, chunks, avg_speed)

# Mutates the G network object
_add_nodes_and_edges(G, name, sid_lookup, nodes, edges)
sid_lookup_sub = _add_nodes_and_edges(G, name, nodes, edges)

# Update the parent sid with new values
for key, val in sid_lookup_sub.items():
sid_lookup[key] = val

# Then add to the running tally of nodes
if all_nodes is None:
all_nodes = nodes.copy()
else:
all_nodes = all_nodes.append(nodes)

# Generate cross feed edge values
exempt_nodes = sid_lookup.values()
cross_feed_edges = generate_cross_feed_edges(G, nodes,
cross_feed_edges = generate_cross_feed_edges(G, all_nodes,
exempt_nodes,
connection_threshold)

# Mutates the G network object
_add_cross_feed_edges(G, sid_lookup, cross_feed_edges, walk_speed_kmph)

Expand Down

0 comments on commit eaaecc2

Please sign in to comment.