"""Build visual inspection page for calibrated UDE development crops."""
import collections,html,json
from pathlib import Path
R=Path(__file__).resolve().parents[1];O=R/'results/r112/diversity-fetch';p=json.loads((O/'assets.json').read_text());audit=json.loads((O/'duplicate-audit.json').read_text());grouped=collections.defaultdict(list)
for r in p['records']:grouped[r['source_species']].append(r)
sections=[]
for species,rows in grouped.items():
 cards=[]
 for r in rows:
  delta=r['crop_vs_annotation_bbox_delta_px'];cards.append(f'''<figure><a href="{html.escape(r['source_crop'])}"><img loading="lazy" src="{html.escape(r['source_crop'])}" alt="Source image of {html.escape(species)}"></a><figcaption>Source slide: {html.escape(r['source_slide_group'])}<br>Source {r['source_dimensions_px']} px; scaled {r['scaled_dimensions_px']} px<br>Annotation: {html.escape(r['source_annotation_shape'])}; box dimension delta {delta}<br><a href="{html.escape(r['scaled_crop'])}">Academy-scale crop</a> · <a href="{html.escape(r['source_crop'])}">Original</a></figcaption></figure>''')
 sections.append(f'<section><h2>{html.escape(species)}</h2><p>Source label; four crops selected from distinct slide-name groups. Labels, sample independence and segment masks have not been verified.</p><div class="grid">{"".join(cards)}</div></section>')
(O/'index.html').write_text('''<!doctype html><html lang="en"><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1"><title>Diverse UDE development crops</title><style>body{font:16px/1.55 system-ui;color:#193b37;background:#f3f6f0;margin:0}main{max-width:1200px;margin:auto;padding:25px}h1,h2{font-family:Georgia,serif}a{color:#096c58}section{border-top:1px solid #ccdcd3;margin-top:30px}.notice,figure{background:white;padding:15px;border:1px solid #ccdcd3}.notice{border-left:5px solid #ba861e}.grid{display:grid;grid-template-columns:repeat(auto-fit,minmax(220px,1fr));gap:12px}figure{margin:0}img{width:100%;height:230px;object-fit:contain;background:#f6f6f4}figcaption{font-size:.88rem;overflow-wrap:anywhere}</style><body><main><nav><a href="/results/r112/">Synthetic training</a> · <a href="/research/R112_DESIGN.md">Design and limitations</a></nav><h1>Broader species source candidates</h1><p class="notice"><strong>Development data only.</strong> Source names are metadata, not independently confirmed species identifications. The original cutouts may contain background and neighboring organisms, and their rough annotations are not verified instance masks. Slide-name groups do not establish independent biological samples.</p><p>Selected without examining pixels: 120 crops from 30 source-labeled species in 28 genera and 93 slide-name groups. The source dataset specifies 0.09 µm/pixel; each scaled crop records its Academy target scan's OME XY scale. Matching pixel size does not ensure equivalent optics, focus or species-identifying detail. Originals are preserved.</p><p>Source: UDE DIATOMS in the Wild 2024, CC0. All files are verified against the public archive's ZIP checksums. An exact byte-hash screen found '''+str(len(audit['duplicate_groups']))+''' duplicate groups among these crops and the checked earlier local UDE images. Exact comparison cannot detect all related specimens or modified duplicates. <a href="selection.json">Frozen selection</a> · <a href="download-report.json">Download checksums</a> · <a href="duplicate-audit.json">Duplicate audit</a> · <a href="assets.json">Crop and calibration provenance</a></p>'''+''.join(sections)+'''</main></body></html>''')
print('Generated page for',len(p['records']),'crops and',len(grouped),'source-labeled taxa.')
