-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_loader.py
More file actions
93 lines (80 loc) · 2.94 KB
/
Copy pathdata_loader.py
File metadata and controls
93 lines (80 loc) · 2.94 KB
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
import csv
import json
import os
from pathlib import Path
_HERE = Path(__file__).resolve().parent
DATASETS = {
"imobench": {
"csv": _HERE / "data" / "imobench" / "answerbench_v2.csv",
"task": "math",
},
"hle": {
"csv": _HERE / "data" / "hle" / "text_mc_nomath.csv",
"task": "mc",
},
}
def load_queries(dataset, limit=None):
csv_path = DATASETS[dataset]["csv"]
if not csv_path.exists():
hint = " Run `cd data && python download_hle.py` first." if dataset == "hle" else ""
raise FileNotFoundError(f"{csv_path} not found.{hint}")
queries = []
with open(csv_path) as f:
for row in csv.DictReader(f):
queries.append({
"id": row["Problem ID"],
"question": row["Problem"].strip(),
"answer": row["Short Answer"].strip(),
"extra": {
"category": row["Category"],
"subcategory": row["Subcategory"],
"source": row["Source"],
},
})
return queries[:limit] if limit else queries
def run_dir(dataset, model, n):
path = _HERE / "runs" / dataset / model / f"predebate_n{n}"
path.mkdir(parents=True, exist_ok=True)
return path
def already_done(d):
return {f[:-5] for f in os.listdir(d) if f.endswith(".json")}
def non_unanimous_qids(predebate_dir, task):
"""Set of query_ids whose round-0 answers are not all identical. Math
uses parsed_math_answer (fallback boxed_answer); MC uses boxed_answer."""
out = set()
for f in sorted(Path(predebate_dir).glob("*.json")):
d = json.loads(f.read_text())
ans = []
for ag in d.get("agents", []):
sr = ag.get("self_round") or {}
if task == "math":
a = sr.get("parsed_math_answer") or sr.get("boxed_answer")
else:
a = sr.get("boxed_answer")
ans.append(a)
if len({a for a in ans if a is not None}) > 1:
out.add(d["query_id"])
return out
def load_predebate(predebate_dir, only_non_unanimous=False):
"""Return [(query, predebate_payload), ...] sorted by query_id.
`query` mirrors the dict shape used by load_queries (id, question, answer, extra).
`predebate_payload` is the full per-question JSON written by generate_predebate.py.
"""
p = Path(predebate_dir)
files = sorted(p.glob("*.json"))
out = []
for f in files:
with open(f) as fh:
payload = json.load(fh)
if only_non_unanimous:
answers = [ag["self_round"].get("boxed_answer") for ag in payload["agents"]]
if len(set(answers)) <= 1:
continue
query = {
"id": payload["query_id"],
"question": payload["question"],
"answer": payload["ground_truth_answer"],
"extra": payload.get("extra", {}),
}
out.append((query, payload))
return out