Preserve offline geometry, marking, replay and evaluation pipelines with source manifests and synthetic tests. Research qualification does not activate these methods as production navigation.
116 lines
4.1 KiB
Python
116 lines
4.1 KiB
Python
"""Packaging-invariant diagnostic evidence on a fixed world lattice.
|
|
|
|
This measures the set union, not object counts. It never changes geometry,
|
|
admission or confidence. Unknown observations cannot become positive evidence.
|
|
The sampler and world origin are independent of the proposed partition.
|
|
"""
|
|
|
|
import hashlib
|
|
from collections import Counter
|
|
|
|
import numpy as np
|
|
import shapely
|
|
from shapely.geometry import box
|
|
|
|
VERSION = "fixed-world-object-evidence-v1"
|
|
CELL = 0.04
|
|
TILE = 3.0
|
|
HALO = 1.0
|
|
CONTEXT = 0.15
|
|
MAX_TILES = 20000
|
|
|
|
|
|
def tile_ids(geometry):
|
|
ids = set()
|
|
for p in getattr(geometry, "geoms", [geometry]):
|
|
if p.is_empty:
|
|
continue
|
|
lo = np.floor(np.array(p.bounds[:2]) / TILE).astype(int)
|
|
hi = np.floor(np.array(p.bounds[2:]) / TILE).astype(int)
|
|
if (hi[0] - lo[0] + 1) * (hi[1] - lo[1] + 1) > MAX_TILES:
|
|
raise ValueError("sampling-budget")
|
|
for x in range(lo[0], hi[0] + 1):
|
|
for y in range(lo[1], hi[1] + 1):
|
|
if p.intersects(box(x * TILE, y * TILE, (x + 1) * TILE, (y + 1) * TILE)):
|
|
ids.add((int(x), int(y)))
|
|
if len(ids) > MAX_TILES:
|
|
raise ValueError("sampling-budget")
|
|
return sorted(ids)
|
|
|
|
|
|
def grid(key, halo=False):
|
|
n = round(TILE / CELL)
|
|
h = round(HALO / CELL) if halo else 0
|
|
ix = key[0] * n + np.arange(-h, n + h)
|
|
iy = key[1] * n + np.arange(-h, n + h)
|
|
xx, yy = np.meshgrid(ix, iy)
|
|
return np.c_[xx.ravel(), yy.ravel()], np.c_[
|
|
(xx.ravel() + 0.5) * CELL, (yy.ravel() + 0.5) * CELL
|
|
]
|
|
|
|
|
|
def counts(mask, evidence):
|
|
physical = np.asarray(evidence["physical"], bool)
|
|
road = np.asarray(evidence["road"], bool)
|
|
paint = np.asarray(evidence["paint"], bool)
|
|
assert physical.shape == road.shape == paint.shape == (2, len(mask))
|
|
known = physical.all(axis=0)
|
|
surface = known & road.all(axis=0)
|
|
positive = paint.all(axis=0)
|
|
flags = dict(
|
|
samples=np.ones(len(mask), bool),
|
|
physical_observed=known,
|
|
road_observed=surface,
|
|
paint_supported=surface & positive,
|
|
observed_nonpaint=surface & ~paint.any(axis=0),
|
|
view_disagreement=surface & (paint[0] != paint[1]),
|
|
outside_road_observed=known & ~road.all(axis=0),
|
|
unknown_physical=~known,
|
|
)
|
|
return {k: int((v & mask).sum()) for k, v in flags.items()}
|
|
|
|
|
|
def inspect(polygons, sampler):
|
|
body = shapely.union_all([shapely.force_2d(p) for p in polygons])
|
|
if not body.is_valid:
|
|
raise ValueError("invalid-geometry")
|
|
surroundings = body.buffer(CONTEXT).difference(body)
|
|
totals = dict(body=Counter(), context=Counter())
|
|
digest = hashlib.sha256()
|
|
keys = tile_ids(body.union(surroundings))
|
|
for key in keys:
|
|
ids, points = grid(key)
|
|
evidence = sampler(key)
|
|
for name, geometry in [("body", body), ("context", surroundings)]:
|
|
mask = shapely.intersects_xy(geometry, points[:, 0], points[:, 1])
|
|
# A partition boundary inside a union is never a second sample.
|
|
if name == "context":
|
|
mask &= ~shapely.intersects_xy(body, points[:, 0], points[:, 1])
|
|
totals[name].update(counts(mask, evidence))
|
|
digest.update(name.encode())
|
|
digest.update(np.asarray(ids[mask], dtype="<i8").tobytes())
|
|
for field in ["physical", "road", "paint"]:
|
|
digest.update(np.asarray(evidence[field], bool)[:, mask].tobytes())
|
|
result = {}
|
|
for name, count in totals.items():
|
|
values = dict(count)
|
|
denominator = values.get("samples", 0)
|
|
values["paint_supported_fraction"] = (
|
|
values.get("paint_supported", 0) / denominator if denominator else None
|
|
)
|
|
result[name] = values
|
|
return dict(
|
|
version=VERSION,
|
|
**result,
|
|
evidence_signature=digest.hexdigest(),
|
|
# Ring/component ordering must not change floating summation order.
|
|
geometry_area_m2=float(shapely.normalize(body).area),
|
|
tiles=len(keys),
|
|
cell_m=CELL,
|
|
origin_xy=[0.0, 0.0],
|
|
context_m=CONTEXT,
|
|
admission_changed=False,
|
|
metric_accuracy_measured=False,
|
|
complete_recall_measured=False,
|
|
)
|