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,83 @@
|
||||
"""Pinned source-reviewed label withdrawal; no class inference or model input."""
|
||||
|
||||
import hashlib
|
||||
import math
|
||||
|
||||
import numpy as np
|
||||
from PIL import Image, ImageDraw
|
||||
|
||||
|
||||
def withdraw_labels(sample, review, target):
|
||||
if (
|
||||
review["name"] != sample["name"]
|
||||
or review["input_sha256"] != sample["input"]["rgb"]["sha256"]
|
||||
):
|
||||
raise ValueError("Pinned raw input required")
|
||||
if (
|
||||
target.dtype != np.uint8
|
||||
or target.ndim != 2
|
||||
or list(target.shape[::-1]) != sample["size"]
|
||||
or not np.isin(target, [0, 1, 255]).all()
|
||||
):
|
||||
raise ValueError("Aligned uint8 target required")
|
||||
actual = hashlib.sha256(target.tobytes()).hexdigest()
|
||||
if actual != sample["target_sha256"] or review["target_sha256"] != actual:
|
||||
raise ValueError("Pinned target changed")
|
||||
expected = {c["id"]: c["sha256"] for c in sample["components"]}
|
||||
if review["reviewed_components"] != expected or not review["raw_first"]:
|
||||
raise ValueError("Complete raw-first component review required")
|
||||
if not isinstance(review["reason"], str) or not review["reason"].strip():
|
||||
raise ValueError("Explicit visual evidence required")
|
||||
zones = review["withdrawals"]
|
||||
if not isinstance(zones, list) or len(zones) > 64:
|
||||
raise ValueError("Bounded explicit withdrawal zones required")
|
||||
changed = np.zeros(target.shape, dtype=bool)
|
||||
ids, counts = set(), []
|
||||
width, height = sample["size"]
|
||||
for zone in zones:
|
||||
if (
|
||||
not isinstance(zone["id"], str)
|
||||
or not zone["id"]
|
||||
or zone["id"] in ids
|
||||
or type(zone["label"]) is not int
|
||||
or zone["label"] not in (0, 1)
|
||||
or not isinstance(zone["reason"], str)
|
||||
or not zone["reason"].strip()
|
||||
):
|
||||
raise ValueError("Unique zone, old class and visual reason required")
|
||||
ids.add(zone["id"])
|
||||
polygon = zone["polygon"]
|
||||
if not isinstance(polygon, list) or not 3 <= len(polygon) <= 16:
|
||||
raise ValueError("Explicit bounded native-pixel polygon required")
|
||||
for point in polygon:
|
||||
if (
|
||||
not isinstance(point, list)
|
||||
or len(point) != 2
|
||||
or any(type(v) not in (int, float) or not math.isfinite(v) for v in point)
|
||||
or not 0 <= point[0] < width
|
||||
or not 0 <= point[1] < height
|
||||
):
|
||||
raise ValueError("Polygon outside native image")
|
||||
canvas = Image.new("1", (width, height))
|
||||
ImageDraw.Draw(canvas).polygon([tuple(p) for p in polygon], fill=1)
|
||||
part = np.asarray(canvas, dtype=bool) & (target == zone["label"])
|
||||
if not part.any():
|
||||
raise ValueError("Withdrawal must touch explicit known labels")
|
||||
counts.append(
|
||||
dict(
|
||||
id=zone["id"],
|
||||
label=zone["label"],
|
||||
selected=int(part.sum()),
|
||||
newly_withdrawn=int((part & ~changed).sum()),
|
||||
)
|
||||
)
|
||||
changed |= part
|
||||
revised = target.copy()
|
||||
revised[changed] = 255
|
||||
return revised, dict(
|
||||
zones=counts,
|
||||
removed_paint=int((changed & (target == 1)).sum()),
|
||||
removed_background=int((changed & (target == 0)).sum()),
|
||||
target_sha256=actual,
|
||||
revised_sha256=hashlib.sha256(revised.tobytes()).hexdigest(),
|
||||
)
|
||||
Reference in New Issue
Block a user