Datasets:
File size: 3,146 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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 | """Shared F-beta score primitives used by all metric tracks.
Provides the core building blocks for computing F-beta scores (default β=2)
from raw TP/FP/FN counts. Both the localisation and classification tracks
use these helpers so the formula is defined in exactly one place.
"""
from __future__ import annotations
from dataclasses import dataclass
@dataclass
class ViolationCounts:
"""Mutable accumulator for TP / FP / FN counts of a single violation."""
tp: int = 0
fp: int = 0
fn: int = 0
def compute_f_beta(tp: int, fp: int, fn: int, beta: float = 2.0) -> float:
"""Return the F-beta score from raw counts.
The general formula is::
F_β = (1 + β²) · (precision · recall) / (β² · precision + recall)
Returns ``0.0`` when both precision and recall are zero (no predictions
and no ground-truth positives).
"""
if tp == 0:
return 0.0
beta_sq = beta ** 2
precision = tp / (tp + fp) if (tp + fp) > 0 else 0.0
recall = tp / (tp + fn) if (tp + fn) > 0 else 0.0
if precision + recall == 0.0:
return 0.0
return (1 + beta_sq) * (precision * recall) / (beta_sq * precision + recall)
def summarise_counts(
counts: dict[str, ViolationCounts],
beta: float = 2.0,
) -> dict[str, dict]:
"""Convert per-violation raw counts into a summary with F-beta, precision, and recall.
Args:
counts: Mapping of violation id → ``ViolationCounts``.
beta: Beta parameter for the F-beta score (default 2).
Returns:
Sorted dict keyed by violation id, each value containing::
{"f2": float, "precision": float, "recall": float,
"tp": int, "fp": int, "fn": int}
"""
summary: dict[str, dict] = {}
for violation_id, c in sorted(counts.items()):
precision = c.tp / (c.tp + c.fp) if (c.tp + c.fp) > 0 else 0.0
recall = c.tp / (c.tp + c.fn) if (c.tp + c.fn) > 0 else 0.0
summary[violation_id] = {
"f2": round(compute_f_beta(c.tp, c.fp, c.fn, beta=beta), 4),
"precision": round(precision, 4),
"recall": round(recall, 4),
"tp": c.tp,
"fp": c.fp,
"fn": c.fn,
}
return summary
def aggregate_summary(counts: dict[str, ViolationCounts], beta: float = 2.0) -> dict:
"""Return micro and macro precision, recall, and F-beta."""
values = list(counts.values())
tp = sum(c.tp for c in values)
fp = sum(c.fp for c in values)
fn = sum(c.fn for c in values)
precision = tp / (tp + fp) if tp + fp else 0.0
recall = tp / (tp + fn) if tp + fn else 0.0
per_class = summarise_counts(counts, beta)
macro = {
key: round(sum(item[key] for item in per_class.values()) / len(per_class), 4)
if per_class else 0.0
for key in ("f2", "precision", "recall")
}
return {
"micro": {
"f2": round(compute_f_beta(tp, fp, fn, beta), 4),
"precision": round(precision, 4),
"recall": round(recall, 4),
"tp": tp,
"fp": fp,
"fn": fn,
},
"macro": macro,
}
|