Spaces:
Sleeping
Sleeping
File size: 11,637 Bytes
a3c58b7 165e2e6 a3c58b7 165e2e6 a3c58b7 165e2e6 a3c58b7 165e2e6 a3c58b7 165e2e6 a3c58b7 165e2e6 a3c58b7 165e2e6 a3c58b7 165e2e6 a3c58b7 165e2e6 a3c58b7 165e2e6 a3c58b7 165e2e6 ed2929d 91d8a03 a3c58b7 165e2e6 a3c58b7 165e2e6 a3c58b7 165e2e6 a3c58b7 165e2e6 a3c58b7 165e2e6 a3c58b7 165e2e6 a3c58b7 91d8a03 a3c58b7 165e2e6 a3c58b7 165e2e6 a3c58b7 165e2e6 a3c58b7 165e2e6 a3c58b7 165e2e6 a3c58b7 ed2929d a3c58b7 165e2e6 a3c58b7 165e2e6 a3c58b7 165e2e6 a3c58b7 165e2e6 a3c58b7 165e2e6 a3c58b7 165e2e6 a3c58b7 165e2e6 a3c58b7 165e2e6 a3c58b7 165e2e6 a3c58b7 165e2e6 a3c58b7 165e2e6 a3c58b7 165e2e6 a3c58b7 91d8a03 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 | import gtfs_kit
import pandas as pd
from shared.api.gtfs import parent_stations_from_gtfs
def _make_feed(**tables) -> gtfs_kit.Feed:
"""Construct a minimal gtfs_kit Feed from raw table dicts."""
feed = gtfs_kit.Feed(dist_units="km")
for attr, rows in tables.items():
setattr(feed, attr, pd.DataFrame(rows))
return feed
CALENDAR_FULL_WEEK = [
{
"service_id": "wk",
"monday": 1,
"tuesday": 1,
"wednesday": 1,
"thursday": 1,
"friday": 1,
"saturday": 1,
"sunday": 1,
"start_date": "20250106",
"end_date": "20250112",
},
]
TRIPS_S1A_S1B = [{"route_id": "801", "trip_id": f"t{i}", "service_id": "wk"} for i in range(6)]
STOPA_W_PARENT = "S1A"
STOPB_W_PARENT = "S1B"
PARENT_STOP = "S1"
STOPS_WITH_PARENT = [
{
"stop_id": PARENT_STOP,
"stop_name": "Main St",
"stop_lat": 34.0,
"stop_lon": -118.0,
"location_type": 1,
"parent_station": "",
},
{
"stop_id": STOPA_W_PARENT,
"stop_name": "Main St NB",
"stop_lat": 34.0,
"stop_lon": -118.0,
"location_type": 0,
"parent_station": "S1",
},
{
"stop_id": STOPB_W_PARENT,
"stop_name": "Main St SB",
"stop_lat": 34.0,
"stop_lon": -118.0,
"location_type": 0,
"parent_station": "S1",
},
]
class TestParentStationsFromGtfs:
def test_parent_station_with_children(self):
"""Child stop route info and trip counts are aggregated up to the parent."""
# Arrange
trip1 = "t1"
trip2 = "t2"
route1 = "801"
feed = _make_feed(
stops=STOPS_WITH_PARENT,
routes=[{"route_id": route1, "route_type": 1, "agency_id": "metro"}],
trips=[
{"route_id": route1, "trip_id": trip1, "service_id": "wk"},
{"route_id": route1, "trip_id": trip2, "service_id": "wk"},
],
stop_times=[
{
"trip_id": trip1,
"stop_id": STOPA_W_PARENT,
"stop_sequence": 1,
"arrival_time": "08:00:00",
"departure_time": "08:00:00",
},
{
"trip_id": trip2,
"stop_id": STOPB_W_PARENT,
"stop_sequence": 1,
"arrival_time": "08:00:00",
"departure_time": "08:00:00",
},
],
calendar=CALENDAR_FULL_WEEK,
)
# Act
result = parent_stations_from_gtfs(feed)
# Assert
assert len(result) == 1
assert result.iloc[0]["stop_id"] == "S1"
assert route1 in result.iloc[0]["route_ids"]
# n_arrivals is now a list of per-route averages (parallel to route_ids)
assert result.iloc[0]["n_arrivals"] == [1.0]
def test_no_parent_stations_falls_back_to_boarding_stops(self):
"""When no location_type=1 stops exist, boarding stops are treated as parents."""
# Arrange
stop1 = "S1"
trip1 = "t1"
route1 = "801"
feed = _make_feed(
stops=[
{
"stop_id": stop1,
"stop_name": "Stop 1",
"stop_lat": 34.0,
"stop_lon": -118.0,
"location_type": 0,
"parent_station": "",
},
],
routes=[{"route_id": route1, "route_type": 1, "agency_id": "metro"}],
trips=[{"route_id": route1, "trip_id": trip1, "service_id": "wk"}],
stop_times=[
{
"trip_id": trip1,
"stop_id": stop1,
"stop_sequence": 1,
"arrival_time": "08:00:00",
"departure_time": "08:00:00",
}
],
calendar=CALENDAR_FULL_WEEK,
)
# Act
result = parent_stations_from_gtfs(feed)
# Assert
assert len(result) == 1
assert result.iloc[0]["stop_id"] == stop1
def test_trip_counts_summed_across_children(self):
# Arrange
feed = _make_feed(
stops=STOPS_WITH_PARENT,
routes=[{"route_id": "801", "route_type": 1, "agency_id": "metro"}],
trips=TRIPS_S1A_S1B,
stop_times=[
*[
{
"trip_id": f"t{i}",
"stop_id": STOPA_W_PARENT,
"stop_sequence": 1,
"arrival_time": "08:00:00",
"departure_time": "08:00:00",
}
for i in range(3)
],
*[
{
"trip_id": f"t{i}",
"stop_id": STOPB_W_PARENT,
"stop_sequence": 1,
"arrival_time": "08:00:00",
"departure_time": "08:00:00",
}
for i in range(3, 6)
],
],
calendar=CALENDAR_FULL_WEEK,
)
# Act
result = parent_stations_from_gtfs(feed)
# Assert
assert len(result) == 1
assert result.iloc[0]["n_arrivals"] == [3.0]
def test_routes_merged_across_children(self):
# Arrange
trip1 = "t1"
trip2 = "t2"
route1 = {"route_id": "route1", "route_type": 1, "agency_id": "agency1"}
route2 = {"route_id": "route2", "route_type": 2, "agency_id": "agency2"}
feed = _make_feed(
stops=STOPS_WITH_PARENT,
routes=[
route1,
route2,
],
trips=[
{"route_id": route1["route_id"], "trip_id": trip1, "service_id": "wk"},
{"route_id": route2["route_id"], "trip_id": trip2, "service_id": "wk"},
],
stop_times=[
{
"trip_id": trip1,
"stop_id": STOPA_W_PARENT,
"stop_sequence": 1,
"arrival_time": "08:00:00",
"departure_time": "08:00:00",
},
{
"trip_id": trip2,
"stop_id": STOPB_W_PARENT,
"stop_sequence": 1,
"arrival_time": "08:00:00",
"departure_time": "08:00:00",
},
],
calendar=CALENDAR_FULL_WEEK,
)
# Act
result = parent_stations_from_gtfs(feed)
# Assert
assert len(result) == 1
# route_ids is now a list (not a set)
assert result.iloc[0]["route_ids"] == [route1["route_id"], route2["route_id"]]
assert result.iloc[0]["route_types"] == [str(route1["route_type"]), str(route2["route_type"])]
def test_stop_with_no_trips_excluded(self):
"""Parent stations with no associated trips are excluded from the result."""
# Arrange
stop1 = "S1"
feed = _make_feed(
stops=[
{
"stop_id": stop1,
"stop_name": "Served",
"stop_lat": 34.0,
"stop_lon": -118.0,
"location_type": 1,
"parent_station": "",
},
{
"stop_id": "S2",
"stop_name": "Unserved",
"stop_lat": 34.1,
"stop_lon": -118.1,
"location_type": 1,
"parent_station": "",
},
],
routes=[{"route_id": "801", "route_type": 1, "agency_id": "metro"}],
trips=[{"route_id": "801", "trip_id": "t1", "service_id": "wk"}],
stop_times=[
{
"trip_id": "t1",
"stop_id": "S1",
"stop_sequence": 1,
"arrival_time": "08:00:00",
"departure_time": "08:00:00",
}
],
calendar=CALENDAR_FULL_WEEK,
)
# Act
result = parent_stations_from_gtfs(feed)
# Assert
assert len(result) == 1
assert result.iloc[0]["stop_id"] == stop1
def test_multiple_parent_stations(self):
"""Each parent station is returned as a separate row with its own aggregated info."""
# Arrange
stop1 = "S1"
stop2 = "S2"
feed = _make_feed(
stops=[
{
"stop_id": stop1,
"stop_name": "Station 1",
"stop_lat": 34.0,
"stop_lon": -118.0,
"location_type": 1,
"parent_station": "",
},
{
"stop_id": stop2,
"stop_name": "Station 2",
"stop_lat": 34.1,
"stop_lon": -118.1,
"location_type": 1,
"parent_station": "",
},
],
routes=[{"route_id": "801", "route_type": 1, "agency_id": "metro"}],
trips=[
{"route_id": "801", "trip_id": "t1", "service_id": "wk"},
{"route_id": "801", "trip_id": "t2", "service_id": "wk"},
],
stop_times=[
{
"trip_id": "t1",
"stop_id": stop1,
"stop_sequence": 1,
"arrival_time": "08:00:00",
"departure_time": "08:00:00",
},
{
"trip_id": "t2",
"stop_id": stop2,
"stop_sequence": 1,
"arrival_time": "08:00:00",
"departure_time": "08:00:00",
},
],
calendar=CALENDAR_FULL_WEEK,
)
# Act
result = parent_stations_from_gtfs(feed)
# Assert
assert len(result) == 2
assert set(result["stop_id"]) == {stop1, stop2}
def test_parent_served_directly_and_via_children(self):
"""Trips at the parent stop_id itself are counted alongside child trips."""
# Arrange
feed = _make_feed(
stops=STOPS_WITH_PARENT,
routes=[{"route_id": "801", "route_type": 1, "agency_id": "metro"}],
trips=[
{"route_id": "801", "trip_id": "t1", "service_id": "wk"},
{"route_id": "801", "trip_id": "t2", "service_id": "wk"},
],
stop_times=[
{
"trip_id": "t1",
"stop_id": PARENT_STOP,
"stop_sequence": 1,
"arrival_time": "08:00:00",
"departure_time": "08:00:00",
}, # direct on parent
{
"trip_id": "t2",
"stop_id": STOPA_W_PARENT,
"stop_sequence": 1,
"arrival_time": "08:00:00",
"departure_time": "08:00:00",
}, # via child
],
calendar=CALENDAR_FULL_WEEK,
)
# Act
result = parent_stations_from_gtfs(feed)
# Assert
assert len(result) == 1
assert result.iloc[0]["n_arrivals"] == [1.0]
|