Download scripts/verify_schema.py from hahela/kine2go: direct link, hf CLI and curl.
- Browser
- Download file 6.34 kB
-
https://huggingface.co/datasets/hahela/kine2go/resolve/main/scripts/verify_schema.py
- Command line
-
hf download hf://datasets/hahela/kine2go/scripts/verify_schema.py
-
curl -L -o verify_schema.py https://huggingface.co/datasets/hahela/kine2go/resolve/main/scripts/verify_schema.py
6.34 kB
| """Verify the on-disk Kine2Go dataset matches the schema documented in metadata.json. | |
| Usage: | |
| python dataset/scripts/verify_schema.py | |
| python dataset/scripts/verify_schema.py --dataset /path/to/kine2go | |
| Exits non-zero on any mismatch. | |
| """ | |
| from __future__ import annotations | |
| import argparse | |
| import json | |
| import sys | |
| from collections import Counter | |
| from pathlib import Path | |
| import numpy as np | |
| N_ROLLOUTS = 20 | |
| MOTION_FEATURE_DIM = 61 | |
| BASE_QUAT_SLICE = slice(51, 55) | |
| QUAT_NORM_TOLERANCE = 1e-2 | |
| DURATION_TOLERANCE_S = 1e-2 | |
| REQUIRED_CLIP_FILES = ("motion.npy", "cfgs.pkl", "config.json", "clip.json") | |
| REQUIRED_CLIP_JSON_KEYS = { | |
| "name", | |
| "source", | |
| "source_url", | |
| "license", | |
| "attribution", | |
| "fps", | |
| "n_frames", | |
| "duration_s", | |
| "description", | |
| "tags", | |
| } | |
| REQUIRED_CONFIG_JSON_KEYS = { | |
| "env_cfg", | |
| "obs_cfg", | |
| "reward_cfg", | |
| "command_cfg", | |
| "policy_cfg", | |
| } | |
| def family_of(clip_name: str) -> str: | |
| if clip_name.startswith("ai4_dog_synth_"): | |
| return "ai4_dog_synth" | |
| if clip_name.startswith("ai4_dog_"): | |
| return "ai4_dog" | |
| if clip_name.startswith("vhdc_"): | |
| return "vhdc" | |
| if clip_name.startswith("solo8_"): | |
| return "solo8" | |
| raise ValueError(f"unknown family for clip: {clip_name}") | |
| def check_clip(clip: Path) -> list[str]: | |
| errors: list[str] = [] | |
| for fname in REQUIRED_CLIP_FILES: | |
| if not (clip / fname).exists(): | |
| errors.append(f"missing {fname}") | |
| motion_path = clip / "motion.npy" | |
| if motion_path.exists(): | |
| motion = np.load(motion_path, mmap_mode="r") | |
| if motion.ndim != 2 or motion.shape[1] != MOTION_FEATURE_DIM: | |
| errors.append( | |
| f"motion.npy shape {motion.shape}, expected (T, {MOTION_FEATURE_DIM})" | |
| ) | |
| if motion.dtype != np.float32: | |
| errors.append(f"motion.npy dtype {motion.dtype}, expected float32") | |
| if motion.ndim == 2 and motion.shape[1] == MOTION_FEATURE_DIM: | |
| quats = np.asarray(motion[:, BASE_QUAT_SLICE]) | |
| norms = np.linalg.norm(quats, axis=1) | |
| if np.any(np.abs(norms - 1.0) > QUAT_NORM_TOLERANCE): | |
| errors.append( | |
| f"motion.npy base_quat norms not unit (min={norms.min():.4f}, max={norms.max():.4f})", | |
| ) | |
| clip_json_path = clip / "clip.json" | |
| if clip_json_path.exists(): | |
| clip_meta = json.loads(clip_json_path.read_text()) | |
| missing = REQUIRED_CLIP_JSON_KEYS - clip_meta.keys() | |
| if missing: | |
| errors.append(f"clip.json missing keys: {sorted(missing)}") | |
| if clip_meta.get("name") != clip.name: | |
| errors.append( | |
| f"clip.json name {clip_meta.get('name')!r} != dir name {clip.name!r}" | |
| ) | |
| fps = clip_meta.get("fps") | |
| n_frames = clip_meta.get("n_frames") | |
| duration = clip_meta.get("duration_s") | |
| if fps and n_frames and duration is not None: | |
| expected = n_frames / fps | |
| if abs(expected - duration) > DURATION_TOLERANCE_S: | |
| errors.append( | |
| f"clip.json duration_s {duration} != n_frames/fps {expected:.3f}" | |
| ) | |
| config_json_path = clip / "config.json" | |
| if config_json_path.exists(): | |
| cfg_obj = json.loads(config_json_path.read_text()) | |
| missing_cfg = REQUIRED_CONFIG_JSON_KEYS - cfg_obj.keys() | |
| if missing_cfg: | |
| errors.append(f"config.json missing keys: {sorted(missing_cfg)}") | |
| for i in range(N_ROLLOUTS): | |
| traj_dir = clip / f"traj_{i:04d}" | |
| if not traj_dir.is_dir(): | |
| errors.append(f"missing rollout dir traj_{i:04d}") | |
| continue | |
| for required in ("traj.pkl", "init_state.pkl", "traj.mp4"): | |
| if not (traj_dir / required).exists(): | |
| errors.append(f"traj_{i:04d}/{required} missing") | |
| logs_dir = clip / "logs" | |
| if not logs_dir.is_dir(): | |
| errors.append("missing logs/ directory") | |
| elif not (logs_dir / "model.pt").exists(): | |
| errors.append("logs/model.pt checkpoint missing") | |
| return errors | |
| def main() -> None: | |
| parser = argparse.ArgumentParser(description="Verify Kine2Go dataset schema.") | |
| parser.add_argument( | |
| "--dataset", | |
| type=Path, | |
| default=Path(__file__).resolve().parent.parent, | |
| help="Dataset root directory (default: parent of this script).", | |
| ) | |
| args = parser.parse_args() | |
| dataset: Path = args.dataset | |
| if not dataset.is_dir(): | |
| sys.exit(f"dataset directory not found: {dataset}") | |
| metadata_path = dataset / "metadata.json" | |
| if not metadata_path.exists(): | |
| sys.exit(f"metadata.json not found at {metadata_path}") | |
| metadata = json.loads(metadata_path.read_text()) | |
| data_dir = dataset / "data" | |
| if not data_dir.is_dir(): | |
| sys.exit(f"data/ directory not found at {data_dir}") | |
| clip_dirs = sorted(p for p in data_dir.iterdir() if p.is_dir()) | |
| top_errors: list[str] = [] | |
| if metadata.get("n_clips") != len(clip_dirs): | |
| top_errors.append( | |
| f"metadata.n_clips {metadata.get('n_clips')} != actual {len(clip_dirs)}" | |
| ) | |
| actual_family_counts = Counter(family_of(c.name) for c in clip_dirs) | |
| declared_subsets = metadata.get("subsets", {}) | |
| for fam, info in declared_subsets.items(): | |
| if info.get("n_clips") != actual_family_counts.get(fam, 0): | |
| top_errors.append( | |
| f"subset {fam!r}: metadata.n_clips={info.get('n_clips')} != actual={actual_family_counts.get(fam, 0)}", | |
| ) | |
| unexpected = set(actual_family_counts) - set(declared_subsets) | |
| if unexpected: | |
| top_errors.append( | |
| f"clips belong to families not in metadata.subsets: {sorted(unexpected)}" | |
| ) | |
| n_failed = 0 | |
| for clip in clip_dirs: | |
| clip_errors = check_clip(clip) | |
| if clip_errors: | |
| n_failed += 1 | |
| print(f"FAIL {clip.name}:") | |
| for e in clip_errors: | |
| print(f" {e}") | |
| else: | |
| print(f"OK {clip.name}") | |
| if top_errors: | |
| print("\nTOP-LEVEL ERRORS:") | |
| for e in top_errors: | |
| print(f" {e}") | |
| print(f"\n{len(clip_dirs) - n_failed}/{len(clip_dirs)} clips passed") | |
| if n_failed or top_errors: | |
| sys.exit(1) | |
| if __name__ == "__main__": | |
| main() | |