#!/usr/bin/env python3 """Validate the public SABRE-Prior release tree before upload.""" from __future__ import annotations import json from collections import Counter, defaultdict from pathlib import Path from typing import Any EXPECTED = { "context": {"questions": 400, "images": 200}, "texture": {"questions": 400, "images": 200}, "attribute": {"questions": 100, "images": 100}, "language": {"questions": 100, "images": 100}, } PROBES = { "context": {"base_source", "base_target", "edited_source", "edited_target"}, "texture": {"base_normal", "base_counterfactual", "edited_normal", "edited_counterfactual"}, } def load_jsonl(path: Path) -> list[dict[str, Any]]: return [json.loads(line) for line in path.read_text(encoding="utf-8").splitlines() if line.strip()] def main() -> int: root = Path(__file__).resolve().parent all_ids: set[str] = set() totals = Counter() for subset, expected in EXPECTED.items(): subset_root = root / "data" / subset metadata_path = subset_root / "metadata.jsonl" rows = load_jsonl(metadata_path) if len(rows) != expected["questions"]: raise ValueError(f"{subset}: expected {expected['questions']} questions, found {len(rows)}") ids = [str(row.get("id") or "") for row in rows] if any(not item_id for item_id in ids) or len(ids) != len(set(ids)): raise ValueError(f"{subset}: IDs must be present and unique") overlap = all_ids.intersection(ids) if overlap: raise ValueError(f"ID appears in multiple subsets: {sorted(overlap)[0]}") all_ids.update(ids) referenced: set[Path] = set() for row in rows: if row.get("subset") != subset: raise ValueError(f"{row['id']}: incorrect subset") file_name = Path(str(row.get("file_name") or "")) if file_name.is_absolute() or ".." in file_name.parts: raise ValueError(f"{row['id']}: unsafe file_name") image_path = subset_root / file_name if not image_path.is_file(): raise FileNotFoundError(f"{row['id']}: {image_path}") referenced.add(image_path.resolve()) image_files = { path.resolve() for path in (subset_root / "images").iterdir() if path.is_file() and not path.name.startswith(".") } if len(image_files) != expected["images"]: raise ValueError(f"{subset}: expected {expected['images']} images, found {len(image_files)}") if image_files != referenced: raise ValueError(f"{subset}: image directory and metadata references differ") if subset in PROBES: by_pair: dict[str, set[str]] = defaultdict(set) for row in rows: if str(row["answer"]).lower() not in {"yes", "no"}: raise ValueError(f"{row['id']}: invalid yes/no answer") by_pair[str(row.get("pair_id") or "")].add(str(row.get("probe") or "")) if len(by_pair) != 100 or any(probes != PROBES[subset] for probes in by_pair.values()): raise ValueError(f"{subset}: expected 100 complete four-probe pairs") elif subset == "attribute": if any(not str(row["answer"]).isdigit() for row in rows): raise ValueError("attribute: every public answer must be an integer count") elif subset == "language": for row in rows: options = row.get("options") if not isinstance(options, dict) or set(options) != {"A", "B", "C", "D"}: raise ValueError(f"{row['id']}: invalid options") if row["answer"] not in options or row.get("answer_text") != options[row["answer"]]: raise ValueError(f"{row['id']}: answer and answer_text disagree") totals["questions"] += len(rows) totals["images"] += len(image_files) print(f"{subset}: {len(rows)} questions, {len(image_files)} images [ok]") if totals != Counter({"questions": 1000, "images": 600}): raise ValueError(f"Unexpected totals: {dict(totals)}") print("SABRE-Prior: 1,000 questions, 600 images [valid]") return 0 if __name__ == "__main__": raise SystemExit(main())