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,212 @@
|
||||
"""Marking editing keeps rigid shape, exact heights and immutable provenance."""
|
||||
|
||||
import copy
|
||||
import math
|
||||
|
||||
import pytest
|
||||
|
||||
from k1link.simulation.ai_polygon.semantic_edits import apply_operations, paths
|
||||
from k1link.simulation.ai_polygon.semantic_objects import catalog, object_operation, pivot
|
||||
|
||||
|
||||
def place(template="zebra"):
|
||||
return dict(
|
||||
kind="place",
|
||||
feature_id="user-1234567890123456",
|
||||
template_id=template,
|
||||
position=[300.0123, -60.034, 2.5206789941457233],
|
||||
surface="gaussian-visual",
|
||||
)
|
||||
|
||||
|
||||
def document():
|
||||
return dict(
|
||||
schema_version="missioncore.urban-map/v3", source=dict(world_sha256="a" * 64), features=[]
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("template", [t["id"] for t in catalog()["templates"]])
|
||||
def test_templates_pass_existing_geometry_checks(template):
|
||||
base = document()
|
||||
op = place(template)
|
||||
result = apply_operations(base, [op])
|
||||
assert base["features"] == []
|
||||
assert all(
|
||||
f["properties"]["review_status"] == "unreviewed"
|
||||
and f["properties"]["annotation_origin"] == "human-edit"
|
||||
for f in result["features"]
|
||||
)
|
||||
assert all(
|
||||
p[2] == op["position"][2] for f in result["features"] for ring in paths(f) for p in ring
|
||||
)
|
||||
assert result == apply_operations(copy.deepcopy(base), [op], verified_history=True)
|
||||
|
||||
|
||||
def test_rigid_group_translate_rotate_and_noop():
|
||||
base = apply_operations(document(), [place()])
|
||||
snapshot = copy.deepcopy(base)
|
||||
op = dict(kind="transform", feature_id=base["features"][3]["id"], delta=[0, 0], angle=0)
|
||||
assert object_operation(base["features"], op) is base["features"]
|
||||
op.update(delta=[0.125, -0.25], angle=0.42)
|
||||
result = apply_operations(base, [op])
|
||||
assert base == snapshot
|
||||
assert len(result["features"]) == 6
|
||||
before = [p for f in base["features"] for r in paths(f) for p in r]
|
||||
after = [p for f in result["features"] for r in paths(f) for p in r]
|
||||
assert all(a[2] == b[2] for a, b in zip(before, after, strict=True))
|
||||
assert math.dist(before[0][:2], before[-1][:2]) == pytest.approx(
|
||||
math.dist(after[0][:2], after[-1][:2]), abs=1e-12
|
||||
)
|
||||
assert pivot(result["features"])[:2] != pivot(base["features"])[:2]
|
||||
rejected = apply_operations(
|
||||
result, [dict(kind="remove", feature_id=result["features"][2]["id"])]
|
||||
)
|
||||
assert all(f["properties"]["review_status"] == "rejected" for f in rejected["features"])
|
||||
assert [f["geometry"] for f in rejected["features"]] == [
|
||||
f["geometry"] for f in result["features"]
|
||||
]
|
||||
|
||||
|
||||
def test_no_marking_vertex_deformation_and_no_unknown_surface():
|
||||
base = apply_operations(document(), [place("letter_A")])
|
||||
f = base["features"][0]
|
||||
with pytest.raises(ValueError, match="целиком"):
|
||||
apply_operations(
|
||||
base, [dict(kind="move", feature_id=f["id"], path=0, index=0, position=paths(f)[0][0])]
|
||||
)
|
||||
with pytest.raises(ValueError):
|
||||
apply_operations(document(), [{**place(), "surface": "invented-ground"}])
|
||||
with pytest.raises(ValueError):
|
||||
apply_operations(base, [place("letter_A")])
|
||||
with pytest.raises(ValueError):
|
||||
apply_operations(document(), [{**place(), "position": [0, 0, float("nan")]}])
|
||||
|
||||
|
||||
def test_legacy_verified_marking_history_still_replays():
|
||||
base = apply_operations(document(), [place("straight_arrow")])
|
||||
p = paths(base["features"][0])[0][0][:]
|
||||
p[0] += 0.02
|
||||
old = dict(kind="move", feature_id=base["features"][0]["id"], path=0, index=0, position=p)
|
||||
result = apply_operations(copy.deepcopy(base), [old], verified_history=True)
|
||||
assert paths(result["features"][0])[0][0] == p
|
||||
|
||||
|
||||
def test_worker_replays_place_transform_remove_with_actual_previous_revision(monkeypatch):
|
||||
import ast
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
from k1link.simulation.ai_polygon import semantic_objects, semantic_vectors
|
||||
|
||||
monkeypatch.setitem(sys.modules, "semantic_objects", semantic_objects)
|
||||
monkeypatch.setitem(sys.modules, "semantic_vectors", semantic_vectors)
|
||||
file = Path(__file__).parents[1] / "simulation/semantic-learning/build_samples.py"
|
||||
functions = [
|
||||
n
|
||||
for n in ast.parse(file.read_text()).body
|
||||
if isinstance(n, ast.FunctionDef) and n.name in ("contexts", "centres", "apply")
|
||||
]
|
||||
scope = {}
|
||||
exec(compile(ast.Module(body=functions, type_ignores=[]), str(file), "exec"), scope)
|
||||
first = place()
|
||||
move = dict(
|
||||
kind="transform", feature_id=first["feature_id"] + "-2", delta=[0.25, 0.5], angle=0.2
|
||||
)
|
||||
package = dict(model=document(), revisions=[dict(operations=[first]), dict(operations=[move])])
|
||||
before, after, affected = scope["contexts"](package)
|
||||
expected = apply_operations(apply_operations(document(), [first]), [move])
|
||||
assert [f["geometry"] for f in after] == [f["geometry"] for f in expected["features"]]
|
||||
assert len(before) == 6 and len(after) == 6 and len(affected) == 1
|
||||
assert before[0]["geometry"] != after[0]["geometry"]
|
||||
package["revisions"].append(
|
||||
dict(operations=[dict(kind="remove", feature_id=first["feature_id"])])
|
||||
)
|
||||
_, after, affected = scope["contexts"](package)
|
||||
assert all(f["properties"]["review_status"] == "rejected" for f in after)
|
||||
|
||||
|
||||
def test_worker_coalesces_parts_of_one_placement(monkeypatch):
|
||||
import ast
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
from k1link.simulation.ai_polygon import semantic_objects, semantic_vectors
|
||||
|
||||
monkeypatch.setitem(sys.modules, "semantic_objects", semantic_objects)
|
||||
monkeypatch.setitem(sys.modules, "semantic_vectors", semantic_vectors)
|
||||
file = Path(__file__).parents[1] / "simulation/semantic-learning/build_samples.py"
|
||||
nodes = [
|
||||
n
|
||||
for n in ast.parse(file.read_text()).body
|
||||
if isinstance(n, ast.FunctionDef) and n.name in ("contexts", "apply")
|
||||
]
|
||||
scope = {}
|
||||
exec(compile(ast.Module(body=nodes, type_ignores=[]), str(file), "exec"), scope)
|
||||
first = place()
|
||||
second = dict(
|
||||
kind="transform", feature_id=first["feature_id"] + "-2", delta=[0.125, 0.25], angle=0.12
|
||||
)
|
||||
_, after, affected = scope["contexts"](
|
||||
dict(
|
||||
model=document(), revisions=[dict(origin="agent-proposal", operations=[first, second])]
|
||||
)
|
||||
)
|
||||
assert len(affected) == 1
|
||||
assert affected[0][0] == first["feature_id"]
|
||||
assert all(f["properties"]["annotation_origin"] == "agent-proposal" for f in after)
|
||||
|
||||
|
||||
def test_road_letter_artwork_and_immutable_legacy_replay():
|
||||
import hashlib
|
||||
import json
|
||||
|
||||
templates = {t["id"]: t for t in catalog()["templates"]}
|
||||
for key, expected in {
|
||||
"letter_A": "2a9a60141bbd80b3544057964526182c479775d98ce88a7fe180377f12ec886e",
|
||||
"letter_A_v2": "4c7d30d688044ffb18debef08f20e4c5aca9c025dd2e0acae8af93715133af08",
|
||||
}.items():
|
||||
raw = json.dumps(templates[key]["polygons"], sort_keys=True).encode()
|
||||
assert hashlib.sha256(raw).hexdigest() == expected
|
||||
assert templates[key]["hidden"]
|
||||
visible = [
|
||||
t["id"] for t in templates.values() if t["label"] == "Буква А" and not t.get("hidden")
|
||||
]
|
||||
assert visible == ["letter_A_v3"]
|
||||
feature = apply_operations(document(), [place("letter_A_v3")])["features"][0]
|
||||
outer, hole = feature["geometry"]["coordinates"]
|
||||
assert max(p[0] for p in outer) - min(p[0] for p in outer) == pytest.approx(3)
|
||||
assert max(p[1] for p in outer) - min(p[1] for p in outer) == pytest.approx(1.4)
|
||||
# A flat 0.25 m tip, open feet and a closed counter distinguish road artwork.
|
||||
tip = [p for p in outer if p[0] == max(v[0] for v in outer)]
|
||||
assert len(tip) == 2 and abs(tip[0][1] - tip[1][1]) == pytest.approx(0.25)
|
||||
assert len(hole) == 4
|
||||
|
||||
|
||||
def test_retemplate_is_reversible_by_history_and_preserves_slope_and_other_objects():
|
||||
base = apply_operations(
|
||||
document(),
|
||||
[place("letter_A"), {**place("linear_marking"), "feature_id": "user-9876543210987654"}],
|
||||
)
|
||||
snapshot = copy.deepcopy(base)
|
||||
op = dict(
|
||||
kind="retemplate",
|
||||
feature_id=base["features"][0]["id"],
|
||||
template_id="letter_A_v3",
|
||||
frame=[[20, 30, 4], [0, 1, 0.1], [-1, 0, 0.2]],
|
||||
)
|
||||
result = apply_operations(base, [op], origin="agent-proposal")
|
||||
assert base == snapshot
|
||||
assert result["features"][1] == base["features"][1]
|
||||
expected = catalog()["templates"]
|
||||
expected = next(t for t in expected if t["id"] == "letter_A_v3")["polygons"][0]
|
||||
assert result["features"][0]["geometry"]["coordinates"] == [
|
||||
[[20 - y, 30 + x, 4 + 0.1 * x + 0.2 * y] for x, y in ring] for ring in expected
|
||||
]
|
||||
assert result == apply_operations(base, [op], origin="agent-proposal", verified_history=True)
|
||||
for frame in (
|
||||
[[0, 0, 0], [0, 0, 0], [0, 1, 0]],
|
||||
[[0, 0, 0], [1, 0, 0], [1, 1, 0]],
|
||||
[[0, 0, 0], [1, 0, 0], [0, -1, 0]],
|
||||
):
|
||||
with pytest.raises(ValueError):
|
||||
apply_operations(base, [{**op, "frame": frame}])
|
||||
Reference in New Issue
Block a user