"""Render fixed-budget replay and actual prompt-rerun diagnostics."""
import json,html
from pathlib import Path
ROOT=Path(__file__).resolve().parents[1];OUT=ROOT/'results/l56';rs=json.loads((OUT/'results.json').read_text())['records'];rr=json.loads((OUT/'rerun-results.json').read_text())['records'];budgets=(5,10,20,40,80)
def table(xs):
 rows=''
 for b in budgets:
  s=[x['scores'][str(b)] for x in xs];vals=[sum(y['budgets'][str(n)]['matched'] for y in s) for n in (5,10,20)]+[sum(y['all']['matched'] for y in s)];prompts=sum(y['contrast_prompt_count'] for y in s);retained=sum(y['three_stream_retained'] for y in s);rows+=f'<tr><td>{b}</td>'+''.join(f'<td>{v}</td>' for v in vals)+f'<td>{prompts}</td><td>{retained}</td></tr>'
 return rows
cards=''
for x in rs:
 den=len(x['source_ids']);vals=', '.join(f"{b}: {x['scores'][str(b)]['budgets']['10']['matched']}/{den}" for b in budgets);cards+=f'<article><h2>{html.escape(x["condition"])} · pair {x["pair"]}</h2><img src="{x["image"]}" alt="Synthetic targets and candidate budget comparisons"><p>Top-10 matches by boxes per plane: {vals}. <a href="{x["source_archive"]}">Source planes</a> · <a href="{x["contrast_archive"]}">All 80-box outputs</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["box_budget_per_plane"]}</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>Contrast proposal budgets</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/L56.md">Research notes</a></nav><h1>Do fewer contrast boxes preserve crossing matches?</h1><p class="notice">Posthoc replay on previously inspected synthetic scenes, followed by a small actual prompt rerun. The same three source pairs recur across conditions. Structural synthetic targets and simulated blur do not establish real-slide or human-level accuracy.</p><p>We simulate taking the first 5, 10, 20, 40 or 80 contrast boxes per focal plane from the original image-only proposal order, then apply the same filters, overlap ranking, three-stream ordering and NMS. Reference masks are used only for one-to-one synthetic matching at IoU ≥0.5. The replay uses already computed masks, so it does not itself measure model time.</p><h2>Nine crossing scenes: 18 targets</h2><div class="table"><table><tr><th>Boxes/plane</th><th>Top 5</th><th>Top 10</th><th>Top 20</th><th>All</th><th>Contrast prompts</th><th>Retained masks</th></tr>ROWS</table></div><h2>Actual inference rerun</h2><p>For a split crossing, a weak crossing and a background control, we reran 5, 40 and 80 saved boxes per plane. Time includes five SAM image encodings and mask prompts but excludes box generation, detector inference, NMS and scoring. One sequential run per case is not a stable production throughput measurement. Pixel difference counts compare each regenerated mask with the prior saved mask; quality is separately checked against eligibility thresholds.</p><div class="table"><table><tr><th>Scene</th><th>Boxes/plane</th><th>Seconds</th><th>Changed masks</th><th>Filter eligibility changes</th></tr>RERUN</table></div><p><a href="protocol.json">Replay protocol</a> · <a href="results.json">Replay data</a> · <a href="rerun-protocol.json">Rerun protocol</a> · <a href="rerun-results.json">Rerun data and masks</a>. Background-only fields have unannotated natural material and cannot establish false positives. Source foreground: Gündüz et al., <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/">CC BY-NC-SA 4.0</a>; provenance in <a href="/results/r64/">R64</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)
