"""Publish cautious per-name apparent-length comparisons."""
import html
import json
from pathlib import Path

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


def main():
    r = json.loads((O / 'results.json').read_text())
    table = []
    for x in r['species_comparisons']:
        table.append('<tr><th><i>' + esc(x['source_name']) + '</i></th><td>' + str(x['gunduz_count']) + '</td><td>' + str(x['ude_count']) + '</td><td>' + str(x['ude_needs_inspection']) + '</td><td>' + f"{x['gunduz_extent_px_median']:.1f}" + '</td><td>' + f"{x['ude_nominal_median_extent_um']:.1f}" + '</td><td>' + f"{x['descriptive_median_length_ratio_um_per_gunduz_px_not_calibration']:.4f}" + '</td></tr>')
    page = '''<!doctype html><html lang="en"><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>Cross-source apparent lengths</title><style>body{font:16px/1.5 system-ui;color:#193b37;background:#f2f6f2;margin:0}main{max-width:1200px;margin:auto;padding:24px}a{color:#076c59}.notice{background:#fff4d7;border-left:5px solid #b88418;padding:15px}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="/results/r112/gunduz-expansion-assets/">Gündüz candidates</a></nav><h1>Comparing apparent source lengths</h1><p class="notice"><strong>These ratios are not microscope calibration and must not be used as resize factors.</strong> These are not paired images of the same known specimen taken with both microscopes. Real size varies within taxa; source names and masks can be wrong; specimens, views, focus and preparation can differ. Eleven of the 19 matched names have only one Gündüz candidate. There is no independent accuracy benchmark here.</p><p>For 19 exactly matching source-name strings, measured the long side of a rotated bounding rectangle around each existing source mask: 33 structurally mapped Gündüz components and 520 unverified UDE center proposals. The UDE publication documents a nominal <a href="https://doi.org/10.1093/gigascience/giae087">0.09 µm/pixel</a> for its images. UDE physical lengths below apply that scale to approximate mask extents; they are not expert measurements. The geometric inspection flag is not an accuracy label.</p><p>All 42 Gündüz development source PNG headers reported approximately 96 DPI, gamma 0.45455 and no EXIF entries. Display DPI is not evidence of object-plane pixel size. The examined XML tags supply raster dimensions, boxes and names, but no calibration field. <a href="/results/r112/gunduz-scale-metadata/results.json">Header and XML audit</a>.</p><p>The descriptive ratio divides each name's UDE median approximate length in micrometres by its Gündüz median extent in pixels. Values differ by name and are sensitive to sample size and mask quality; do not combine them into a calibration. <a href="protocol.json">Method and source restrictions</a> · <a href="results.json">All mask measurements</a> · <a href="verify.json">Recomputation</a>.</p><table><tr><th>Exact source name</th><th>Gündüz crops</th><th>UDE crops</th><th>UDE geometric flags</th><th>Gündüz median extent (px)</th><th>UDE nominal median extent (µm)</th><th>Descriptive ratio (µm/px), not calibration</th></tr>''' + ''.join(table) + '</table></main></html>'
    (O / 'index.html').write_text(page)
    print('published', len(table), 'name comparisons')


if __name__ == '__main__':
    main()
