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

81 lines
3.2 KiB
Python

"""TRAIN-only coverage/mass controls with sample-stable augmentation.
No coordinates or per-street exceptions. Existing pixel labels are immutable.
"""
import hashlib
import json
import numpy as np
from spatial_decoder_probe import dense_weights
POLICIES = {"base", "equal_window", "mass"}
def prepare(records, policy):
if policy not in POLICIES or not records:
raise ValueError("Known policy and nonempty TRAIN records required")
ids, names = set(), set()
for r in records:
sid, name = r["sample_id"], r["name"]
if r["split"] != "train" or not sid or sid in ids or name in names:
raise ValueError("Unique TRAIN samples required")
ids.add(sid)
names.add(name)
t, old = r["target"], r["base_target"]
if (
t.dtype != np.uint8
or old.dtype != np.uint8
or t.ndim != 2
or t.shape != old.shape
or not np.isin(t, [0, 1, 255]).all()
or not np.isin(old, [0, 1, 255]).all()
):
raise ValueError("Aligned uint8 targets required")
if not np.array_equal(t[old != 255], old[old != 255]):
raise ValueError("Existing labels are immutable")
if not np.array_equal(t == 0, old == 0):
raise ValueError("This comparison admits paint only")
ordered = sorted(records, key=lambda r: r["sample_id"])
base = [dict(r, target=r["base_target"]) for r in ordered]
original = dense_weights(base)
if policy == "base":
return base, original
if policy == "equal_window":
return ordered, dense_weights(ordered)
weights = {name: w.copy() for name, w in original.items()}
for group in sorted({r["group"] for r in ordered}):
rr = [r for r in ordered if r["group"] == group]
old_count = sum(int((r["base_target"] == 1).sum()) for r in rr)
new_count = sum(int(((r["base_target"] == 255) & (r["target"] == 1)).sum()) for r in rr)
if not old_count:
raise ValueError("Every street needs original positive support")
if not new_count:
continue
mass = sum(
float(original[r["name"]][r["base_target"] == 1].sum(dtype=np.float64)) for r in rr
)
factor = old_count / (old_count + new_count)
for r in rr:
weights[r["name"]][r["base_target"] == 1] *= factor
added = (r["base_target"] == 255) & (r["target"] == 1)
weights[r["name"]][added] = mass / (old_count + new_count)
return ordered, weights
def rotation(seed, step, sample_id):
if not isinstance(seed, int) or not isinstance(step, int) or step < 0 or not sample_id:
raise ValueError("Integer seed/update and sample identity required")
payload = json.dumps(
["coverage-rotation-v1", seed, step, sample_id], separators=(",", ":")
).encode()
return hashlib.sha256(payload).digest()[0] % 4
def schedule_digest(records, seed, steps):
rows = [
(r["sample_id"], [rotation(seed, step, r["sample_id"]) for step in range(steps)])
for r in sorted(records, key=lambda r: r["sample_id"])
]
return hashlib.sha256(json.dumps(rows, separators=(",", ":")).encode()).hexdigest()