import json,html
from pathlib import Path
R=Path(__file__).resolve().parents[1];O=R/'results/r93';d=json.loads((O/'results.json').read_text());rows=d['records'];assert len(rows)==45
labels=[('khanom','r84','all','Khanom R84'),('khanom','r85','all','Khanom R85'),('gunduz','real','all','Gündüz real'),('gunduz','synthetic','all','Gündüz synthetic'),('human','human','collapsed','Human cells, geometry-collapsed'),('human','human','severe','Human cells, severe subset')];tables={}
for pol in ('08','095'):
 table=[]
 for fam,cat,tag,label in labels:
  a=[x for x in rows if (x['family'],x['category'])==(fam,cat)];z=[x['policies'][pol]['budgets']['10'] for x in a];refs=sum(x['refset_counts'].get(tag,0) for x in a);groups=sum(x['groups_opened'] for x in z);masks=sum(x['masks_examined'] for x in z);b=sum(x['grouped'][tag]['0.5']['matched'] for x in z);f=sum(x['flat_equal_masks'][tag]['0.5']['matched'] for x in z);table.append(f'<tr><td>{label} ({len(a)} fields)</td><td>{groups}</td><td>{masks}</td><td>{b}/{refs}</td><td>{f}/{refs}</td></tr>')
 tables[pol]=''.join(table)
sections=[]
for r in rows:
 tag='collapsed' if r['family']=='human' else 'all';b=r['policies']['08']['budgets']['10'];sections.append(f'<section><h3>{html.escape(r["name"])}</h3><p>{b["groups_opened"]} groups contain {b["masks_examined"]} masks. Grouped / flat equal-mask matches at IoU 0.5: {b["grouped"][tag]["0.5"]["matched"]} / {b["flat_equal_masks"][tag]["0.5"]["matched"]} of {r["refset_counts"][tag]} reference regions. <a href="/{html.escape(r["source"])}">Original source image</a></p><img src="{r["image"]}" alt="Reference masks, original candidate order, and grouped alternatives at equal mask count"></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>R93 ambiguity-aware grouping</title><style>body{font:16px/1.55 system-ui;max-width:1150px;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/l80/">Prior deferral experiment</a><h1>R93: Can we show similar masks without losing alternatives?</h1><p class="box">Retrospective replay on previously viewed cases. Human-cell fields are out-of-domain mechanism controls, not diatoms. The real diatom annotations have no or slight intersections, and synthetic crossings do not reproduce microscope optics. These results do not establish real severe-overlap or human-level performance.</p><p>Each group contains an earlier candidate and cross-model masks whose IoU with it is at least 0.8. The link is a presentation suggestion, not a same-organism decision. Every candidate remains individually accessible. The comparison opens up to ten groups per field, then compares those <em>actual masks</em> against the same number from the ungrouped original ordering in each field. It does not estimate time or assume an expanded group is a single review action.</p><table><tr><th>Previously examined cases</th><th>Groups opened</th><th>Masks examined</th><th>Grouped matches</th><th>Flat equal-mask matches</th></tr>'''+tables['08']+'''</table><p>Matches use one-to-one IoU ≥0.5 against the available source regions. The severe human-cell subset overlaps the collapsed-reference row and is not additive. Human-cell cases include uncertain exact-coordinate duplicate annotations, so a geometry-collapsed sensitivity analysis is shown; their identity is not resolved. The 12 severe references have nonidentical partners with at least half of the smaller polygon covered.</p><p>At the stricter 0.95 grouping threshold:</p><table><tr><th>Cases</th><th>Groups opened</th><th>Masks examined</th><th>Grouped matches</th><th>Flat equal-mask matches</th></tr>'''+tables['095']+'''</table><p>The flat comparison cost includes every mask in the opened groups, not just one representative. These are candidate-access simulations, not observations of human review. In two deterministic near-coincident-mask controls with swapped source models, the first expanded group contains both true masks and costs two mask inspections; a flat two-mask list also recovers both. An overlapping group may contain two different specimens, and a single mask may match more than one plausible reference: neither grouping nor matching settles identity.</p><p><a href="viewer/">Inspect every mask in one example as a separate image</a>. The original source pixels are retained, and group labels do not count organisms.</p><p>Sources: <a href="https://zenodo.org/records/21295927">Khanom diatom dataset</a>, CC BY 4.0; <a href="https://www.kaggle.com/datasets/huseyingunduz/diatom-dataset">Gündüz diatoms</a>, CC BY-NC-SA 4.0; <a href="https://huggingface.co/datasets/YaroslavPrytula/Revvity-25">Revvity human cells</a>, CC BY-NC 4.0. Model terms also apply; Academy-derived synthetic backgrounds remain subject to local conditions.</p><p><a href="protocol.json">Protocol</a> · <a href="results.json">All groups and scores</a> · <a href="verify.log">Replay verification</a> · <a href="/research/R93.md">Research note</a></p><h2>Case comparisons</h2>'''+''.join(sections)+'</html>')
