import html,json
from pathlib import Path
R=Path(__file__).resolve().parents[1];O=R/'results/r95/partial/train-diagnostic';d=json.loads((O/'results.json').read_text());rows=d['records'];assert len(rows)==28;table=[]
for th in ('0.5','0.75'):
 for tag,title in [('hidden','Withheld annotations'),('visible','Retained training annotations'),('all','All source annotations')]:
  den=sum(r['scores']['standard']['30']['subsets'][tag][th]['reference_count'] for r in rows);vals=[sum(r['scores'][m]['30']['subsets'][tag][th]['matched'] for r in rows) for m in ('standard','oracle','full')];table.append(f'<tr><td>{title}</td><td>{th}</td>'+''.join(f'<td>{n}/{den}</td>' for n in vals)+'</tr>')
sections=[]
for r in rows:
 vals=[r['scores'][m]['30']['subsets']['hidden']['0.5']['matched'] for m in ('standard','oracle','full')];hidden='none' if r['hidden_id'] is None else str(r['hidden_id']);sections.append(f'<section><h3>{html.escape(r["name"])} · {html.escape(r["source_group"])}</h3><p>Withheld source annotation: {html.escape(hidden)}. Hidden-only top-30 matches, standard / oracle-ignore / full-label: {vals[0]} / {vals[1]} / {vals[2]}. <a href="/{html.escape(r["image"])}">Training input</a> · <a href="{r["archive"]}">Predictions and reference masks</a></p><img src="{r["comparison"]}" alt="Visible and withheld source masks, followed by predictions from the three training variants"></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>R95 withheld training annotations</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/r95/partial/">Partial-label experiment</a> · <a href="/results/board.html">Research board</a><h1>R95: Did the models recover deliberately withheld labels?</h1><p class="box">This is a <strong>training-image diagnostic</strong>, not an independent test. All three models saw these images; the full-label model also received all displayed labels, and the oracle-ignore model was told the hidden boxes. Results cannot estimate unseen-slide performance, human proficiency, or severe real-overlap recovery.</p><p>The original source annotations contain 47 polygons. One was deliberately hidden on each of 14 multi-label fields; 33 remained visible as labels. We rasterized each source polygon at 512 pixels, then scored YOLO masks at fixed confidence 0.25 with at most 30 masks per field.</p><table><tr><th>Reference subset</th><th>IoU</th><th>Partial standard</th><th>Partial oracle-ignore</th><th>Full source labels</th></tr>'''+''.join(table)+'''</table><p>Hidden-only and visible-only rows are calculated with separate one-to-one assignments: a prediction may be matched in both separate calculations. The all-reference row assigns candidates jointly and must not be obtained by adding those rows. In the single joint assignment at IoU 0.5, the hidden IDs matched were 2, 2 and 4 respectively; identities can depend on assignment ties.</p><p>Across 28 training images, the models emitted 30, 32 and 40 post-YOLO-NMS masks respectively. Other material in the images can be unannotated, so unmatched outputs are not established false positives. The oracle boxes masked only direct classification-negative gradients; they did not modify instance-mask loss or eliminate shared-parameter effects.</p><p>Sources: <a href="https://zenodo.org/records/21295927">Khanom diatoms</a> and <a href="https://doi.org/10.5281/zenodo.7079005">River Menne data</a>, CC BY 4.0. Starting model and software terms also apply.</p><p><a href="protocol.json">Protocol</a> · <a href="results.json">Per-field predictions and scores</a> · <a href="verify.log">Verification</a> · <a href="/research/R95.md">Research note</a></p><h2>Training-field comparisons</h2><p>Green regions were retained as labels; orange regions were deliberately withheld. A colored prediction is a candidate, not a verified diatom identification.</p>'''+''.join(sections)+'</html>')
