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

104 lines
3.5 KiB
Python

"""Isolation, partial-pixel pooling and complete-background checks."""
import copy
import importlib
import sys
from pathlib import Path
import pytest
np = pytest.importorskip("numpy")
for dependency in ["cv2", "scipy", "shapely"]:
pytest.importorskip(dependency)
from shapely.geometry import box, mapping # noqa: E402
ROOT = Path(__file__).resolve().parents[1]
for directory in [
"simulation/semantic-learning",
"simulation/urban-plan-research/systematic",
"simulation/urban-plan-research/marking_pass",
]:
sys.path.insert(0, str(ROOT / directory))
study = importlib.import_module("visual_admission_study")
def test_thin_masks_survive_fractional_pooling():
mask = np.zeros((512, 512), bool)
mask[100:400, 103] = True
weights = study.fractional_weights(mask, (128, 128))
assert weights.sum() == pytest.approx(1)
assert np.count_nonzero(weights) == 75
assert weights[:, 25].sum() == pytest.approx(1)
with pytest.raises(ValueError):
study.fractional_weights(np.zeros_like(mask), (128, 128))
def test_complete_background_counts_false_objects_but_preserves_unknown_and_paint():
review = dict(
objects=[
dict(id="truth", family="bar", complete=True, geometry=mapping(box(10, 10, 30, 20)))
],
negative=[],
complete_background=[mapping(box(0, 0, 100, 100).difference(box(8, 8, 32, 22)))],
)
cs = [
dict(id="paint", geometry=mapping(box(10, 10, 30, 20))),
dict(id="false", geometry=mapping(box(40, 40, 60, 60))),
dict(id="unknown", geometry=mapping(box(110, 110, 130, 130))),
]
result = study.assessed(cs, review)
assert result["found"] == 1
assert result["unsupported_sparse"] == 0
assert result["false_ids"] == ["false"]
def test_feature_pooling_is_independent_of_labels():
mask = np.zeros((16, 16), bool)
mask[3:9, 7:12] = True
weights = study.fractional_weights(mask, (4, 4))
assert np.all(weights >= 0)
assert weights.sum() == pytest.approx(1)
constant = np.full((4, 4), 7.0)
assert np.sum(weights * constant) == pytest.approx(7)
def test_fit_uses_only_passed_development_records(monkeypatch):
records = [
dict(
id="dev",
variants={
"retained": [
dict(features=[0], geometry=mapping(box(0, 0, 10, 10))),
dict(features=[1], geometry=mapping(box(20, 20, 30, 30))),
dict(features=[2], geometry=mapping(box(40, 40, 50, 50))),
dict(features=[3], geometry=mapping(box(60, 60, 70, 70))),
]
},
)
]
truth = dict(
objects=[
dict(geometry=mapping(box(0, 0, 10, 10))),
dict(geometry=mapping(box(20, 20, 30, 30))),
],
negative=[[(35, 35), (80, 35), (80, 80), (35, 80)]],
)
review = {"dev": truth, "control": copy.deepcopy(truth)}
data = {
"dev": {"visual512": np.arange(8).reshape(4, 2)},
"control": {"visual512": np.full((4, 2), 999)},
}
captured = []
def fitted(x, y, l2, maxiter):
captured.append((x.copy(), y.copy()))
return {}
monkeypatch.setattr(study, "fit_classifier", fitted)
study.fit_head(records, review, data, "visual512", 0.1)
review["control"] = {"objects": [], "negative": []}
data["control"]["visual512"] *= 2
study.fit_head(records, review, data, "visual512", 0.1)
assert np.array_equal(captured[0][0], captured[1][0])
assert np.array_equal(captured[0][1], captured[1][1])