Add portable corpus, annotation provenance, admission, review and bounded agent-cycle contracts. Preserve dataset ownership and explicit research acceptance limits.
108 lines
4.3 KiB
Python
108 lines
4.3 KiB
Python
"""Offline loss policy for two immutable annotation releases.
|
|
|
|
History cohorts are not paint-quality labels. Keep class/street/window mass fixed;
|
|
only split that mass equally between original and newly reviewed pixels.
|
|
"""
|
|
|
|
import math
|
|
|
|
import numpy as np
|
|
from spatial_decoder_probe import dense_weights
|
|
from transfer_gates import component_regressions
|
|
|
|
|
|
def release_balanced_weights(records):
|
|
if not records or any(r["split"] != "train" for r in records):
|
|
raise ValueError("Training records only")
|
|
if len({r["name"] for r in records}) != len(records):
|
|
raise ValueError("Duplicate sample")
|
|
for r in records:
|
|
t, old = r["target"], r["old_target"]
|
|
if (
|
|
t.shape != old.shape
|
|
or not set(np.unique(t)) <= {0, 1, 255}
|
|
or not set(np.unique(old)) <= {0, 1, 255}
|
|
):
|
|
raise ValueError("Invalid target contract")
|
|
if not np.array_equal(t[old != 255], old[old != 255]):
|
|
raise ValueError("Original labels changed")
|
|
weights = dense_weights(records)
|
|
for r in records:
|
|
t, old, w = r["target"], r["old_target"], weights[r["name"]]
|
|
for label in (0, 1):
|
|
mask = t == label
|
|
cohorts = [mask & (old != 255), mask & (old == 255)]
|
|
present = [part for part in cohorts if part.any()]
|
|
if len(present) == 2:
|
|
mass = w[mask].sum(dtype=np.float64)
|
|
for part in present:
|
|
w[part] = mass / 2 / part.sum()
|
|
return weights
|
|
|
|
|
|
def assess_balance(rows, seeds):
|
|
expected = {"old-linear", "new-linear"} | {
|
|
f"{policy}-{family}-{seed}"
|
|
for policy in ["uniform", "balanced"]
|
|
for family in ["head", "decoder"]
|
|
for seed in seeds
|
|
}
|
|
if not seeds or len(set(seeds)) != len(seeds):
|
|
raise ValueError("Unique preregistered seeds required")
|
|
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 arms")
|
|
groups = {r["held_out"] for r in rows}
|
|
if len(groups) != 4:
|
|
raise ValueError("Four physical streets required")
|
|
summary = {}
|
|
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("Unpaired or duplicate folds")
|
|
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("Invalid development support")
|
|
errors.append((1 - m["recall"] + m["fpr"]) / 2)
|
|
summary[arm] = dict(mean=sum(errors) / 4, worst=max(errors))
|
|
audits = []
|
|
eligible = []
|
|
policy_non_regressing = []
|
|
for family in ["head", "decoder"]:
|
|
passed = True
|
|
paired_passed = True
|
|
for seed in seeds:
|
|
arm = f"balanced-{family}-{seed}"
|
|
references = {"old-linear", "new-linear", f"uniform-{family}-{seed}"}
|
|
if family == "decoder":
|
|
references.add(f"balanced-head-{seed}")
|
|
for ref in sorted(references):
|
|
cc = component_regressions(rows, ref, [arm])
|
|
a, b = summary[arm], summary[ref]
|
|
composite = a["mean"] < b["mean"] - 1e-12 and a["worst"] <= b["worst"] + 1e-12
|
|
audits.append(dict(arm=arm, composite_pass=composite, **cc))
|
|
passed &= composite and cc["all_components_non_regressing"]
|
|
if ref == f"uniform-{family}-{seed}":
|
|
paired_passed &= composite and cc["all_components_non_regressing"]
|
|
if passed:
|
|
eligible.append("balanced-" + family)
|
|
if paired_passed:
|
|
policy_non_regressing.append("balanced-" + family)
|
|
return dict(
|
|
eligible_families=eligible,
|
|
paired_policy_pass=policy_non_regressing,
|
|
summaries=summary,
|
|
audits=audits,
|
|
all_seeds_required=True,
|
|
production_eligible=False,
|
|
control_selection=False,
|
|
)
|