import html,json
from pathlib import Path
R=Path(__file__).resolve().parents[1];O=R/'results/r108';rows=json.loads((O/'results.json').read_text())['records'];selections={x['index']:x for x in json.loads((O/'selection.json').read_text())['records']};table=[]
for view in ('clipped','complete'):
 a=[x for x in rows if x['view']==view]
 for t in ('0.5','0.75'):
  for n in ('10','30','60','all'):
   vals=[sum(x['scores'][k]['ranked'][n][t]['matched'] for x in a) for k in ('variance','boundary')];table.append(f'<tr><td>{view}</td><td>{t}</td><td>{n}</td><td>{vals[0]}/8</td><td>{vals[1]}/8</td></tr>')
cards=[]
for r in rows:
 s=selections[r['index']];v=[r['scores'][k]['ranked']['10']['0.5']['matched'] for k in ('variance','boundary')];source=f'<a href="{html.escape(r["clipped_image"])}">Clipped input</a> · ' if r['clipped_image'] else '';cards.append(f'<section><h2>Field {r["index"]:02d}, {html.escape(r["side"])} edge: {html.escape(r["view"])}</h2><p>Top-ten variance / boundary matches: {v[0]} / {v[1]} of selected source region. Visible fraction: {100*r["visible_fraction"]:.1f}%; planned clipped target: {100*s["target_fraction"]:.0f}% (subject to minimum retained image size).</p><a href="{html.escape(r["panel"])}"><img loading="lazy" src="{html.escape(r["panel"])}" alt="Selected source region and two candidate-stream overlays"></a><p>{source}<a href="/{html.escape(r["source"])}">Native public source image</a> · <a href="{html.escape(r["archive"])}">Masks, crops and points</a></p></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>R108 four-edge controlled replay</title><style>body{font:16px/1.5 system-ui;color:#18342f;max-width:1200px;margin:2em auto;padding:0 1em}a{color:#006b59}img{max-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/r107/">Prior boundary study</a><h1>R108: Frozen boundary prompts on four edges</h1><p class="warn">Different source archive, but all eight fields were studied previously and some data from the archive supplied earlier model training. These are source-reference-constructed clips, not independently annotated naturally broken valves. Biological independence, real severe overlap and human-level segmentation are not established.</p><p>Choose eight R105-retained source fields from saved annotation geometry. For each source, construct one partial view using a scheduled left, right, top or bottom edge and a target remaining-area fraction, while also testing the complete view. The source mask selects the crop and the single target for scoring; model prompts use only image pixels and the known content rectangle. Other diatoms in the field are not evaluated.</p><table><tr><th>View</th><th>Mask IoU ≥</th><th>Budget per field</th><th>Variance-only</th><th>Frozen boundary allocation</th></tr>'''+''.join(table)+'''</table><p>Each cell counts one-to-one matches to the eight selected source regions. At IoU ≥0.5, both methods match 8/8 clipped targets within 30 masks. In the first ten, variance / boundary matches are 5/8 / 4/8 for clipped views and 8/8 / 6/8 for complete views. The earlier 5/8 → 7/8 clipped top-30 gain did not repeat here; this small, nonrandom, previously viewed source group cannot establish generalization.</p><p>Two fields were assigned to each edge. Actual retained source-region fractions range from 34.6% to 74.2%. One right-edge case targeted 35% but the first eligible source could retain no less than 74.2% while meeting the frozen minimum image-width rule; it was retained without replacement. Percentages measure the published source mask, not an expert-confirmed complete valve.</p><p>Each method uses 256 point queries and 16 crop encodings per view when run independently. The clipped-view implementation shares its 16 encodings across both prompt sets; the full-view variance predictions are cached. The corrected study issued 6,144 new point queries and 256 crop encodings across eight sources. Remaining-pixel and annotation selection does not imply biological identity, and unscored candidates in these busy images are not confirmed false positives. Original RGB, crop coordinates, raw masks and NMS aliases remain available for later classification.</p><p>Verification caught an initial filename collision between the generated clipped input and its visual panel. The input filename was corrected and all 16 views were rerun; this report uses the corrected rerun. Source images, clipped pixels, candidate masks, rankings and scores were reconstructed using the shared clipping/point selector and matcher; eight selected single-query replays reproduced their batch masks. The source-selection script reran byte-identically.</p><p>The earlier similarity screen flagged no selected annotated crops against the R107 source fields, but coarse hashes and filename differences do not establish independent specimens or model-training exposure. Source images: <a href="https://zenodo.org/records/21295927">Khanom, Images of diatoms for CNN model training</a>, CC BY 4.0; separate model/software terms apply.</p><p><a href="protocol.json">Protocol</a> · <a href="selection.json">Geometry-based selection</a> · <a href="results.json">Masks, ranks and scores</a> · <a href="verify.log">Verification</a> · <a href="sample-replay.json">Sample inference replay</a> · <a href="/research/R108.md">Research note</a></p>'''+''.join(cards)+'</html>')
