Yuquanlee's picture
MunicipalBench dataset files
7825400 verified
Raw History Blame Contribute Delete
1.15 kB
"""The one mapping between a model's ``category_id`` and a ``use_case_id``.
Ground truth stores violation codes zero-padded (``U03``); models emit them as
plain integers (``3``). Getting the padding wrong does not raise — it produces a
label that matches nothing, so every single-digit class silently becomes a false
positive and a phantom row in the report.
This lived as a copy-pasted one-liner in both metric modules and was wrong in
both. It is one function now so it can only ever be wrong in one place.
"""
from __future__ import annotations
# Violation codes are two digits in the taxonomy (U01-U97). Wider ids are passed
# through unpadded rather than truncated, so an out-of-range id stays visible as
# an unknown label instead of colliding with a real one.
_CODE_WIDTH = 2
def to_use_case_id(category_id: int) -> str:
"""Map an integer ``category_id`` to its ``use_case_id``.
``3`` -> ``"U03"``, ``17`` -> ``"U17"``.
"""
return f"U{category_id:0{_CODE_WIDTH}d}"
def to_category_id(use_case_id: str) -> int:
"""Inverse of :func:`to_use_case_id`. ``"U03"`` -> ``3``."""
return int(use_case_id.lstrip("Uu"))