"""Publish paired renderer views and developmental source-name comparisons."""
import html,json
from pathlib import Path
from PIL import Image,ImageDraw
R=Path(__file__).resolve().parents[1];S=R/'results/r112/weak-development-v1';P=R/'results/r112/design/synthetic-training-pilot';L=R/'results/r112/design/linear-training-pilot';O=R/'results/r112/design/transmission-study';m=json.loads((O/'manifest.json').read_text());r=json.loads((O/'results.json').read_text());p=json.loads((P/'protocol.json').read_text());prev=json.loads((L/'results.json').read_text());e=html.escape
methods=[('Real only',r['baseline_counts']['real']),('Original additive only',r['baseline_counts']['synthetic']),('Transmission proxy only',r['alternative']['transmission_only']['correct']),('Real + original additive',r['baseline_counts']['real_plus_synthetic']),('Real + transmission proxy',r['alternative']['real_plus_transmission']['correct']),('Real + transformed control',r['baseline_counts']['real_plus_transformed'])];table=''.join('<tr><th>'+e(k)+'</th><td>'+str(v)+'/50</td></tr>' for k,v in methods)
cases=[]
for row in m['records'][:12]:
 name=row['scene_id'];a=Image.open(S/(name+'.png')).convert('L').resize((380,380));b=Image.open(O/row['image']).convert('L').resize((380,380));pane=Image.new('L',(780,420),255);d=ImageDraw.Draw(pane);d.text((5,5),'Original additive',fill=30);d.text((395,5),'Transmission proxy',fill=30);pane.paste(a,(5,30));pane.paste(b,(395,30));fn=name+'-comparison.jpg';pane.save(O/fn,quality=90);cases.append('<article><h3>'+e(name)+'</h3><a href="'+fn+'"><img loading="lazy" src="'+fn+'" alt="Original and transmission proxy scene comparison"></a></article>')
original=prev['records']['real_plus_synthetic']['predictions'];alternative=r['alternative']['real_plus_transmission']['predictions'];gain=sum(x!=c['species'] and y==c['species'] for c,x,y in zip(p['validation'],original,alternative));lost=sum(x==c['species'] and y!=c['species'] for c,x,y in zip(p['validation'],original,alternative));clip=sum(x['above_255_before_clipping'] for x in m['records']);boundary=sum(x['new_boundary_values_vs_background_in_support'] for x in m['records']);page='''<!doctype html><html lang="en"><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>Translucency rendering comparison</title><style>body{font:16px/1.55 system-ui;background:#f4f6f0;color:#193b37;margin:0}main{max-width:1050px;margin:auto;padding:24px}a{color:#096c58}.notice,article{background:white;border:1px solid #cddbd2;padding:16px;margin:14px 0}.notice{border-left:5px solid #bc851b}img{max-width:100%;height:auto}table{border-collapse:collapse;background:white}td,th{border:1px solid #cddbd2;padding:8px;text-align:left}</style><body><main><nav><a href="/results/r112/">Synthetic training</a> · <a href="/results/r112/design/linear-training-pilot/">Prior classifier comparison</a></nav><h1>Testing a transmission-style renderer</h1><p class="notice"><strong>Development study, not a validated optical model or held-out accuracy test.</strong> The source names and supports are unverified; backgrounds may contain other diatoms. The 50 validation crops were used in earlier experiments. The classifier is closed-set and uses known insertion rectangles, so this does not test detection, segmentation or Academy performance.</p><p>In 240 paired scenes, the same unverified source cutouts were inserted at the same positions and scales over the same backgrounds. The alternative multiplies the background by a function of each source's intensity relative to its estimated local background, with the prior opacity and contrast settings. That is a simple grayscale transmission proxy: camera response and optical parameters are not calibrated. The formula applies to overlapping inserts, but does not prove realistic transparency.</p><table><tr><th>Linear-head reference views</th><th>Matches to source names</th></tr>'''+table+'''</table><p>The mixed transmission head and the prior mixed additive head both matched 32/50, with '''+str(gain)+''' newly matching and '''+str(lost)+''' no longer matching. Transmission-only matched 17/50 versus 24/50 for additive-only. These correlated, previously used crops do not establish whether either renderer generalizes or preserves the details needed for species identification.</p><p>The proxy produced '''+format(clip,',')+''' scene-pixels above 255 before final clipping, and '''+format(boundary,',')+''' pixels inside at least one projected support ended at 0 or 255 where the original background had neither boundary value. These are saturation indicators, not measures of biological realism. Source/background differences, unmodeled optics and clipping all remain limitations.</p><p><a href="protocol.json">Predeclared protocol</a> · <a href="manifest.json">All paired images and provenance</a> · <a href="results.json">Training and prediction scores</a> · <a href="verify.json">Reconstruction checks</a></p><h2>Paired scene examples</h2><p>The first 12 scenes in the existing manifest are shown; insertions and weak supports are identical between each pair. Click to enlarge.</p>'''+''.join(cases)+'''</main></body></html>''';(O/'index.html').write_text(page);print('gain',gain,'lost',lost,'preclip',clip,'boundary',boundary)
