File size: 2,750 Bytes
32030e4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""Score predictions produced by your own harness.

Input: JSON lines with ``task_id`` and either ``response`` (the answer object) or ``text``
(raw model output; the final JSON object is extracted). An optional ``sample`` field allows
several samples per task. Multi-step tasks must be run interactively (``run_model.py`` or
``StoredDataEnv``); here only their final submitted answer is scored.

    python eval/score_predictions.py --dataset . --split test --predictions preds.jsonl
"""

from __future__ import annotations

import argparse
import json
import sys
from pathlib import Path

sys.path.insert(0, str(Path(__file__).resolve().parent))
from ejbench_eval.data import load_tasks  # noqa: E402
from ejbench_eval.parsing import extract_json  # noqa: E402
from ejbench_eval.report import summarise  # noqa: E402
from ejbench_eval.scoring import score_task  # noqa: E402


def main(argv: list[str] | None = None) -> int:
    ap = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
    ap.add_argument("--dataset", default=str(Path(__file__).resolve().parents[1]))
    ap.add_argument("--split", default="test", choices=("train", "test", "test_unseen_cell"))
    ap.add_argument("--predictions", required=True)
    ap.add_argument("--out", default=None)
    args = ap.parse_args(argv)

    tasks = {task["task_id"]: task for task in load_tasks(args.dataset, args.split)}
    rows, seen = [], set()
    for line in Path(args.predictions).read_text().splitlines():
        if not line.strip():
            continue
        pred = json.loads(line)
        task = tasks.get(pred["task_id"])
        if task is None:
            raise KeyError(f"{pred['task_id']} is not in the {args.split} split")
        answer = pred.get("response")
        if answer is None:
            answer = extract_json(pred.get("text"))
        result = score_task(task, answer if isinstance(answer, dict) else {})
        seen.add(task["task_id"])
        rows.append({"task_id": task["task_id"], "family": task["family"], "tags": task.get("tags"),
                     "parsed": answer, **result})
    missing = sorted(set(tasks) - seen)
    for task_id in missing:   # unanswered tasks score 0
        task = tasks[task_id]
        rows.append({"task_id": task_id, "family": task["family"], "tags": task.get("tags"),
                     "parsed": None, "score": 0.0, "calibration_sq_error": None})
    summary = {"split": args.split, "missing_tasks_scored_zero": len(missing), **summarise(rows)}
    if args.out:
        Path(args.out).write_text(json.dumps(summary, indent=2) + "\n")
    print(json.dumps(summary, indent=2))
    return 0


if __name__ == "__main__":
    raise SystemExit(main())