"""Apply reviewed known-to-unknown withdrawals to TRAIN-only paired targets.""" import numpy as np def training_view(record, revised): if record["split"] != "train": raise ValueError("TRAIN records only") old, base = record["target"], record["base_target"] arrays = [old, base, revised] if any( a.dtype != np.uint8 or a.ndim != 2 or a.shape != old.shape or not np.isin(a, [0, 1, 255]).all() for a in arrays ): raise ValueError("Aligned uint8 targets required") if not np.array_equal(old[base != 255], base[base != 255]): raise ValueError("Parent base labels conflict") if not np.array_equal(old == 0, base == 0): raise ValueError("Parent comparison admits paint only") changed = old != revised if np.any(changed & ((old == 255) | (revised != 255))): raise ValueError("Known-to-unknown withdrawal only") for name in ["weak", "clear", "added"]: mask = record[name] if mask.dtype != bool or mask.shape != old.shape or np.any(mask & (old != 1)): raise ValueError("Pinned condition masks must select parent paint") if not np.array_equal(record["added"], (base == 255) & (old == 1)): raise ValueError("Added cohort differs from parent labels") if np.any(changed & (record["weak"] | record["added"])): raise ValueError("This fixed comparison preserves all weak and added paint") target = revised.copy() original = base.copy() original[changed] = 255 return dict( record, target=target, base_target=original, weak=record["weak"].copy(), added=record["added"].copy(), clear=record["clear"] & (target == 1), )