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

102 lines
4.4 KiB
Python

"""CPU-only representation gate; synthetic contracts, NOT street/model quality."""
import argparse
import json
import time
from pathlib import Path
import numpy as np
from paint_geometry import editor_features, vectorize
from PIL import Image, ImageDraw
from shapely import contains_xy
from shapely.geometry import Polygon, shape
from shapely.ops import unary_union
def fixtures():
dash = np.zeros((64, 96), np.uint8)
for a, b in [(6, 23), (28, 43), (57, 88)]:
dash[29:35, a:b] = 1
zebra = np.zeros_like(dash)
for a in [10, 28, 46, 64, 82]:
zebra[8:56, a:a + 7] = 1
# Raster A is deliberately synthetic, with a small but explicit hole.
yy, xx = np.indices(dash.shape)
outer = Polygon([(20, 56), (43, 7), (53, 7), (77, 56),
(63, 56), (57, 43), (39, 43), (33, 56)])
hole = Polygon([(44, 31), (48, 20), (52, 31)])
letter = contains_xy(outer.difference(hole), xx + .5, yy + .5).astype(np.uint8)
clipped = np.zeros_like(dash)
clipped[27:36, :90] = 1
clipped[:, 43:50] = 255
return {"separate_dashes": dash, "zebra_bars": zebra,
"letter_hole": letter, "unknown_gap_and_edge": clipped}
def rasterize(features, shape_hw, matrix):
yy, xx = np.indices(shape_hw)
a, b, d, e, xoff, yoff = matrix
x, y = a * (xx + .5) + b * (yy + .5) + xoff, d * (xx + .5) + e * (yy + .5) + yoff
return contains_xy(unary_union([shape(f["geometry"]) for f in features]), x, y)
def run(destination):
destination = Path(destination)
destination.mkdir(parents=True, exist_ok=False)
start = time.monotonic()
matrix = [.1, 0, 0, -.1, 10, 20]
report = dict(
scope="Synthetic discrete geometry contract; no model inference or live map",
frame="pixel edges; supplied synthetic height plane only",
upstream_vma_executed=False, production_heuristic_executed=False, cases=[],
)
canvas = Image.new("RGB", (980, 670), "#111820")
draw = ImageDraw.Draw(canvas)
draw.text((16, 12), "SYNTHETIC CONTRACT CHECK - not street predictions", fill="white")
for x, label in [(16, "Input (purple = unknown)"),
(340, "Exact cells -> editor features"),
(664, "Envelope only (lossy illustration)")]:
draw.text((x, 38), label, fill="white")
for index, (name, labels) in enumerate(fixtures().items()):
proposal = vectorize(labels, pixel_to_scene=matrix, source_id="synthetic/" + name)
features = editor_features(
proposal, height_at=lambda xy: 2 + xy[:, 0] * .01,
height_source="synthetic-known-plane",
)
recovered = rasterize(features, labels.shape, matrix)
truth = labels == 1
assert np.array_equal(recovered, truth)
exact = unary_union([shape(f["geometry"]) for f in features])
# Structural illustration only, not an execution/emulation of VMA.
envelope = exact.envelope
simplified = rasterize([dict(geometry=envelope.__geo_interface__)], labels.shape, matrix)
row = dict(
case=name, foreground_pixels=int(truth.sum()), exact_pixel_errors=0,
features=len(features),
holes=sum(len(shape(f["geometry"]).interiors) for f in features),
support_boundary=[f["properties"]["support_boundary"] for f in features],
envelope_pixel_errors=int(np.count_nonzero(simplified != truth)),
proposal_sha256=proposal["proposal_sha256"],
)
report["cases"].append(row)
y = 66 + index * 146
draw.text((16, y), name, fill="white")
for x, mask in [(16, truth), (340, recovered), (664, simplified)]:
rgb = np.zeros((*labels.shape, 3), np.uint8)
rgb[:] = [28, 38, 49]
rgb[mask] = [246, 215, 100]
if x == 16:
rgb[labels == 255] = [136, 91, 185]
# 96x64 to 288x112 display; dimensions are shared across columns.
im = Image.fromarray(rgb).resize((288, 112), Image.Resampling.NEAREST)
canvas.paste(im, (x, y + 18))
report["seconds"] = time.monotonic() - start
(destination / "gate.json").write_text(json.dumps(report, indent=2) + "\n")
canvas.save(destination / "geometry-proof.png")
return report
if __name__ == "__main__":
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("destination", type=Path)
print(json.dumps(run(parser.parse_args().destination), indent=2))