import json,html
from pathlib import Path
R=Path(__file__).resolve().parents[1];O=R/'results/l82';d=json.loads((O/'results.json').read_text());rows=d['records'];assert len(rows)==19
summary=[]
for cat,label in [('real','Gündüz real'),('synthetic','Gündüz synthetic')]:
 a=[r for r in rows if r['category']==cat];den=sum(r['references'] for r in a)
 for th in ('0.5','0.75'):
  v=[sum(r['scores'][name]['30'][th]['matched'] for r in a) for name in ('original_yolo','adapted_yolo','baseline','combined')];summary.append(f'<tr><td>{label} ({len(a)} cases)</td><td>{th}</td>'+''.join(f'<td>{n}/{den}</td>' for n in v)+'</tr>')
sections=[]
for r in rows:
 v=[r['scores'][name]['30']['0.5']['matched'] for name in ('original_yolo','adapted_yolo','baseline','combined')];sections.append(f'<section><h3>{html.escape(r["name"])}</h3><p>{r["references"]} reference regions; adapted YOLO produced {r["adapted_count"]} candidates; original YOLO / adapted YOLO / SAM+Cellpose / combined top-30 IoU 0.5 matches: {v[0]} / {v[1]} / {v[2]} / {v[3]}. <a href="/{html.escape(r["source"])}">Original source image</a> · <a href="{r["archive"]}">Adapted model masks, confidences and boxes</a></p><img src="{r["image"]}" alt="Source regions, original YOLO masks, adapted YOLO masks and combined candidates"></section>')
(O/'index.html').write_text('''<!doctype html><html lang="en"><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>L82 freshwater YOLO pilot</title><style>body{font:16px/1.55 system-ui;max-width:1200px;margin:2em auto;padding:0 1em;color:#18342f}a{color:#006b59}img{max-width:100%}.box{background:#fff2d6;padding:1em}section{border-top:1px solid #ccc}td,th{padding:.4em;border:1px solid #bbc}table{border-collapse:collapse}</style><a href="/results/board.html">Research board</a> · <a href="/results/r94/">Original marine model transfer</a><h1>L82: A small freshwater YOLO adaptation pilot</h1><p class="box">Exploratory training on 16 public images and 18 polygons; labels are incomplete and cover only two taxa. The 19 evaluation cases have been viewed in earlier studies. File and coarse visual-hash checks do not establish separate biological specimens, slides or model-training independence. Real source regions exclude intersecting boxes, and synthetic crossings are not physical microscopy. These results do not demonstrate severe real-overlap or human-level performance.</p><p>Starting with the tested marine chain checkpoint, train on the fixed freshwater images for 20 CPU epochs and use the final-epoch weights. The source annotations omit some organisms, which may teach the model to treat them as background. The framework's required validation path points to the training images; its training-time validation is not an independent metric and was not used to pick the evaluation checkpoint. Compare original and adapted YOLO at the same frozen confidence and inference settings; the SAM/Cellpose pipelines use different computation.</p><table><tr><th>Cases</th><th>IoU</th><th>Original YOLO</th><th>Adapted YOLO</th><th>SAM + Cellpose</th><th>SAM + Cellpose + adapted YOLO</th></tr>'''+''.join(summary)+'''</table><p>Cells show one-to-one source/visible-region matches with up to 30 masks per image. The adapted model improved over its marine starting point on these viewed cases but did not improve the combined SAM/Cellpose top-30 total. On synthetic crossings it matched 0/4 in one front order and 1/4 in the other. Incomplete source labels mean unmatched model masks are not verified false positives.</p><p>The training/evaluation field screen compared all 18 training polygons with all 11 real evaluation fields; none met the predeclared coarse similarity flag (minimum hash distance 10; threshold 8). This screen can miss related images and is not proof of independence. Original RGB, source polygons and focal-stack backgrounds are preserved; no inferred hidden texture is used for species identification.</p><p>Sources: <a href="https://zenodo.org/records/21295927">Khanom training images</a>, CC BY 4.0; <a href="https://www.kaggle.com/datasets/huseyingunduz/diatom-dataset">Gündüz evaluation images</a>, CC BY-NC-SA 4.0; <a href="https://huggingface.co/patcdaniel/pseudo-nitzschia-yolo11s-seg">original marine model</a> (MIT-tagged card). Derived checkpoint and synthetic output reuse requires checking all source and software terms, including noncommercial conditions. Academy backgrounds remain subject to local terms.</p><p><a href="protocol.json">Protocol</a> · <a href="source-audit.json">Source similarity audit</a> · <a href="training-manifest.json">Training data manifest</a> · <a href="training-run.json">Checkpoint provenance</a> · <a href="results.json">All scores and prediction archives</a> · <a href="verify.log">Verification</a> · <a href="/research/L82.md">Research note</a></p><h2>Case comparisons</h2>'''+''.join(sections)+'</html>')
