"""Render public real-image box audit and automatic mask crops."""
import html,json
from pathlib import Path
ROOT=Path(__file__).resolve().parents[1];OUT=ROOT/'results/r73';rows=json.loads((OUT/'results.json').read_text())['records'];cards=[]
for r in rows:
 full=r['scores']['full']['0.5'];tiled=r['scores']['tiled']['0.5'];gallery=''.join(f'<a class="mask" href="{html.escape(q["mask"])}"><img src="{html.escape(q["preview"])}" alt="Automatic mask on its original 640 pixel crop"><span>{html.escape(q["candidate_id"])} · origin {q["origin"]} · binary mask</span></a>' for q in r['sam_candidates']) or '<p>No tiled detector proposals were available for SAM.</p>'
 cards.append(f'''<article><h2>Field {r['index']} · {html.escape(r['site'])}</h2><img class="montage" src="{r['image']}" alt="Expert single-taxon reference boxes in green and detector proposals in red"><p>Published species boxes: {len(r['target_boxes'])}; full-image matches at IoU ≥0.5: {full['matched']}; tiled matches: {tiled['matched']}. Tiled proposals after box NMS: {len(r['tiled_kept_ids'])}. The unlabelled remainder is not a negative set. <a href="sources/{r['index']:02d}-image.jpg">Original RGB tile</a> · <a href="sources/{r['index']:02d}-label.txt">Published YOLO box file</a>.</p><details><summary>Inspect {len(r['sam_candidates'])} automatically selected segmentation masks</summary><p>The model chose these candidates by detector confidence before reference boxes were read. Each linked binary mask uses local 640 × 640 pixel coordinates; original image origin is shown. Masks have no expert segmentation labels.</p><div class="gallery">{gallery}</div></details></article>''')
page='''<!doctype html><html lang="en"><meta charset="utf-8"><meta name="viewport" content="width=device-width"><title>Public real-image object-box audit</title><style>body{font:16px/1.5 system-ui;background:#f3f6ef;color:#203a38}main{max-width:1920px;margin:auto;padding:24px}.notice{background:#fff2d6;padding:16px}article{background:white;padding:14px;margin:16px 0}.montage{width:100%}.gallery{display:grid;grid-template-columns:repeat(auto-fit,minmax(200px,1fr));gap:12px}.mask img{width:100%}.mask span{display:block}summary{cursor:pointer;font-weight:bold}</style><main><nav><a href="/results/performance/">Performance monitor</a> · <a href="/research/R73.md">Research note</a></nav><h1>Object proposals on published real microscope images</h1><p class="notice">The published annotations are bounding boxes for one marine fossil species, not full per-diatom masks. Other species, debris and ambiguous objects are not exhaustively labeled: unmatched predictions are not verified false positives. Box matches do not establish mask accuracy, freshwater performance or human-level segmentation.</p><p>Nine image/label pairs from three sites were selected by filename before their images or labels were retrieved; published lengths and SHA-256s were verified. The same detector processed each full 2400 × 2400 image and overlapping 640 × 640 patches. Green outlines are published boxes; red outlines are automatic detector proposals. One-to-one box matching uses IoU ≥0.5; the labels were read only after proposals and up to eight automatically chosen SAM masks had been generated.</p><p>The full-image detector matched <strong>1 of 10</strong> published boxes; tiled detection matched <strong>3 of 10</strong>. This tiny nonrandom single-taxon sample and unknown model-training overlap do not establish a general detection rate. Neither predicted mask boundaries nor object identity are evaluated. Adjacent source tiles may not be independent. The existing IoU-based box NMS may suppress real overlaps; raw predictions and aliases are retained.</p><p><a href="site-selection.json">Preselected sites</a> · <a href="pair-selection.json">Preselected file pairs</a> · <a href="protocol.json">Frozen protocol</a> · <a href="results.json">Box coordinates, scores, aliases, masks and timings</a>. Source: Ishino, Itaki and Fukuda, <a href="https://doi.org/10.17632/457x9c7rp3.2">public fossil-diatom dataset</a>, <a href="https://creativecommons.org/licenses/by/4.0/">CC BY 4.0</a>; image tiles are copied and the overlays are derived from them.</p>CARDS</main></html>'''.replace('CARDS',''.join(cards));(OUT/'index.html').write_text(page)
