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,135 @@
|
||||
import copy
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
import numpy as np
|
||||
import pytest
|
||||
|
||||
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "simulation/semantic-learning"))
|
||||
import coverage_mass_control as m
|
||||
|
||||
|
||||
def records():
|
||||
out = []
|
||||
for group in ["a", "b"]:
|
||||
for j in range(2):
|
||||
old = np.array([[1, 1, 0, 255], [1, 0, 255, 255]], dtype="uint8")
|
||||
out.append(
|
||||
dict(
|
||||
name=f"{group}{j}",
|
||||
sample_id=f"id-{group}{j}",
|
||||
group=group,
|
||||
split="train",
|
||||
target=old.copy(),
|
||||
base_target=old,
|
||||
)
|
||||
)
|
||||
new = np.full((2, 4), 255, dtype="uint8")
|
||||
out.append(
|
||||
dict(
|
||||
name="new",
|
||||
sample_id="id-new",
|
||||
group="a",
|
||||
split="train",
|
||||
target=new.copy(),
|
||||
base_target=new,
|
||||
)
|
||||
)
|
||||
return out
|
||||
|
||||
|
||||
def test_zero_add_preserves_exact_weights_including_zero_window():
|
||||
rr = records()
|
||||
_, base = m.prepare(rr, "base")
|
||||
for policy in ["mass", "equal_window"]:
|
||||
_, ww = m.prepare(rr, policy)
|
||||
for name in base:
|
||||
assert np.array_equal(ww[name], base[name])
|
||||
_, small = m.prepare(rr[:-1], "base")
|
||||
assert all(np.array_equal(v, base[n]) for n, v in small.items())
|
||||
|
||||
|
||||
def test_positive_mass_and_background_are_preserved_without_small_window_bonus():
|
||||
rr = records()
|
||||
rr[-1]["target"][0, 0] = 1
|
||||
_, old = m.prepare(rr, "base")
|
||||
_, ww = m.prepare(rr, "mass")
|
||||
_, equal = m.prepare(rr, "equal_window")
|
||||
assert ww["new"].sum() == pytest.approx(0.25 / 7)
|
||||
assert equal["new"].sum() == pytest.approx(0.25 / 3)
|
||||
for group in ["a", "b"]:
|
||||
g = [r for r in rr if r["group"] == group]
|
||||
assert sum(float(ww[r["name"]][r["target"] == 1].sum()) for r in g) == pytest.approx(0.25)
|
||||
for r in g:
|
||||
bg = r["base_target"] == 0
|
||||
assert np.array_equal(ww[r["name"]][bg], old[r["name"]][bg])
|
||||
assert not ww[r["name"]][r["target"] == 255].any()
|
||||
for r in rr[:2]:
|
||||
known = r["base_target"] == 1
|
||||
np.testing.assert_allclose(ww[r["name"]][known], old[r["name"]][known] * 6 / 7)
|
||||
|
||||
|
||||
def test_controls_do_not_mutate_targets_or_depend_on_window_order():
|
||||
rr = records()
|
||||
rr[0]["target"][1, 2] = 1
|
||||
before = copy.deepcopy(rr)
|
||||
for policy in m.POLICIES:
|
||||
a, wa = m.prepare(rr, policy)
|
||||
b, wb = m.prepare(list(reversed(rr)), policy)
|
||||
assert [r["sample_id"] for r in a] == [r["sample_id"] for r in b]
|
||||
for n in wa:
|
||||
assert np.array_equal(wa[n], wb[n])
|
||||
for a, b in zip(rr, before, strict=True):
|
||||
assert np.array_equal(a["target"], b["target"])
|
||||
assert np.array_equal(a["base_target"], b["base_target"])
|
||||
|
||||
|
||||
def test_augmentation_does_not_shift_when_a_window_is_added():
|
||||
rr = records()
|
||||
assert m.schedule_digest(rr, 7, 120) == m.schedule_digest(rr[::-1], 7, 120)
|
||||
for step in range(120):
|
||||
small = {r["sample_id"]: m.rotation(7, step, r["sample_id"]) for r in rr[:-1]}
|
||||
full = {r["sample_id"]: m.rotation(7, step, r["sample_id"]) for r in rr}
|
||||
assert all(full[k] == v for k, v in small.items())
|
||||
assert len({m.rotation(7, step, rr[0]["sample_id"]) for step in range(120)}) == 4
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"bad",
|
||||
[
|
||||
"split",
|
||||
"duplicate",
|
||||
"old_changed",
|
||||
"background_added",
|
||||
"shape",
|
||||
"class",
|
||||
"dtype",
|
||||
"unsupported",
|
||||
"no_old_paint",
|
||||
],
|
||||
)
|
||||
def test_invalid_or_leaking_inputs_are_rejected(bad):
|
||||
rr = records()
|
||||
policy = "mass"
|
||||
if bad == "split":
|
||||
rr[0]["split"] = "regression"
|
||||
if bad == "duplicate":
|
||||
rr.append(copy.deepcopy(rr[0]))
|
||||
if bad == "old_changed":
|
||||
rr[0]["target"][0, 0] = 0
|
||||
if bad == "background_added":
|
||||
rr[-1]["target"][0, 0] = 0
|
||||
if bad == "shape":
|
||||
rr[0]["target"] = rr[0]["target"][:1]
|
||||
if bad == "class":
|
||||
rr[0]["target"][0, 3] = 2
|
||||
if bad == "dtype":
|
||||
rr[0]["target"] = rr[0]["target"].astype("int16")
|
||||
if bad == "unsupported":
|
||||
policy = "other"
|
||||
if bad == "no_old_paint":
|
||||
for r in rr[:2]:
|
||||
r["base_target"][r["base_target"] == 1] = 255
|
||||
r["target"][:] = r["base_target"]
|
||||
with pytest.raises(ValueError):
|
||||
m.prepare(rr, policy)
|
||||
Reference in New Issue
Block a user