File size: 9,322 Bytes
6e8a458
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
"""Generate a cross-model leaderboard from benchmark results.

Reads all result directories, computes scores, and outputs a Markdown
leaderboard table grouped by exam+year.

Usage:
    uv run python scripts/generate_leaderboard.py
    uv run python scripts/generate_leaderboard.py --results-dir results --min-questions 10
    uv run python scripts/generate_leaderboard.py --output leaderboard.json
"""

import argparse
import json
import os
import re
import sys
from collections import defaultdict


# Known exam names that may contain underscores
KNOWN_EXAMS = {"JEE_ADVANCED", "JEE_MAIN", "NEET"}


def parse_result_dirname(dirname: str) -> dict | None:
    """Parse a result directory name into model, exam, year, timestamp.

    Format: {model_id}_{EXAM_NAME}_{YEAR}_{YYYYMMDD}_{HHMMSS}
    where model_id has slashes replaced with underscores.

    Returns dict with keys: model, exam, year, timestamp, or None if unparseable.
    """
    parts = dirname.split("_")
    if len(parts) < 5:
        return None

    # Last 2 parts are timestamp
    timestamp = "_".join(parts[-2:])

    # Validate timestamp format
    if not (len(parts[-2]) == 8 and parts[-2].isdigit() and
            len(parts[-1]) == 6 and parts[-1].isdigit()):
        return None

    # Year is parts[-3]
    year = parts[-3]
    if not (len(year) == 4 and year.isdigit()):
        return None

    # Exam name: check if it's a 2-part exam (JEE_ADVANCED, JEE_MAIN)
    if len(parts) >= 6 and parts[-4] in ("ADVANCED", "MAIN"):
        exam = parts[-5] + "_" + parts[-4]
        model_parts = parts[:-5]
    else:
        exam = parts[-4]
        model_parts = parts[:-4]

    if exam not in KNOWN_EXAMS:
        return None

    if not model_parts:
        return None

    # Reconstruct model name: first underscore -> slash (provider/model)
    model_name = "_".join(model_parts)
    first_underscore = model_name.find("_")
    if first_underscore > 0:
        model_name = model_name[:first_underscore] + "/" + model_name[first_underscore + 1:]

    return {
        "model": model_name,
        "exam": exam,
        "year": year,
        "timestamp": timestamp,
    }


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


def extract_max_score_from_md(filepath: str) -> int | None:
    """Extract max possible score from summary.md's Overall Score line.

    Handles both formats:
    - New: **Overall Score:** **315** / **360**
    - Old: **Overall Score:** **727 / 800**
    """
    try:
        with open(filepath, "r") as f:
            for line in f:
                # New format: **315** / **360**
                match = re.search(r"\*\*Overall Score:\*\*\s+\*\*\d+\*\*\s*/\s*\*\*(\d+)\*\*", line)
                if match:
                    return int(match.group(1))
                # Old format: **727 / 800**
                match = re.search(r"\*\*Overall Score:\*\*\s+\*\*\d+\s*/\s*(\d+)\*\*", line)
                if match:
                    return int(match.group(1))
                # Oldest format: **322** (Max score is 360)
                match = re.search(r"\(Max score is (\d+)\)", line)
                if match:
                    return int(match.group(1))
    except (OSError, ValueError):
        pass
    return None


def compute_stats(records: list[dict]) -> dict:
    """Compute aggregate stats from summary.jsonl records."""
    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"))
    partial = sum(1 for r in records if r.get("evaluation_status", "").startswith("partial_"))
    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", "error_bad_ground_truth"))

    return {
        "score": total_score,
        "correct": correct,
        "partial": partial,
        "incorrect": incorrect,
        "skipped": skipped,
        "failures": failures,
        "num_questions": len(records),
    }


def load_summary_json(filepath: str) -> dict | None:
    """Load stats from an old-format summary.json file."""
    try:
        with open(filepath, "r") as f:
            data = json.load(f)
        return {
            "score": data.get("overall_score", 0),
            "correct": data.get("overall_correct", data.get("overall_correct_full", 0)),
            "partial": data.get("overall_partial_correct", 0),
            "incorrect": data.get("overall_incorrect", data.get("overall_incorrect_choice", 0)),
            "skipped": data.get("overall_skipped", 0),
            "failures": data.get("overall_api_parse_failures", 0),
            "num_questions": data.get("total_questions_processed", 0),
        }
    except (OSError, json.JSONDecodeError, KeyError):
        return None


def scan_results(results_dir: str, min_questions: int) -> list[dict]:
    """Scan result directories and collect stats."""
    entries = []

    if not os.path.isdir(results_dir):
        print(f"Results directory not found: {results_dir}", file=sys.stderr)
        return entries

    for dirname in sorted(os.listdir(results_dir)):
        dirpath = os.path.join(results_dir, dirname)
        if not os.path.isdir(dirpath):
            continue

        parsed = parse_result_dirname(dirname)
        if not parsed:
            continue

        # Try summary.jsonl first (new format), then summary.json (old format)
        stats = None
        summary_jsonl_path = os.path.join(dirpath, "summary.jsonl")
        summary_json_path = os.path.join(dirpath, "summary.json")

        if os.path.exists(summary_jsonl_path):
            records = load_summary_jsonl(summary_jsonl_path)
            if records:
                stats = compute_stats(records)
        elif os.path.exists(summary_json_path):
            stats = load_summary_json(summary_json_path)

        if not stats or stats.get("num_questions", 0) < min_questions:
            continue

        # Try to get max score from summary.md
        md_path = os.path.join(dirpath, "summary.md")
        max_score = extract_max_score_from_md(md_path)

        entry = {
            **parsed,
            **stats,
            "max_score": max_score,
            "result_dir": dirname,
        }
        entries.append(entry)

    return entries


def generate_markdown(entries: list[dict]) -> str:
    """Generate a Markdown leaderboard table grouped by exam+year."""
    if not entries:
        return "# Benchmark Leaderboard\n\nNo results found.\n"

    # Group by exam+year
    groups: dict[str, list[dict]] = defaultdict(list)
    for e in entries:
        key = f"{e['exam']}_{e['year']}"
        groups[key].append(e)

    lines = ["# Benchmark Leaderboard\n"]

    for group_key in sorted(groups.keys()):
        group_entries = groups[group_key]
        # Sort by score descending
        group_entries.sort(key=lambda x: x["score"], reverse=True)

        exam_display = group_key.replace("_", " ").replace("JEE ADVANCED", "JEE Advanced").replace("JEE MAIN", "JEE Main")
        lines.append(f"\n## {exam_display}\n")
        lines.append("| Rank | Model | Score | Max | % | Correct | Partial | Incorrect | Skipped | Failures |")
        lines.append("|------|-------|-------|-----|---|---------|---------|-----------|---------|----------|")

        for rank, e in enumerate(group_entries, 1):
            max_score = e.get("max_score") or "?"
            if isinstance(max_score, int) and max_score > 0:
                pct = f"{e['score'] / max_score * 100:.1f}%"
            else:
                pct = "?"
            lines.append(
                f"| {rank} | {e['model']} | {e['score']} | {max_score} | {pct} "
                f"| {e['correct']} | {e['partial']} | {e['incorrect']} | {e['skipped']} | {e['failures']} |"
            )

    lines.append("")
    return "\n".join(lines)


def main():
    parser = argparse.ArgumentParser(description="Generate benchmark leaderboard.")
    parser.add_argument(
        "--results-dir",
        type=str,
        default="results",
        help="Path to the results directory (default: results).",
    )
    parser.add_argument(
        "--min-questions",
        type=int,
        default=10,
        help="Minimum questions to include a run (default: 10, filters incomplete runs).",
    )
    parser.add_argument(
        "--output",
        type=str,
        help="Output path for leaderboard.json.",
    )
    args = parser.parse_args()

    entries = scan_results(args.results_dir, args.min_questions)

    if not entries:
        print("No valid results found.", file=sys.stderr)
        sys.exit(1)

    md = generate_markdown(entries)
    print(md)

    if args.output:
        with open(args.output, "w") as f:
            json.dump(entries, f, indent=2)
        print(f"\nJSON output saved to {args.output}", file=sys.stderr)


if __name__ == "__main__":
    main()