Files
NODEDC_MISSION_CORE/tests/test_score_diagnostic.py
T
DCCONSTRUCTIONS 25dc0a1b44 feat(semantics): add portable annotation, learning and review contracts
Add portable corpus, annotation provenance, admission, review and bounded agent-cycle contracts. Preserve dataset ownership and explicit research acceptance limits.
2026-10-29 09:10:00 +03:00

105 lines
3.7 KiB
Python

import importlib.util
from pathlib import Path
import numpy as np
import pytest
PATH = Path(__file__).resolve().parents[1] / "simulation/semantic-learning/score_diagnostic.py"
spec = importlib.util.spec_from_file_location("score_diagnostic", PATH)
m = importlib.util.module_from_spec(spec)
spec.loader.exec_module(m)
def masks(y):
y = np.array(y)
return {"overall": y == 1}, {"bg": y == 0}
def test_known_ap_auc_example():
p, n = masks([0, 0, 1, 1])
c = m.curve(np.array([0.1, 0.4, 0.35, 0.8]), p, n)
r = m.ranking(c, "overall", ["bg"])
assert r["ap"] == pytest.approx(5 / 6)
assert r["auc"] == 0.75
def test_ties_cannot_be_split_and_zero_budget_is_possible():
p, n = masks([1, 0, 1])
c = m.curve(np.array([1.0, 1.0, 0.0]), p, n)
assert m.matched(c, {"bg": 0})["counts"] == {"overall": 0, "bg": 0}
assert m.at_threshold(c, 1)["counts"] == {"overall": 1, "bg": 1}
assert m.ranking(c, "overall", ["bg"])["ap"] == pytest.approx(7 / 12)
def test_both_models_get_oracle_and_shift_does_not_improve_ranking():
p, n = masks([1, 1, 0, 0])
r = m.paired(np.array([2.0, -0.1, -0.2, -1.0]), np.array([1.0, -1.1, -1.2, -2.0]), p, n)
assert r["fixed"]["old"]["counts"]["overall"] == 1
assert r["oracle"]["old"]["counts"] == r["oracle"]["new"]["counts"] == {"overall": 2, "bg": 0}
assert r["ranking"]["old"] == r["ranking"]["new"]
def test_two_negative_budgets_prevent_swapping_errors():
s = np.array([1.0, 0.5, 0.1])
c = m.curve(
s,
{"overall": np.array([1, 0, 0], bool)},
{"old_bg": np.array([0, 1, 0], bool), "added_bg": np.array([0, 0, 1], bool)},
)
r = m.matched(c, {"old_bg": 0, "added_bg": 1})
assert r["counts"] == {"overall": 1, "old_bg": 0, "added_bg": 0}
def test_unknown_scores_cannot_change_metrics():
p, n = masks([1, 0, 255])
a = m.paired(np.array([1.0, -1.0, 10.0]), np.array([1.0, -1.0, -99.0]), p, n)
assert a["ranking"]["old"] == a["ranking"]["new"]
assert a["support"] == {"overall": 1, "bg": 1}
@pytest.mark.parametrize("scores", [[], [np.nan], [np.inf], [[1.0]]])
def test_invalid_scores_rejected(scores):
p, n = masks([1])
with pytest.raises(ValueError):
m.curve(np.array(scores), p, n)
@pytest.mark.parametrize("budget", [-1, 0.5, True, 3])
def test_invalid_budgets_rejected(budget):
p, n = masks([1, 0])
with pytest.raises(ValueError):
m.matched(m.curve(np.array([1.0, 0.0]), p, n), {"bg": budget})
def test_overlap_rejected():
with pytest.raises(ValueError):
m.curve(np.array([1.0]), {"overall": np.ones(1, bool)}, {"bg": np.ones(1, bool)})
def test_all_background_and_no_background_are_undefined():
for y in [[0, 0], [1, 1]]:
p, n = masks(y)
r = m.ranking(m.curve(np.array([1.0, -1.0]), p, n), "overall", ["bg"])
assert r["ap"] is None and r["auc"] is None
def test_exhaustive_threshold_counts_and_budget_maximality():
rng = np.random.default_rng(7)
for _ in range(30):
s = rng.integers(-3, 4, 25).astype(float)
y = rng.integers(0, 2, 25)
p, n = masks(y)
c = m.curve(s, p, n)
for t in np.unique(np.r_[s, np.inf]):
expected = {k: int(((s >= t) & v).sum()) for k, v in dict(p, **n).items()}
got = m.point(c, int(np.flatnonzero(c["thresholds"] == t)[0]))
assert got["counts"] == expected
for budget in range(int((y == 0).sum()) + 1):
got = m.matched(c, {"bg": budget})
candidates = [
int(((s >= t) & (y == 1)).sum())
for t in np.r_[np.unique(s), np.inf]
if ((s >= t) & (y == 0)).sum() <= budget
]
assert got["counts"]["overall"] == max(candidates)