import html,json
from pathlib import Path
R=Path(__file__).resolve().parents[1];O=R/'results/l85';rows=json.loads((O/'results.json').read_text())['records'];methods=('confidence','mask_agreement','sam_quality');tables=[]
for group in ('hidden training','real','synthetic','R97 real'):
 a=[r for r in rows if r['group']==group];total=sum(r['references'] for r in a)
 for t in ('0.5','0.75'):
  for n in ('5','10','20','30'):
   values=[sum(x['scores'][m][n][t]['matched'] for x in a) for m in methods];tables.append(f'<tr><td>{group} ({len(a)} fields)</td><td>{t}</td><td>{n}</td>'+''.join(f'<td>{v}/{total}</td>' for v in values)+'</tr>')
cards=[]
for r in rows:
 if r['group'] not in ('synthetic','R97 real'):continue
 s=[r['scores'][m]['10']['0.5']['matched'] for m in methods];name=html.escape(r['name']);src=html.escape(r['source']);cards.append(f'<section><h3>{src}: {name}</h3><p>Top 10 matches at IoU 0.5: confidence {s[0]}, agreement {s[1]}, SAM quality {s[2]} of {r["references"]}. The image shows original candidate streams, not the new rankings.</p><a href="/results/{src}/{name}.png"><img loading="lazy" src="/results/{src}/{name}.png" alt="Source and original candidate overlays"></a></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>L85 candidate ranking</title><style>body{font:16px/1.5 system-ui;color:#18342f;max-width:1200px;margin:2em auto;padding:0 1em}a{color:#006b59}img{width:100%}table{border-collapse:collapse}td,th{border:1px solid #bbc;padding:.4em}.warn{background:#fff2d6;padding:1em}section{border-top:1px solid #ccd}</style><a href="/results/board.html">Research board</a> · <a href="/results/r96/">Original paired-mask study</a> · <a href="/results/r97/">Different-file replay</a><h1>L85: Does paired-mask agreement help rank masks?</h1><p class="warn">Exploratory retrospective analysis on already inspected cases; not an independent benchmark. The training targets were in training images; real references exclude intersecting boxes; synthetic scenes do not model real optics. Agreement is not independent consensus: SAM was prompted by the YOLO box.</p><p>Keep the exact same up-to-30 SAM masks per field. Compare their original YOLO box-confidence order with descending IoU between the corresponding YOLO and SAM masks, and with descending SAM mask-quality score. Original rank breaks ties. The rankings use no reference labels and discard no candidate; counts are one-to-one reference matches at a fixed mask budget.</p><table><tr><th>Group</th><th>IoU ≥</th><th>Budget</th><th>YOLO confidence</th><th>Mask agreement</th><th>SAM quality</th></tr>'''+''.join(tables)+'''</table><p>Agreement did not consistently improve early recovery. SAM quality ranked more source regions within the first 5 or 10 masks in R97, but this is a development finding on viewed fields, not evidence of generalization or trustworthy confidence calibration. The four synthetic crossing scenes together contain eight reference supports: at IoU 0.5 each ranking matched only two at top 30 and no crossing recovered both supports. These are artificial opaque composites.</p><p>Low-confidence candidate generation, internal NMS and the 100-output cap remain unchanged. Source annotations can omit real objects, so unmatched candidates are not validated false positives. The experiment did not measure review time, true real-overlap segmentation, species identification, or human-level performance.</p><p>All 56 candidate archives, per-pair agreement, rankings and scores were reconstructed; verification uses the same matching helper and did not repeat model inference. The visual panels below come from the parent studies and show their original streams, not re-ranked overlays.</p><p><a href="protocol.json">Frozen protocol</a> · <a href="results.json">All ranks and scores</a> · <a href="verify.log">Verification</a> · <a href="/research/L85.md">Research note</a></p>'''+''.join(cards)+'</html>')
