"""Publish the fixed seam-repair comparison and input image triplets."""
import html,json
from pathlib import Path
R=Path(__file__).resolve().parents[1];D=R/'results/r112/species-scaleup';O=D/'seam-repair-comparison';b=json.loads((D/'real-crop-baseline/results.json').read_text());old=json.loads((D/'synthetic-crop-comparison/results.json').read_text());r=json.loads((O/'results.json').read_text());s=json.loads((D/'location-partitions.json').read_text());tuning=[x for x in s['records'] if x['partition']=='tuning'];assert len(tuning)==604
E=html.escape;methods=[('Real only',b['methods']['linear']),('Earlier mixed composite',old['methods']['mixed']),('Repaired composite only',r['methods']['synthetic']),('Real + repaired composite',r['methods']['mixed']),('Real + ordinary augmentation',r['methods']['augmentation'])]
rows=''.join('<tr><th>'+name+'</th>'+''.join(f'<td>{m[k]}/604 ({100*m[k]/604:.1f}%)</td>' for k in ('top1','top5','genus'))+'</tr>' for name,m in methods)
examples=[]
for x,o in zip(r['records'][:80],old['composites'][:80]):
 assert x['annotation_id']==o['annotation_id']; imgs=[('Original','/results/r112/species-scaleup/images/'+x['annotation_id']+'.png'),('Earlier composite','/'+o['example']),('Repaired composite','/'+x['composite_example']),('Augmented original','/'+x['augment_example'])];examples.append('<article><h3>'+E(o['source_name'])+' · '+E(x['annotation_id'])+'</h3><div class="grid">'+''.join('<figure><a href="'+E(src)+'"><img loading="lazy" src="'+E(src)+'" alt="'+E(name)+'"></a><figcaption>'+E(name)+'</figcaption></figure>' for name,src in imgs)+'</div></article>')
cases=[]
for i,x in enumerate(tuning):
 name=x['source_species'];pred=[m['predictions'][i] for _,m in methods];cases.append('<tr><td><a href="/results/r112/species-scaleup/images/'+E(x['annotation_id'])+'.png">'+E(x['annotation_id'])+'</a></td><td>'+E(name)+'</td>'+''.join('<td>'+E(q)+(' ✓' if q==name else '')+'</td>' for q in pred)+'</tr>')
page='''<!doctype html><html lang="en"><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>Seam-repaired synthetic crop comparison</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;max-width:100%;display:block;overflow:auto}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:170px;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/synthetic-crop-comparison/">Earlier comparison</a></nav><h1>Seam-repaired composites versus augmentation</h1><p class="notice"><strong>Exploratory development comparison.</strong> This test uses previously consumed tuning crops and was designed after seeing problems in the earlier renderer. The source names, mask proposals and optics are not independently verified against Academy specimens. No field detection, overlap recovery, unknown-species rejection or Academy accuracy is measured.</p><p>The earlier renderer wrapped Academy background pixels, causing visible seams. The revision takes a contiguous window of the same background; it reflects the edge only when the source crop is too large (55 of 2,830 crops). It uses the same source, background, mask and blending parameters as before. Ordinary augmentation changes the gain and contrast of each original crop. Each mixed condition uses the same originals and one added image per source.</p><table><tr><th>Training</th><th>Exact source name, top 1</th><th>Exact source name, top 5</th><th>Genus, top 1</th></tr>'''+rows+'''</table><p>The frozen feature extractor and training procedure are unchanged. Differences on the reused tuning crops are exploratory and do not prove causation; reflected padding, imperfect masks and hidden background diatoms can still affect images. The separate check pixels were not read.</p><p><a href="protocol.json">Fixed protocol</a> · <a href="results.json">Records and predictions</a> · <a href="verify.json">Reconstruction checks</a></p><h2>First 80 paired inputs</h2><p>Each row uses the same source. The earlier and repaired composites use the same Academy background and blend parameters, though their crop locations differ. These examples are in fixed source order, not hand-picked.</p>'''+''.join(examples)+'''<h2>All tuning predictions</h2><table><tr><th>Crop</th><th>Source name</th>'''+''.join('<th>'+name+'</th>' for name,_ in methods)+'''</tr>'''+''.join(cases)+'''</table></main></html>''';(O/'index.html').write_text(page);print('published',len(cases),'tuning cases and',len(examples),'paired examples')
