from __future__ import annotations import argparse import hashlib import json from pathlib import Path import sys ROOT = Path(__file__).resolve().parents[1] sys.path.insert(0, str(ROOT / "src")) from sparsewake.data import LABEL_NAMES, describe_h5, load_h5 def sha256(path: Path) -> str: digest = hashlib.sha256() with path.open("rb") as handle: for chunk in iter(lambda: handle.read(1024 * 1024), b""): digest.update(chunk) return digest.hexdigest() def verify_checksums(manifest: Path, root: Path) -> dict[str, object]: checked = [] for raw in manifest.read_text().splitlines(): line = raw.strip() if not line or line.startswith("#"): continue expected, rel = line.split(None, 1) path = root / rel if not path.exists(): raise SystemExit(f"Checksum target is missing: {rel}") found = sha256(path) if found.lower() != expected.lower(): raise SystemExit(f"Checksum mismatch for {rel}: expected {expected}, found {found}") checked.append(rel) return {"checksum_manifest": manifest.as_posix(), "files_checked": len(checked)} def main() -> None: parser = argparse.ArgumentParser() parser.add_argument("--data", required=True) parser.add_argument("--checksums", default=None) args = parser.parse_args() data = load_h5(args.data) required = ["X_raw", "y", "groups", "region_id", "sensor_world_positions"] missing = [key for key in required if key not in data] if missing: raise SystemExit(f"Missing required datasets: {missing}") if data["input"].ndim != 3 or data["input"].shape[1:] != (6, 3): raise SystemExit(f"Expected input shape [N, 6, 3], found {data['input'].shape}") if data["y"].shape[1] < len(LABEL_NAMES): raise SystemExit(f"Expected at least {len(LABEL_NAMES)} label columns, found {data['y'].shape[1]}") if len(set(data["pose_id"].tolist())) < 2: raise SystemExit("Pose-holdout verification requires at least two inferred poses") summary = describe_h5(args.data) summary["n_samples"] = int(data["y"].shape[0]) summary["n_poses"] = int(len(set(data["pose_id"].tolist()))) if args.checksums: summary["checksums"] = verify_checksums(Path(args.checksums), ROOT) print(json.dumps(summary, indent=2)) if __name__ == "__main__": main()