"""Publish projected same-model proposal disagreement, not confidence."""
import html,json
from pathlib import Path
R=Path(__file__).resolve().parents[1];O=R/'results/r112/projected-mask-uncertainty';r=json.loads((O/'results.json').read_text());E=html.escape;table=''
for family in ('crowded','separated','breadth','all'):
 s=r['summary'][family];table+='<tr><th>'+E(family.title())+'</th><td>'+str(s['instances'])+'</td><td>'+str(s['nonzero_disagreement'])+'</td><td>'+f"{100*s['fraction_q10_q50_q90'][1]:.1f}%"+'</td><td>'+f"{100*s['fraction_q10_q50_q90'][2]:.1f}%"+'</td><td>'+str(s['center_overlap_pairs'])+'</td><td>'+str(s['possible_union_overlap_pairs'])+'</td></tr>'
examples=[x for x in r['records'] if 'examples' in x];examples.sort(key=lambda x:(-x['disputed_fraction_of_union'],x['family'],x['scene_id'],x['instance_index']));cards=[]
for x in examples:
 imgs=[('Center proposal','center'),('Minus proposal','minus'),('Plus proposal','plus'),('Proposal agreement','vote')];source=('/results/r112/species-breadth/images/' if x['family']=='breadth' else '/results/r112/species-scaleup/images/')+x['annotation_id']+'.png';cards.append('<article><h3>'+E(x['family'])+' · '+E(x['scene_id'])+' · instance '+str(x['instance_index']+1)+'</h3><p><a href="'+E(source)+'">'+E(x['source_name'])+' · '+E(x['annotation_id'])+'</a> · proposed union '+str(x['union_area'])+' pixels; '+f"{100*x['disputed_fraction_of_union']:.1f}%"+' of union is not shared by all three proposals · '+E(x['mask_triage'])+'</p><div class="views">'+''.join('<figure><a href="/'+E(x['examples'][key]['path'])+'"><img loading="lazy" src="/'+E(x['examples'][key]['path'])+'" alt="'+E(name)+'"></a><figcaption>'+E(name)+'</figcaption></figure>' for name,key in imgs)+'</div></article>')
page='''<!doctype html><html lang="en"><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>Projected mask proposal disagreement</title><style>body{font:16px/1.5 system-ui;color:#193b37;background:#f2f6f2;margin:0}main{max-width:1250px;margin:auto;padding:24px}a{color:#076c59}.notice{background:#fff8df;border-left:5px solid #b88418;padding:15px}article{background:white;border:1px solid #cbd8d1;padding:12px;margin:14px 0}.views{display:grid;grid-template-columns:repeat(4,minmax(0,1fr));gap:8px}figure{margin:0}.views img{width:100%;height:220px;object-fit:contain;background:#e8eee8}figcaption{font-size:.85rem}table{border-collapse:collapse;background:white;max-width:100%;display:block;overflow:auto}td,th{border:1px solid #cbd8d1;padding:8px;text-align:left}pre{overflow:auto;background:#e6eee8;padding:12px}@media(max-width:700px){.views{grid-template-columns:repeat(2,minmax(0,1fr))}}</style><main><nav><a href="/results/r112/">Synthetic training</a> · <a href="/results/r112/instance-contribution-audit/">Rendered-pixel contributions</a></nav><h1>Projected mask proposal disagreement</h1><p class="notice"><strong>Three correlated model proposals are not a confidence estimate or verified anatomy.</strong> All masks were produced by the same model with different prompts, and they can agree while selecting the wrong object or background. Source names are not independently reconciled, and the Academy background may contain unlabeled organisms, including under insertions.</p><p>Each of 3,998 insertions has three source-mask proposals reprojected into its original development scene. The existing scene was rendered using the center proposal only; the other outlines are alternatives placed over that same image and were not separately rendered. “Disagreement” is the fraction of the union of proposed support pixels that is not included in all three proposals. It is a geometric comparison, not an error rate, probability, visibility score or biological overlap measurement.</p><table><tr><th>Scene set</th><th>Instances</th><th>Any proposal difference</th><th>Median disputed union</th><th>90th percentile</th><th>Center support overlap pairs</th><th>Union overlap pairs</th></tr>'''+table+'''</table><p>The union overlap count asks whether the unions of two instances' alternatives intersect; it does not mean the underlying diatoms overlap. The first 20 cases below have the largest disputed-union fraction, and 10 are evenly spaced references. These are deliberately challenging examples, not a representative accuracy estimate.</p><p><a href="protocol.json">Frozen audit</a> · <a href="results.json">All per-instance and per-scene measurements</a> · <a href="verify.json">Verification</a> · <a href="/diatom_pilot/scaleup_crowded_dataset.py">Dataset readers</a></p><pre>from diatom_pilot.scaleup_crowded_dataset import ScaleupCrowdedDataset
scenes = ScaleupCrowdedDataset("development_training", include_mask_variants=True)
example = scenes[0]  # weak_mask_proposals: [instance, proposal, y, x]</pre><h2>Projected proposal comparisons</h2><p>Orange, cyan and magenta outline the center, minus and plus proposals respectively. In the last panel, green marks pixels proposed by all three and orange marks pixels proposed by one or two. Agreement is not proof of correctness. Click any panel for native pixels.</p>'''+''.join(cards)+'''</main></html>''';(O/'index.html').write_text(page);print('published',len(cards),'comparisons')
