"""Publish non-quarantined development crop intake."""
import collections,html,json
from pathlib import Path
R=Path(__file__).resolve().parents[1];O=R/'results/r112/new-taxa';s=json.loads((O/'selection.json').read_text());a=json.loads((O/'development-assets.json').read_text());screen=json.loads((O/'similarity-screen.json').read_text());m=json.loads((O/'mask-hypotheses/results.json').read_text());qa=collections.Counter(x['triage'] for x in m['records']);by=collections.defaultdict(list)
for r in a['records']:by[r['source_species']].append(r)
e=html.escape;sections=[]
for name,rows in sorted(by.items()):
 cards=''.join('<figure><a href="'+e(r['source_crop'])+'"><img loading="lazy" src="'+e(r['source_crop'])+'" alt="Development crop"></a><figcaption>'+e(r['annotation_id'])+' · '+e(r['source_slide_group'])+'</figcaption></figure>' for r in rows)
 sections.append('<section><h2>'+e(name)+' · '+str(len(rows))+' crops</h2><div class="grid">'+cards+'</div></section>')
page='''<!doctype html><html lang="en"><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>More development source specimens</title><style>body{font:16px/1.55 system-ui;background:#f4f6f0;color:#193b37;margin:0}main{max-width:1200px;margin:auto;padding:24px}a{color:#096c58}section,.notice{background:white;border:1px solid #cddbd2;padding:16px;margin:14px 0}.notice{border-left:5px solid #bc851b}.grid{display:grid;grid-template-columns:repeat(auto-fit,minmax(190px,1fr));gap:12px}figure{margin:0}img{width:100%;height:190px;object-fit:contain;background:#f4f4f3}figcaption{font-size:.8rem;overflow-wrap:anywhere}</style><body><main><nav><a href="/results/r112/">Synthetic training</a></nav><h1>Additional source specimens</h1><p class="notice"><strong>Development candidates, not verified training labels.</strong> The source names, model masks and specimen independence have not been established. Crop backgrounds can contain unlabeled organisms; filename-group separation is not proof of separate biological samples.</p><p>We selected '''+str(len(s['rows']))+''' crops across 36 additional source-name strings using metadata alone, from '''+str(len({r['source_slide'].removesuffix('.tif').removesuffix('.tiff') for r in s['rows']}))+''' previously unused filename groups (which can be shared by different source names). Similarity screening quarantined '''+str(len(screen['quarantined_new_ids']))+''' of the new candidates; the gallery shows '''+str(len(a['records']))+''' remaining crops. A quarantine flag is a possible match, not proof; unflagged crops may still be related.</p><p>Three model mask proposals were generated for each retained crop. The geometric agreement rule marked '''+str(qa['consistent-unverified'])+''' consistent-unverified and '''+str(qa['needs-inspection'])+''' needing inspection; agreement is not correctness. Candidate validation and test images were used only for the previously specified similarity screen and were not displayed or used for training.</p><p><a href="selection.json">Preselected crops</a> · <a href="download-report.json">Archive checksum report</a> · <a href="similarity-screen.json">Similarity flags</a> · <a href="development-assets.json">Crop provenance and scale</a> · <a href="mask-hypotheses/">Inspect all mask hypotheses</a></p>'''+''.join(sections)+'''</main></body></html>''';(O/'index.html').write_text(page);print('published',len(a['records']),'crops')
