"""Rebuild the verified MedAraBench test set from QIMMA's public copy. Writes one file per config, //data.parquet, with the source columns unchanged. Usage: python apply_filter.py [output_dir] """ import hashlib import json import os import sys import pandas as pd from huggingface_hub import hf_hub_download SOURCE = "qimma/MCQ_MedAraBench" SOURCE_REVISION = "c62cc54f8d9d9e9e4be5aca82982b58e2da3cf0e" THIS_REPO = "IFM/MedAraBench_verified" COLUMNS = ["prompt", "question", "choices", "index"] EXPECTED_KEPT = { "Anatomy": 830, "Histology": 188, "Histology and Anatomy": 39, "Histology and Systemic Anatomy": 8, "Anesthesia": 70, "Biochemistry": 303, "Cell Biology": 197, "Genetics": 176, "Physics": 521, "Chemistry": 71, "Embryology": 5, "Emergency Medicine": 21, "Gastroenterology": 29, "Neurology": 43, "Cardiology": 17, "Thorasic Internal": 17, "Oncology": 12, "Chest Diseases": 6, "Gastroenterology & Hepatology": 38, "Microbiology": 115, "Ophthalmology": 234, "Pathology": 43, "Pharmacology": 62, "Physiology": 573, "Statistics": 310, "Surgery": 103, } def fingerprint(row): values = [row["prompt"], row["question"], json.dumps(list(row["choices"]), ensure_ascii=False), str(row["index"])] return hashlib.sha256("\x1f".join(values).encode()).hexdigest()[:16] ids = pd.read_csv(hf_hub_download(THIS_REPO, "ids.csv", repo_type="dataset")) out = sys.argv[1] if len(sys.argv) > 1 else "medarabench_verified" total = 0 for config, expected in EXPECTED_KEPT.items(): cfg = ids[ids["config"].eq(config)].sort_values("row").reset_index(drop=True) source = pd.read_parquet(hf_hub_download(SOURCE, f"{config}/data.parquet", repo_type="dataset", revision=SOURCE_REVISION)) assert len(source) == len(cfg), f"{config}: expected {len(cfg)} source rows, got {len(source)}" mismatch = [i for i, row in source.iterrows() if fingerprint(row) != cfg.at[i, "fingerprint"]] assert not mismatch, f"{config}: source rows differ from the audited version: {mismatch[:10]}" verified = source[cfg["status"].eq("kept").to_numpy()].reset_index(drop=True) assert len(verified) == expected, f"{config}: expected {expected} rows, got {len(verified)}" os.makedirs(os.path.join(out, config), exist_ok=True) verified.to_parquet(os.path.join(out, config, "data.parquet")) total += len(verified) assert total == 4031, f"expected 4031 rows, got {total}" print(f"Wrote {total} rows in {len(EXPECTED_KEPT)} configs to {out}/")