#!/usr/bin/env python3 """Smoke-sweep table: each title's board run against PicoDrive (detector). For each saved board composite (board/SLUG/fNNNNN.png) the closest PicoDrive frame within +-WINDOW frames is found and the share of differing pixels reported; a frame of one colour is marked blank. Run from the repository root with the venv's python: .venv/bin/python reference/m6/triage.py [--window 90] [--tolerance 12] """ from __future__ import annotations import argparse import os import re import sys from pathlib import Path sys.path.insert(0, "scripts") from compare_frames import compare # noqa: E402 from megadrive_python.png import read_png # noqa: E402 M = Path(os.environ.get("SMOKE_DIR", "reference/m6")) def board_status(slug: str) -> str: log = M / "logs" / f"{slug}-board.log" text = log.read_text(errors="replace") if log.exists() else "" if "Lockup" in text or "Traceback" in text: last = [ln for ln in text.splitlines() if ln.strip()][-2:] return "ERROR: " + " | ".join(last)[:140] match = re.search(r"EXIT (\d+) (\d+) s", text) return f"exit {match[1]} in {match[2]} s" if match else "running" def main() -> int: parser = argparse.ArgumentParser() parser.add_argument("--window", type=int, default=90) parser.add_argument("--tolerance", type=int, default=12) parser.add_argument("--detector", default="pico", help="pico or blastem") parser.add_argument("only", nargs="*", help="substrings of the titles to triage") args = parser.parse_args() for board_dir in sorted((M / "board").iterdir()): slug = board_dir.name if args.only and not any(s in slug for s in args.only): continue print(f"== {slug}: {board_status(slug)}") pico = M / args.detector / slug for frame_png in sorted(board_dir.glob("f[0-9][0-9][0-9][0-9][0-9].png")): n = int(frame_png.stem[1:]) board = read_png(frame_png) flat = {p for row in board for p in row} best = None for m in range(max(2, n - args.window), n + args.window + 1, 2): path = pico / f"f{m:05d}.png" if not path.exists(): continue theirs = read_png(path) h = min(len(board), len(theirs)) w = min(len(board[0]), len(theirs[0])) diff = len(compare([r[:w] for r in board[:h]], theirs[:h], 0, 0, args.tolerance)) share = diff / (w * h) if best is None or share < best[1]: best = (m, share, len(board[0]), len(theirs[0])) tag = " BLANK" if len(flat) == 1 else "" if best: m, share, bw, pw = best size = "" if bw == pw else f" (widths {bw}/{pw})" print(f" f{n:5d}: closest pico {m:5d} {share:6.1%} differ{size}{tag}") else: print(f" f{n:5d}: no pico frame{tag}") return 0 if __name__ == "__main__": sys.exit(main())