File size: 1,924 Bytes
cc858da
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/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()