#!/usr/bin/env python3 import json import math from pathlib import Path import numpy as np DATA_DIR = Path("data") EXPECTED_SHAPE = (64, 32, 32) def fail(msg): raise RuntimeError(msg) def validate_one(npz_path: Path): json_path = npz_path.with_suffix(".json") if not json_path.exists(): fail(f"missing json: {json_path}") with np.load(npz_path, allow_pickle=False) as z: if "frames" not in z.files: fail(f"{npz_path}: missing key frames") frames = z["frames"] if tuple(frames.shape) != EXPECTED_SHAPE: fail(f"{npz_path}: bad shape {frames.shape}") if frames.dtype != np.float32: fail(f"{npz_path}: bad dtype {frames.dtype}") if not np.isfinite(frames).all(): fail(f"{npz_path}: contains NaN or Inf") with json_path.open("r", encoding="utf-8") as f: meta = json.load(f) if meta.get("file_name") != npz_path.name: fail(f"{json_path}: file_name does not match npz name") targets = meta.get("targets") if not isinstance(targets, dict): fail(f"{json_path}: missing targets") label = targets.get("deformation_response", targets.get("rigidity")) if label not in ("rigid", "deformable"): fail(f"{json_path}: invalid label {label!r}") stiffness = targets.get("stiffness") if stiffness is not None: if not isinstance(stiffness, (int, float)) or not math.isfinite(float(stiffness)): fail(f"{json_path}: invalid stiffness {stiffness!r}") def main(): total = 0 for split in ["train", "validation", "val", "test"]: split_dir = DATA_DIR / split if not split_dir.exists(): continue for npz_path in sorted(split_dir.glob("*.npz")): validate_one(npz_path) total += 1 print(f"OK: validated {total} npz/json pairs") if __name__ == "__main__": main()