Skip to content

Commit

Permalink
Set flag for stop times interpolation (#22)
Browse files Browse the repository at this point in the history
* pass through flag

* implement if flag

* udpate pre-comm

* udpate pre-comm

* update coverage
  • Loading branch information
kuanb committed Feb 14, 2018
1 parent 18b42e9 commit 1fb58dd
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 14 deletions.
6 changes: 4 additions & 2 deletions peartree/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,13 @@ def nameify_stop_id(name, sid):

def generate_summary_graph_elements(feed: ptg.gtfs.feed,
target_time_start: int,
target_time_end: int):
target_time_end: int,
interpolate_times: bool):
(all_edge_costs,
all_wait_times) = generate_edge_and_wait_values(feed,
target_time_start,
target_time_end)
target_time_end,
interpolate_times)

# Handle if there are no valid edges returned (or wait times)
if all_edge_costs is None or len(all_edge_costs) == 0:
Expand Down
6 changes: 4 additions & 2 deletions peartree/paths.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ def load_feed_as_graph(feed: ptg.gtfs.feed,
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,
interpolate_times: bool=True):
"""
Convert a feed object into a NetworkX Graph, connect to an existing
NetworkX graph if one is supplied
Expand Down Expand Up @@ -112,7 +113,8 @@ def load_feed_as_graph(feed: ptg.gtfs.feed,
(summary_edge_costs,
wait_times_by_stop) = generate_summary_graph_elements(feed,
start_time,
end_time)
end_time,
interpolate_times)

# This is a flag used to check if we need to run any additional steps
# after the feed is returned to ensure that new nodes and edge can connect
Expand Down
21 changes: 13 additions & 8 deletions peartree/summarizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,19 +294,24 @@ def linearly_interpolate_infill_times(stops_orig_df):
return cleaned


def generate_edge_and_wait_values(feed: ptg.gtfs.feed,
target_time_start: int,
target_time_end: int) -> Tuple[pd.DataFrame]:
def generate_edge_and_wait_values(
feed: ptg.gtfs.feed,
target_time_start: int,
target_time_end: int,
interpolate_times: bool) -> Tuple[pd.DataFrame]:
# Initialize the trips dataframe to be worked with
ftrips = feed.trips.copy()
ftrips = ftrips[~ftrips['route_id'].isnull()]
ftrips = ftrips.set_index('route_id', drop=False)

# Similarly, prepare the stops times dataframe by also
# infilling all stop times that are NaN with their linearly
# interpolated values based on their nearest numerically valid
# neighbors
stop_times = linearly_interpolate_infill_times(feed.stop_times)
# Flags whether we interpolate intermediary stops or not
if interpolate_times:
# Prepare the stops times dataframe by also infilling
# all stop times that are NaN with their linearly interpolated
# values based on their nearest numerically valid neighbors
stop_times = linearly_interpolate_infill_times(feed.stop_times)
else:
stop_times = feed.stop_times.copy()

all_edge_costs = None
all_wait_times = None
Expand Down
6 changes: 5 additions & 1 deletion tests/test_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,13 @@ def test_generate_summary_graph_elements():

start = 7 * 60 * 60
end = 10 * 60 * 60
interpolate_times = True

(summary_edge_costs,
wait_times_by_stop) = generate_summary_graph_elements(feed_1, start, end)
wait_times_by_stop) = generate_summary_graph_elements(feed_1,
start,
end,
interpolate_times)

# Ensure that the summary edge cost dataframe looks as it should
ec_cols = ['edge_cost', 'from_stop_id', 'to_stop_id']
Expand Down
4 changes: 3 additions & 1 deletion tests/test_graph_assembly.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ def test_feed_to_graph_performance():
# but open up to expose to benchmarking/performance profiling
start = 7 * 60 * 60
end = 10 * 60 * 60
interpolate_times = True

print('Running time profiles on each major '
'function in graph generation workflow')
Expand All @@ -34,7 +35,8 @@ def test_feed_to_graph_performance():
(all_edge_costs,
all_wait_times) = generate_edge_and_wait_values(feed,
start,
end)
end,
interpolate_times)
elapsed = round(time() - a, 2)
print('Perf of generate_edge_and_wait_values: {}s'.format(elapsed))

Expand Down

0 comments on commit 1fb58dd

Please sign in to comment.