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,122 @@
|
||||
"""Safety and scientific-isolation checks for the offline assembly study."""
|
||||
|
||||
import copy
|
||||
import importlib
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
|
||||
np = pytest.importorskip("numpy")
|
||||
pytest.importorskip("cv2")
|
||||
pytest.importorskip("shapely")
|
||||
pytest.importorskip("scipy")
|
||||
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))
|
||||
from shapely.geometry import box, mapping, shape # noqa: E402
|
||||
from shapely.ops import unary_union # noqa: E402
|
||||
|
||||
study = importlib.import_module("object_assembly_study")
|
||||
|
||||
|
||||
def policy():
|
||||
return dict(
|
||||
mpp=12 / 512,
|
||||
min_length=0.35,
|
||||
max_length=12,
|
||||
min_width=0.045,
|
||||
max_width=1.2,
|
||||
min_aspect=2,
|
||||
min_fill=0.4,
|
||||
max_pair_angle_deg=6,
|
||||
max_pair_lateral_fraction=0.25,
|
||||
max_pair_width_ratio=1.8,
|
||||
max_join_gap=0.15,
|
||||
max_group=12,
|
||||
min_group_fill=0.55,
|
||||
min_total_weak_paint=0.85,
|
||||
min_added_weak_paint=0.9,
|
||||
min_physical_valid=0.97,
|
||||
)
|
||||
|
||||
|
||||
def candidate(poly, name, value=1):
|
||||
return dict(id=name, geometry=mapping(poly), features=[value] * 22, fixed_admitted=True)
|
||||
|
||||
|
||||
def test_supported_bridge_requires_observation_and_physical_validity():
|
||||
source = unary_union([box(100, 100, 150, 110), box(154, 100, 204, 110)])
|
||||
valid = np.ones((512, 512), bool)
|
||||
weak = valid.copy()
|
||||
fitted, verdict = study.supported_envelope(source, weak, valid, policy())
|
||||
assert verdict == "supported"
|
||||
assert fitted.bounds == source.bounds
|
||||
weak[100:111, 151:154] = False
|
||||
assert study.supported_envelope(source, weak, valid, policy())[1] == "added_paint"
|
||||
valid[100:111, 149:155] = False
|
||||
assert study.supported_envelope(source, np.ones_like(valid), valid, policy())[1] == "physical"
|
||||
|
||||
|
||||
def test_exact_union_preserves_holes_and_original_candidates():
|
||||
p = box(100, 100, 180, 110).difference(box(130, 102, 140, 108))
|
||||
q = box(160, 100, 204, 110)
|
||||
original = [candidate(p, "p"), candidate(q, "q")]
|
||||
snapshot = copy.deepcopy(original)
|
||||
rgb = np.full((512, 512, 3), 180, np.uint8)
|
||||
fields = {
|
||||
k: np.zeros((512, 512), np.float32)
|
||||
for k in ["vehicle", "road", "roadway", "sidewalk", "road marking", "crosswalk"]
|
||||
}
|
||||
fields["alpha"] = np.ones((512, 512), np.float32)
|
||||
out, _ = study.assemble(original, rgb, fields, policy())
|
||||
unions = [
|
||||
shape(c["geometry"]) for c in out if c.get("properties", {}).get("kind") == "exact_union"
|
||||
]
|
||||
assert len(unions) == 1
|
||||
assert unions[0].symmetric_difference(p.union(q)).area < 1e-8
|
||||
assert len(unions[0].interiors) == 1
|
||||
assert original == snapshot
|
||||
assert [c["id"] for c in out[:2]] == ["p", "q"]
|
||||
|
||||
|
||||
def test_adjacent_crosswalk_bars_are_not_joined():
|
||||
p, q = box(100, 100, 300, 110), box(100, 116, 300, 126)
|
||||
assert not study.pair_allowed(
|
||||
p, q, study.rectangle(p, 12 / 512), study.rectangle(q, 12 / 512), policy()
|
||||
)
|
||||
|
||||
|
||||
def test_merged_reference_objects_are_counted_even_with_one_match():
|
||||
p, q = box(0, 0, 10, 100), box(12, 0, 22, 100)
|
||||
review = {"objects": [dict(geometry=mapping(t), complete=True) for t in [p, q]]}
|
||||
assert study.merged_count([candidate(p.union(q).convex_hull, "bad")], review) == 1
|
||||
assert study.merged_count([candidate(p, "p"), candidate(q, "q")], review) == 0
|
||||
|
||||
|
||||
def test_control_geometry_and_labels_cannot_change_fit_or_selection():
|
||||
records, review = [], {}
|
||||
for split in ["train", "validation", "heldout"]:
|
||||
cs = [
|
||||
candidate(box(10 + i * 20, 10, 20 + i * 20, 30), str(i), float(i % 2)) for i in range(6)
|
||||
]
|
||||
variants = {
|
||||
k: copy.deepcopy(cs) for k in ["retained_on", "retained_off", "hard_off", "hard_on"]
|
||||
}
|
||||
records.append(dict(id=split, split=split, variants=variants))
|
||||
review[split] = dict(
|
||||
objects=[
|
||||
dict(id=str(i), family="stripe", complete=True, geometry=cs[i]["geometry"])
|
||||
for i in [1, 3, 5]
|
||||
],
|
||||
negative=[[(0, 0), (200, 0), (200, 100), (0, 100)]],
|
||||
)
|
||||
first = study.fit_models(records, review, [0.1, 0.5, 0.9])
|
||||
records[-1]["variants"] = {"unreadable": "never accessed"}
|
||||
review["heldout"] = {"corrupted": True}
|
||||
second = study.fit_models(records, review, [0.1, 0.5, 0.9])
|
||||
assert first == second
|
||||
Reference in New Issue
Block a user