"""Publish the 80-name development crop baseline without implying real-field accuracy."""
import html,json
from pathlib import Path
import numpy as np
R=Path(__file__).resolve().parents[1];D=R/'results/r112/species-scaleup';O=D/'real-crop-baseline';r=json.loads((O/'results.json').read_text());z=np.load(O/'tuning-features.npz');assets={x['annotation_id']:x for x in json.loads((D/'assets.json').read_text())['records']};rows=[assets[x] for x in z['annotation_ids'].tolist()];labels=r['labels'];names={'centroid':'Class centroids','linear':'Learned linear head'};e=html.escape;table=''
for key in ('centroid','linear'):
 x=r['methods'][key];macro=sum(v['top1']/v['count'] for v in x['per_source_name'].values())/len(labels);table+='<tr><th>'+names[key]+'</th><td>'+str(x['top1'])+'/604 ('+f"{100*x['top1']/604:.1f}"+'%)</td><td>'+str(x['top5'])+'/604 ('+f"{100*x['top5']/604:.1f}"+'%)</td><td>'+str(x['genus'])+'/604 ('+f"{100*x['genus']/604:.1f}"+'%)</td><td>'+f'{100*macro:.1f}'+'%</td></tr>'
per=''
for label in labels:
 a=r['methods']['centroid']['per_source_name'][label];b=r['methods']['linear']['per_source_name'][label];per+='<tr><th>'+e(label)+'</th><td>'+str(a['count'])+'</td><td>'+str(a['top1'])+'/'+str(a['count'])+'</td><td>'+str(b['top1'])+'/'+str(b['count'])+'</td></tr>'
cards=[]
for i,x in enumerate(rows):
 preds=''.join('<li>'+names[key]+': '+e(r['methods'][key]['predictions'][i])+(' ✓' if r['methods'][key]['predictions'][i]==x['source_species'] else '')+'</li>' for key in ('centroid','linear'));top=', '.join(e(v) for v in r['methods']['linear']['top5_names'][i]);cards.append('<article><a href="/results/r112/species-scaleup/'+e(x['source_crop'])+'"><img loading="lazy" src="/results/r112/species-scaleup/'+e(x['source_crop'])+'" alt="Source crop"></a><div><h3>'+e(x['source_species'])+'</h3><p>Annotation '+e(x['annotation_id'])+' · source location component '+e(x['source_site_component'])+'</p><ul>'+preds+'</ul><p>Linear-head top five: '+top+'</p></div></article>')
page='''<!doctype html><html lang="en"><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>80-name real-crop development baseline</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}article{display:flex;gap:20px;background:white;border:1px solid #d0ddd5;margin:12px 0;padding:14px}article img{width:180px;height:180px;object-fit:contain;background:#eee}article h3{margin:0}@media(max-width:600px){article{display:block}}</style><body><main><nav><a href="/results/r112/">Synthetic training</a> · <a href="/results/r112/species-scaleup/">Source pool</a></nav><h1>80-name real-crop development baseline</h1><p class="notice"><strong>Not Academy-scan or independent biological accuracy.</strong> These are pre-cut source crops, not automatically detected diatoms. The UDE team supplied the names, which have not been independently reconciled with Academy taxonomy. The local location components are conservative proxies and may miss related samples; some source locations and images were used earlier. The model must choose one of 80 known labels and cannot reject an unknown species.</p><p>Frozen ImageNet ResNet-18 features were fitted with either one centroid per source name or a linear classification head, using 2,830 training crops. Results use all 604 location-separated tuning crops; these tuning images are now consumed for development. The separate 566-crop check partition was not used in this experiment. Later, a source-breadth extraction accessed 164 other crops from its check-location components and 148 from tuning-location components. Training on that full pool would invalidate a location-unseen comparison using this split. <a href="/results/r112/species-breadth/prior-split-exposure.json">See the location exposure audit.</a> Grayscale input is letterboxed to 224 pixels, which can discard fine diagnostic detail.</p><table><tr><th>Method</th><th>Top-1 source-name match</th><th>Top-5 source-name match</th><th>Top-1 genus match</th><th>Macro source-name match</th></tr>'''+table+'''</table><p>These counts compare predictions with source dataset names, not independently verified Academy species. Top-5 and genus results use the same forced-choice 80-name vocabulary. No segmentation, overlap recovery, out-of-distribution rejection or field-level result is measured.</p><p><a href="protocol.json">Frozen protocol</a> · <a href="results.json">All scores and per-name metrics</a> · <a href="verify.json">Recomputation and feature replay</a></p><h2>By source name</h2><table><tr><th>UDE source name</th><th>Tuning crops</th><th>Centroid top-1</th><th>Linear top-1</th></tr>'''+per+'''</table><h2>Individual tuning crops</h2><p>A check mark means the prediction matches the source dataset name, not that a scientist confirmed the identification. Click a crop to enlarge.</p>'''+''.join(cards)+'''</main></html>''';(O/'index.html').write_text(page);print('published',len(cards),'tuning cases')
