#!/usr/bin/env python3 """Verify MDU-RiskText/MDU-RiskBench row alignment and file hashes.""" from __future__ import annotations import gzip import hashlib import json from collections import Counter from pathlib import Path ROOT = Path(__file__).resolve().parents[1] SETTINGS = ("video_only", "non_video_only", "all_filtered_text") SPLITS = ("train", "validation", "test") def digest(path: Path) -> str: value = hashlib.sha256() with path.open("rb") as handle: for chunk in iter(lambda: handle.read(1024 * 1024), b""): value.update(chunk) return value.hexdigest() def rows(path: Path): with gzip.open(path, "rt", encoding="utf-8") as handle: for line in handle: if line.strip(): yield json.loads(line) def main() -> None: manifest = json.loads((ROOT / "MANIFEST.json").read_text(encoding="utf-8")) for entry in manifest["files"]: path = ROOT / entry["path"] if path.stat().st_size != entry["bytes"]: raise SystemExit(f"size mismatch: {entry['path']}") if digest(path) != entry["sha256"]: raise SystemExit(f"hash mismatch: {entry['path']}") identities = {} for setting in SETTINGS: current = {} for split in SPLITS: path = ROOT / "MDU-RiskText" / setting / f"{split}.jsonl.gz" for row in rows(path): if row["split"] != split or row["benchmark_setting"] != setting: raise SystemExit(f"metadata mismatch: {path}") package = row["llm_evidence_package"] if package["sample_id"] != row["sample_id"]: raise SystemExit(f"package ID mismatch: {row['sample_id']}") if json.loads(row["text_evidence"]) != package: raise SystemExit(f"text package mismatch: {row['sample_id']}") current[row["sample_id"]] = ( split, row["target_label"]["risk_level"], ) if len(current) != 850: raise SystemExit(f"{setting}: expected 850 records, got {len(current)}") if identities and current != identities: raise SystemExit(f"cross-setting identity mismatch: {setting}") identities = current split_counts = Counter(split for split, _ in identities.values()) label_counts = Counter(label for _, label in identities.values()) expected_splits = {"train": 593, "validation": 131, "test": 126} expected_labels = { "no_observed_risk": 226, "mild_risk": 432, "moderate_risk": 172, "high_risk": 20, } if dict(split_counts) != expected_splits: raise SystemExit(f"split counts differ: {dict(split_counts)}") if dict(label_counts) != expected_labels: raise SystemExit(f"label counts differ: {dict(label_counts)}") print("Verified 850 aligned participants across 3 settings and 9 data files.") if __name__ == "__main__": main()