Files
NODEDC_MISSION_CORE/tests/test_weak_paint_admission.py
T
DCCONSTRUCTIONS 25dc0a1b44 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.
2026-10-29 09:10:00 +03:00

83 lines
2.7 KiB
Python

import copy
import hashlib
import importlib.util
import sys
from pathlib import Path
import numpy as np
import pytest
ROOT = Path(__file__).resolve().parents[1] / "simulation/semantic-learning"
for name in ["condition_inventory", "weak_paint_admission"]:
spec = importlib.util.spec_from_file_location(name, ROOT / (name + ".py"))
mod = importlib.util.module_from_spec(spec)
sys.modules[name] = mod
spec.loader.exec_module(mod)
m = sys.modules["weak_paint_admission"]
def example():
t = np.full((16, 16), 255, dtype="uint8")
t[0, 0] = 0
t[15, 15] = 1
s = dict(
name="a",
kind="existing",
split="train",
size=[16, 16],
rgb_sha256="a" * 64,
target_sha256=hashlib.sha256(t.tobytes()).hexdigest(),
)
r = dict(
name="a",
input_sha256=s["rgb_sha256"],
target_sha256=s["target_sha256"],
boundary_ignore_px=1,
paint_fragments=[[[128, 128], [384, 128], [384, 384], [128, 384]]],
)
return s, r, t, [[0, 0], [512, 0], [512, 512], [0, 512]]
def test_only_explicit_paint_added_and_original_untouched():
s, r, t, roi = example()
before = t.copy()
t.flags.writeable = False
out, added = m.add_fragments(s, r, t, roi)
assert np.array_equal(t, before) and np.array_equal(out[t != 255], t[t != 255])
assert added.sum() == 49 and np.all(out[added] == 1) and np.array_equal(out == 0, t == 0)
assert out[4, 4] == 255 and out[5, 5] == 1 and out[3, 3] == 255
rr = m.runs(out)
rebuilt = np.full_like(out, 255)
for kind, label in [("paint", 1), ("background", 0)]:
for y, a, b in rr[kind]:
rebuilt[y, a:b] = label
assert np.array_equal(out, rebuilt)
@pytest.mark.parametrize(
"mutation", ["control", "source", "target", "conflict", "roi", "empty", "policy", "candidate"]
)
def test_invalid_admission_rejected(mutation):
s, r, t, roi = example()
r = copy.deepcopy(r)
if mutation == "control":
s["split"] = "regression"
elif mutation == "source":
r["input_sha256"] = "b" * 64
elif mutation == "target":
t[0, 1] = 1
elif mutation == "conflict":
t[8, 8] = 0
s["target_sha256"] = r["target_sha256"] = hashlib.sha256(t.tobytes()).hexdigest()
elif mutation == "roi":
roi = [[0, 0], [64, 0], [64, 64], [0, 64]]
elif mutation == "empty":
r["paint_fragments"] = [[[128, 128], [130, 128], [130, 130], [128, 130]]]
elif mutation == "policy":
r["boundary_ignore_px"] = 0
elif mutation == "candidate":
s["kind"] = "candidate"
s["target_sha256"] = r["target_sha256"] = None
with pytest.raises(ValueError):
m.add_fragments(s, r, t, roi)