From 762d110946292df24d05c3531e52e9626964b992 Mon Sep 17 00:00:00 2001 From: Kuan Butts Date: Tue, 6 Mar 2018 00:31:22 -0800 Subject: [PATCH] Parameterize close-node edge creation on new nodes (#30) * 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 --- peartree/graph.py | 25 +++++++++++++++++-------- peartree/paths.py | 12 ++++++++---- 2 files changed, 25 insertions(+), 12 deletions(-) diff --git a/peartree/graph.py b/peartree/graph.py index 5a43268..d34cddd 100644 --- a/peartree/graph.py +++ b/peartree/graph.py @@ -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: @@ -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 = [] @@ -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 @@ -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(): @@ -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 @@ -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) @@ -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 = {} @@ -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 diff --git a/peartree/paths.py b/peartree/paths.py index 5e1723c..a2be8f2 100644 --- a/peartree/paths.py +++ b/peartree/paths.py @@ -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 @@ -131,7 +132,8 @@ 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( @@ -139,7 +141,8 @@ def load_synthetic_network_as_graph( 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: @@ -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)