import json,collections
from pathlib import Path
import numpy as np
import matplotlib;matplotlib.use('Agg')
import matplotlib.pyplot as plt
R=Path(__file__).resolve().parents[1];O=R/'results/l73';rows=json.loads((O/'results.json').read_text())['records']; groups=collections.defaultdict(list)
for r in rows:groups[r['group'].strip()].append(r)
names=['synthetic separated','synthetic crossing','menne','new source gomphonema','new source pinnularia'];fig,axes=plt.subplots(1,2,figsize=(11,4));x=np.arange(len(names));labels=['synthetic\nseparated','synthetic\ncrossing','Menne','new source\nGomphonema','new source\nPinnularia'];
for off,p,color in [(-.25,'nms08','#367f70'),(0,'nms095','#ca7754'),(.25,'eligible','#7059a5')]:axes[0].bar(x+off,[sum(r['scores'][p]['all'] for r in groups[n]) for n in names],.25,label=p,color=color)
axes[0].set_xticks(x,labels);axes[0].set_ylabel('All-candidate matched source polygons');axes[0].legend(fontsize=8);axes[1].bar(['IoU 0.8 NMS','IoU 0.95 NMS','No NMS'],[sum(r[k] for r in rows) for k in ('nms08_count','nms095_count','eligible_count')],color=['#367f70','#ca7754','#7059a5']);axes[1].set_ylabel('Total eligible candidate masks');fig.tight_layout();fig.savefig(O/'suppression-comparison.png',dpi=140);plt.close(fig)
trs=''.join(f'<tr><th>{n}</th><td>{sum(r["references"] for r in groups[n])}</td><td>{sum(r["scores"]["nms08"]["all"] for r in groups[n])}</td><td>{sum(r["scores"]["nms095"]["all"] for r in groups[n])}</td><td>{sum(r["scores"]["eligible"]["all"] for r in groups[n])}</td><td>{sum(r["raw_oracle_all"] for r in groups[n])}</td></tr>' for n in names)
(O/'index.html').write_text(f'''<!doctype html><html lang="en"><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>L73 suppression audit</title><style>body{{font:16px/1.55 system-ui;max-width:1050px;margin:2em auto;padding:0 1em;color:#18342f}}img{{max-width:100%}}a{{color:#006b59}}table{{border-collapse:collapse}}td,th{{border:1px solid #b8ccc0;padding:.5em;text-align:left}}.box{{background:#fff2d6;padding:1em}}</style><a href="/results/board.html">Research board</a><h1>L73: Are suppressed masks hiding source matches?</h1><p class="box">All 40 fields are already examined development scenes. Real severe overlaps are absent or unvalidated; this replay does not prove suppression safe for overlapping diatoms.</p><p>Across 70 source polygons, all eligible masks matched 58 at IoU ≥0.5, and NMS at 0.8 retained enough candidates to match those same <em>counts</em>. It retained 1,343 candidates versus 2,306 eligible masks. A raw-mask oracle matched 59, indicating one reference was unavailable after area/quality eligibility, although this is a retrospective diagnostic and unmatched masks are not verified false positives.</p><img src="suppression-comparison.png" alt="Matched counts and candidate volumes under suppression policies"><table><thead><tr><th>Source group</th><th>Reference polygons</th><th>All with NMS 0.8</th><th>All with NMS 0.95</th><th>All eligible, no NMS</th><th>All raw oracle</th></tr></thead><tbody>{trs}</tbody></table><p>At top 30 per field, NMS 0.8 and deferring its aliases both matched 52 source polygons, NMS 0.95 matched 50, and no NMS matched 48. These are same-budget ranking outcomes, not precision measurements. Deferral preserves every eligible candidate but does not restore masks rejected by area or quality. All original raw point masks and suppression links remain archived.</p><p>The earlier <a href="/results/r71/">constructed overlap control</a> demonstrated that a high-IoU rule can suppress one of two valid overlapping masks; its risk is not disproved by this particular real-source replay. One-to-one reference matching maximizes total matched counts and does not prove that matching pairs have the same biological identities under every policy.</p><p><a href="/research/L73.md">Research note</a> · <a href="protocol.json">Frozen protocol</a> · <a href="results.json">Full ranks, links and counts</a> · <a href="/research/l73_verify.py">Replay verifier</a> · <a href="/results/l71/">Original synthetic and Menne masks</a> · <a href="/results/r84/">Original new-source masks</a></p></html>''')
