Skip to content

Commit

Permalink
Fix trip splitting when a block extends over 3 days or more. (#22)
Browse files Browse the repository at this point in the history
* halp

* Possible bug fix

* Yoink silly prints

* Push updated snapshots maybeee

* Add simple case for multi day blocks (#23)

* Add simple case

* Update snapshots

* Back

---------

Co-authored-by: Nicholas Paun <[email protected]>

---------

Co-authored-by: Jeremy Steele <[email protected]>
Co-authored-by: Jeremy Steele <[email protected]>
  • Loading branch information
3 people authored May 13, 2024
1 parent 0ea1d23 commit ff35e0a
Show file tree
Hide file tree
Showing 15 changed files with 169 additions and 4 deletions.
3 changes: 1 addition & 2 deletions blocks_to_transfers/service_days.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,6 @@ def get_or_assign(self, trip, days):

# Restore original representation of trip's days if it started after midnight
days = days.shift(-trip.shift_days)

service_id = self.service_by_days.get(days)
if service_id:
return service_id
Expand All @@ -155,7 +154,7 @@ def get_or_assign(self, trip, days):
exception_type=ExceptionType.ADD)
for date in self.to_dates(days)
]

return service_id

def to_dates(self, day_set):
Expand Down
6 changes: 4 additions & 2 deletions blocks_to_transfers/simplify_linear.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ def find_paths(graph):

def add_path_to_graph(t_graph, last_transition, days):
composite_nodes = {}
parent_days = days
current_transition = last_transition
split_node = get_path_node(t_graph, composite_nodes, current_transition,
days)
Expand All @@ -161,12 +162,13 @@ def add_path_to_graph(t_graph, last_transition, days):
if not parent_transition or not parent_transition.has_trip():
# Reached the end of the path
# Even though export will work anyway, injecting a source node can improve the readability of transfers.txt
split_node.source_node.days = days
split_node.source_node.days = parent_days
t_graph.sources.add(split_node.source_node)
break

parent_days = t_graph.services.days_in_from_frame(
parent_transition.trip, last_transition.trip, days)
parent_transition.trip, current_transition.trip, parent_days)

transfer = parent_transition.out_edges[current_transition.node]
parent_split_node = get_path_node(t_graph, composite_nodes,
parent_transition, parent_days)
Expand Down
4 changes: 4 additions & 0 deletions tests/base/calendar.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,7 @@ fri,0,0,0,0,1,0,0,20210101,20211231
fri-sat-sun,0,0,0,0,1,1,1,20210101,20211231
fri-sat,0,0,0,0,1,1,0,20210101,20211231
sun,0,0,0,0,0,0,1,20210101,20211231
weekday,1,1,1,1,1,0,0,20240506,20240521
friday,0,0,0,0,1,0,0,20240506,20240521
sunday,0,0,0,0,0,0,1,20240506,20240521
saturday,0,0,0,0,0,1,0,20240506,20240521
16 changes: 16 additions & 0 deletions tests/test_multi_day_blocks/expected_linear/calendar.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
service_id,monday,tuesday,wednesday,thursday,friday,saturday,sunday,start_date,end_date
fri,0,0,0,0,1,0,0,20210101,20211231
fri-sat,0,0,0,0,1,1,0,20210101,20211231
fri-sat-sun,0,0,0,0,1,1,1,20210101,20211231
friday,0,0,0,0,1,0,0,20240506,20240521
mon,1,0,0,0,0,0,0,20210101,20211231
mon-tues-thurs-fri-sat,1,1,0,1,1,1,0,20210101,20211231
mon-tues-wed-thurs,1,1,1,1,0,0,0,20210101,20211231
mon-tues-wed-thurs-fri-sat,1,1,1,1,1,1,0,20210101,20211231
mon-tues-wed-thurs-fri-sat-sun,1,1,1,1,1,1,1,20210101,20211231
mon-wed,1,0,1,0,0,0,0,20210101,20211231
saturday,0,0,0,0,0,1,0,20240506,20240521
sun,0,0,0,0,0,0,1,20210101,20211231
sunday,0,0,0,0,0,0,1,20240506,20240521
wed,0,0,1,0,0,0,0,20210101,20211231
weekday,1,1,1,1,1,0,0,20240506,20240521
11 changes: 11 additions & 0 deletions tests/test_multi_day_blocks/expected_linear/calendar_dates.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
service_id,date,exception_type
b2t:service_0,20240506,1
b2t:service_0,20240507,1
b2t:service_0,20240508,1
b2t:service_0,20240509,1
b2t:service_0,20240513,1
b2t:service_0,20240514,1
b2t:service_0,20240515,1
b2t:service_0,20240516,1
b2t:service_0,20240520,1
b2t:service_0,20240521,1
25 changes: 25 additions & 0 deletions tests/test_multi_day_blocks/expected_linear/stop_times.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
trip_id,stop_sequence,stop_id,arrival_time,departure_time,start_pickup_drop_off_window,end_pickup_drop_off_window,pickup_type,drop_off_type,mean_duration_factor,mean_duration_offset,safe_duration_factor,safe_duration_offset
trip_0_b2t:if_b2t:service_0,0,slocan-park,05:50:00,05:50:00,,,0,0,,,,
trip_0_b2t:if_b2t:service_0,1,junction,05:51:15,05:51:15,,,0,0,,,,
trip_0_b2t:if_friday,0,slocan-park,05:50:00,05:50:00,,,0,0,,,,
trip_0_b2t:if_friday,1,junction,05:51:15,05:51:15,,,0,0,,,,
trip_0b_b2t:if_b2t:service_0,0,junction,05:52:00,05:52:00,,,0,0,,,,
trip_0b_b2t:if_b2t:service_0,1,slocan-park,05:59:15,05:59:15,,,0,0,,,,
trip_0b_b2t:if_friday,0,junction,05:52:00,05:52:00,,,0,0,,,,
trip_0b_b2t:if_friday,1,slocan-park,05:59:15,05:59:15,,,0,0,,,,
trip_1_b2t:if_b2t:service_0,0,slocan-park,06:00:00,06:00:00,,,0,0,,,,
trip_1_b2t:if_b2t:service_0,1,junction,24:15:15,24:15:15,,,0,0,,,,
trip_1_b2t:if_friday,0,slocan-park,06:00:00,06:00:00,,,0,0,,,,
trip_1_b2t:if_friday,1,junction,24:15:15,24:15:15,,,0,0,,,,
trip_2_b2t:if_b2t:service_0,0,junction,24:15:30,24:15:30,,,0,0,,,,
trip_2_b2t:if_b2t:service_0,1,slocan-park,28:59:00,28:59:00,,,0,0,,,,
trip_2_b2t:if_friday,0,junction,24:15:30,24:15:30,,,0,0,,,,
trip_2_b2t:if_friday,1,slocan-park,28:59:00,28:59:00,,,0,0,,,,
trip_2a,0,slocan-park,05:00:00,05:00:00,,,0,0,,,,
trip_2a,1,junction,05:01:00,05:01:00,,,0,0,,,,
trip_3,0,junction,05:02:00,05:02:00,,,0,0,,,,
trip_3,1,slocan-park,24:15:15,24:15:15,,,0,0,,,,
trip_4,1,slocan-park,24:17:15,24:17:15,,,0,0,,,,
trip_4,2,junction,24:19:15,24:19:15,,,0,0,,,,
trip_5,1,junction,05:59:00,05:59:00,,,0,0,,,,
trip_5,2,slocan-park,06:00:00,06:00:00,,,0,0,,,,
10 changes: 10 additions & 0 deletions tests/test_multi_day_blocks/expected_linear/transfers.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from_trip_id,to_trip_id,transfer_type
trip_0_b2t:if_b2t:service_0,trip_0b_b2t:if_b2t:service_0,5
trip_0_b2t:if_friday,trip_0b_b2t:if_friday,5
trip_0b_b2t:if_b2t:service_0,trip_1_b2t:if_b2t:service_0,5
trip_0b_b2t:if_friday,trip_1_b2t:if_friday,5
trip_1_b2t:if_b2t:service_0,trip_2_b2t:if_b2t:service_0,5
trip_1_b2t:if_friday,trip_2_b2t:if_friday,5
trip_2_b2t:if_friday,trip_2a,5
trip_2a,trip_3,5
trip_3,trip_4,5
13 changes: 13 additions & 0 deletions tests/test_multi_day_blocks/expected_linear/trips.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
route_id,trip_id,service_id,block_id
red,trip_0_b2t:if_b2t:service_0,b2t:service_0,1
red,trip_0_b2t:if_friday,friday,1
red,trip_0b_b2t:if_b2t:service_0,b2t:service_0,1
red,trip_0b_b2t:if_friday,friday,1
red,trip_1_b2t:if_b2t:service_0,b2t:service_0,1
red,trip_1_b2t:if_friday,friday,1
red,trip_2_b2t:if_b2t:service_0,b2t:service_0,1
red,trip_2_b2t:if_friday,friday,1
red,trip_2a,saturday,1
red,trip_3,saturday,1
red,trip_4,saturday,1
red,trip_5,sunday,1
17 changes: 17 additions & 0 deletions tests/test_multi_day_blocks/input/stop_times.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
trip_id,stop_sequence,stop_id,arrival_time,departure_time
trip_0,0,slocan-park,05:50:00,05:50:00,
trip_0,1,junction,05:51:15,05:51:15
trip_0b,0,junction,05:52:00,05:52:00
trip_0b,1,slocan-park,05:59:15,05:59:15
trip_1,0,slocan-park,06:00:00,06:00:00,
trip_1,1,junction,24:15:15,24:15:15
trip_2,0,junction,24:15:30,24:15:30
trip_2,1,slocan-park,28:59:00,28:59:00
trip_2a,0,slocan-park,05:00:00,05:00:00,
trip_2a,1,junction,05:01:00,05:01:00
trip_3,0,junction,05:02:00,05:02:00
trip_3,1,slocan-park,24:15:15,24:15:15
trip_4,1,slocan-park,24:17:15,24:17:15
trip_4,2,junction,24:19:15,24:19:15
trip_5,1,junction,05:59:00,05:59:00
trip_5,2,slocan-park,06:00:00,06:00:00
9 changes: 9 additions & 0 deletions tests/test_multi_day_blocks/input/trips.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
route_id,trip_id,service_id,block_id
red,trip_0,weekday,1
red,trip_0b,weekday,1
red,trip_1,weekday,1
red,trip_2,weekday,1
red,trip_2a,saturday,1
red,trip_3,saturday,1
red,trip_4,saturday,1
red,trip_5,sunday,1
17 changes: 17 additions & 0 deletions tests/test_simple_multi_day_block/expected_linear/stop_times.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
trip_id,stop_sequence,stop_id,arrival_time,departure_time,start_pickup_drop_off_window,end_pickup_drop_off_window,pickup_type,drop_off_type,mean_duration_factor,mean_duration_offset,safe_duration_factor,safe_duration_offset
trip_0,0,slocan-park,05:50:00,05:50:00,,,0,0,,,,
trip_0,1,junction,05:51:15,05:51:15,,,0,0,,,,
trip_0b,0,junction,05:52:00,05:52:00,,,0,0,,,,
trip_0b,1,slocan-park,05:59:15,05:59:15,,,0,0,,,,
trip_1,0,slocan-park,06:00:00,06:00:00,,,0,0,,,,
trip_1,1,junction,24:15:15,24:15:15,,,0,0,,,,
trip_2,0,junction,24:15:30,24:15:30,,,0,0,,,,
trip_2,1,slocan-park,28:59:00,28:59:00,,,0,0,,,,
trip_2a,0,slocan-park,05:00:00,05:00:00,,,0,0,,,,
trip_2a,1,junction,05:01:00,05:01:00,,,0,0,,,,
trip_3,0,junction,05:02:00,05:02:00,,,0,0,,,,
trip_3,1,slocan-park,24:15:15,24:15:15,,,0,0,,,,
trip_4,1,slocan-park,24:17:15,24:17:15,,,0,0,,,,
trip_4,2,junction,24:19:15,24:19:15,,,0,0,,,,
trip_5,1,junction,05:59:00,05:59:00,,,0,0,,,,
trip_5,2,slocan-park,06:00:00,06:00:00,,,0,0,,,,
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from_trip_id,to_trip_id,transfer_type
trip_0,trip_0b,5
trip_0b,trip_1,5
trip_1,trip_2,5
trip_2,trip_2a,5
trip_2a,trip_3,5
trip_3,trip_4,5
9 changes: 9 additions & 0 deletions tests/test_simple_multi_day_block/expected_linear/trips.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
route_id,trip_id,service_id,block_id
red,trip_0,friday,1
red,trip_0b,friday,1
red,trip_1,friday,1
red,trip_2,friday,1
red,trip_2a,saturday,1
red,trip_3,saturday,1
red,trip_4,saturday,1
red,trip_5,sunday,1
17 changes: 17 additions & 0 deletions tests/test_simple_multi_day_block/input/stop_times.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
trip_id,stop_sequence,stop_id,arrival_time,departure_time
trip_0,0,slocan-park,05:50:00,05:50:00,
trip_0,1,junction,05:51:15,05:51:15
trip_0b,0,junction,05:52:00,05:52:00
trip_0b,1,slocan-park,05:59:15,05:59:15
trip_1,0,slocan-park,06:00:00,06:00:00,
trip_1,1,junction,24:15:15,24:15:15
trip_2,0,junction,24:15:30,24:15:30
trip_2,1,slocan-park,28:59:00,28:59:00
trip_2a,0,slocan-park,05:00:00,05:00:00,
trip_2a,1,junction,05:01:00,05:01:00
trip_3,0,junction,05:02:00,05:02:00
trip_3,1,slocan-park,24:15:15,24:15:15
trip_4,1,slocan-park,24:17:15,24:17:15
trip_4,2,junction,24:19:15,24:19:15
trip_5,1,junction,05:59:00,05:59:00
trip_5,2,slocan-park,06:00:00,06:00:00
9 changes: 9 additions & 0 deletions tests/test_simple_multi_day_block/input/trips.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
route_id,trip_id,service_id,block_id
red,trip_0,friday,1
red,trip_0b,friday,1
red,trip_1,friday,1
red,trip_2,friday,1
red,trip_2a,saturday,1
red,trip_3,saturday,1
red,trip_4,saturday,1
red,trip_5,sunday,1

0 comments on commit ff35e0a

Please sign in to comment.