"""Publish paired visual review for the capped contrast proxy."""
import html,json
from pathlib import Path
R=Path(__file__).resolve().parents[1];D=R/'results/r112/species-scaleup';O=D/'contrast-proxy-study';A=D/'render-detail-audit';r=json.loads((O/'results.json').read_text());audit={x['annotation_id']:x for x in json.loads((A/'results.json').read_text())['records']};g=r['groups'];E=html.escape;items=[]
for x in sorted((x for x in r['records'] if 'example' in x),key=lambda x:(x['original_mean_change'],x['annotation_id'])):
 a=audit[x['annotation_id']];src='/results/r112/species-scaleup/images/'+x['annotation_id']+'.png';imgs=[('Source',src),('Center-mask outline','/'+a['review_examples']['overlay']['path']),('Background','/'+a['review_examples']['background']['path']),('Earlier composite','/'+a['review_examples']['composite']['path']),('Contrast variant','/'+x['example'])];items.append('<article><h3>'+E(x['source_name'])+' · '+E(x['annotation_id'])+'</h3><p>Selection: '+E(', '.join(x['review_reasons']))+' · Gain '+f"{x['gain']:.2f}"+(' · assigned to unadjusted group' if x['preserve_faint'] else ' · adjustable group')+' · Mean change: '+f"{x['original_mean_change']:.2f} → {x['variant_mean_change']:.2f}"+' of 255 · Clipped pixels: '+str(x['clipped_pixels_before'])+' → '+str(x['clipped_pixels_after'])+'</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>')
a=g['all'];adj=g['adjustable'];page='''<!doctype html><html lang="en"><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>Capped contrast proxy</title><style>body{font:16px/1.5 system-ui;color:#193b37;background:#f2f6f2;margin:0}main{max-width:1250px;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(5,minmax(0,1fr));gap:8px}article{background:white;border:1px solid #cbd8d1;padding:12px;margin:12px 0}figure{margin:0}figure img{height:190px;width:100%;object-fit:contain;background:#eee}figcaption{font-size:.85rem}@media(max-width:700px){.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/render-detail-audit/">Earlier detail audit</a></nav><h1>Capped contrast proxy: visual study</h1><p class="notice"><strong>Not a validated optical model or approved training set.</strong> This is a numerical rendering change to address very faint insertions. It can amplify noise, halos and incorrect mask edges as well as specimen detail. More change from the background does not prove that diagnostic structures are visible or biologically faithful. Background organisms and source labels remain unverified.</p><p>The same 2,830 development source/background pairs were re-rendered. A deterministic 30%-probability group keeps gain 1 to retain faint examples; for other cases an arbitrary target of 4 grayscale levels inside the proposed mask determines a gain capped at 4. The target is an experiment setting, not a biological acceptance threshold. Masks, backgrounds, opacity and source identity are unchanged. No model was trained and no tuning or check-partition images were read.</p><table><tr><th>Measure</th><th>Result</th></tr><tr><td>Original source pairs</td><td>'''+str(a['n'])+'''</td></tr><tr><td>Randomly assigned unadjusted group</td><td>'''+str(g['preserved_faint']['n'])+'''</td></tr><tr><td>Gain above 1</td><td>'''+str(a['gain_gt_one'])+'''</td></tr><tr><td>Gain capped at 4</td><td>'''+str(a['gain_capped_at_four'])+'''</td></tr><tr><td>Cases with more clipped pixels</td><td>'''+str(a['cases_with_new_clipping'])+'''</td></tr><tr><td>All-mask clipped pixels before → after</td><td>'''+str(a['pixels_clipped_before'])+' → '+str(a['pixels_clipped_after'])+''' out of '''+str(a['mask_pixels'])+'''</td></tr><tr><td>Pixels changed outside mask</td><td>'''+str(a['changed_outside_mask'])+'''</td></tr><tr><td>10th-percentile mean change inside mask, all cases</td><td>'''+f"{a['mean_original_q10_q50'][0]:.2f} → {a['mean_variant_q10_q50'][0]:.2f}"+''' / 255</td></tr></table><p>Clipped-pixel counts measure values outside the 0–255 range before rounding; they do not detect every artifact. Only the 80 pictured variant images are saved, alongside a reproducible recipe and metrics for all pairs. They are research images, not automatically accepted labels.</p><p><a href="protocol.json">Fixed protocol</a> · <a href="results.json">All measurements and settings</a> · <a href="verify.json">Reconstruction checks</a></p><h2>80 preselected paired cases</h2><p>These are the same cases chosen in the prior audit; the red outline is the unverified center mask. Some were intentionally left at gain 1. Click any image for its native pixels.</p>'''+''.join(items)+'''</main></html>''';(O/'index.html').write_text(page);print('published',len(items),'cases')
