File size: 2,229 Bytes
6a92040
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""Score RefBench v1.0 runs from the released files only (standard library).

    python3 score.py                 # run 2, main arm, all models
    python3 score.py --run run1 --arm text

Balanced accuracy (BA) = mean of accuracy on "yes" items and on "no" items, so a constant answer scores 0.5.
A missing or unparseable answer counts as wrong. 95% intervals: cluster bootstrap over source videos, 10,000 resamples.
BA and accuracy reproduce results/<run>_summary.json exactly; the bootstrap uses a different random generator,
so interval ends can differ in the third decimal."""
import argparse, json, random
from collections import defaultdict
from pathlib import Path

H = Path(__file__).resolve().parent
ap = argparse.ArgumentParser(); ap.add_argument("--run", default="run2"); ap.add_argument("--arm", default="main", choices=["main", "text"])
ap.add_argument("--boot", type=int, default=10000); a = ap.parse_args()
items = [json.loads(l) for l in open(H / "data/items.jsonl")]
lab = {x["id"]: x["label"] for x in items}
clusters = defaultdict(list)
for x in items: clusters[x["source_video"]].append(x["id"])

def ba(ids, pred):
    pos = [i for i in ids if lab[i] == "yes"]; neg = [i for i in ids if lab[i] == "no"]
    if not pos or not neg: return None
    return (sum(pred[i] == "yes" for i in pos) / len(pos) + sum(pred[i] == "no" for i in neg) / len(neg)) / 2

rng = random.Random(0); keys = sorted(clusters)
boots = [[i for c in (rng.choice(keys) for _ in keys) for i in clusters[c]] for _ in range(a.boot)]
rows = []
for f in sorted((H / "runs" / a.run / a.arm).glob("*.jsonl")):
    pred = {}
    for l in open(f):
        o = json.loads(l); ans = o.get("answer") if o.get("ok") else None
        pred[o["id"]] = ans if ans in ("yes", "no") else ("no" if lab[o["id"]] == "yes" else "yes")
    ids = list(lab); v = sorted(x for x in (ba(s, pred) for s in boots) if x is not None)
    rows.append((ba(ids, pred), f.stem, sum(pred[i] == lab[i] for i in ids) / len(ids), v[int(.025 * len(v))], v[int(.975 * len(v)) - 1]))
print(f"{'model':<20}{'BA':>7}{'95% CI':>17}{'acc':>8}")
for b, m, acc, lo, hi in sorted(rows, reverse=True): print(f"{m:<20}{b:>7.3f}   [{lo:.3f}, {hi:.3f}]{acc:>8.3f}")