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,130 @@
|
||||
"""Coverage is explicit, source labels and unknown exclusions remain protected."""
|
||||
|
||||
import importlib.util
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
|
||||
SOURCE = Path(__file__).parents[1] / "simulation/semantic-learning"
|
||||
sys.path.insert(0, str(SOURCE))
|
||||
spec = importlib.util.spec_from_file_location(
|
||||
"tested_region_coverage", SOURCE / "region_coverage.py"
|
||||
)
|
||||
m = importlib.util.module_from_spec(spec)
|
||||
spec.loader.exec_module(m)
|
||||
|
||||
|
||||
def inputs():
|
||||
raw = bytes([255] * 80)
|
||||
packet = dict(
|
||||
size=[10, 8],
|
||||
roi=[1, 1, 9, 7],
|
||||
view="top",
|
||||
target_sha256=m.sha(raw),
|
||||
protected_sha256=m.sha(b""),
|
||||
)
|
||||
proposal = dict(
|
||||
reason="synthetic visible stripe and background",
|
||||
background_reviewed=True,
|
||||
boundary_ignore_px=1,
|
||||
paint_polygons=[[[3, 1], [7, 1], [7, 7], [3, 7]]],
|
||||
unknown_polygons=[],
|
||||
)
|
||||
return raw, packet, proposal
|
||||
|
||||
|
||||
def test_complete_region_and_no_implicit_background_elsewhere():
|
||||
original, packet, proposal = inputs()
|
||||
record = m.raster_proposal(packet, proposal)
|
||||
out, audit = m.merge(packet, record, original)
|
||||
assert audit["roi_pixels"] == 48
|
||||
assert audit["added"] == dict(paint=12, background=12)
|
||||
assert out[:10] == original[:10]
|
||||
assert out[2 * 10 + 4] == 1
|
||||
assert out[2 * 10 + 3] == 255 # uncertain boundary
|
||||
assert out[2 * 10 + 1] == 0
|
||||
|
||||
|
||||
def test_missing_pixels_and_outside_roi_are_rejected():
|
||||
original, packet, proposal = inputs()
|
||||
record = m.raster_proposal(packet, proposal)
|
||||
record["background"].pop()
|
||||
with pytest.raises(ValueError, match="Incomplete"):
|
||||
m.merge(packet, record, original)
|
||||
record = m.raster_proposal(packet, proposal)
|
||||
record["background"].insert(0, [0, 0, 1])
|
||||
with pytest.raises(ValueError, match="outside"):
|
||||
m.merge(packet, record, original)
|
||||
|
||||
|
||||
def test_existing_label_conflict_protected_unknown_and_pin():
|
||||
original, packet, proposal = inputs()
|
||||
record = m.raster_proposal(packet, proposal)
|
||||
old = bytearray(original)
|
||||
old[21] = 1
|
||||
packet["target_sha256"] = m.sha(old)
|
||||
with pytest.raises(ValueError, match="conflicts"):
|
||||
m.merge(packet, record, old)
|
||||
packet["target_sha256"] = m.sha(original)
|
||||
protected = bytearray(len(original))
|
||||
protected[21] = 1
|
||||
packet["protected_sha256"] = m.sha(protected)
|
||||
with pytest.raises(ValueError, match="protected unknown"):
|
||||
m.merge(packet, record, original, protected)
|
||||
with pytest.raises(ValueError, match="identity changed"):
|
||||
m.merge(packet, record, original)
|
||||
|
||||
|
||||
def test_explicit_background_and_finite_polygons():
|
||||
_, packet, proposal = inputs()
|
||||
proposal["background_reviewed"] = False
|
||||
with pytest.raises(ValueError, match="Explicit"):
|
||||
m.raster_proposal(packet, proposal)
|
||||
proposal["background_reviewed"] = True
|
||||
proposal["paint_polygons"][0][0] = [float("nan"), 0]
|
||||
with pytest.raises(ValueError, match="outside"):
|
||||
m.raster_proposal(packet, proposal)
|
||||
|
||||
|
||||
def test_protected_unknown_cannot_overlap_accepted_label():
|
||||
original, packet, proposal = inputs()
|
||||
original = bytearray(original)
|
||||
original[23] = 1 # New proposal marks its boundary unknown.
|
||||
packet["target_sha256"] = m.sha(original)
|
||||
protected = bytearray(len(original))
|
||||
protected[23] = 1
|
||||
packet["protected_sha256"] = m.sha(protected)
|
||||
with pytest.raises(ValueError, match="only unknown"):
|
||||
m.merge(packet, m.raster_proposal(packet, proposal), original, protected)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("known", [0, 1])
|
||||
def test_explicit_source_uncertainty_cannot_silently_keep_known_label(known):
|
||||
original, packet, proposal = inputs()
|
||||
original = bytearray(original)
|
||||
original[24] = known
|
||||
packet["target_sha256"] = m.sha(original)
|
||||
proposal["unknown_polygons"] = [[[4, 2], [5, 2], [5, 3], [4, 3]]]
|
||||
proposal["unknown_reason"] = "Source is obscured here"
|
||||
record = m.raster_proposal(packet, proposal)
|
||||
with pytest.raises(ValueError, match="Explicit source uncertainty"):
|
||||
m.merge(packet, record, original)
|
||||
|
||||
|
||||
def test_boundary_ignore_does_not_revoke_prior_known_label():
|
||||
original, packet, proposal = inputs()
|
||||
original = bytearray(original)
|
||||
original[23] = 1
|
||||
packet["target_sha256"] = m.sha(original)
|
||||
out, _ = m.merge(packet, m.raster_proposal(packet, proposal), original)
|
||||
assert out[23] == 1
|
||||
|
||||
|
||||
def test_explicit_unknown_on_unreviewed_source_stays_unknown():
|
||||
original, packet, proposal = inputs()
|
||||
proposal["unknown_polygons"] = [[[4, 2], [5, 2], [5, 3], [4, 3]]]
|
||||
proposal["unknown_reason"] = "Visible ambiguity"
|
||||
out, _ = m.merge(packet, m.raster_proposal(packet, proposal), original)
|
||||
assert out[24] == 255
|
||||
assert out[14] == 1
|
||||
Reference in New Issue
Block a user