File size: 4,971 Bytes
f36cc9a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Join panel question IDs to the public benchmark datasets. The package itself contains no question text.

    uv run --no-project --with pandas --with pyarrow --with datasets python code/load_questions.py \
        panels/main256.parquet questions_main256.jsonl --math-dir path/to/MATH [--jmmlu-dir path/to/JMMLU]

Sources and keys (column source_key of every panel file):
  MMLU-Pro    TIGER-Lab/MMLU-Pro, split test, field question_id      (ID mmlu_pro__test_<question_id, 5 digits>)
  SuperGPQA   m-a-p/SuperGPQA, split train, field uuid                (ID supergpqa__<uuid>)
  MathArena   MathArena/<contest>, split train, field problem_idx     (ID <contest>__<problem_idx>)
  MATH        the original release of Hendrycks et al. (2021), file test/<subject>/<number>.json
              (ID math__test_<subject>_<number>); pass the unpacked folder with --math-dir
  JMMLU       nlp-waseda/JMMLU at revision 762cbf192c9e4588b574d718f95afd64c96fcbf4, JMMLU/<subject>.csv,
              zero-based CSV row (ID <subject>.<row>); pass a clone at that revision with --jmmlu-dir

Each dataset keeps its own licence; check it before use. JMMLU's licence does not allow redistributing modified
copies, which is one reason the package ships IDs only. The output file is for local use and is not part of the
package. Rows that cannot be resolved are reported, not guessed.
"""

from __future__ import annotations

import argparse
import csv
import json
from pathlib import Path

import pandas as pd


def mmlu_pro():
    from datasets import load_dataset  # noqa: PLC0415

    return {int(r["question_id"]): r for r in load_dataset("TIGER-Lab/MMLU-Pro", split="test")}


def supergpqa():
    from datasets import load_dataset  # noqa: PLC0415

    return {r["uuid"]: r for r in load_dataset("m-a-p/SuperGPQA", split="train")}


def matharena(contest):
    from datasets import load_dataset  # noqa: PLC0415

    rows = load_dataset(f"MathArena/{contest}", split="train")
    key = "problem_idx" if "problem_idx" in rows.column_names else None
    if key is None:
        raise KeyError(f"MathArena/{contest} has no problem_idx column: {rows.column_names}")
    return {int(r[key]): r for r in rows}


def main():
    ap = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
    ap.add_argument("panel", type=Path)
    ap.add_argument("output", type=Path)
    ap.add_argument("--math-dir", type=Path)
    ap.add_argument("--jmmlu-dir", type=Path)
    a = ap.parse_args()
    panel = pd.read_parquet(a.panel)
    cache, out, missing = {}, [], []

    def get(name, loader):
        if name not in cache:
            cache[name] = loader()
        return cache[name]

    for row in panel.itertuples():
        qid, src = row.question_id, row.source_dataset
        try:
            if src == "MMLU-Pro":
                r = get("mmlu_pro", mmlu_pro)[int(row.source_key)]
                text = {"question": r["question"], "options": list(r["options"]), "answer": r["answer"]}
            elif src == "SuperGPQA":
                r = get("supergpqa", supergpqa)[row.source_key]
                text = {"question": r["question"], "options": list(r["options"]), "answer": r.get("answer_letter", r.get("answer"))}
            elif src == "MathArena":
                contest = row.hf_dataset.split("/", 1)[1]
                r = get(contest, lambda: matharena(contest))[int(row.source_key)]
                text = {"question": r["problem"], "answer": r.get("answer")}
            elif src == "MATH":
                if a.math_dir is None:
                    raise FileNotFoundError("--math-dir not given")
                r = json.loads((a.math_dir / row.source_key).read_text())
                text = {"question": r["problem"], "solution": r["solution"], "level": r.get("level")}
            elif src == "JMMLU":
                if a.jmmlu_dir is None:
                    raise FileNotFoundError("--jmmlu-dir not given")
                subject, line = row.question_id.rsplit(".", 1)
                def read_csv(subject=subject):
                    with (a.jmmlu_dir / "JMMLU" / f"{subject}.csv").open(encoding="utf-8-sig", newline="") as f:
                        return list(csv.reader(f))

                stem, *options, label = get("jmmlu/" + subject, read_csv)[int(line)]
                text = {"question": stem, "options": options, "answer": label.strip()}
            else:
                raise KeyError(src)
        except (KeyError, FileNotFoundError, IndexError) as e:
            missing.append((qid, repr(e)))
            continue
        out.append({"question_id": qid, "source_dataset": src, **text})
    with a.output.open("w", encoding="utf-8") as f:
        for r in out:
            f.write(json.dumps(r, ensure_ascii=False) + "\n")
    print(f"resolved {len(out)} of {len(panel)}; wrote {a.output}")
    for qid, why in missing:
        print("unresolved", qid, why)


if __name__ == "__main__":
    main()