File size: 3,138 Bytes
7332d7b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Inspect a v9 raw episode; does not construct aligned training targets."""
import argparse
import io
import json

import h5py
import numpy as np
from PIL import Image


def inspect(path):
    with h5py.File(path, "r") as episode:
        times = episode["sim/time"][:]
        if times.ndim != 1 or len(times) == 0:
            raise ValueError("Expected a nonempty sim/time vector")
        if not np.isfinite(times).all() or not np.all(np.diff(times) > 0):
            raise ValueError("sim/time must be finite and strictly increasing")
        count = len(times)
        fields = {}
        for key, width in [("observations/qpos", 16), ("leader/qpos", 16),
                           ("observations/eef", 14), ("leader/eef", 14),
                           ("lifter/action_mm", 2)]:
            dataset = episode[key]
            if dataset.shape != (count, width):
                raise ValueError(f"Unexpected shape for {key}: {dataset.shape}")
            if not np.isfinite(dataset[:]).all():
                raise ValueError(f"Nonfinite values in {key}")
            fields[key] = {
                "shape": list(dataset.shape),
                "dtype": str(dataset.dtype),
                "attributes": {k: str(v) for k, v in dataset.attrs.items()},
            }
        cameras = {}
        for camera in ["right_front_camera", "left_back_camera",
                       "left_wrist_camera", "back_wrist_camera"]:
            frames = episode[f"observations/images/{camera}"]
            if frames.shape != (count,):
                raise ValueError(f"Camera length mismatch: {camera}")
            samples = []
            for index in sorted({0, count // 2, count - 1}):
                payload = frames[index]
                blob = payload.tobytes() if hasattr(payload, "tobytes") else payload
                with Image.open(io.BytesIO(blob)) as image:
                    image.load()
                    samples.append({"frame": index, "size": list(image.size)})
            cameras[camera] = samples
        timing = {}
        for key in ["physical/issued_command/tick", "physical/applied_command/tick"]:
            if key in episode:
                data = episode[key]
                timing[key] = {"shape": list(data.shape),
                               "first": int(data[0]), "last": int(data[-1])}
        return {
            "frames": count,
            "sim_time_first_last": [float(times[0]), float(times[-1])],
            "metadata": {k: str(episode.attrs[k]) for k in
                         ["schema_version", "fps", "physics_hz", "command_hz",
                          "extra_command_delay_ms", "task_success", "timestamp_clock"]
                         if k in episode.attrs},
            "fields": fields, "sampled_images": cameras, "command_ticks": timing,
            "scope": "Raw inspection only; no action alignment, posture or success certification.",
        }


if __name__ == "__main__":
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument("episode", help="Local HDF5 episode path")
    print(json.dumps(inspect(parser.parse_args().episode), indent=2))