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

107 lines
3.4 KiB
Python

"""Focused checks for the offline object study and opt-in proposal policy."""
import importlib.util
import sys
from pathlib import Path
import pytest
np = pytest.importorskip("numpy")
cv2 = pytest.importorskip("cv2")
pytest.importorskip("shapely")
pytest.importorskip("scipy")
ROOT = Path(__file__).resolve().parents[1]
for path in [
ROOT / "simulation/semantic-learning",
ROOT / "simulation/urban-plan-research/systematic",
ROOT / "simulation/urban-plan-research/marking_pass",
]:
sys.path.insert(0, str(path))
spec = importlib.util.spec_from_file_location(
"object_study_test", ROOT / "simulation/semantic-learning/object_proposal_study.py"
)
m = importlib.util.module_from_spec(spec)
spec.loader.exec_module(m)
from shapely.geometry import box, mapping # noqa: E402
def candidate(poly, **kw):
return dict(geometry=mapping(poly), **kw)
def test_unknown_is_never_a_negative():
review = dict(
objects=[dict(geometry=mapping(box(0, 0, 1, 1)), complete=True, id="p", family="stripe")],
negative=[[(3, 3), (5, 3), (5, 5), (3, 5)]],
)
cs = [candidate(box(0, 0, 1, 1)), candidate(box(3.2, 3.2, 4, 4)), candidate(box(8, 8, 9, 9))]
assert m.labels(cs, review).tolist() == [1, 0, -1]
def test_one_proposal_cannot_recall_two_objects():
review = dict(
objects=[
dict(geometry=mapping(box(0, 0, 1, 1)), complete=True, id=str(i), family="stripe")
for i in range(2)
],
negative=[],
)
assert m.matched([candidate(box(0, 0, 1, 1))], review)["found"] == 1
def test_heldout_cannot_change_model_or_selection():
review, records = {}, []
for split in ["train", "validation", "heldout"]:
objects = [
dict(
geometry=mapping(box(i * 3, 0, i * 3 + 1, 1)),
complete=True,
id=str(i),
family="stripe",
)
for i in range(2)
]
review[split] = dict(objects=objects, negative=[[(0, 3), (6, 3), (6, 6), (0, 6)]])
cs = [
candidate(box(i * 3, 0, i * 3 + 1, 1), features=[1 + i * 0.1] * 19, fixed_admitted=True)
for i in range(2)
]
cs += [
candidate(
box(i * 3, 4, i * 3 + 1, 5), features=[-1 - i * 0.1] * 19, fixed_admitted=False
)
for i in range(2)
]
records.append(dict(id=split, split=split, variants=dict(hard=cs, retained=cs)))
a = m.fit_models(records, review, [0.1, 0.5, 0.9])
review["heldout"] = None
b = m.fit_models(records, review, [0.1, 0.5, 0.9])
assert a == b
def test_retaining_context_does_not_bypass_physical_validity():
image = np.full((512, 512, 3), 45, np.uint8)
image[245:253, 100:400] = 210
zero = np.zeros((512, 512), np.float32)
g = dict(
view=np.array([[1, 0, 0, -6], [0, -1, 0, 6], [0, 0, -1, 10], [0, 0, 0, 1]], float),
intrinsics=np.array([[100, 0, 600], [0, 100, 600], [0, 0, 1]], float),
)
args = (
image,
zero,
g,
zero,
zero,
np.zeros((120, 120), bool),
[0, 0],
[12, 12],
{"id": "test"},
np.full((120, 120), 100.0),
)
assert m.collect(*args, span_m=12) == ([], [])
assert m.collect(*args, span_m=12, retain_uncertain=True)[0]
assert m.collect(*args, span_m=12, retain_uncertain=True, valid=zero) == ([], [])
with pytest.raises(ValueError):
m.collect(*args, span_m=0)