"""Build posthoc target-exclusive diagnostic pages."""
import html,json
from pathlib import Path
ROOT=Path(__file__).resolve().parents[1];OUT=ROOT/'results/l59';rs=json.loads((OUT/'results.json').read_text())['records'];rows=''
for stage in ('pre_final','final'):
 values=[a['own_minus_other_exclusive'] for r in rs for a in r['stages'][stage]['assignments']];pos=sum(v is not None and v>0 for v in values);zero=sum(v is not None and v==0 for v in values);neg=sum(v is not None and v<0 for v in values);unknown=sum(v is None for v in values);rows+=f'<tr><td>{stage}</td><td>{len(values)}</td><td>{pos}</td><td>{zero}</td><td>{neg}</td><td>{unknown}</td></tr>'
def fmt(x):return 'unidentifiable' if x is None else f'{x:.3f}'
cards=''
for r in rs:
 f=r['stages']['final'];pre=r['stages']['pre_final'];details=[]
 for stage,val in [('pre-final',pre),('final',f)]:
  summary='; '.join('target '+str(a['target']+1)+' · '+html.escape(str(a['candidate_id']))+' · IoU '+fmt(a['target_iou'])+' · exclusive coverage ['+', '.join(fmt(x) for x in a['exclusive_coverage'])+'] · own-minus-other '+fmt(a['own_minus_other_exclusive']) for a in val['assignments']) or 'no assigned masks';details.append('<p><strong>'+stage+':</strong> '+summary+'.</p>')
 cards+=f'<article><h2>{html.escape(r["dataset"])} · {html.escape(r["condition"])} · pair {r["pair"]}</h2><img src="{r["image"]}" alt="Synthetic targets, their geometric exclusive regions, and reference-selected final and pre-final masks"><p>Geometric exclusive pixel counts: {f["exclusive_areas"]}; target-mask IoU: {fmt(f["target_iou"])}. The two assigned-mask panels are selected using target labels after prediction, solely to explain matching.</p>'+''.join(details)+f'<p><a href="{r["source_archive"]}">Original simulated planes and synthetic targets</a> · <a href="{r["candidate_results"]}">Candidate rankings</a>.</p></article>'
page='''<!doctype html><html lang="en"><meta charset="utf-8"><meta name="viewport" content="width=device-width"><title>Object-specific evidence audit</title><style>body{font:16px/1.5 system-ui;background:#f3f6ef;color:#203a38}main{max-width:1250px;margin:auto;padding:24px}.notice{background:#fff2d6;padding:16px}article{background:white;padding:14px;margin:16px 0}img{width:100%}table{border-collapse:collapse}th,td{padding:8px;border:1px solid #aaa}.table{overflow:auto}</style><main><nav><a href="/results/performance/">Performance monitor</a> · <a href="/research/L59.md">Research notes</a></nav><h1>Object-specific geometry in synthetic crossings</h1><p class="notice">Reference masks are used to choose assignments and compute every measurement on this page. This is posthoc diagnosis, not an inference feature. A target-exclusive region is geometric and is not a label of what a microscope can actually see. The images and targets are synthetic and do not establish human-level or real-slide accuracy.</p><p>For each assigned mask, we measure coverage of the assigned target outside the other target and coverage of the other target outside the assigned target. Their difference can expose masks covering more of the other target’s exclusive pixels. A negative value is an ambiguous geometric pattern, not a confirmed error. If either exclusive region is empty, the difference is unidentifiable. We display pre-final and final maximum-cardinality, maximum-total-IoU one-to-one assignments; changing tie resolution or reference conventions can change the observations.</p><table><tr><th>Stage</th><th>Assigned masks</th><th>Positive differences</th><th>Zero</th><th>Negative</th><th>Unidentifiable</th></tr>ROWS</table><p>Unmatched targets are not included in the assignment rows. These counts are descriptive, not detection accuracy. True distinct objects may overlap so strongly that exclusive geometry is tiny or empty; the independent tests include an entirely coincident pair. <a href="protocol.json">Audit protocol</a> · <a href="results.json">Per-assignment diagnostics</a> · <a href="/results/l58/">Near-duplicate scoring example</a>. Foreground source: Gündüz et al., <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/">CC BY-NC-SA 4.0</a>.</p>CARDS</main></html>'''.replace('ROWS',rows).replace('CARDS',cards);(OUT/'index.html').write_text(page)
