transitlos.scoring.distance_matrix¶
Builds a UrbanAccessAnalyzer-compatible distance_matrix from transitlos's own
D(t; stop_score) decay, with output discretized onto a fixed {0.0, 0.1, ..., 1.0}
level_of_service grid -- replacing UrbanAccessAnalyzer.isochrones.default_distance_matrix's
own step-function decay, at the user's explicit request (2026-08-12).
2026-09-28 note (restored stop_score/mode coupling): D(t; stop_score)
is once again a function of stop_score (via t0(stop_score), mode-scaled
-- see walk_access.py), so this module now requires a single mode per
call and treats the maximum stop_score present as the reference score
used only to pick distance-column thresholds (the most generous, longest-
tolerance curve among the given stops, so no real stop's natural D(t)=0.05
threshold falls beyond the chosen columns). Each stop's own actual
stop_score (not the reference) is still used when computing that stop's
signature/bucket at those columns, so per-stop accuracy is unaffected --
only the set of distance columns computed is shared. Different modes have
different t0_base, and therefore different distance-column sets; callers
with a mode-mixed stops table must call this once per mode and run
AccessibilityAnalyzer once per mode's subset of stops (see
level_of_service.py, which does this and combines results by taking the
per-edge maximum access_score across mode-runs).
Why this exists (read level_of_service.py's module docstring first): the
previous pipeline deliberately never called walk_access.walk_decay because
AccessibilityAnalyzer.default_distance_matrix already applies its own
(unrelated) rank-based falloff, and stacking both would double-count walk
decay. This module resolves that by building the distance_matrix argument
AccessibilityAnalyzer.run expects from transitlos.scoring's own
accessibility_score, so the isochrone engine now runs transitLOS's
own decay instead of default_distance_matrix's generic one.
"Intelligent discretization" (the user's phrase) means two distinct things, both implemented here:
- Output side: every level_of_service this module ever produces -- both
inside the matrix and (via
ceil_to_grid) as a post-processing safety net on the final per-edge result -- is ceiling-rounded onto the fixed grid{0.0, 0.1, 0.2, ..., 1.0}(e.g. 0.91 -> 1.0, not rounded to nearest), for legible output. - Input side (run-count minimization): rather than accept whatever
flat
walk_distance_stepsconvention the caller passes, this module derives distance thresholds by invertingwalk_decayat the midpoint of each of the 10 output-grid bands (0.05, 0.15, ..., 0.95) for a reference stop_score -- so every distance column that gets computed maps onto an output-visible boundary, and no finer. Separately, every distinctstop_scorepresent is collapsed into a bucket sharing one representative value whenever two scores would produce an identical ceiling-discretized output at every chosen distance -- i.e. buckets are made as coarse as possible without losing any information the final output can actually distinguish. SinceUrbanAccessAnalyzer's ownisochrones.distance_matrix_to_processing_order/compute_node_accessalready batch every POI sharing one(level_of_service, distance)pair into a single multi-source Dijkstra call (see that module's docstring), collapsing rows here directly collapses the number of underlying isochrone/shortest-path runs -- this is "as little as possible" isochrone-run count and "stops grouped together as much as possible", not a separate optimization layered on top.
Processing order (informational, not implemented here): UrbanAccessAnalyzer
already processes distance_matrix_to_processing_order's rows best-rank
(highest level_of_service) first and reconciles each node/edge to the best tier
it qualifies for across all rows -- see isochrones.py's module docstring,
step 3: "A node's final level of service is the best (lowest rank) tier for
which it has positive remaining distance". This already guarantees every
street segment ends up with the maximum level_of_service any stop/distance
combination can justify, regardless of the order rows are submitted in
this module's matrix -- no additional ordering logic is needed here.
ceil_to_grid(value, step=OUTPUT_GRID_STEP)
¶
Ceiling-round a scalar level_of_service onto the {0, step, 2*step, ..., 1.0} grid.
E.g. ceil_to_grid(0.91) == 1.0, ceil_to_grid(0.0) == 0.0,
ceil_to_grid(0.30) == 0.3 (exact multiples are not pushed to the next
tier -- the small epsilon subtraction guards against float error like
0.3 / 0.1 == 2.9999999999999996 incorrectly ceiling-ing to 0.4).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
float
|
The raw level_of_service, expected in |
required |
step
|
float
|
Grid spacing. Defaults to |
OUTPUT_GRID_STEP
|
Returns:
| Type | Description |
|---|---|
float
|
A float on the grid, clipped to |
ceil_to_grid_array(values, step=OUTPUT_GRID_STEP)
¶
Vectorized ceil_to_grid, for a final safety-net pass over a whole level_of_service column.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
values
|
ndarray
|
Scores. |
required |
step
|
float
|
Grid step. |
OUTPUT_GRID_STEP
|
Returns:
| Type | Description |
|---|---|
ndarray
|
Scores rounded up to the grid, clipped to [0, 1]. |
build_intelligent_distance_matrix(stop_scores, mode, region='global', max_distance_m=None)
¶
Build a distance_matrix + stop_score-bucket map from transitlos's own decay.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
stop_scores
|
Sequence[float]
|
The distinct (or a representative sample of) stop_score values actually present in the stops being scored, for ONE mode (see module docstring). Only their range matters -- the maximum is used as the reference stop_score for distance-step placement; each stop's own value is used for its own bucket signature. |
required |
mode
|
str
|
One of |
required |
region
|
str
|
Region key forwarded to |
'global'
|
max_distance_m
|
float | None
|
Optional hard outer walk-distance cutoff (meters).
Since D(t) is a gentle, asymptotically-declining curve rather
than a hard cutoff, its own natural output-tier thresholds can
extend arbitrarily far (the lowest 0.05-D0.1 tier's threshold
is several kilometers out) -- an isochrone search out to that
distance for every stop is expensive and, past some point, not
meaningfully different from "no access". When given, any
natural threshold beyond |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
distance_matrix |
DataFrame
|
a |
bucket_map |
dict[float, float]
|
|
distance_steps_m |
list[float]
|
the chosen distance thresholds in meters (ascending), for logging/inspection. |