import importlib.util import sys from pathlib import Path import pytest SRC = Path(__file__).resolve().parents[1] / "simulation/semantic-learning" sys.path.insert(0, str(SRC)) spec = importlib.util.spec_from_file_location("decoder_probe", SRC / "decoder_adaptation_probe.py") probe = importlib.util.module_from_spec(spec) spec.loader.exec_module(probe) sys.path.remove(str(SRC)) SEEDS = [20261023, 20261024, 20261025] def fixture(): result = [] for arm in ["old-linear", "new-linear"] + [ f"{f}-{s}" for f in ["head", "decoder"] for s in SEEDS ]: error = 0.2 if arm.endswith("linear") else 0.15 if arm.startswith("head") else 0.1 for i in range(4): result.append( dict( phase="development", arm=arm, held_out=str(i), fitted_groups=[str(j) for j in range(4) if j != i], completed=True, metrics=dict(recall=1 - error, fpr=error), ) ) return result def test_decoder_requires_every_seed_and_improvement_over_paired_optimizer(): rows = fixture() assert probe.choose(rows, SEEDS)["selected_family"] == "decoder" # One weak seed cannot be hidden by selecting the other two. bad = [ dict(r, metrics=dict(recall=0.79, fpr=0.21)) if r["arm"] == f"decoder-{SEEDS[-1]}" else r for r in rows ] assert probe.choose(bad, SEEDS)["selected_family"] == "head" # Beating the linear reference alone is insufficient for decoder attribution. tied = [ dict(r, metrics=dict(recall=0.85, fpr=0.15)) if r["arm"].startswith("decoder") else r for r in rows ] assert probe.choose(tied, SEEDS)["selected_family"] == "head" def test_selection_rejects_leakage_controls_or_missing_folds(): rows = fixture() with pytest.raises(ValueError, match="leakage"): probe.choose([dict(rows[0], fitted_groups=["0", "1", "2"])] + rows[1:], SEEDS) with pytest.raises(ValueError, match="Development"): probe.choose([dict(rows[0], phase="regression")] + rows[1:], SEEDS) with pytest.raises(ValueError, match="Incomplete"): probe.choose(rows[:-1], SEEDS)