"""Render separate-source candidate-ranking evaluation."""
import json,html
from pathlib import Path
ROOT=Path(__file__).resolve().parents[1];OUT=ROOT/'results/r64';rs=json.loads((OUT/'results.json').read_text())['records'];modes=('pool','original','detector_overlap')
def table(xs):
 rows=''
 for m in modes:
  vals=[sum(x['scores'][m]['budgets'][str(n)]['matched'] for x in xs) for n in (5,10,20)]+[sum(x['scores'][m]['all']['matched'] for x in xs)];cnt=sum(x['scores'][m]['retained'] for x in xs);rows+=f'<tr><td>{m}</td>'+''.join(f'<td>{v}</td>' for v in vals)+f'<td>{cnt}</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 modes);cards+=f'<article><h2>{html.escape(x["condition"])} · pair {x["pair"]}</h2><img src="{x["image"]}" alt="Synthetic targets and fixed-order candidate masks"><p>Top 10: {vals}. <a href="{x["baseline_archive"]}">Source planes and detector masks</a> · <a href="{x["contrast_archive"]}">All contrast masks and boxes</a> · <a href="{x["features"]}">Ranking features</a>.</p></article>'
page='''<!doctype html><html lang="en"><meta charset="utf-8"><meta name="viewport" content="width=device-width"><title>Separate-source ranking validation</title><style>body{font:16px/1.5 system-ui;background:#f3f6ef;color:#203a38}main{max-width:1250px;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}</style><main><nav><a href="/results/performance/">Performance monitor</a> · <a href="/research/R64.md">Research notes</a></nav><h1>Separate-source ranking validation</h1><p class="notice">These are synthetic crossings on previously examined slide backgrounds, using newly selected public image IDs. Different IDs and slide names do not prove independent samples. Structural synthetic targets are not scientist annotations, and these scores cannot establish real-slide overlap or human-level accuracy.</p><p>The detector-overlap policy was frozen before source image retrieval. Five-plane detector and contrast-box masks are compared with the original and detector-overlap contrast orderings, each at the same review budgets. The overlap feature is approximate 64×64 mask IoU without registration, not calibrated confidence. Predictions match synthetic targets one-to-one at IoU ≥0.5; NMS uses IoU ≥0.8. All raw candidates remain available. <a href="policy.json">Pre-retrieval policy</a> · <a href="selection.json">Frozen source selection</a> · <a href="rank-protocol.json">Ranking inputs</a> · <a href="results.json">Full results</a>.</p><h2>Nine crossing scenes: 18 targets</h2><table><tr><th>Method</th><th>Top 5</th><th>Top 10</th><th>Top 20</th><th>All</th><th>Retained</th></tr>CROSS</table><h2>Twelve object scenes: 24 targets</h2><table><tr><th>Method</th><th>Top 5</th><th>Top 10</th><th>Top 20</th><th>All</th><th>Retained</th></tr>ALL</table><p>Three background-only controls contain unlabeled natural objects; their candidate counts are not false positives. Original foreground images: Gündüz et al., <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/">CC BY-NC-SA 4.0</a>; masks and synthetic composites retain same attribution and license.</p>CARDS</main></html>'''.replace('CROSS',table([x for x in rs if x['condition'].startswith('crossing_')])).replace('ALL',table([x for x in rs if x['condition']!='background_only'])).replace('CARDS',cards);(OUT/'index.html').write_text(page)
