Motion data tools¶
ImitationLab’s data package prepares reference motions and manages replay storage, trajectory cursors, and reset frames. This page describes the package’s Python API and is for developers who call it directly or extend it. To download, convert, and pack a dataset from the command line, follow the motion data guide instead.
Pixi installs the dependencies from the repository root; see setup.
Modules¶
Module |
Purpose |
|---|---|
|
Normalize raw G1 CSVs and apply the public SONIC motion-name filter |
|
Pack an NPZ motion manifest into validated memory-mapped arrays |
|
Transfer packed banks through authorized Hugging Face storage |
|
Resample CSV or NPZ motions and write a Zarr store |
|
Build or reopen a TorchRL replay buffer from a Zarr store |
|
Track a reference trajectory and frame per environment |
|
Sample start frames, including failure-adaptive SONIC sampling |
|
Resolve named dataset loaders, including the |
Commands¶
imitationlab data has three subcommands. Run imitationlab data --help for
their options.
Subcommand |
Purpose |
|---|---|
|
Run a resumable dataset conversion |
|
Pack an NPZ manifest into a reference bank |
|
Validate a reference bank directory or a clip manifest |
Dataset preparation keeps completed batches and can resume from the same work directory. Packing writes the manifest last, so an interrupted pack does not appear to be a complete bank.
LAFAN1 loader¶
Lafan1CsvLoader reads the CSV layout of the retargeted LAFAN1 release for the
Unitree G1 (one row per frame):
columns
0:3: root position in the world frame,columns
3:7: root orientation as anxyzwquaternion (stored aswxyz),columns
7:: joint positions.
NPZ files with joint_pos, joint_vel, body_pos_w, body_quat_w, … and an
fps entry are accepted as well. Motions are resampled to control_freq and
root/joint velocities are computed by finite differences. The motion data
itself is distributed under its own license and is not part of this package.
from imitationlab.data_tools.lafan1.loader import Lafan1CsvLoader
cfg = {
"dataset": {
"trajectories": {
"lafan1_csv": [
"/path/to/motions/walk1_subject1.csv",
{"path": "/path/to/motions/dance1_subject2.csv",
"input_fps": 30, "frame_range": [1, 1200]},
{"name": "run_combo",
"paths": ["/path/to/motions/run1_subject2.csv",
"/path/to/motions/run2_subject1.csv"]},
]
}
},
"joint_names": [...], # CSV column order; defaults to joint_0, joint_1, ...
"input_fps": 30, # default source rate for entries without input_fps
"control_freq": 50, # output rate written to the store
}
loader = Lafan1CsvLoader(cfg=cfg, build_zarr_dataset=True,
zarr_path="/path/to/lafan1.zarr")
print(loader.metadata) # DatasetMeta: trajectory lengths, dt, joint names, keys
Each entry of lafan1_csv is a path, a {"path": ..., "input_fps": ..., "frame_range": [start, end]} dict (1-indexed, inclusive), or a
{"name": ..., "paths": [...]} group that stores several files as
trajectories of one motion. cfg may be a plain mapping or any object with
attribute access (an OmegaConf DictConfig works). Set
canonical_joint_names to write every motion in one joint order.
The store is laid out as <dataset>/<motion>/trajectory_<i>/<key>, with the
per-frame arrays qpos, qvel, root_pos, root_quat, root_lin_vel,
root_ang_vel, joint_pos, joint_vel, the corresponding next_* arrays,
and body_*_w arrays when the source provides them. The dataset group’s
attributes carry the trajectory manifest, dt, control_freq and the joint
names.
Replay buffer and trajectory manager¶
This example continues from the loader above: it reads the Zarr store the loader wrote and steps one reference cursor per environment.
import torch
from imitationlab.data_tools.common.trajectories import ParallelTrajectoryManager, ResetSchedule
from imitationlab.data_tools.common.reset_sampling import StartFrameSampler
from imitationlab.data_tools.common.replay import make_rb_from
rb, info = make_rb_from(
"/path/to/lafan1.zarr",
datasets="lafan1",
keys=["qpos", "qvel", "joint_pos", "joint_vel", "root_pos", "root_quat"],
persist_dir="/path/to/lafan1_rb", # optional memmap copy, reopened on later runs
)
num_envs = 4096
manager = ParallelTrajectoryManager(
rb=rb,
traj_info=info,
num_envs=num_envs,
reset_schedule=ResetSchedule.RANDOM, # or SEQUENTIAL, ROUND_ROBIN, CUSTOM
reference_joint_names=loader.metadata.joint_names,
target_joint_names=robot_joint_names, # the articulation's joint order
reset_generator=torch.Generator().manual_seed(0),
)
start_frames = StartFrameSampler(manager.length, mode="uniform")
env_ids = torch.arange(num_envs)
manager.reset_envs(env_ids) # pick trajectories
steps = start_frames.sample_steps(manager.env_traj_rank[env_ids])
manager.reset_envs(env_ids, steps=steps) # pick start frames
reference = manager.sample() # TensorDict [num_envs] of the current frame
# ... step the simulation; manager.sample() advanced every cursor by one frame
rank = manager.get_traj_rank("lafan1", "walk1_subject1", "trajectory_0")
make_rb_from fills a CPU memmap (or GPU) TensorDictReplayBuffer with every
selected frame and returns, alongside it, start_index, end_index and
ordered_traj_list, which the manager uses to address trajectories by rank.
With persist_dir, a later call memory-maps the filled buffer instead of
walking the Zarr store again; pass persist_rebuild=True after the store
changes.
ParallelTrajectoryManager keeps a (trajectory_rank, local_step) cursor per
environment.
Method |
Behavior |
|---|---|
|
Indexes the buffer storage directly with the global indices of all cursors (one frame per environment) and, by default, advances them |
|
Returns a window per environment |
|
Moves cursors without reading storage |
|
Reassigns trajectories according to |
Both joint-name lists are required: the manager maps the reference joint order to the target order and marks joints the reference does not provide.
Reset sampling¶
Trajectory selection belongs to the manager. StartFrameSampler answers the
second question, which frame to start at, in four modes:
Mode |
Start frame |
|---|---|
|
Always |
|
Uniform in a configured range |
|
Uniform over each trajectory’s own length |
|
Proportional to a caller-supplied |
SonicAdaptiveResetSampler is a failure-aware sampler: it keeps per-bin visit
and failure counts, mixes the failure-rate distribution with a uniform floor,
and can be used standalone (sample(count) returns ranks and frames jointly)
or as the weight_fn of StartFrameSampler.
from imitationlab.data_tools.common.reset_sampling import SonicAdaptiveResetSampler, StartFrameSampler
adaptive = SonicAdaptiveResetSampler(manager.length, bin_size=50)
adaptive.record_visits(manager.env_traj_rank, manager.env_step)
adaptive.record_failures(manager.env_traj_rank[failed], manager.env_step[failed])
start_frames = StartFrameSampler(manager.length, mode="adaptive", weight_fn=adaptive)
Tests¶
pixi run --locked python -m pytest -q imitationlab/data_tools/tests
The tests build small synthetic CSV / NPZ motions in a temporary directory, so no dataset download is needed.
Third-party attributions¶
The CSV layout read by
Lafan1CsvLoaderand its preprocessing assumptions (root position,xyzwroot quaternion, joint positions; resampling and finite-difference velocities) followscripts/mimic/csv_to_npz.pyofunitree_rl_lab(Unitree Robotics), as noted inlafan1/loader.py.SonicAdaptiveResetSamplerreimplements the failure-rate-weighted motion/frame reset sampling of SONIC’s public motion library.
License¶
MIT, see LICENSE. Motion datasets are licensed separately by their owners.