Download tests/test_metrics.py from fahadhafeezofficial/clsa-code: direct link, hf CLI and curl.
- Browser
- Download file 2.51 kB
-
https://huggingface.co/datasets/fahadhafeezofficial/clsa-code/resolve/main/tests/test_metrics.py
- Command line
-
hf download hf://datasets/fahadhafeezofficial/clsa-code/tests/test_metrics.py
-
curl -L -o test_metrics.py https://huggingface.co/datasets/fahadhafeezofficial/clsa-code/resolve/main/tests/test_metrics.py
2.51 kB
| # Copyright (c) 2026 CLSA contributors | |
| # SPDX-License-Identifier: MIT | |
| """Metrics: bootstrap behaviour, calibration, Clopper-Pearson bounds.""" | |
| from __future__ import annotations | |
| import sys | |
| from pathlib import Path | |
| sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "src")) | |
| import numpy as np # noqa: E402 | |
| from clsa.controller.risk import ( # noqa: E402 | |
| clopper_pearson_upper, | |
| conformal_threshold, | |
| expected_calibration_error, | |
| ) | |
| from clsa.eval.metrics import cluster_bootstrap, harm_rate, paired_bootstrap # noqa: E402 | |
| def _rows(decs, harms, episodes, successes=None): | |
| successes = successes or [0.0] * len(decs) | |
| return [ | |
| { | |
| "state_key": f"{e}-{i}", | |
| "decision": d, | |
| "harm": float(h), | |
| "success": float(s), | |
| "irreversible": bool(h > 0.8), | |
| "episode_id": e, | |
| "tokens": 10.0, | |
| "p_hat": float(h), | |
| "irr": bool(h > 0.8), | |
| } | |
| for i, (d, h, e, s) in enumerate(zip(decs, harms, episodes, successes)) | |
| ] | |
| def test_clopper_pearson_is_monotone(): | |
| assert clopper_pearson_upper(0, 100) > 0.0 | |
| assert clopper_pearson_upper(5, 1000) < clopper_pearson_upper(50, 1000) | |
| assert clopper_pearson_upper(100, 100) == 1.0 | |
| def test_ece_zero_when_perfect_and_large_when_wrong(): | |
| p = np.array([0.0, 0.0, 1.0, 1.0]) | |
| y = np.array([0, 0, 1, 1]) | |
| assert expected_calibration_error(p, y) < 1e-9 | |
| assert expected_calibration_error(np.array([0.9, 0.9]), np.array([0, 0])) > 0.5 | |
| def test_conformal_threshold_controls_miscoverage(): | |
| safe = np.linspace(0, 0.9, 200) | |
| t = conformal_threshold(safe, alpha=0.1) | |
| assert 0.0 < t <= 0.9 | |
| assert (safe > t).mean() <= 0.1 + 1e-9 | |
| def test_paired_bootstrap_is_deterministic_and_signed(): | |
| eps = ["e0", "e0", "e1", "e1", "e2", "e2"] | |
| a = _rows(["execute"] * 6, [0, 0, 0, 1, 0, 1], eps) | |
| b = _rows(["execute"] * 6, [1, 1, 1, 1, 0, 1], eps) | |
| r1 = paired_bootstrap(a, b, harm_rate, n_boot=200, seed=3) | |
| r2 = paired_bootstrap(a, b, harm_rate, n_boot=200, seed=3) | |
| assert r1["diff"] == r2["diff"] | |
| assert r1["diff"] < 0, "the first arm should show lower harm in this fixture" | |
| def test_cluster_bootstrap_reports_cluster_count(): | |
| rows = _rows( | |
| ["execute"] * 6, [0, 1, 0, 1, 0, 1], ["e0", "e0", "e1", "e1", "e2", "e2"] | |
| ) | |
| out = cluster_bootstrap(rows, harm_rate, n_boot=200, seed=1) | |
| assert out["n"] == len(rows) | |
| assert out["lo"] <= out["value"] <= out["hi"] | |