"""Render new-source spatial-box validation and rerun."""
import json,html
from pathlib import Path
ROOT=Path(__file__).resolve().parents[1];OUT=ROOT/'results/r69';rs=json.loads((OUT/'results.json').read_text())['records'];rr=json.loads((OUT/'rerun-results.json').read_text())['records'];methods=('prefix_40','spatial_40','prefix_80')
def table(xs):
 rows=''
 for method in methods:
  scores=[x['scores'][method] for x in xs];vals=[sum(y['budgets'][str(n)]['matched'] for y in scores) for n in (5,10,20)]+[sum(y['all']['matched'] for y in scores)];ret=sum(y['retained'] for y in scores);rows+=f'<tr><td>{method}</td>'+''.join(f'<td>{v}</td>' for v in vals)+f'<td>{ret}</td></tr>'
 return rows
cards=''
for x in rs:
 den=len(x['source_ids']);vals=', '.join(f"{m} {x['scores'][m]['budgets']['10']['matched']}/{den}" for m in methods);cards+=f'<article><h2>{html.escape(x["condition"])} · pair {x["pair"]}</h2><img src="{x["image"]}" alt="Synthetic targets and box selection comparisons"><p>Top-10 matches: {vals}. <a href="{x["source_archive"]}">Original planes</a> · <a href="{x["contrast_archive"]}">All candidate boxes and masks</a> · <a href="{x["clahe_archive"]}">CLAHE outputs</a>.</p></article>'
rerun=''.join(f'<tr><td>{x["scene_index"]} · {html.escape(x["condition"])}</td><td>{x["policy"]}</td><td>{x["inference_seconds"]:.2f}</td><td>{sum(y["mask_changed"] for y in x["per_plane"])}</td><td>{sum(y["eligibility_changes"] for y in x["per_plane"])}</td></tr>' for x in rr)
page='''<!doctype html><html lang="en"><meta charset="utf-8"><meta name="viewport" content="width=device-width"><title>New-source spatial box validation</title><style>body{font:16px/1.5 system-ui;background:#f3f6ef;color:#203a38}main{max-width:1520px;margin:auto;padding:24px}.notice{background:#fff2d6;padding:16px}article{background:white;padding:14px;margin:16px 0}img{width:100%}table{border-collapse:collapse}th,td{padding:8px;border:1px solid #aaa;text-align:left}.table{overflow:auto}</style><main><nav><a href="/results/performance/">Performance monitor</a> · <a href="/research/R69.md">Research notes</a></nav><h1>Spatial box selection on new source groups</h1><p class="notice">Artificial scenes using public IDs frozen before image retrieval and slide filenames distinct from earlier spatial-selection cohorts; biological independence is unverified. Three source pairs recur across conditions. Structural synthetic masks do not establish real-slide, species or human-level accuracy.</p><p>We freeze the 4×4 spatial round-robin ordering and take 40 boxes per plane before consulting the saved mask or target labels. It is compared with the original 40-box and 80-box prefixes. All methods use the fixed three-stream policy and mask IoU ≥0.8 NMS; one-to-one synthetic target matching uses IoU ≥0.5. The replay uses already computed masks; timings come from the separate small rerun below.</p><h2>Nine crossing scenes: 18 targets</h2><div class="table"><table><tr><th>Policy</th><th>Top 5</th><th>Top 10</th><th>Top 20</th><th>All</th><th>Retained masks</th></tr>ROWS</table></div><h2>Actual prompt inference on four scenes</h2><p>For three crossing cases and a background control, the chosen box coordinates were rerun on SAM. Times include five image encodings and prompt inference, excluding box generation, detector, NMS and scoring; single runs are not production benchmarks. Each regenerated mask was compared pixel-for-pixel with its saved counterpart.</p><div class="table"><table><tr><th>Scene</th><th>Policy</th><th>Seconds</th><th>Changed masks</th><th>Eligibility changes</th></tr>RERUN</table></div><p><a href="protocol.json">Frozen protocol</a> · <a href="results.json">All rankings and scores</a> · <a href="rerun-protocol.json">Rerun protocol</a> · <a href="rerun-results.json">Rerun results</a>. Background-only fields contain unlabeled natural material; candidates are not false positives. Public foreground: Gündüz et al., <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/">CC BY-NC-SA 4.0</a>; scene provenance in <a href="selection.json">the frozen selection</a>.</p>CARDS</main></html>'''.replace('ROWS',table([x for x in rs if x['condition'].startswith('crossing_')])).replace('RERUN',rerun).replace('CARDS',cards);(OUT/'index.html').write_text(page)
