"""Publish point-prompt decoder adaptation on consumed source components."""
import html
import json
from pathlib import Path

R = Path(__file__).resolve().parents[1]
O = R / 'results/r117'
esc = html.escape


def main():
    result = json.loads((O / 'results.json').read_text())
    verify = json.loads((O / 'verify.json').read_text())
    a, b = result['summary']['original'], result['summary']['adapted']
    cards = []
    for row in result['records']:
        x, y = row['outputs']['original'], row['outputs']['adapted']
        cards.append('<article><h3>' + esc(row['id']) + ' · <i>' + esc(row['source_name']) + '</i></h3><p>Source-component IoU: original ' + f"{x['reference_iou']:.3f}" + ' · adapted ' + f"{y['reference_iou']:.3f}" + '</p><a href="' + esc(row['comparison'], quote=True) + '"><img loading="lazy" src="' + esc(row['comparison'], quote=True) + '" alt="Source, candidate reference, original and adapted segmentation"></a><p><a href="' + esc(x['mask'], quote=True) + '">Original mask</a> · <a href="' + esc(y['mask'], quote=True) + '">Adapted mask</a></p></article>')
    page = '''<!doctype html><html lang="en"><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>Source-component decoder adaptation</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:#fff4d7;border-left:5px solid #b88418;padding:15px}article{background:white;border:1px solid #cbd8d1;padding:12px;margin:14px 0}img{width:100%;height:auto}table{border-collapse:collapse;background:white;display:block;overflow:auto}td,th{padding:8px;border:1px solid #cbd8d1;text-align:left}</style><main><nav><a href="/results/r112/">Synthetic training</a> · <a href="/research/R117.md">Research note</a></nav><h1>Point-prompt decoder adaptation on source components</h1><p class="notice"><strong>Development proxy, not automatic Academy segmentation.</strong> Every click is derived from the reference component and the source crop location is already known. Training and comparison images have different source image IDs but were previously used; sample, site and model-pretraining independence are unknown. The source binary component-to-name association is structural and not independently adjudicated.</p><p>Adapted the MobileSAM decoder on 26 source components from 20 images, with the encoder and prompt encoder frozen. Compared it with the original decoder on 25 components from 15 other image IDs using the same reference-derived interior click. Labels outside the target mean “not this queried instance”; they do not establish absence of other diatoms.</p><table><tr><th>Component IoU against source reference</th><th>Original</th><th>Adapted</th></tr><tr><th>At least 0.50</th><td>''' + str(a['iou_ge_05']) + ''' / 25</td><td>''' + str(b['iou_ge_05']) + ''' / 25</td></tr><tr><th>At least 0.75</th><td>''' + str(a['iou_ge_075']) + ''' / 25</td><td>''' + str(b['iou_ge_075']) + ''' / 25</td></tr><tr><th>Mean</th><td>''' + f"{a['mean_iou']:.3f}" + '</td><td>' + f"{b['mean_iou']:.3f}" + '''</td></tr></table><p>Adapted IoU was higher in ''' + str(result['summary']['adapted_higher_iou']) + ''' cases and lower in ''' + str(result['summary']['adapted_lower_iou']) + '''. The source foreground mask contains no separately labeled other-foreground pixels in these 25 comparison crops. Some crops visibly include other diatom-like material; for example <a href="comparisons/1238-box1.png">1238-box1</a> shows a neighboring structure that is not marked in the source foreground mask. These references cannot establish complete-object recall or reliable separation of neighbors.</p><p>The adapted model's quality-head outputs were not trained or calibrated as accuracy estimates; one exceeds 1. Do not use them as confidence thresholds. No species classifier or Academy scan performance was evaluated, and this small reused set cannot justify deployment.</p><p><a href="protocol.json">Frozen protocol</a> · <a href="training.json">Training record</a> · <a href="results.json">All comparisons</a> · <a href="neighbor-audit.json">Source foreground audit</a> · <a href="verify.json">Geometry and score checks</a> · <a href="replay.json">Six inference replays</a>. Gündüz data is CC BY-NC-SA 4.0; retain attribution, noncommercial and share-alike requirements.</p><h2>All comparison components</h2><p>Blue marks the source component or model proposal; orange traces its outline. The pink mark is a reference-derived point, not an automatic detector output.</p>''' + ''.join(cards) + '</main></html>'
    (O / 'index.html').write_text(page)
    print('published', len(cards), 'source-component comparisons')


if __name__ == '__main__':
    main()
