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,70 @@
|
||||
"""TRAIN-only grouped score thresholds. Outer/evaluation labels are not inputs."""
|
||||
|
||||
import numpy as np
|
||||
from score_diagnostic import curve, matched
|
||||
|
||||
|
||||
def nested_folds(groups):
|
||||
groups = list(groups)
|
||||
if len(groups) < 3 or any(not isinstance(g, str) or not g for g in groups):
|
||||
raise ValueError("At least three named groups required")
|
||||
if len(set(groups)) != len(groups):
|
||||
raise ValueError("Duplicate physical group")
|
||||
return [
|
||||
dict(outer=outer, inner=inner, training_groups=sorted(set(groups) - {outer, inner}))
|
||||
for outer in sorted(groups)
|
||||
for inner in sorted(set(groups) - {outer})
|
||||
]
|
||||
|
||||
|
||||
def calibrate_inner(rows, outer, groups):
|
||||
"""One threshold per family obeying every inner-street background budget.
|
||||
|
||||
Each row aggregates one held-inner street and contains only its scores and
|
||||
reviewed masks. Equal scores are indivisible; unreviewed pixels never supply
|
||||
a threshold candidate. Both families receive the same old-fixed0 budgets.
|
||||
"""
|
||||
plans = [p for p in nested_folds(groups) if p["outer"] == outer]
|
||||
if not plans or len(rows) != len(plans):
|
||||
raise ValueError("Complete inner folds for a known outer group required")
|
||||
by_group = {r["group"]: r for r in rows}
|
||||
if len(by_group) != len(rows) or set(by_group) != {p["inner"] for p in plans}:
|
||||
raise ValueError("Inner streets must exclude outer and cover the permitted groups")
|
||||
arrays, masks, budgets, ids = [], [], {}, set()
|
||||
for plan in plans:
|
||||
r = by_group[plan["inner"]]
|
||||
expected = plan["training_groups"]
|
||||
if (
|
||||
sorted(r["training_groups"]) != expected
|
||||
or sorted(r["initialization_groups"]) != expected
|
||||
):
|
||||
raise ValueError("Fit and initialization must exclude both held streets")
|
||||
if r["split"] != "train" or not r["sample_ids"]:
|
||||
raise ValueError("Only identified TRAIN observations may calibrate")
|
||||
if len(set(r["sample_ids"])) != len(r["sample_ids"]) or ids.intersection(r["sample_ids"]):
|
||||
raise ValueError("Duplicate calibration observation")
|
||||
ids.update(r["sample_ids"])
|
||||
old, new = np.asarray(r["old"]), np.asarray(r["new"])
|
||||
if old.shape != new.shape:
|
||||
raise ValueError("Paired scores must align")
|
||||
pos = {"paint": r["paint"]}
|
||||
neg = {k: r[k] for k in ["original_bg", "added_bg"]}
|
||||
curve(old, pos, neg)
|
||||
curve(new, pos, neg)
|
||||
arrays.append((old, new))
|
||||
masks.append((r["paint"], neg))
|
||||
for part, mask in neg.items():
|
||||
budgets[r["group"] + "/" + part] = int(np.count_nonzero((old >= 0) & mask))
|
||||
lengths = [len(a[0]) for a in arrays]
|
||||
positive = {"paint": np.concatenate([m[0] for m in masks])}
|
||||
negative = {}
|
||||
for i, plan in enumerate(plans):
|
||||
for part in ["original_bg", "added_bg"]:
|
||||
negative[plan["inner"] + "/" + part] = np.concatenate(
|
||||
[masks[j][1][part] if i == j else np.zeros(n, bool) for j, n in enumerate(lengths)]
|
||||
)
|
||||
points = {}
|
||||
for i, origin in enumerate(["old", "new"]):
|
||||
c = curve(np.concatenate([a[i] for a in arrays]), positive, negative)
|
||||
points[origin] = matched(c, budgets)
|
||||
return dict(outer=outer, calibration_groups=sorted(by_group), budgets=budgets, **points)
|
||||
Reference in New Issue
Block a user