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.
This commit is contained in:
@@ -0,0 +1,114 @@
|
||||
"""Training-label factorial with common held-out targets and non-compensating gates.
|
||||
|
||||
Offline diagnostic: exposed controls never select recipes; no production publication.
|
||||
"""
|
||||
|
||||
import copy
|
||||
import math
|
||||
|
||||
from transfer_gates import component_regressions
|
||||
|
||||
|
||||
def learning_view(records, coverage):
|
||||
"""Swap only training supervision, preserving the common evaluation records."""
|
||||
if coverage not in ("old", "new") or any(r["split"] != "train" for r in records):
|
||||
raise ValueError("Training records and registered coverage only")
|
||||
key = "old_target" if coverage == "old" else "target"
|
||||
return [dict(r, target=r[key]) for r in records]
|
||||
|
||||
|
||||
def relabel_parent(rows, session):
|
||||
result = copy.deepcopy(rows)
|
||||
for r in result:
|
||||
if r["arm"].startswith(("head-", "decoder-")):
|
||||
r["arm"] = "new-" + r["arm"]
|
||||
r["origin"] = session
|
||||
return result
|
||||
|
||||
|
||||
def assess(rows, seeds):
|
||||
if not seeds or len(set(seeds)) != len(seeds):
|
||||
raise ValueError("Unique preregistered seeds required")
|
||||
expected = {"old-linear", "new-linear"} | {
|
||||
f"{c}-{f}-{s}" for c in ("old", "new") for f in ("head", "decoder") for s in seeds
|
||||
}
|
||||
if not rows or any(r["phase"] != "development" for r in rows):
|
||||
raise ValueError("Development only")
|
||||
if {r["arm"] for r in rows} != expected:
|
||||
raise ValueError("Incomplete factorial")
|
||||
groups = {r["held_out"] for r in rows}
|
||||
if len(groups) != 4:
|
||||
raise ValueError("Four physical groups required")
|
||||
summaries = {}
|
||||
for arm in sorted(expected):
|
||||
rr = [r for r in rows if r["arm"] == arm]
|
||||
if len(rr) != 4 or {r["held_out"] for r in rr} != groups:
|
||||
raise ValueError("Incomplete/duplicate group")
|
||||
if any(not r["completed"] or r["held_out"] in r["fitted_groups"] for r in rr):
|
||||
raise ValueError("Incomplete or leaked fit")
|
||||
errors = []
|
||||
for r in rr:
|
||||
m = r["metrics"]
|
||||
if any(
|
||||
m[k] is None or not math.isfinite(m[k]) or not 0 <= m[k] <= 1
|
||||
for k in ("recall", "fpr")
|
||||
):
|
||||
raise ValueError("Both reviewed classes required")
|
||||
errors.append((1 - m["recall"] + m["fpr"]) / 2)
|
||||
summaries[arm] = dict(mean=sum(errors) / 4, worst=max(errors))
|
||||
audits = []
|
||||
eligible = []
|
||||
for coverage in ("old", "new"):
|
||||
for family in ("head", "decoder"):
|
||||
failures = []
|
||||
for seed in seeds:
|
||||
arm = f"{coverage}-{family}-{seed}"
|
||||
refs = {"old-linear", f"{coverage}-linear"}
|
||||
if family == "decoder":
|
||||
refs.add(f"{coverage}-head-{seed}")
|
||||
for ref in sorted(refs):
|
||||
a, b = summaries[arm], summaries[ref]
|
||||
cc = component_regressions(rows, ref, [arm])
|
||||
composite = a["mean"] < b["mean"] - 1e-12 and a["worst"] <= b["worst"] + 1e-12
|
||||
check = dict(arm=arm, composite_pass=composite, **cc)
|
||||
audits.append(check)
|
||||
if not composite or not cc["all_components_non_regressing"]:
|
||||
failures.append(check)
|
||||
if not failures:
|
||||
eligible.append(f"{coverage}-{family}")
|
||||
return dict(
|
||||
eligible_families=eligible,
|
||||
summaries=summaries,
|
||||
audits=audits,
|
||||
all_seeds_required=True,
|
||||
production_eligible=False,
|
||||
control_selection=False,
|
||||
)
|
||||
|
||||
|
||||
def effects(rows, seeds):
|
||||
"""Paired effects in metric fractions; descriptive, no pixel-independence CI."""
|
||||
assess(rows, seeds)
|
||||
by_key = {(r["held_out"], r["arm"]): r["metrics"] for r in rows}
|
||||
result = []
|
||||
for group in sorted({r["held_out"] for r in rows}):
|
||||
for seed in seeds:
|
||||
for metric in ("recall", "fpr"):
|
||||
|
||||
def val(coverage, family, group=group, seed=seed, metric=metric):
|
||||
return by_key[(group, f"{coverage}-{family}-{seed}")][metric]
|
||||
|
||||
old_gain = val("old", "decoder") - val("old", "head")
|
||||
new_gain = val("new", "decoder") - val("new", "head")
|
||||
result.append(
|
||||
dict(
|
||||
group=group,
|
||||
seed=seed,
|
||||
metric=metric,
|
||||
old_decoder_minus_head=old_gain,
|
||||
new_decoder_minus_head=new_gain,
|
||||
interaction=new_gain - old_gain,
|
||||
new_minus_old_decoder=val("new", "decoder") - val("old", "decoder"),
|
||||
)
|
||||
)
|
||||
return result
|
||||
Reference in New Issue
Block a user