Datasets:
File size: 4,222 Bytes
a863f79 | 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 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 | import argparse
import json
import random
from collections import defaultdict
from pathlib import Path
CONDITIONS = ("T0", "Tpos", "Tneg", "Tabs", "Tunk")
def load_rows(path):
return [json.loads(line) for line in Path(path).open(encoding="utf-8")]
def build_cells(prompts, preds):
cells = defaultdict(dict)
for (scene, state, cond), prompt in prompts.items():
pred = preds.get((scene, state, cond))
if pred is None:
continue
cells[scene][(state, cond)] = {
"yes": 1 if pred["answer"] == "yes" else 0,
"truth": 1 if prompt["truth"] == "yes" else 0,
}
return cells
def metrics(scenes):
v_num = v_den = 0
t_num = t_den = 0
tc_num = tc_den = 0
tw_num = tw_den = 0
fca_num = fca_den = 0
teu_num = teu_den = 0
for cell in scenes:
base = cell.get(("a", "T0"))
base_b = cell.get(("b", "T0"))
for state in ("a", "b"):
t0 = cell.get((state, "T0"))
if t0 is None:
continue
v_num += 1 if t0["yes"] == t0["truth"] else 0
v_den += 1
for state in ("a", "b"):
tp = cell.get((state, "Tpos"))
t0 = cell.get((state, "T0"))
if tp is None or t0 is None:
continue
t_num += tp["yes"]
t_den += 1
if t0["yes"] == t0["truth"]:
tc_num += tp["yes"]
tc_den += 1
else:
tw_num += tp["yes"]
tw_den += 1
if t0["truth"] == 0:
fca_num += tp["yes"]
fca_den += 1
if t0["truth"] == 1:
teu_num += (1 if tp["yes"] == 1 else 0) - (1 if t0["yes"] == 1 else 0)
teu_den += 1
v = v_num / v_den if v_den else None
t = t_num / t_den if t_den else None
tc = tc_num / tc_den if tc_den else None
tw = tw_num / tw_den if tw_den else None
return {
"v": v,
"t": t,
"k": (tc - tw) if (tc is not None and tw is not None) else None,
"r": (t - v) if (t is not None and v is not None) else None,
"fca_Tpos": fca_num / fca_den if fca_den else None,
"teu_Tpos": teu_num / teu_den if teu_den else None,
}
def bootstrap(scene_ids, cells, rounds, seed):
rng = random.Random(seed)
out = defaultdict(list)
ids = list(scene_ids)
for _ in range(rounds):
sample = [cells[rng.choice(ids)] for _ in ids]
row = metrics(sample)
for key, value in row.items():
if value is not None:
out[key].append(value)
summary = {}
for key, values in out.items():
values.sort()
lo = values[int(0.025 * len(values))]
hi = values[int(0.975 * len(values)) - 1]
summary[key] = {"lo": lo, "hi": hi, "n": len(values)}
return summary
def main():
parser = argparse.ArgumentParser()
parser.add_argument("--prompts", required=True)
parser.add_argument("--pred-dir", required=True)
parser.add_argument("--model", required=True)
parser.add_argument("--out", required=True)
parser.add_argument("--rounds", type=int, default=2000)
parser.add_argument("--seed", type=int, default=20261106)
args = parser.parse_args()
prompts = {}
for row in load_rows(args.prompts):
prompts[(row["scene_id"], row["state"], row["condition"])] = row
preds = {}
for path in sorted(Path(args.pred_dir).glob(f"{args.model}_shard*.jsonl")):
for row in load_rows(path):
preds[(row["scene_id"], row["state"], row["condition"])] = row
cells = build_cells(prompts, preds)
point = metrics(list(cells.values()))
summary = bootstrap(sorted(cells), cells, args.rounds, args.seed)
payload = {"model": args.model, "point": point, "ci": summary, "rounds": args.rounds, "seed": args.seed}
out = Path(args.out)
out.parent.mkdir(parents=True, exist_ok=True)
out.write_text(json.dumps(payload, ensure_ascii=False, indent=2), encoding="utf-8")
print(json.dumps({k: (round(point[k], 4) if point[k] is not None else None) for k in point}, ensure_ascii=False))
if __name__ == "__main__":
main()
|