Skip to content

Fix gtfs io discrepancy - #57

Merged
Hussein-Mahfouz merged 3 commits into
fix_gtfs_creation_from_solutionfrom
fix_gtfs_writing_2
Mar 18, 2026
Merged

Fix gtfs io discrepancy #57
Hussein-Mahfouz merged 3 commits into
fix_gtfs_creation_from_solutionfrom
fix_gtfs_writing_2

Conversation

@Hussein-Mahfouz

@Hussein-Mahfouz Hussein-Mahfouz commented Mar 18, 2026

Copy link
Copy Markdown
Owner

Problem:

We identified a discrepancy between how the GTFS is read/encoded in prepare_gtfs.py and how it is written/decoded in gtfs.py. Previously, the optimization pipeline was generating a deflated service level.

This issue was traced to extract_route_templates in the GTFS writer. When a route had multiple templates, the legacy code attempted to find the template of the last trip for a given headsign/direction. If that specific template did not exist or failed to map cleanly, the script would silently fail to write the trip. As a result, large chunks of optimized trips were simply dropped during the final GTFS construction.

Changes in this PR:

  • Spatial Grouping & Direction Inference: Replaced previous "splitting factor" logic with spatial grouping. The code now infers route directions by analyzing trip origins and destinations, efficiently grouping all disparate headsigns into two spatially-defined directions and mapping each headsign accordingly (extract_route_templates, gtfs.py).
  • Proportional Round-Robin Template Handling: For routes with multiple headsigns (branches), the code now analyzes historical trip frequencies and populates a weighted template list in proportion to each branch's frequency. (extract_route_templates, gtfs.py).
  • Round-Robin Trip Generation: When generating trips, the function now cycles through the list of templates for each direction. This ensures that generated trips are distributed according to real-world service ratios, and that the aggregate service frequency matches the optimized headway precisely without dropping trips (generate_trips_and_stop_times, gtfs.py).
  • Validation Notebook: Introduced 4_validate_gtfs_io.ipynb to mathematically show read/write pipeline discrepancies across the network. It isolates actual headway snapping (e.g. boundary constraints dropping/adding trips at the 5-min or 60-min marks) from unintentional bugs, showing the I/O pipeline now has minimal discrepancy

@Hussein-Mahfouz
Hussein-Mahfouz merged commit ff7a0ca into fix_gtfs_creation_from_solution Mar 18, 2026
3 checks passed
@Hussein-Mahfouz
Hussein-Mahfouz deleted the fix_gtfs_writing_2 branch March 18, 2026 21:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant