Datasets:
Download code/bootstrap.py from spatial-evidence-bench/SpatialEvidenceBench: direct link, hf CLI and curl.
- Browser
- Download file 4.22 kB
-
https://huggingface.co/datasets/spatial-evidence-bench/SpatialEvidenceBench/resolve/5c8b07c8e3679dc7d2dd54b6fae5e27f9d82c4db/code/bootstrap.py
- Command line
-
hf download hf://datasets/spatial-evidence-bench/SpatialEvidenceBench@5c8b07c8e3679dc7d2dd54b6fae5e27f9d82c4db/code/bootstrap.py
-
curl -L -o bootstrap.py https://huggingface.co/datasets/spatial-evidence-bench/SpatialEvidenceBench/resolve/5c8b07c8e3679dc7d2dd54b6fae5e27f9d82c4db/code/bootstrap.py
4.22 kB
| 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() | |