File size: 2,423 Bytes
a7e82fd | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | #!/usr/bin/env python3
# SPDX-License-Identifier: MIT
"""Plain per-field accuracy (argmax == expected) of run files on public rows with `expected`; paired difference vs the first
run with a bootstrap 95% CI over rows. Not the Decision Index (no chance correction, no benchmark weights): a quality guard.
python bench/field_accuracy.py --rows shard*.jsonl.gz --runs ref.jsonl cand.jsonl ..."""
import argparse, gzip, json, random
from pathlib import Path
def norm(v):
if isinstance(v, bool): return "true" if v else "false"
return str(v).lower() if str(v).lower() in ("true", "false") else str(v)
def main():
ap = argparse.ArgumentParser(); ap.add_argument("--rows", nargs="+", required=True); ap.add_argument("--runs", nargs="+", required=True)
a = ap.parse_args()
runs = [{json.loads(l)["id"]: json.loads(l) for l in Path(p).read_text().splitlines() if l.strip()} for p in a.runs]
want = set(runs[0])
exp = {}
for p in a.rows:
with (gzip.open if p.endswith(".gz") else open)(p, "rt") as f:
for line in f:
r = json.loads(line)
if r.get("id") in want and isinstance(r.get("expected"), dict):
exp[r["id"]] = r["expected"]
ids = [i for i in exp if all(i in R and R[i]["status"] == "ok" for R in runs)]
per = []
for R in runs:
row_scores = {}
for i in ids:
s, n = 0, 0
for q, e in exp[i].items():
pa = R[i]["answers"].get(q)
if pa is None or isinstance(e, (list, dict)) or e is None: continue
n += 1; s += norm(max(pa, key=pa.get)) == norm(e)
if n: row_scores[i] = (s, n)
per.append(row_scores)
keys = [i for i in ids if all(i in p for p in per)]
rng = random.Random(1)
out = []
for j, p in enumerate(per):
acc = sum(p[i][0] for i in keys) / sum(p[i][1] for i in keys)
rec = {"run": a.runs[j], "rows": len(keys), "fields": sum(p[i][1] for i in keys), "accuracy": round(acc, 5)}
if j:
d = [(p[i][0] - per[0][i][0]) / per[0][i][1] for i in keys]
boots = sorted(sum(rng.choice(d) for _ in d) / len(d) for _ in range(2000))
rec.update(diff_vs_first=round(sum(d) / len(d), 5), ci95=[round(boots[50], 5), round(boots[1949], 5)])
out.append(rec)
print(json.dumps(out, indent=1))
if __name__ == "__main__":
main()
|