import json,html
from pathlib import Path
R=Path(__file__).resolve().parents[1];O=R/'results/l80';data=json.loads((O/'results.json').read_text());rows=data['records'];assert len(rows)==39
summary=[]
for family,category,label in [('khanom','r84','Khanom R84'),('khanom','r85','Khanom R85'),('gunduz','real','Gündüz real'),('gunduz','synthetic','Gündüz synthetic')]:
 group=[x for x in rows if (x['family'],x['category'])==(family,category)];total=sum(x['references'] for x in group)
 for budget in (5,10,30):
  vals=[sum(x['scores'][name][str(budget)]['0.5']['matched'] for x in group) for name in ('original','defer08','defer095')];summary.append(f'<tr><td>{label} ({len(group)} fields)</td><td>{budget}</td>'+''.join(f'<td>{v}/{total}</td>' for v in vals)+'</tr>')
sections=[]
for x in rows:
 vals=[x['scores'][name]['10']['0.5']['matched'] for name in ('original','defer08','defer095')];sections.append(f'<section><h3>{html.escape(x["name"])}</h3><p>{x["references"]} source regions; top-10 matches at 0.5: original {vals[0]}, defer 0.8 {vals[1]}, defer 0.95 {vals[2]}. {len(x["links"]["defer08"])} candidates delayed at 0.8, retained in the list. <a href="/{html.escape(x["source"])}">Source image</a></p><img src="{x["image"]}" alt="Source reference, original and deferred top candidates and postponed alternatives"></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>L80 deferred alternatives</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/r92/khanom/">Underlying model results</a><h1>L80: Can we prioritize diverse masks without deleting alternatives?</h1><p class="box">Retrospective replay on 39 previously viewed cases; real references have no or only slight annotated intersections, and synthetic crossings are not microscope optics. The scores do not establish severe-overlap recovery, sample independence, false-positive rate, review time or human-level performance.</p><p>The unchanged input is a SAM-first alternating list. The previously tested L69 rule moves a mask later if it overlaps an earlier nondeferred mask from the other model at IoU ≥0.8 or ≥0.95. Every alternative and its link remain available. A link means only a ranking decision, not a biological identity judgment. Both model streams already contain their own filtering; this replay cannot recover a mask discarded earlier.</p><table><tr><th>Cases</th><th>Max candidates per field</th><th>Unchanged</th><th>Defer 0.8</th><th>Defer 0.95</th></tr>'''+''.join(summary)+'''</table><p>At 0.8 the replay delayed 54 candidates across the 39 cases, retaining all of them. On the two weak-intersection pairs, all four reference regions were matched in the first ten under all three orders.</p><p><strong>Important counterexample:</strong> Two distinct, perfect masks with IoU 0.882 both match their targets in the first two positions of the original list. With 0.8 deferral, one is moved behind an unrelated third candidate and only one of two targets is matched at that budget; both remain recoverable when the entire list is inspected. The 0.95 rule does not delay that particular pair, but offers no general guarantee.</p><p>Images and derivatives: Khanom, <a href="https://zenodo.org/records/21295927">Images of diatoms for CNN model training</a>, CC BY 4.0; Gündüz <a href="https://www.kaggle.com/datasets/huseyingunduz/diatom-dataset">Diatom Dataset</a>, CC BY-NC-SA 4.0. Synthetic scenes include local Academy scan backgrounds. Separate model terms apply.</p><p><a href="protocol.json">Frozen replay protocol</a> · <a href="results.json">All ranks, links, scores and counterexample</a> · <a href="verify.log">Verification</a> · <a href="/research/L80.md">Research note</a></p><h2>Comparisons</h2>'''+''.join(sections)+'</html>')
