Skip to content

Commit

Permalink
Parameterize close-node edge creation on new nodes (#30)
Browse files Browse the repository at this point in the history
* parameterize graph methods

* thread through param

* len check

* drop prints

* update tests

* udpate nodes df before it goes into cross feed checker

* nameify sid

* update tests

* drop commented out code
  • Loading branch information
kuanb committed Mar 6, 2018
1 parent eaaecc2 commit 762d110
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 12 deletions.
25 changes: 17 additions & 8 deletions peartree/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ def generate_summary_graph_elements(feed: ptg.gtfs.feed,


def generate_cross_feed_edges(G: nx.MultiDiGraph,
name: str,
stops_df: pd.DataFrame,
exempt_nodes: List[str],
connection_threshold: float) -> pd.DataFrame:
Expand All @@ -69,7 +70,8 @@ def generate_cross_feed_edges(G: nx.MultiDiGraph,
node_df = generate_graph_node_dataframe(G)

# Remove all nodes that are part of the new additions to the graph
node_df = node_df[~node_df.index.isin(exempt_nodes)]
if len(exempt_nodes) > 0:
node_df = node_df[~node_df.index.isin(exempt_nodes)]

stop_ids = []
to_nodes = []
Expand All @@ -79,6 +81,7 @@ def generate_cross_feed_edges(G: nx.MultiDiGraph,
# be a way to condense these two steps well
for i, row in stops_df.iterrows():
sid = str(row.stop_id)
full_sid = nameify_stop_id(name, sid)

# Ensure that each value is typed correctly prior to being
# fed into the nearest node method
Expand All @@ -88,7 +91,7 @@ def generate_cross_feed_edges(G: nx.MultiDiGraph,
nearest_nodes = get_nearest_nodes(node_df,
point,
connection_threshold,
exempt_id=sid)
exempt_id=full_sid)

# Iterate through series results and add to output
for node_id, dist_val in nearest_nodes.iteritems():
Expand Down Expand Up @@ -171,7 +174,8 @@ def populate_graph(G: nx.MultiDiGraph,
wait_times_by_stop: pd.DataFrame,
summary_edge_costs: pd.DataFrame,
connection_threshold: Union[int, float],
walk_speed_kmph: float=4.5):
walk_speed_kmph: float=4.5,
exempt_internal_edge_imputation: bool=False):
# 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
Expand All @@ -182,8 +186,10 @@ def populate_graph(G: nx.MultiDiGraph,
sid_lookup = _add_nodes_and_edges(G, name, stops_df, summary_edge_costs)

# Generate cross feed edge values
exempt_nodes = sid_lookup.values()
cross_feed_edges = generate_cross_feed_edges(G, stops_df,
exempt_nodes = []
if exempt_internal_edge_imputation:
exempt_nodes = sid_lookup.values()
cross_feed_edges = generate_cross_feed_edges(G, name, stops_df,
exempt_nodes,
connection_threshold)

Expand All @@ -198,7 +204,8 @@ def make_synthetic_system_network(
name: str,
reference_geojson: Dict,
connection_threshold: Union[int, float],
walk_speed_kmph: float=4.5):
walk_speed_kmph: float=4.5,
exempt_internal_edge_imputation: bool=False):
# Same as populate_graph, we use this dict to monitor the stop ids
# that are created
sid_lookup = {}
Expand Down Expand Up @@ -237,8 +244,10 @@ def make_synthetic_system_network(
all_nodes = all_nodes.append(nodes)

# Generate cross feed edge values
exempt_nodes = sid_lookup.values()
cross_feed_edges = generate_cross_feed_edges(G, all_nodes,
exempt_nodes = []
if exempt_internal_edge_imputation:
exempt_nodes = sid_lookup.values()
cross_feed_edges = generate_cross_feed_edges(G, name, all_nodes,
exempt_nodes,
connection_threshold)
# Mutates the G network object
Expand Down
12 changes: 8 additions & 4 deletions peartree/paths.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ def load_feed_as_graph(feed: ptg.gtfs.feed,
existing_graph: nx.MultiDiGraph=None,
connection_threshold: float=50.0,
walk_speed_kmph: float=4.5,
interpolate_times: bool=True):
interpolate_times: bool=True,
exempt_internal_edge_imputation: bool=True):
"""
Convert a feed object into a NetworkX Graph, connect to an existing
NetworkX graph if one is supplied
Expand Down Expand Up @@ -131,15 +132,17 @@ def load_feed_as_graph(feed: ptg.gtfs.feed,
wait_times_by_stop,
summary_edge_costs,
connection_threshold,
walk_speed_kmph)
walk_speed_kmph,
exempt_internal_edge_imputation)


def load_synthetic_network_as_graph(
reference_geojson: Dict,
name: str=None,
existing_graph: nx.MultiDiGraph=None,
connection_threshold: float=50.0,
walk_speed_kmph: float=4.5):
walk_speed_kmph: float=4.5,
exempt_internal_edge_imputation: bool=False):

# Generate a random name for name if it is None
if not name:
Expand All @@ -164,4 +167,5 @@ def load_synthetic_network_as_graph(
name,
reference_geojson,
connection_threshold,
walk_speed_kmph)
walk_speed_kmph,
exempt_internal_edge_imputation)

0 comments on commit 762d110

Please sign in to comment.