Skip to content

UrbanAccessAnalyzer.scoring

POI quality scoring: from raw attributes to a calibrated [0, 1] access score.

Consolidates three previously separate, duplicated modules: scoring.py and quality.py were byte-for-byte near-duplicates (a "POI scoring" vs. "transit stop quality" naming fork of the same elasticity/calibration code), and poi_utils.py internally duplicated score_by_values/ score_by_area under both "quality_" and "score_" names. Per the package's naming convention, everything here is called score/ access_score -- never "quality".

Methodology:

  • :func:score_by_values and :func:score_by_area produce a simple [0, 1] ordinal score from a categorical value or a continuous area, respectively, given an explicit priority ordering.
  • :func:elasticity_based_score computes a continuous decay score using the economic notion of elasticity: score(v) = (v / reference) ** elasticity for constant elasticity, or the exponential of the integral of elasticity(x)/x for a value- or interval-dependent elasticity (matching how price elasticity of demand extends to non-constant elasticities). This gives a principled decay curve (e.g. for distance or headway) instead of an arbitrary hand-picked shape.
  • :func:build_adaptive_grids builds a non-uniform grid per input variable so that adjacent grid points never differ by more than delta in a given (possibly expensive/non-vectorizable) score function -- used to precompute a lookup table instead of recomputing the function per row.
  • :func:calibrate_scoring_func linearly rescales an arbitrary score function's output range to a target [min_score, max_score] given calibration anchor points.

condense_rows(df, row_values=None, columns=None)

Collapse multiple boolean/tag columns into a single value per row.

Parameters:

Name Type Description Default
df DataFrame

Input DataFrame (attribute columns to condense, e.g. one column per OSM tag like shop, amenity).

required
row_values Optional[list]

Priority order of values; the first matching value (by this order) found in any of columns wins for each row. If None, the first non-null value across columns is used.

None
columns Optional[list]

Columns to consider; defaults to every column except geometry.

None

Returns:

Type Description
list

List of condensed values, one per row (None where no column

list

matched).

score_by_values(values, value_priority)

Map categorical values to a [0, 1] score by explicit priority order.

Parameters:

Name Type Description Default
values Union[list, Series]

Categorical values to score (e.g. amenity subtype).

required
value_priority list

Values in priority order, best first; must be unique and contain no null.

required

Returns:

Type Description
list

List of scores in [0, 1] (best value -> 1.0), same length as

list

values. This is a "higher is better" score, so values not found

list

in value_priority (including null input) score 0.0 -- the

list

worst possible score, not None -- consistent with the package's

list

convention that non-computed/unreachable values on a bounded 0-1

list

"higher is better" scale collapse to 0, while non-computed values on

list

an unbounded "lower is better" scale (e.g. a distance) should stay

list

None/null instead (representing an effectively infinite,

list

i.e. unreached, distance).

Raises:

Type Description
Exception

If value_priority is empty, has duplicates, or contains a null.

score_by_area(gdf, area_steps, large_is_better=True)

Score geometries by which area threshold bin they fall into.

Parameters:

Name Type Description Default
gdf Union[GeoDataFrame, GeoSeries]

Input geometries (any CRS; reprojected to local UTM to measure area).

required
area_steps list[float]

Area breakpoints (m²), any order (sorted internally).

required
large_is_better bool

If True, larger area -> higher score.

True

Returns:

Type Description
list

List of scores in [0, 1], one per input geometry. This is a

list

"higher is better" score: geometries that don't clear any threshold

list

(never assigned a bucket) score 0.0 rather than None -- see

list

func:score_by_values for the package's None-vs-0 convention.

polygons_to_points(poi, street_edges)

Convert polygon/line POIs to points by intersecting with street edges.

Points and multipoints pass through unchanged. Polygons are reduced to their boundary; everything else is intersected with the union of street_edges, which pulls out the point(s) where the POI actually touches the walkable network (used before snapping POIs onto a graph).

Parameters:

Name Type Description Default
poi GeoDataFrame

POI geometries (any mix of Point/Polygon/MultiPolygon/etc.).

required
street_edges GeoDataFrame

Street edge geometries to intersect non-point POIs with.

required

Returns:

Type Description
GeoDataFrame

GeoDataFrame of Point geometries (exploded, empty intersections

GeoDataFrame

dropped), with a poi_id column pointing back to the original

GeoDataFrame

poi row index for non-point inputs.

build_adaptive_grids(func, variables, delta=0.1, max_iters=30)

Build non-uniform grids so func changes by at most delta between adjacent points.

Parameters:

Name Type Description Default
func Callable

Broadcastable callable, func(var1, var2, ...).

required
variables List[Union[List[float], tuple, ndarray]]

Per-variable spec: a continuous [min, max]/(min, max) pair to refine, or a pre-discretized list/array to leave as-is.

required
delta float

Maximum allowed change in func between adjacent grid points along any one continuous variable's axis.

0.1
max_iters int

Maximum refinement iterations.

30

Returns:

Type Description
List[ndarray]

List of 1-D numpy arrays, one refined grid per variable.

elasticity_from_linear_decay(decay, point)

Convert a linear decay rate at a reference point into an equivalent elasticity.

Parameters:

Name Type Description Default
decay float

Linear decay rate (fraction lost per unit of point).

required
point float

The point at which decay applies.

required

Returns:

Type Description
float

The elasticity value e such that a constant-elasticity curve

float

matches the given linear decay at point.

elasticity_based_score(value, reference, elasticity, steps=200)

Compute a decay score via elasticity-based integration, vectorized.

Parameters:

Name Type Description Default
value Union[float, List[float], ndarray]

Current value(s) of the variable (e.g. distance, headway).

required
reference float

Reference value where score == 1.0.

required
elasticity Union[float, Callable[[float], float], List[Sequence[float]]]

Constant elasticity (float, analytic solution), a piecewise elasticity [[lower_bound, e], ...] (analytic per segment), or a callable e(x) (numerically integrated).

required
steps int

Integration steps, used only for the callable case.

200

Returns:

Type Description
Union[float, ndarray]

Score(s) in (0, 1], decreasing as value moves away from

Union[float, ndarray]

reference.

Raises:

Type Description
TypeError

If elasticity is not a float, callable, or piecewise list.

calibrate_scoring_func(score_func, *, min_score=0.1, max_score=1.0, min_point=None, max_point=None, variable_steps=None)

Linearly rescale a multi-parameter score function to [min_score, max_score].

Parameters:

Name Type Description Default
score_func Callable[..., Union[float, ndarray]]

Function accepting positional arguments; may return a scalar or vectorized np.ndarray.

required
min_score float

Output value at the calibration minimum.

0.1
max_score float

Output value at the calibration maximum.

1.0
min_point Optional[Sequence[T]]

Explicit argument tuple defining the minimum; if omitted, inferred as the minimum over variable_steps combinations.

None
max_point Optional[Sequence[T]]

Explicit argument tuple defining the maximum; if omitted, inferred as the maximum over variable_steps combinations.

None
variable_steps Optional[List[Any]]

Per-argument value lists used to search for the calibration min/max when min_point/max_point aren't given.

None

Returns:

Type Description
Callable[..., Union[float, ndarray]]

A new function with score_func's signature returning the

Callable[..., Union[float, ndarray]]

rescaled score.

Raises:

Type Description
ValueError

If no calibration points can be determined, or if the calibration min and max coincide.

Exception

If every evaluated score is zero while min_score > 0.