Preprocessing & Normalization¶
preprocessing
¶
Batch preprocessing of BVH directories into ML-ready datasets.
Converts a directory of BVH files into on-disk arrays (npz or
hdf5) with skeleton metadata and normalization statistics.
extract_repr(bvh: Bvh, representation: str) -> tuple[npt.NDArray[np.float64], npt.NDArray[np.float64]]
¶
Extract (root_pos, joint_rot) for the given representation.
Thin dispatcher over pybvh's to_* methods; exposed publicly so
the PyTorch datasets can reuse the same mapping without reaching
into a private symbol.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bvh
|
Bvh
|
|
required |
representation
|
('euler', 'quat', '6d', 'axisangle')
|
|
"euler"
|
Returns:
| Name | Type | Description |
|---|---|---|
root_pos |
(ndarray, shape(F, 3))
|
|
joint_rot |
(ndarray, shape(F, J, C_repr))
|
|
compute_normalization_stats(bvh_list: list[Bvh], representation: str = 'euler', include_root_pos: bool = True, center_root: bool = False) -> dict[str, npt.NDArray]
¶
Compute per-channel mean and std across a dataset of BVH objects.
Extracts every clip in the given representation, concatenates all
frames, then computes mean and standard deviation per feature
channel. Compatible with the Mean.npy / Std.npy convention
used by HumanML3D and MDM. The channel layout matches
:func:pybvh_ml.pack_to_flat and the arrays saved by
:func:preprocess_directory: [root_pos (3), joint_data flattened
over (J, C)] per frame.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bvh_list
|
list of Bvh
|
Dataset of BVH objects. Clips must share the same skeleton
graph (joint names + parent indices); bone-length variation
across actors is accepted, matching the loose compatibility
convention of :func: |
required |
representation
|
str
|
Rotation representation: |
'euler'
|
include_root_pos
|
bool
|
If True (default), include root position in the features. |
True
|
center_root
|
bool
|
If True, subtract each clip's first-frame root position before
computing the stats — reproducing exactly the |
False
|
Returns:
| Type | Description |
|---|---|
dict
|
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Notes
Save/load stats with np.savez("stats.npz", **stats) and
dict(np.load("stats.npz")). Bool arrays round-trip cleanly
through .npz.
normalize_array(data: npt.NDArray[np.float64], stats: dict[str, npt.NDArray[np.float64]]) -> npt.NDArray[np.float64]
¶
Apply z-score normalization: (data - mean) / std.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
ndarray
|
Data to normalize. Last dimension must match |
required |
stats
|
dict
|
|
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
Normalized data, same shape as input. |
denormalize_array(data: npt.NDArray[np.float64], stats: dict[str, npt.NDArray[np.float64]]) -> npt.NDArray[np.float64]
¶
Reverse z-score normalization: data * std + mean.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
ndarray
|
Normalized data to denormalize. |
required |
stats
|
dict
|
|
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
Denormalized data, same shape as input. |
preprocess_directory(bvh_dir: str | Path, output_path: str | Path, representation: str = '6d', center_root: bool = True, include_positions: bool = False, position_space: str = 'joint', position_centering: str = 'world', include_quaternions: bool = False, include_velocities: bool = False, include_foot_contacts: bool = False, foot_joints: list[str] | None = None, label_fn: Callable[[str], int] | None = None, filter_fn: Callable[[str], bool] | None = None, file_pattern: str = '*.bvh', skip_errors: bool = False, world_up: str = 'auto', lr_mapping: dict[str, str] | None = None, harmonize: bool = False, retarget: bool = False, target_world_up: str | None = None, target_rest_forward: str | None = None, target_rest_up: str | None = None, target_euler_order: str | None = None, target_fps: float | None = None, parallel: bool = False, max_workers: int | None = None) -> dict
¶
Convert a directory of BVH files to an on-disk dataset.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bvh_dir
|
path - like
|
Directory containing BVH files. |
required |
output_path
|
path - like
|
Output file path. Extension determines format:
|
required |
representation
|
str
|
Rotation representation for joint data. |
'6d'
|
center_root
|
bool
|
If True, subtract first frame's root position per clip.
The flag is recorded in the saved dataset's metadata and surfaced by :func: With |
True
|
include_positions
|
bool
|
If True, also store per-vertex 3-D positions — the stream
skeleton action-recognition models consume. Derived from
:meth: Unlike |
False
|
position_space
|
('joint', 'node')
|
Which index space to store. Recorded in |
"joint"
|
position_centering
|
('world', 'skeleton', 'first')
|
Which frame the stored positions are in, passed to pybvh's
Recording it is mandatory for anything this library writes: a
position array whose frame convention we failed to record is
exactly the case a caller cannot recover from. (A
hand-assembled :class: |
"world"
|
include_quaternions
|
bool
|
If True, also store pre-computed quaternion arrays per clip
(useful for runtime speed perturbation / dropout). When
|
False
|
include_velocities
|
bool
|
If True, compute per-joint linear velocities via
:meth: |
False
|
include_foot_contacts
|
bool
|
If True, compute binary foot-contact labels via
:meth: |
False
|
foot_joints
|
list of str
|
Explicit foot joint names for contact detection. |
None
|
label_fn
|
callable
|
|
None
|
filter_fn
|
callable
|
|
None
|
file_pattern
|
str
|
Glob pattern for BVH files (default |
'*.bvh'
|
skip_errors
|
bool
|
If True, files that fail to load emit a |
False
|
world_up
|
str
|
Forwarded to :func: |
'auto'
|
lr_mapping
|
dict or None
|
Forwarded to :func: |
None
|
harmonize
|
bool
|
If True, run :func: Harmonization is pure reorientation/resampling: each actor's
bone lengths are preserved (bone-length variation across
actors is intrinsic data, see the skeleton-compatibility
notes). Pass Default |
False
|
retarget
|
bool
|
Only honored with |
False
|
target_world_up
|
str or None
|
Signed-axis string ( |
None
|
target_rest_forward
|
str or None
|
Same dual semantics as |
None
|
target_rest_up
|
str or None
|
Same dual semantics as |
None
|
target_euler_order
|
str or None
|
Canonical Euler order ( |
None
|
target_fps
|
float or None
|
Frame rate in Hz to resample every clip to, applied before
extraction via :meth: Same dual semantics as |
None
|
parallel
|
bool
|
If True, load BVH files using a :class: |
False
|
max_workers
|
int
|
Thread count when |
None
|
Notes
Uniformity warnings. After loading, this function inspects
every clip's frame rate, animation-derived world_up, rest-pose
forward direction, and rest-pose up axis. It emits one aggregated
:class:UserWarning per category when files disagree, plus a
separate aggregated warning when any file's rest-pose up axis
disagrees with its own animation-derived world_up (pybvh's
per-file rest/animation-disagreement warning is suppressed during
load in favor of this one batch-level message). Warnings include
the distribution of values, the first three example filenames per
minority value, and the exact kwarg that would fix it
(target_fps, target_world_up, target_rest_forward,
target_rest_up). When the corresponding target_* kwarg is
explicitly set, that category's check is skipped (the target value
becomes the post-reorient ground truth).
Returns:
| Type | Description |
|---|---|
dict
|
Summary with keys: This is a report on the run, not the dataset. It carries
the decisions that shaped the file plus the heterogeneity
audit, and deliberately none of the arrays —
The four distributions capture the pre-transform state of
the dataset (useful for CI gates that want to fail on
heterogeneity); what was then done to it is the other two
keys, exactly one of which can be present.
When Otherwise |
load_preprocessed(path: str | Path) -> dict
¶
Load a preprocessed dataset from disk.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
path - like
|
Path to |
required |
Returns:
| Type | Description |
|---|---|
dict
|
Keys:
|