"""Publish visual triage for the renderer, without classifying mask quality."""
import html,json
from pathlib import Path
R=Path(__file__).resolve().parents[1];D=R/'results/r112/species-scaleup';O=D/'render-detail-audit';r=json.loads((O/'results.json').read_text());E=html.escape;s=r['summary'];fmt=lambda k: f"{s[k]['0.5']:.2f}";selected=[x for x in r['records'] if 'review_examples' in x];selected.sort(key=lambda x:('low_inserted_change' not in x['review_reasons'],x['mean_inserted_gray_change_inside_mask'],x['annotation_id']));items=[]
for x in selected:
 paths=x['review_examples'];src='/results/r112/species-scaleup/images/'+x['annotation_id']+'.png';imgs=[('Source',src),('Center-mask outline','/'+paths['overlay']['path']),('Background window','/'+paths['background']['path']),('Composite','/'+paths['composite']['path'])];outside=x['source_sobel_outside_mask_fraction'];items.append('<article><h3>'+E(x['source_name'])+' · '+E(x['annotation_id'])+'</h3><p>Review queue: '+E(', '.join(x['review_reasons']))+' · Source edges outside mask: '+('unavailable' if outside is None else f'{outside:.1%}')+' · Mean inserted gray change inside mask: '+f"{x['mean_inserted_gray_change_inside_mask']:.2f}"+' of 255 · Mask area: '+f"{x['mask_area_fraction']:.1%}"+' · '+E(x['mask_triage'])+'</p><div class="grid">'+''.join('<figure><a href="'+E(u)+'"><img loading="lazy" src="'+E(u)+'" alt="'+E(k)+'"></a><figcaption>'+E(k)+'</figcaption></figure>' for k,u in imgs)+'</div></article>')
page='''<!doctype html><html lang="en"><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>Composite detail audit</title><style>body{font:16px/1.5 system-ui;color:#193b37;background:#f2f6f2;margin:0}main{max-width:1150px;margin:auto;padding:24px}a{color:#076c59}.notice{background:#fff8df;border-left:5px solid #b88418;padding:15px}table{border-collapse:collapse;background:white}td,th{border:1px solid #cbd8d1;padding:8px;text-align:left}.grid{display:grid;grid-template-columns:repeat(4,minmax(0,1fr));gap:8px}article{background:white;border:1px solid #cbd8d1;padding:12px;margin:12px 0}figure{margin:0}figure img{height:200px;width:100%;object-fit:contain;background:#eee}figcaption{font-size:.85rem}@media(max-width:600px){.grid{grid-template-columns:repeat(2,minmax(0,1fr))}}</style><main><nav><a href="/results/r112/">Synthetic training</a> · <a href="/results/r112/species-scaleup/">Source pool</a> · <a href="/results/r112/species-scaleup/seam-repair-comparison/">Composite comparison</a></nav><h1>Inspecting source detail in composites</h1><p class="notice"><strong>Review prompts, not pass/fail scores.</strong> A source edge outside the outlined mask may be a neighboring organism, optical halo or noise. A small grayscale change does not prove an inserted diatom is invisible. Conversely, a large change does not guarantee diagnostic structures survived. Source masks are unverified and Academy backgrounds can contain other organisms.</p><p>Measured all 2,830 training sources and their existing seam-repaired composites, using no tuning or check-partition pixels. The source Sobel measure summarizes image gradient outside the center mask; the gray-change measure compares the composite with the underlying background within that mask. These are image statistics, not species or segmentation accuracy.</p><table><tr><th>Measure</th><th>Median</th></tr><tr><td>Fraction of source image edge strength outside the mask</td><td>'''+f"{100*float(fmt('source_sobel_outside_mask_fraction')):.0f}%"+'''</td></tr><tr><td>Mean absolute inserted grayscale change inside mask</td><td>'''+fmt('mean_inserted_gray_change_inside_mask')+''' / 255 levels</td></tr><tr><td>Mask pixels unchanged after compositing</td><td>'''+f"{100*float(fmt('fraction_mask_pixels_unchanged')):.0f}%"+'''</td></tr></table><p><a href="protocol.json">Audit protocol</a> · <a href="results.json">All measurements</a> · <a href="verify.json">Reconstruction checks</a></p><h2>Review queue: '''+str(len(selected))+''' source crops</h2><p>The queue includes the 30 lowest inserted changes, the 30 highest outside-mask edge fractions and 20 evenly spaced references, with duplicates removed. Red outlines show the center mask only; alternate model proposals are not shown. The source and output are native crop pixels displayed at the same size.</p>'''+''.join(items)+'''</main></html>''';(O/'index.html').write_text(page);print('published',len(selected),'review cases')
