"""verify_bundle.py — prove this bundle is byte-identical to the frozen TEST-100 benchmark. Run it after downloading. It needs only numpy (+ the bundled JSON); no GPU, no model, no source data. python verify_bundle.py # from inside the bundle directory Two independent checks per slice, 800 slices total: 1. HASH IDENTITY. `clean_sha256` / `meas_sha256` recomputed from the arrays and compared byte-for- byte against the frozen manifest. This is the binding check: it proves the pixels and the measurements are the same ones every published number was computed on, not a re-derivation that happens to look similar. 2. INPUT-METRIC ANCHORS. `input_psnr` / `input_ssim` are recomputed from `naive` vs `clean` with scikit-image at its DEFAULT settings (7x7 uniform window; a hand-rolled Gaussian-window SSIM will NOT match) and compared against the manifest at the protocol tolerances (1e-5 / 1e-5). If your loader or metric convention differs from the protocol's, this is where it shows up. (`input_lpips` needs the `lpips` package and a network download, so it is checked only against the value stored in the npz, not recomputed. The stored value came from the reference run.) Exit code 0 = every check passed. Non-zero = do not use this bundle. """ import hashlib, json, os, sys import numpy as np TOL = {"psnr": 1e-5, "ssim": 1e-5} def sha(a): return hashlib.sha256(np.ascontiguousarray(a).tobytes()).hexdigest() # The protocol's own metric definitions (medical/inverse/test100.py:_psnr/_ssim): skimage with # DEFAULT parameters -- a 7x7 UNIFORM window, not a Gaussian one -- and the estimate clipped to # [0,1] first. Reimplementing SSIM by hand does NOT reproduce these; use skimage or skip the check. def psnr(a, b): from skimage.metrics import peak_signal_noise_ratio as sk_psnr return float(sk_psnr(a, np.clip(b, 0, 1), data_range=1.0)) def ssim(a, b): from skimage.metrics import structural_similarity as sk_ssim return float(sk_ssim(a, np.clip(b, 0, 1), data_range=1.0)) def main(): here = os.path.dirname(os.path.abspath(__file__)) man = json.load(open(os.path.join(here, "test100_cases.json"))) blob = open(os.path.join(here, "test100_cases.json"), "rb").read() print(f"manifest sha256 = {hashlib.sha256(blob).hexdigest()}") have_sk = True try: import skimage.metrics # noqa: F401 except ImportError: have_sk = False print("! scikit-image not installed — skipping the metric recomputation " "(the sha256 identity checks, which are the binding ones, still run)") total = bad = 0 for cb in man["classes"]: f = os.path.join(here, f"test100_{cb['class']}.npz") if not os.path.exists(f): print(f"MISSING {os.path.basename(f)}"); bad += 1; continue d = np.load(f, allow_pickle=False) clean, y, naive = d["clean"], d["y"], d["naive"] tags = [str(t) for t in d["case_tag"]] errs = [] for i, case in enumerate(cb["cases"]): total += 1 if tags[i] != case["case_tag"]: errs.append(f"{tags[i]}: tag != manifest {case['case_tag']}"); continue if sha(clean[i].astype(np.float32)) != case["clean_sha256"]: errs.append(f"{tags[i]}: clean_sha256 MISMATCH"); continue # y is stored squeezed; the manifest hashed it with the solver's leading dims if sha(y[i]) != case["meas_sha256"] and sha(y[i][None, None]) != case["meas_sha256"]: errs.append(f"{tags[i]}: meas_sha256 MISMATCH"); continue if have_sk: dp = abs(psnr(clean[i], naive[i]) - case["input_psnr"]) if dp > TOL["psnr"]: errs.append(f"{tags[i]}: input_psnr drift {dp:.2e} > {TOL['psnr']}"); continue ds = abs(ssim(clean[i], naive[i]) - case["input_ssim"]) if ds > TOL["ssim"]: errs.append(f"{tags[i]}: input_ssim drift {ds:.2e} > {TOL['ssim']}") bad += len(errs) status = "OK" if not errs else f"{len(errs)} FAILURES" print(f" class {cb['class_id']} {cb['class']:<22} n={len(cb['cases']):<4} " f"{clean.shape} {y.dtype} {status}") for e in errs[:5]: print(f" {e}") print(f"\n{total - bad}/{total} slices verified") if bad: print("FAILED — do not use this bundle."); return 1 print("All checks passed: this bundle is byte-identical to the frozen TEST-100 benchmark.") return 0 if __name__ == "__main__": sys.exit(main())