Files
NODEDC_MISSION_CORE/tests/test_annotation_boundary_review.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

93 lines
3.0 KiB
Python

import copy
import hashlib
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"))
from annotation_boundary_review import withdraw_labels
def fixture():
target = np.full((8, 9), 255, dtype="uint8")
target[1:7, 1:4] = 0
target[1:7, 5:8] = 1
h = hashlib.sha256(target.tobytes()).hexdigest()
sample = dict(
name="frame",
input=dict(rgb=dict(sha256="raw")),
size=[9, 8],
target_sha256=h,
components=[dict(id="B1", sha256="b"), dict(id="P1", sha256="p")],
)
review = dict(
name="frame",
input_sha256="raw",
target_sha256=h,
raw_first=True,
reviewed_components={"B1": "b", "P1": "p"},
reason="All regions inspected",
withdrawals=[
dict(
id="edge",
label=0,
polygon=[[0, 0], [6, 0], [6, 3], [0, 3]],
reason="Visible contradictory edge",
)
],
)
return sample, review, target
def test_only_explicit_old_class_withdrawn_without_mutating_source():
s, r, t = fixture()
before = t.copy()
got, stats = withdraw_labels(s, r, t)
assert np.array_equal(t, before)
assert np.all(got[t == 255] == 255) and np.array_equal(got[t == 1], t[t == 1])
assert stats["removed_background"] == 9 and stats["removed_paint"] == 0
assert np.array_equal(got[4:], t[4:])
def test_foreground_uncertainty_and_overlapping_zones_count_once():
s, r, t = fixture()
r["withdrawals"][0]["label"] = 1
duplicate = copy.deepcopy(r["withdrawals"][0])
duplicate["id"] = "overlap"
r["withdrawals"].append(duplicate)
got, stats = withdraw_labels(s, r, t)
assert stats["removed_paint"] == 6 and stats["removed_background"] == 0
assert stats["zones"][1]["newly_withdrawn"] == 0
assert not np.any((got != t) & (got != 255))
def test_empty_review_preserves_every_pixel():
s, r, t = fixture()
r["withdrawals"] = []
got, stats = withdraw_labels(s, r, t)
assert np.array_equal(got, t) and stats["revised_sha256"] == s["target_sha256"]
@pytest.mark.parametrize(
"change",
[
lambda s, r, t: r.update(input_sha256="other"),
lambda s, r, t: r.update(target_sha256="other"),
lambda s, r, t: s.update(target_sha256="other"),
lambda s, r, t: r.update(reviewed_components={"B1": "b"}),
lambda s, r, t: r.update(raw_first=False),
lambda s, r, t: r["withdrawals"][0].update(label=255),
lambda s, r, t: r["withdrawals"][0].update(reason=""),
lambda s, r, t: r["withdrawals"][0].update(polygon=[[0, 0], [9, 0], [0, 3]]),
lambda s, r, t: r["withdrawals"][0].update(polygon=[[0, 0], [float("nan"), 0], [0, 3]]),
lambda s, r, t: r["withdrawals"][0].update(polygon=[[0, 0], [0, 7], [0, 3]]),
],
)
def test_invalid_or_unbound_review_rejected(change):
s, r, t = fixture()
change(s, r, t)
with pytest.raises(ValueError):
withdraw_labels(s, r, t)