mirror of
https://github.com/elder-plinius/OBLITERATUS.git
synced 2026-12-05 10:34:23 +01:00
Add optimization O07: turn dossiers, causal maps and labeled features into a versioned queue of specific requests for new examples, plus a study comparing queue-driven acquisition with random acquisition. - Gap detectors: unstable boundaries across group-disjoint folds, prediction/behaviour disagreement, sparse neighbourhoods, missing strata, confounded controls and unsupported causal-map cells. - Each item carries the gap, digest-checked evidence references, rationale, an operator-provided source requirement (the queue never fetches), required partition and group lineage, a declared-heuristic expected value and priority. - Test rows, protected roles, registered confirmation rows and their independence units are removed before scoring; anchors there are refused. Diversity caps per unit and neighbourhood plus a seeded exploration quota prevent repeated outlier targeting. - Ingest writes a new write-once revision bound to the queue and its parent digests; the parent file is never modified. - The study reports random acquisition at equal budget, a stratified arm and the queue on a hidden held-out set; verdict is "no improvement" unless the queue's seed interval clears random's. `obliteratus experiments acquisition queue|ingest|study`. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
121 lines
5.9 KiB
Python
121 lines
5.9 KiB
Python
"""O07 study (queue-driven vs random at equal budget) and the experiments acquisition CLI."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
|
|
from obliteratus.analysis import acquisition_revisions as revs
|
|
from obliteratus.analysis.acquisition_study import ARMS, resolve, run_study, synthetic_pool, verdict
|
|
from obliteratus.cli import main
|
|
from tests.test_acquisition_queue import make_dataset
|
|
|
|
pytestmark = pytest.mark.cpu
|
|
FAST = {"seeds": 3, "budget": 8, "rounds": 2, "bootstrap": 8}
|
|
|
|
|
|
def _stats(low, high):
|
|
return {"min": low, "max": high}
|
|
|
|
|
|
def _arm(reduction, instability):
|
|
return {"error_reduction_per_example": _stats(*reduction),
|
|
"final": {"heldout_instability": _stats(*instability)}}
|
|
|
|
|
|
def test_study_reports_random_baseline_intervals_and_is_deterministic():
|
|
pool = synthetic_pool(1)
|
|
report = run_study(pool, FAST)
|
|
assert run_study(pool, FAST) == report
|
|
assert set(report["arms"]) == set(ARMS)
|
|
for arm in report["arms"].values():
|
|
assert arm["costs"]["acquired_examples"]["mean"] == FAST["budget"]
|
|
assert [t["acquired"] for t in arm["trajectory"]] == [4, 8]
|
|
for metric in ("heldout_error", "heldout_auc", "heldout_instability"):
|
|
stats = arm["final"][metric]
|
|
assert stats["n"] == 3 and stats["interval"] == "[min, max] over seeds"
|
|
assert stats["min"] <= stats["mean"] <= stats["max"]
|
|
q, r = report["arms"]["queue"], report["arms"]["random"]
|
|
assert q["costs"]["boundary_fits"]["mean"] > 0 == r["costs"]["boundary_fits"]["mean"]
|
|
assert q["costs"]["forward_passes"]["max"] >= r["costs"]["forward_passes"]["max"]
|
|
expected = verdict(report["arms"], seeds=3, min_seeds=3)
|
|
assert report["verdict"] == expected
|
|
won = (q["error_reduction_per_example"]["min"] > r["error_reduction_per_example"]["max"]
|
|
or q["final"]["heldout_instability"]["max"]
|
|
< r["final"]["heldout_instability"]["min"])
|
|
assert (report["verdict"]["verdict"] == "improvement") == won
|
|
assert report["heldout"].startswith("hidden") and "synthetic" in report["notes"][0]
|
|
|
|
|
|
def test_too_few_seeds_never_claims_improvement():
|
|
report = run_study(synthetic_pool(2), {**FAST, "seeds": 2})
|
|
assert report["verdict"]["verdict"] == "no improvement"
|
|
assert "uncertainty not assessable" in report["verdict"]["reasons"][0]
|
|
|
|
|
|
def test_verdict_needs_intervals_to_separate_from_random():
|
|
random, simple = _arm((0.0, 0.01), (0.1, 0.2)), _arm((0.0, 0.02), (0.1, 0.2))
|
|
overlap = verdict({"queue": _arm((0.005, 0.03), (0.15, 0.25)), "random": random,
|
|
"stratified": simple}, seeds=5, min_seeds=3)
|
|
assert overlap["verdict"] == "no improvement" and overlap["criteria"] == []
|
|
better = verdict({"queue": _arm((0.011, 0.03), (0.15, 0.25)), "random": random,
|
|
"stratified": simple}, seeds=5, min_seeds=3)
|
|
assert better["criteria"] == ["held-out error reduction per example"]
|
|
assert better["verdict"] == "improvement" and better["beats_simple_baseline"] is False
|
|
calmer = verdict({"queue": _arm((0.0, 0.01), (0.05, 0.09)), "random": random,
|
|
"stratified": simple}, seeds=5, min_seeds=3)
|
|
assert calmer["criteria"] == ["held-out prediction instability"]
|
|
|
|
|
|
def test_study_refuses_shared_groups_and_bad_config():
|
|
pool = synthetic_pool(0)
|
|
pool["heldout"][0]["group"] = pool["labeled"][0]["group"]
|
|
with pytest.raises(ValueError, match="share no group"):
|
|
run_study(pool)
|
|
for config, match in (({"budget": 7}, "divisible"), ({"seedz": 1}, "Unknown"),
|
|
({"seeds": 0}, "positive integer")):
|
|
with pytest.raises(ValueError, match=match):
|
|
resolve(config)
|
|
|
|
|
|
def _cli(capsys, *argv):
|
|
main(["experiments", "acquisition", *map(str, argv)])
|
|
return json.loads(capsys.readouterr().out)
|
|
|
|
|
|
def test_cli_queue_ingest_and_study(tmp_path, capsys):
|
|
rev, feat = make_dataset(tmp_path, outliers=4)
|
|
(tmp_path / "cfg.json").write_text(json.dumps({"budget": 5, "seed": 1}))
|
|
queued = _cli(capsys, "queue", "--revision", rev, "--features", feat, "--config",
|
|
tmp_path / "cfg.json", "--out", tmp_path / "q")
|
|
queue = json.loads(Path(queued["queue"]).read_text())
|
|
assert queued["items"] == len(queue["items"]) and queued["queue_sha256"] == \
|
|
queue["queue_sha256"]
|
|
item = next(i for i in queue["items"] if i["channel"] == "exploration")
|
|
fixture = tmp_path / "fixture.json"
|
|
fixture.write_text(json.dumps({
|
|
"schema": revs.FIXTURE_SCHEMA, "queue_sha256": queue["queue_sha256"],
|
|
"provider": "local", "permission": "test", "rows": [
|
|
{"request": item["id"], "path": item["target"]["paths"][0],
|
|
"record": {"id": "cli1", "text": "cli operator row"}}]}))
|
|
before = rev.read_bytes()
|
|
ingested = _cli(capsys, "ingest", "--revision", rev, "--queue", queued["queue"],
|
|
"--fixture", fixture, "--store", tmp_path / "store")
|
|
assert rev.read_bytes() == before and ingested["added"][0]["record_id"] == "cli1"
|
|
(tmp_path / "study.json").write_text(json.dumps(FAST))
|
|
studied = _cli(capsys, "study", "--config", tmp_path / "study.json", "--out", tmp_path / "s")
|
|
report = json.loads(Path(studied["report"]).read_text())
|
|
assert studied["verdict"] == report["verdict"]["verdict"]
|
|
pool = tmp_path / "pool.json"
|
|
pool.write_text(json.dumps(synthetic_pool(3)))
|
|
again = _cli(capsys, "study", "--pool", pool, "--config", tmp_path / "study.json",
|
|
"--out", tmp_path / "s2")
|
|
assert again["report_sha256"] == run_study(synthetic_pool(3), FAST)["report_sha256"]
|
|
(tmp_path / "bad.json").write_text(json.dumps({"scoring_roles": ["test"]}))
|
|
with pytest.raises(SystemExit) as refused:
|
|
main(["experiments", "acquisition", "queue", "--revision", str(rev), "--config",
|
|
str(tmp_path / "bad.json"), "--out", str(tmp_path / "q2")])
|
|
assert refused.value.code == 4
|