#!/usr/bin/env python3 """Sample N MathVision test items with a fixed seed and write images + items.jsonl. Items without an image are excluded before sampling. Every item keeps its official id, question, options, answer, level, subject, and image sha256. """ from __future__ import annotations import argparse import hashlib import json import random from pathlib import Path import pyarrow.parquet as pq def main() -> None: parser = argparse.ArgumentParser() parser.add_argument("--parquet", type=Path, required=True) parser.add_argument("--out", type=Path, required=True) parser.add_argument("--n", type=int, default=300) parser.add_argument("--seed", type=int, default=20261106) args = parser.parse_args() rows = [r for r in pq.read_table(args.parquet).to_pylist() if r["decoded_image"] and r["decoded_image"]["bytes"]] rows.sort(key=lambda r: int(r["id"])) sample = sorted(random.Random(args.seed).sample(rows, args.n), key=lambda r: int(r["id"])) image_dir = args.out / "images" image_dir.mkdir(parents=True, exist_ok=True) with (args.out / "items.jsonl").open("w", encoding="utf-8") as handle: for r in sample: data = r["decoded_image"]["bytes"] suffix = Path(r["image"]).suffix or ".png" path = image_dir / f"{r['id']}{suffix}" path.write_bytes(data) handle.write(json.dumps({ "item_id": f"mathvision_{r['id']}", "id": r["id"], "question": r["question"], "options": r["options"], "answer": r["answer"], "level": r["level"], "subject": r["subject"], "image": f"images/{path.name}", "image_sha256": hashlib.sha256(data).hexdigest(), }, ensure_ascii=False) + "\n") print(json.dumps({"pool": len(rows), "sampled": len(sample), "seed": args.seed, "multiple_choice": sum(bool(r["options"]) for r in sample)})) if __name__ == "__main__": main()