Files
NODEDC_MISSION_CORE/simulation/semantic-learning/final_snapshot.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

121 lines
4.5 KiB
Python

"""Deterministic review windows over final geometry, independent of mouse history."""
import copy
import math
from dataset_store import digest
from semantic_objects import object_operation, paths
from semantic_vectors import vector_operation
VECTOR = {
"curve",
"delete_vertices",
"region",
"translate",
"translate_vertices",
"duplicate_vertex",
"duplicate_marking",
}
def replay(package):
features = copy.deepcopy(package["model"]["features"])
for row in package["revisions"]:
for op in row["operations"]:
kind = op["kind"]
origin = row.get("origin", "human-edit")
f = next((f for f in features if f["id"] == op.get("feature_id")), None)
if kind in {"place", "transform", "remove", "retemplate"}:
features = object_operation(features, op, origin)
elif (
kind in VECTOR
or f
and f["properties"].get("editor_curves", {}).get(str(op.get("path")))
):
features = vector_operation(features, op, origin)
else:
if not f:
raise ValueError("Unknown feature in annotation history")
points = paths(f)[op["path"]]
i = op["index"]
if kind == "move":
points[i] = op["position"][:]
elif kind == "insert":
a, b = points[i : i + 2]
points.insert(i + 1, [a[k] + op["t"] * (b[k] - a[k]) for k in range(3)])
elif kind == "delete":
points.pop(i)
else:
raise ValueError("Unknown annotation operation")
if f["geometry"]["type"] == "Polygon":
points[-1] = points[0][:]
if {f["id"]: f["geometry"] for f in features} != {
f["id"]: f["geometry"] for f in package["edited"]["features"]
}:
raise ValueError("Replayed geometry differs from admitted edit")
return features
def segment_cells(a, b, step=10):
"""Exact segment/grid intersections: long edges need no inserted label vertices."""
cuts = {0.0, 1.0}
for k in (0, 1):
if a[k] == b[k]:
continue
lo, hi = sorted((a[k], b[k]))
for n in range(math.floor(lo / step) + 1, math.ceil(hi / step)):
cuts.add((n * step - a[k]) / (b[k] - a[k]))
ts = sorted(cuts)
for left, right in zip(ts, ts[1:], strict=False):
t = (left + right) / 2
cell = tuple(math.floor((a[k] + t * (b[k] - a[k])) / step) for k in (0, 1))
yield cell, a[2] + left * (b[2] - a[2])
yield cell, a[2] + right * (b[2] - a[2])
def plan(package, classes=None):
replay(package)
before = package["model"]["features"]
after = package["edited"]["features"]
originals = {f["id"]: f for f in before}
if classes is None:
classes = sorted({f["properties"]["class"] for f in after if originals.get(f["id"]) != f})
if not classes or any(
c not in {f["properties"]["class"] for f in before + after} for c in classes
):
raise ValueError("Unknown/empty review scope")
cells = {}
for _source, features in (("model", before), ("edited", after)):
for f in features:
if f["properties"]["class"] not in classes:
continue
# Rejected geometry remains review evidence, never positive supervision.
for ring in paths(f):
for a, b in zip(ring, ring[1:], strict=False):
for cell, z in segment_cells(a, b):
item = cells.setdefault(cell, {"features": set(), "low": z, "high": z})
item["features"].add(f["id"])
item["low"] = min(z, item["low"])
item["high"] = max(z, item["high"])
windows = [
dict(
id=f"tile-{x}-{y}",
center=[(x + 0.5) * 10, (y + 0.5) * 10, (v["low"] + v["high"]) / 2],
features=sorted(v["features"]),
)
for (x, y), v in sorted(cells.items())
]
if not windows or len(windows) > 1024:
raise ValueError("Review scope exceeds bounded snapshot budget")
return dict(
schema_version="missioncore.semantic-snapshot-plan/v1",
revision=package["revision"],
world_sha256=package["world_sha256"],
classes=sorted(classes),
stride_m=10,
width_m=12,
windows=windows,
geometry_sha256=digest([f for f in after if f["properties"]["class"] in classes]),
training_eligible=False,
)