"""Publish comparison of original and existing adapted decoder masks."""
import html
import json
from pathlib import Path

R = Path(__file__).resolve().parents[1]
O = R / 'results/r112/local-decoder-source-pilot'
esc = html.escape


def main():
    result = json.loads((O / 'results.json').read_text())
    check = json.loads((O / 'verify.json').read_text())
    high, ref = check['summary']['high_coverage'], check['summary']['reference']
    cards = []
    for row in result['records']:
        cards.append('<article><h3>' + esc(row['annotation_id']) + ' · ' + esc(row['source_name']) + '</h3><p>' + esc(row['group']) + ' · original area ' + str(row['original_area_px']) + ' · R54 ' + str(row['masks']['r54']['area_px']) + ' · R112 ' + str(row['masks']['r112']['area_px']) + ' pixels</p><a href="' + esc(row['comparison'], quote=True) + '"><img loading="lazy" src="' + esc(row['comparison'], quote=True) + '" alt="Source, original mask and two adapted decoder candidates"></a></article>')
    page = '''<!doctype html><html lang="en"><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>Adapted decoder source pilot</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}td,th{padding:8px;border:1px solid #cbd8d1}</style><main><nav><a href="/results/r112/">Synthetic training</a> · <a href="/results/r112/outside-box-prompt-pilot/">Negative-prompt comparison</a></nav><h1>Existing adapted decoder source pilot</h1><p class="notice"><strong>Neither adapted decoder fixes the two inspected failure cases.</strong> They still select broad background. This is a small, previously viewed development sample without expert mask truth; these results cannot establish segmentation accuracy or support promotion to training labels.</p><p>Compared the original MobileSAM center proposal with R54 and R112 adapted decoder outputs on the same 36 source crops and positive center point. R54 was trained on a small box-prompted synthetic freshwater set; R112 used weak positive-only synthetic scenes and teacher regularization. The first two cases were selected after spotting failures; the remaining cases were previously selected by geometry and hash. Both adapted models share the original encoder.</p><table><tr><th>Group</th><th>Cases</th><th>Original &gt;75% crop</th><th>R54 &gt;75%</th><th>R112 &gt;75%</th><th>R54 smaller / larger</th><th>R112 smaller / larger</th></tr>''' + ''.join('<tr><th>' + title + '</th><td>' + str(s['cases']) + '</td><td>' + str(s['original_gt_075']) + '</td><td>' + str(s['r54_gt_075']) + '</td><td>' + str(s['r112_gt_075']) + '</td><td>' + str(s['r54_smaller']) + ' / ' + str(s['r54_larger']) + '</td><td>' + str(s['r112_smaller']) + ' / ' + str(s['r112_larger']) + '</td></tr>' for title, s in [('High coverage', high), ('Reference', ref)]) + '''</table><p>Large or small coverage is not an accuracy measure; a valid specimen can fill a crop. R112 produced a larger mask in all 36 cases. This observation does not prove why it grew or how it would perform on other data. R54 used noncommercial, share-alike source-derived training; license obligations require consideration before reuse.</p><p><a href="protocol.json">Pilot protocol</a> · <a href="results.json">All masks and records</a> · <a href="verify.json">File and geometry checks</a></p><p>Pink crosses indicate the common positive prompt; blue overlays and orange outlines show candidate masks. Images are resized for display.</p>''' + ''.join(cards) + '</main></html>'
    (O / 'index.html').write_text(page)
    print('published', len(cards), 'paired comparisons')


if __name__ == '__main__':
    main()
