"""Render separate-source CLAHE validation."""
import json,html
from pathlib import Path
ROOT=Path(__file__).resolve().parents[1];OUT=ROOT/'results/r66';rs=json.loads((OUT/'results.json').read_text())['records'];modes=('original','clahe','original_plus_clahe')
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, original and enhanced candidates, and transformed image"><p>Top 10: {vals}. <a href="{x["source_archive"]}">Original planes and masks</a> · <a href="{x["archive"]}">Enhanced planes and all new masks</a>.</p></article>'
page='''<!doctype html><html lang="en"><meta charset="utf-8"><meta name="viewport" content="width=device-width"><title>Separate-source CLAHE 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/R66.md">Research notes</a></nav><h1>Does CLAHE help on different source groups?</h1><p class="notice">Preselected test on 15 previously inspected R62 synthetic scenes, using source IDs and slide filenames different from the L54 development scenes. Three source pairs recur across conditions. Synthetic masks and additive blur do not establish real-slide or human-level accuracy.</p><p>We independently enhance all five planes with fixed CLAHE, then run the same detector and SAM weights. The enhanced pool is also alternated with the original-plane detector pool. Original pixels and all unsuppressed masks are preserved; enhanced images can alter fine ornamentation and should not replace original image data for species identification. Predictions are matched one-to-one to synthetic full shapes at IoU ≥0.5; mask IoU ≥0.8 NMS is applied. Panels show masks on the original middle plane, plus the enhanced CLAHE middle plane.</p><h2>Nine crossing scenes: 18 targets</h2><div class="table"><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></div><h2>Three weak-crossing scenes: six targets</h2><div class="table"><table><tr><th>Method</th><th>Top 5</th><th>Top 10</th><th>Top 20</th><th>All</th><th>Retained</th></tr>WEAK</table></div><p>Background-only fields contain natural unlabeled structures; candidate counts are not false positives. The combined approaches add five more detector and segmentation passes, and timings exclude the original baseline inference. <a href="protocol.json">Frozen protocol</a> · <a href="results.json">Scores, inference times and box provenance</a>. 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>; original scene provenance in <a href="/results/r62/">R62</a>.</p>CARDS</main></html>'''.replace('CROSS',table([x for x in rs if x['condition'].startswith('crossing_')])).replace('WEAK',table([x for x in rs if x['condition']=='crossing_weak'])).replace('CARDS',cards);(OUT/'index.html').write_text(page)
