crashmetrics.margins¶
margins
¶
Distance to performance limits, as continuous targets for ML models.
NCAP points and colour bands jump in steps, which regression models learn badly. The distance of each criterion to its limits is continuous, so it is a better training target; apply the scoring rules to the prediction afterwards.
For a criterion value v with higher performance limit HPL and lower performance limit LPL:
position = (v - HPL) / (LPL - HPL): 0 at the HPL, 1 at the LPL, negative inside the full-points region, above 1 beyond the LPL.margin_lpl = (LPL - v) / LPL: share of the LPL still available (1 at zero, 0 at the LPL, negative beyond). For a single limit (a regulation limit) this is the safety margin.margin_hpl = (HPL - v) / HPL: the same for the HPL.sliding = clip(1 - position, 0, 1): the linear sliding-scale fraction (1 at or below the HPL, 0 at or beyond the LPL), as used by Bharat NCAP and older Euro NCAP protocols.
Values are compared by magnitude (|v|).
Functions:
margin,margins: For any limits (aLimit, an(hpl, lpl)pair or a single limit).euroncap_margins: Euro NCAP 2026 frontal limits of a dummy.margin_features: Flat dict forcrashmetrics.features.feature_table.
Margin
dataclass
¶
margin
¶
margin(name: str, value: float, limit: LimitLike) -> Margin
Margin of one value to a limit (Limit, (hpl, lpl) or one limit).
margins
¶
margins(
values: Mapping[str, float],
limits: Mapping[str, LimitLike],
) -> dict[str, Margin]
margin for every value that has a limit (others are skipped).
euroncap_margins
¶
euroncap_margins(
dummy: str, values: Mapping[str, float], speed: int = 50
) -> dict[str, Margin]
Margins to the Euro NCAP 2026 frontal limits of a dummy
(crashmetrics.frontal_scoring.DUMMY_LIMITS; chest limits by
speed). Values without a limit for that dummy are skipped.
margin_features
¶
margin_features(
result: Mapping[str, Margin],
prefix: str = "",
fields: tuple[str, ...] = (
"position",
"margin_lpl",
"sliding",
),
) -> dict[str, float]
Flatten margins into {"<prefix><criterion>.<field>": value}.
Fields that are None (no HPL) become NaN, so feature tables keep their columns.