"""Is this wall finished, and if not, where does the robot go back to? Coverage from the argmax mask is the wrong number to sign a wall off on: the first mixed test set showed the argmax declaring 81% of unfinished walls done. Here a pixel counts as done only when the model calls it painted *and* its calibrated P(painted) clears the checkpoint's ``completion_threshold``, which ``train.py`` picks on validation to keep false completes under a target (``validation.MAX_FALSE_COMPLETE``). Uncertain wall counts as not done. A checkpoint without a threshold (older checkpoints, or one where no threshold met the target) never reports ``complete``: the status is ``not_calibrated`` and a person signs the wall off. Everything left over becomes touch-up regions, largest first, and a binary mask of confidently painted pixels that ``robot_planner.py --observe`` can merge into its progress grid. numpy + scipy only. """ import numpy as np from scipy import ndimage from validation import COMPLETE MAX_TOUCH_UP_REGIONS = 20 def completion_summary(wall, painted, painted_probability, threshold, uncertain=None, min_region_px=64): """Return ``(summary, confident_painted_mask, touch_up_mask)`` for one frame. ``wall`` and ``painted`` are boolean masks (painted is a subset of wall); ``painted_probability`` is the calibrated P(painted) per pixel. """ wall = np.asarray(wall, bool) painted = np.asarray(painted, bool) & wall uncertain = np.zeros_like(wall) if uncertain is None else np.asarray(uncertain, bool) & wall total = int(wall.sum()) summary = {"completion_threshold": threshold, "complete_at_fraction": COMPLETE, "min_region_px": min_region_px} if not total: empty = np.zeros_like(wall) return {**summary, "status": "no_wall_detected", "confident_painted_fraction": None, "touch_up_regions": [], "touch_up_regions_total": 0}, empty, empty if threshold is None: confident = np.zeros_like(wall) # nothing is trusted as done without a validated threshold else: confident = painted & (np.asarray(painted_probability) >= threshold) remaining = wall & ~confident fraction = float(confident.sum()) / total if threshold is None: status = "not_calibrated" else: status = "complete" if fraction >= COMPLETE else "incomplete" labels, count = ndimage.label(remaining, structure=np.ones((3, 3), bool)) regions = [] if count: sizes = np.bincount(labels.ravel(), minlength=count + 1) slices = ndimage.find_objects(labels) for index in np.argsort(-sizes[1:]) + 1: if sizes[index] < min_region_px: break ys, xs = slices[index - 1] region = labels[ys, xs] == index cy, cx = ndimage.center_of_mass(region) regions.append({ "bbox_xyxy": [xs.start, ys.start, xs.stop - 1, ys.stop - 1], "center_xy": [round(float(cx) + xs.start, 1), round(float(cy) + ys.start, 1)], "pixels": int(sizes[index]), # Why it is not done: never painted, model unsure, or painted but below the threshold. "unpainted_px": int((region & wall[ys, xs] & ~painted[ys, xs] & ~uncertain[ys, xs]).sum()), "uncertain_px": int((region & uncertain[ys, xs]).sum()), "low_confidence_painted_px": int((region & painted[ys, xs]).sum()), }) touch_up = np.zeros_like(wall) for region in regions[:MAX_TOUCH_UP_REGIONS]: x0, y0, x1, y1 = region["bbox_xyxy"] touch_up[y0:y1 + 1, x0:x1 + 1] |= remaining[y0:y1 + 1, x0:x1 + 1] summary.update({ "status": status, "confident_painted_fraction": round(fraction, 5), "argmax_painted_fraction": round(float(painted.sum()) / total, 5), "remaining_fraction": round(float(remaining.sum()) / total, 5), "touch_up_regions": regions[:MAX_TOUCH_UP_REGIONS], "touch_up_regions_total": len(regions), }) return summary, confident, touch_up