This commit is contained in:
Andras Schmelczer 2026-07-12 21:31:13 +01:00
parent ca771a7edf
commit d7f844d566
12 changed files with 198 additions and 251 deletions

View file

@ -1,6 +1,9 @@
import polars as pl
from pipeline.transform.poi_proximity import (
COMBINED_STATION_DISPLAY_NAME,
COMBINED_STATION_GROUP_KEY,
COMBINED_STATION_SOURCE_CATEGORIES,
GREENSPACE_PARK_FUNCTIONS,
POI_GROUPS_2KM,
_build_poi_category_groups,
@ -8,7 +11,7 @@ from pipeline.transform.poi_proximity import (
_greenspace_count_frame,
_groceries_categories,
)
from pipeline.utils.poi_counts import count_pois_per_postcode
from pipeline.utils.poi_counts import count_pois_per_postcode, min_distance_per_postcode
def test_groceries_2km_counts_geolytix_brand_categories() -> None:
@ -84,6 +87,42 @@ def test_dynamic_poi_groups_include_requested_categories_only() -> None:
assert "poi_school" not in groups
def test_combined_station_distance_is_nearest_of_any_rail_mode() -> None:
"""The combined "Any station" distance is the nearest of Rail/Tube/DLR/Tram,
materialized as its own column so the server loads it directly (no runtime
synthesis). A far rail station and a near DLR stop: the combined distance
must follow the DLR stop."""
postcodes = pl.DataFrame(
{"postcode": ["E14 5AB"], "lat": [51.5054], "lon": [-0.0235]}
)
pois = pl.DataFrame(
{
"category": ["Rail station", "DLR station"],
"group": ["Public Transport", "Public Transport"],
"lat": [51.6000, 51.5055],
"lng": [-0.2000, -0.0236],
}
)
distance_groups = {
"poi_rail_station": ["Rail station"],
COMBINED_STATION_GROUP_KEY: COMBINED_STATION_SOURCE_CATEGORIES,
}
result = min_distance_per_postcode(postcodes, pois, groups=distance_groups)
renames = _dynamic_poi_metric_renames(
{COMBINED_STATION_GROUP_KEY: COMBINED_STATION_DISPLAY_NAME}
)
result = result.rename({k: v for k, v in renames.items() if k in result.columns})
combined = result["Distance to nearest amenity (Any station) (km)"][0]
rail_only = result["poi_rail_station_nearest_km"][0]
# The near DLR stop is within a few hundred metres; the only rail station is
# kilometres away. The combined distance must follow the DLR stop.
assert combined < 0.2
assert rail_only > 5.0
assert combined < rail_only
def test_dynamic_poi_metric_renames_support_park_count_options() -> None:
assert _dynamic_poi_metric_renames({"parks": "Park"}) == {
"parks_nearest_km": "Distance to nearest amenity (Park) (km)",