Datasets:
Download code/metrics/_f_beta.py from govtech/municipalbench-full: direct link, hf CLI and curl.
- Browser
- Download file 3.15 kB
-
https://huggingface.co/datasets/govtech/municipalbench-full/resolve/main/code/metrics/_f_beta.py
- Command line
-
hf download hf://datasets/govtech/municipalbench-full/code/metrics/_f_beta.py
-
curl -L -o _f_beta.py https://huggingface.co/datasets/govtech/municipalbench-full/resolve/main/code/metrics/_f_beta.py
3.15 kB
| """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 | |
| 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, | |
| } | |