"""Static, inspectable report for the fixed real-vs-composite comparison."""
import html,json
from pathlib import Path
R=Path(__file__).resolve().parents[1];D=R/'results/r112/species-scaleup';O=D/'synthetic-crop-comparison';b=json.loads((D/'real-crop-baseline/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']),('Synthetic only',r['methods']['synthetic']),('Real + synthetic',r['methods']['mixed'])]
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=''.join('<figure><img loading="lazy" src="/results/r112/species-scaleup/images/'+E(x['annotation_id'])+'.png" alt="Original source"><a href="/'+E(x['example'])+'"><img loading="lazy" src="/'+E(x['example'])+'" alt="Synthetic crop"></a><figcaption>'+E(x['source_name'])+' · '+E(x['annotation_id'])+' · '+E(x['mask_triage'])+'</figcaption></figure>' for x in r['composites'] if 'example' in x)
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>Synthetic versus real training crops</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(auto-fit,minmax(160px,1fr));gap:12px}figure{margin:0;background:white;padding:8px}figure img{height:170px;width:100%;object-fit:contain;background:#eee}figcaption{font-size:.85rem}</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/real-crop-baseline/">Real baseline</a></nav><h1>Do synthetic crops improve the baseline?</h1><p class="notice"><strong>Development experiment, not Academy species accuracy.</strong> Models choose among 80 source-name strings on prepared public crops. Names and masks are not independently checked against Academy taxonomy; prior exposure, imperfect site grouping and unrealistic compositing limit conclusions. Backgrounds may contain other diatoms. No Academy field, segmentation, unknown rejection or unseen species performance was measured.</p><p>Each of 2,830 training crops produced one composite using its unverified center-mask proposal and a grayscale Academy development background. Source and background pixel scales were matched nominally; opacity and contrast vary. The real-only and synthetic-only versions contain the same sources; the mixed version uses both. The frozen feature extractor and fixed linear training procedure are the same. All 604 previously consumed tuning crops were scored; the separate check pixels were not opened.</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>Different training data volumes and background artifacts complicate interpretation. One experiment cannot establish whether other synthetic approaches will help; differences on this tuning set are exploratory.</p><p><a href="protocol.json">Fixed protocol</a> · <a href="results.json">All records and predictions</a> · <a href="verify.json">Reconstruction checks</a></p><h2>First 80 composites</h2><p>Original at left, composite at right. Some composites show obvious background seams caused by modulo tiling; others obscure diagnostic structures. Each background may contain diatoms, and masks may be wrong or incomplete. These are the first 80 cases, not hand-selected examples.</p><div class="grid">'''+examples+'''</div><h2>All tuning predictions</h2><table><tr><th>Source crop</th><th>Source name</th><th>Real only</th><th>Synthetic only</th><th>Mixed</th></tr>'''+''.join(cases)+'''</table></main></html>''';(O/'index.html').write_text(page);print('published',len(cases),'cases',len([x for x in r['composites'] if 'example' in x]),'examples')
