File size: 7,650 Bytes
d630df8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
"""Aggregate results from multiple benchmark runs of the same model/exam/year.

Computes mean, std dev, 95% CI, min/max scores, and per-question stability.

Usage:
    uv run python scripts/aggregate_runs.py results/model_EXAM_YEAR_*/
    uv run python scripts/aggregate_runs.py --pattern "openai_o3_JEE_ADVANCED_2025"
"""

import argparse
import glob
import json
import math
import os
import sys
from collections import defaultdict


def load_summary_jsonl(path: str) -> list[dict]:
    """Load all records from a summary.jsonl file."""
    records = []
    with open(path, "r") as f:
        for line in f:
            line = line.strip()
            if line:
                records.append(json.loads(line))
    return records


def compute_run_stats(records: list[dict]) -> dict:
    """Compute aggregate stats for a single run."""
    total_score = sum(r.get("marks_awarded", 0) for r in records)
    correct = sum(1 for r in records if r.get("evaluation_status") in ("correct", "correct_full"))
    incorrect = sum(1 for r in records if r.get("evaluation_status") in ("incorrect", "incorrect_negative"))
    skipped = sum(1 for r in records if r.get("evaluation_status") == "skipped")
    failures = sum(1 for r in records if r.get("evaluation_status") in ("failure_api_or_parse", "failure_unexpected_type"))
    partial = sum(1 for r in records if r.get("evaluation_status", "").startswith("partial_"))
    return {
        "score": total_score,
        "correct": correct,
        "partial": partial,
        "incorrect": incorrect,
        "skipped": skipped,
        "failures": failures,
        "num_questions": len(records),
    }


def aggregate_runs(run_dirs: list[str]) -> dict:
    """Aggregate stats across multiple runs."""
    if not run_dirs:
        return {"error": "No run directories provided."}

    all_run_stats = []
    per_question_answers: dict[str, list] = defaultdict(list)

    for run_dir in run_dirs:
        summary_path = os.path.join(run_dir, "summary.jsonl")
        if not os.path.exists(summary_path):
            print(f"Warning: {summary_path} not found, skipping.", file=sys.stderr)
            continue

        records = load_summary_jsonl(summary_path)
        stats = compute_run_stats(records)
        stats["run_dir"] = run_dir
        all_run_stats.append(stats)

        for r in records:
            qid = r.get("question_id")
            if qid:
                per_question_answers[qid].append({
                    "predicted_answer": r.get("predicted_answer"),
                    "evaluation_status": r.get("evaluation_status"),
                    "marks_awarded": r.get("marks_awarded", 0),
                })

    if not all_run_stats:
        return {"error": "No valid runs found."}

    scores = [s["score"] for s in all_run_stats]
    n = len(scores)
    mean_score = sum(scores) / n
    if n > 1:
        variance = sum((s - mean_score) ** 2 for s in scores) / (n - 1)
        std_dev = math.sqrt(variance)
        # 95% CI using t-distribution approximation (for small n, use 2.0 as rough multiplier)
        t_multiplier = 2.0 if n < 30 else 1.96
        ci_half = t_multiplier * std_dev / math.sqrt(n)
    else:
        std_dev = 0.0
        ci_half = 0.0

    # Per-question stability
    question_stability = {}
    for qid, answers in per_question_answers.items():
        statuses = [a["evaluation_status"] for a in answers]
        most_common = max(set(statuses), key=statuses.count)
        agreement_rate = statuses.count(most_common) / len(statuses)
        question_stability[qid] = {
            "agreement_rate": round(agreement_rate, 3),
            "dominant_status": most_common,
            "all_statuses": statuses,
            "scores": [a["marks_awarded"] for a in answers],
        }

    # Unstable questions (agreement < 100%)
    unstable = {qid: v for qid, v in question_stability.items() if v["agreement_rate"] < 1.0}

    return {
        "num_runs": n,
        "scores": scores,
        "mean_score": round(mean_score, 2),
        "std_dev": round(std_dev, 2),
        "ci_95_lower": round(mean_score - ci_half, 2),
        "ci_95_upper": round(mean_score + ci_half, 2),
        "min_score": min(scores),
        "max_score": max(scores),
        "per_run": all_run_stats,
        "num_questions": all_run_stats[0]["num_questions"] if all_run_stats else 0,
        "unstable_questions": len(unstable),
        "total_questions": len(question_stability),
        "question_stability": question_stability,
    }


def print_report(result: dict):
    """Print a human-readable report."""
    if "error" in result:
        print(f"Error: {result['error']}")
        return

    print(f"# Multi-Run Aggregation Report")
    print(f"\n**Runs:** {result['num_runs']}")
    print(f"**Questions per run:** {result['num_questions']}")
    print(f"\n## Score Summary")
    print(f"| Metric | Value |")
    print(f"|--------|-------|")
    print(f"| Mean | {result['mean_score']} |")
    print(f"| Std Dev | {result['std_dev']} |")
    print(f"| 95% CI | [{result['ci_95_lower']}, {result['ci_95_upper']}] |")
    print(f"| Min | {result['min_score']} |")
    print(f"| Max | {result['max_score']} |")
    print(f"| Scores | {result['scores']} |")

    print(f"\n## Per-Question Stability")
    print(f"- **Stable questions:** {result['total_questions'] - result['unstable_questions']}/{result['total_questions']}")
    print(f"- **Unstable questions:** {result['unstable_questions']}/{result['total_questions']}")

    if result["unstable_questions"] > 0:
        print(f"\n### Unstable Questions (different answers across runs)")
        print(f"| Question ID | Agreement | Dominant Status | Scores |")
        print(f"|-------------|-----------|-----------------|--------|")
        for qid, info in sorted(result["question_stability"].items()):
            if info["agreement_rate"] < 1.0:
                print(f"| {qid} | {info['agreement_rate']:.0%} | {info['dominant_status']} | {info['scores']} |")


def main():
    parser = argparse.ArgumentParser(
        description="Aggregate results from multiple benchmark runs."
    )
    parser.add_argument(
        "run_dirs",
        nargs="*",
        help="Paths to result directories to aggregate.",
    )
    parser.add_argument(
        "--pattern",
        type=str,
        help="Glob pattern to match result directories (e.g., 'openai_o3_JEE_ADVANCED_2025').",
    )
    parser.add_argument(
        "--results-dir",
        type=str,
        default="results",
        help="Base results directory (default: results).",
    )
    parser.add_argument(
        "--output",
        type=str,
        help="Output JSON file path for aggregated results.",
    )
    args = parser.parse_args()

    run_dirs = list(args.run_dirs) if args.run_dirs else []

    if args.pattern:
        pattern = os.path.join(args.results_dir, f"*{args.pattern}*")
        matched = sorted(glob.glob(pattern))
        run_dirs.extend(d for d in matched if os.path.isdir(d))

    if not run_dirs:
        parser.error("No run directories specified. Provide paths or use --pattern.")

    run_dirs = sorted(set(run_dirs))
    print(f"Aggregating {len(run_dirs)} runs...", file=sys.stderr)

    result = aggregate_runs(run_dirs)
    print_report(result)

    if args.output:
        # Remove per-question detail for compact output unless needed
        output_data = {k: v for k, v in result.items() if k != "question_stability"}
        with open(args.output, "w") as f:
            json.dump(output_data, f, indent=2)
        print(f"\nResults saved to {args.output}", file=sys.stderr)


if __name__ == "__main__":
    main()