"""Parity / accuracy check between two runs of run.py on BIRD dev (e.g. --backend mlx vs --backend torch). python compare.py --data dev.json --db_root dev_databases RUN_A.progress.jsonl RUN_B.progress.jsonl Prints, per run, execution accuracy against the gold SQL (set comparison of result rows), and between runs the share of identical greedy drafts / greedy candidates and the paired gained/lost counts. """ import argparse import json import sqlite3 from math import comb from pathlib import Path def rows(db, sql): try: con = sqlite3.connect(f"file:{db}?mode=ro", uri=True) con.text_factory = lambda b: b.decode(errors="replace") r = set(con.execute(sql).fetchall()) con.close() return r except Exception: # noqa: BLE001 return None def main(): ap = argparse.ArgumentParser() ap.add_argument("--data", required=True) ap.add_argument("--db_root", required=True) ap.add_argument("runs", nargs="+") a = ap.parse_args() data = json.loads(Path(a.data).read_text()) runs = [{r["idx"]: r for r in (json.loads(l) for l in Path(f).read_text().splitlines() if l.strip())} for f in a.runs] ids = sorted(set.intersection(*[set(r) for r in runs])) gold = {} correct = [{} for _ in runs] for i in ids: ex = data[i] db = str(Path(a.db_root) / ex["db_id"] / f"{ex['db_id']}.sqlite") gold[i] = rows(db, ex["SQL"]) for k, run in enumerate(runs): p = rows(db, run[i]["sql"]) correct[k][i] = p is not None and p == gold[i] for k, f in enumerate(a.runs): print(f"{f}: EX {100 * sum(correct[k].values()) / len(ids):.1f} on {len(ids)} questions") if len(runs) == 2: A, B = runs same_d = sum(A[i].get("draft", "").strip() == B[i].get("draft", "").strip() for i in ids) same_g = sum((A[i].get("candidates") or [""])[0].strip() == (B[i].get("candidates") or [""])[0].strip() for i in ids) w = sum(correct[1][i] and not correct[0][i] for i in ids) l = sum(correct[0][i] and not correct[1][i] for i in ids) n = w + l p = min(1.0, 2 * sum(comb(n, j) for j in range(min(w, l) + 1)) / 2 ** n) if n else 1.0 print(f"identical greedy drafts {same_d}/{len(ids)}, identical greedy candidates {same_g}/{len(ids)}") print(f"run B vs run A: +{w} / -{l} (McNemar p = {p:.3f})") if __name__ == "__main__": main()