"""Render fixed image-enhancement diagnostic."""
import json,html
from pathlib import Path
ROOT=Path(__file__).resolve().parents[1];OUT=ROOT/'results/l54';rs=json.loads((OUT/'results.json').read_text())['records'];modes=('original','clahe','original_plus_clahe','highpass','original_plus_highpass')
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>Image enhancement for faint crossings</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/L54.md">Research notes</a></nav><h1>Can enhancement recover faint crossings?</h1><p class="notice">Exploratory test on the same 15 synthetic scenes previously examined in R64. 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 or a fixed high-pass transform, then run the same detector and SAM weights. Each 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/r64/">R64</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)
