Datasets:
Download grader/grade.py from GenData-Research/experiment-selection-old: direct link, hf CLI and curl.
- Browser
- Download file 3.8 kB
-
https://huggingface.co/datasets/GenData-Research/experiment-selection-old/resolve/main/grader/grade.py
- Command line
-
hf download hf://datasets/GenData-Research/experiment-selection-old/grader/grade.py
-
curl -L -o grade.py https://huggingface.co/datasets/GenData-Research/experiment-selection-old/resolve/main/grader/grade.py
3.8 kB
| #!/usr/bin/env python3 | |
| """grade.py: score model replies on Experiment Selection v2. Run from the dataset folder; no dependencies. | |
| python3 grader/grade.py --predictions my_replies.jsonl | |
| python3 grader/grade.py --predictions traces/claude-opus-5-5.jsonl.gz # reproduces the reference results | |
| --predictions: JSON lines {"id": "<prompt id>", "reply": "<raw model text>", "attempt": 1} (.jsonl or .jsonl.gz). | |
| Each reply's decision and confidence (0-100) become the probability the experiment meets its goal: confidence if the | |
| decision is "run", 100 minus confidence if "don't run". A missing or unreadable reply counts as wrong, at probability 0.5. | |
| Per arm (successes_only, failures_replace_successes, successes_failures_hidden, successes_failures_shown): | |
| both_verdicts_correct = a task counts when both its runs are right in the same attempt (the failed run judged "don't run" and | |
| its successful counterpart "run"); averaged over attempts, then over tasks; chance is 0.25 | |
| failed_runs_withheld / successful_runs_approved = share of failed runs answered "don't run" and of successful runs | |
| answered "run", averaged over attempts, then over tasks | |
| brier = mean squared gap between that probability and the recorded outcome (1 met the goal, 0 missed); lower | |
| is better, always answering 50% scores 0.25 | |
| """ | |
| import argparse, collections, gzip, json, os, statistics, sys | |
| sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) | |
| from parse import parse_answer # noqa: E402 | |
| ap = argparse.ArgumentParser() | |
| ap.add_argument("--predictions", required=True) | |
| ap.add_argument("--prompts", default=os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "data", "prompts.jsonl")) | |
| a = ap.parse_args() | |
| key = {r["id"]: r for r in map(json.loads, open(a.prompts, encoding="utf-8"))} | |
| op = gzip.open if a.predictions.endswith(".gz") else open | |
| replies = collections.defaultdict(dict) # attempt -> id -> reply | |
| for line in op(a.predictions, "rt", encoding="utf-8"): | |
| r = json.loads(line) | |
| if r["id"] in key: replies[int(r.get("attempt", 1))][r["id"]] = r.get("reply") or "" | |
| attempts = sorted(replies) or [1] | |
| def judge(pid, k): | |
| """(right, squared error) for one prompt in one attempt""" | |
| truth = key[pid]["answer"] == "run"; ans = parse_answer(replies[k].get(pid, "")) | |
| if ans is None: return False, 0.25 | |
| p_run = 0.5 if ans["confidence"] is None else ans["confidence"] / 100 | |
| p = p_run if ans["decision"] == "run" else 1 - p_run | |
| return (ans["decision"] == "run") == truth, (p - truth) ** 2 | |
| by = collections.defaultdict(dict) # (task, arm) -> role -> id | |
| for pid, r in key.items(): by[(r["task_name"], r["arm"])][r["asks_about"]] = pid | |
| out = {} | |
| for arm in ("successes_only", "failures_replace_successes", "successes_failures_hidden", "successes_failures_shown"): | |
| pair, brier, withheld, approved = [], [], [], [] | |
| for (task, ar), ids in sorted(by.items()): | |
| if ar != arm: continue | |
| js = [(judge(ids["failed_run"], k), judge(ids["successful_run"], k)) for k in attempts] | |
| pair.append(statistics.mean(f[0] and s[0] for f, s in js)) | |
| brier.append(statistics.mean((f[1] + s[1]) / 2 for f, s in js)) | |
| withheld.append(statistics.mean(f[0] for f, _ in js)) | |
| approved.append(statistics.mean(s[0] for _, s in js)) | |
| out[arm] = {"tasks": len(pair), "both_verdicts_correct": round(statistics.mean(pair), 3), | |
| "failed_runs_withheld": round(statistics.mean(withheld), 3), | |
| "successful_runs_approved": round(statistics.mean(approved), 3), "brier": round(statistics.mean(brier), 3)} | |
| seen = sum(len(v) for v in replies.values()) | |
| print(json.dumps({"attempts": attempts, "replies_matched": seen, "results": out}, indent=1)) | |