"""Checks the v1 README results table against the computed analysis JSON. The v1 README table is an artifact people may still quote, so it must be generated from the analysis output rather than retyped: python tests/test_readme_table.py Parses only the Results section (between `## Results` and `### Paired`), matches each row by model and dataset, and requires every metric value to appear at three decimals. Also fails if a computed cell has no README row. """ from __future__ import annotations import json import re import sys from pathlib import Path ROOT = Path(__file__).resolve().parents[1] ANN = ROOT / "analysis_canon.json" README = ROOT / "README-v1.md" # v1 tables moved here when the card took README.md METRICS = ["t2a_r1", "t2a_r5", "t2a_r10", "t2a_mrr", "a2t_r1", "a2t_r5", "a2t_r10", "a2t_map"] def results_section(text: str) -> str: start = text.index("## Results") end = text.index("### Paired") return text[start:end] def main() -> int: if not ANN.exists(): print(f"missing {ANN.name}; run `python analysis.py sweep_canon` first") return 1 cells = json.loads(ANN.read_text(encoding="utf-8"))["cells"] section = results_section(README.read_text(encoding="utf-8")) rows = [ln for ln in section.splitlines() if ln.startswith("| laion/")] fails = [] for c in cells: # the README writes "AudioCaps"; the JSON writes "audiocaps" match = [r for r in rows if c["model"] in r and f"| {c['dataset'].lower()} |" in r.lower()] if not match: fails.append(f"no README row for {c['model']} {c['dataset']}") print(f"FAIL missing row: {c['model']} / {c['dataset']}") continue row = match[0] missing = [m for m in METRICS if f"{c['metrics'][m]['value']:.3f}" not in row] print(f"{'PASS' if not missing else 'FAIL'} {c['model']} / {c['dataset']}" f"{'' if not missing else ' missing ' + ', '.join(missing)}") if missing: fails.append(f"{c['model']} {c['dataset']}: {missing}") print("\n" + ("README-v1 table matches analysis_canon.json" if not fails else f"FAILURES: {fails}")) return 0 if not fails else 1 if __name__ == "__main__": raise SystemExit(main())