File size: 1,150 Bytes
7825400
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
"""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"))