code-HFD / src /psi /data /seed /motion_frame_index.py
Tinghong-Ye22's picture
Add files using upload-large-folder tool
06f9561 verified
Raw History Blame Contribute Delete
4.5 kB
from __future__ import annotations
from pathlib import Path
from typing import Any, Iterable
import pandas as pd
MOTION_CSV_NAMES = ("joint_pos.csv", "body_pos.csv", "body_quat.csv")
def read_motion_frame_count(motion_dir: str | Path) -> int:
"""Return the real number of frames that can form the motion array.
Metadata durations are deliberately not accepted here: the training motion
array is assembled from these three CSV files, so their shared row count is
the only valid source of truth for sample indexing.
"""
motion_dir = Path(motion_dir)
lengths: dict[str, int] = {}
for filename in MOTION_CSV_NAMES:
path = motion_dir / filename
if not path.exists():
raise FileNotFoundError(f"Motion CSV not found: {path}")
# Parse only one column. This preserves pandas' CSV row semantics while
# avoiding the cost of loading all 36 motion dimensions during indexing.
lengths[filename] = int(len(pd.read_csv(path, usecols=[0])))
if len(set(lengths.values())) != 1:
details = ", ".join(f"{name}={length}" for name, length in lengths.items())
raise ValueError(f"Frame length mismatch at {motion_dir}: {details}")
n_frames = next(iter(lengths.values()))
if n_frames <= 0:
raise ValueError(f"Motion contains no frames: {motion_dir}")
return n_frames
def valid_sample_count(
n_frames: int,
*,
index_step_frames: int,
action_step_frames: int,
use_delta_actions: bool,
supervision_prefix_frames: int = 0,
) -> int:
"""Count starts with a real target after any condition-only prefix.
The returned count is defined on the final action/mask time axis emitted by
``SeedPosttrainRepackTransform``. For absolute/TextOp actions, output token
zero targets the next motion frame. Delta mode loads the current frame as
an extra input and then drops it during repacking, so its output token zero
also targets the next motion frame.
"""
n_frames = int(n_frames)
index_step_frames = int(index_step_frames)
action_step_frames = int(action_step_frames)
supervision_prefix_frames = int(supervision_prefix_frames)
if index_step_frames <= 0 or action_step_frames <= 0:
raise ValueError(
"index_step_frames and action_step_frames must be positive, got "
f"{index_step_frames} and {action_step_frames}"
)
if supervision_prefix_frames < 0:
raise ValueError(
"supervision_prefix_frames must be non-negative, got "
f"{supervision_prefix_frames}"
)
# Both branches have the same final-output alignment for different reasons.
if use_delta_actions:
# Delta datasets load current frame at raw step zero, but repacking
# drops that element; final output token zero therefore uses raw step 1.
first_output_target_step = 1
else:
# Absolute/TextOp datasets directly place the next frame at token zero.
first_output_target_step = 1
first_supervised_target_step = first_output_target_step + supervision_prefix_frames
first_target_offset = first_supervised_target_step * action_step_frames
max_start_frame = n_frames - 1 - first_target_offset
if max_start_frame < 0:
return 0
return max_start_frame // index_step_frames + 1
def summarize_declared_vs_actual(records: Iterable[dict[str, Any]]) -> dict[str, int | float]:
"""Summarize how much a metadata-driven index would have overrun motion."""
motions = 0
mismatch_motions = 0
actual_frames = 0
declared_frames = 0
declared_overrun_frames = 0
for record in records:
motions += 1
actual = max(int(record.get("n_frames", 0) or 0), 0)
declared = max(int(record.get("declared_n_frames", 0) or 0), 0)
actual_frames += actual
if declared > 0:
declared_frames += declared
if declared != actual:
mismatch_motions += 1
declared_overrun_frames += max(declared - actual, 0)
overrun_fraction = (
float(declared_overrun_frames) / float(declared_frames)
if declared_frames > 0
else 0.0
)
return {
"motions": motions,
"mismatch_motions": mismatch_motions,
"actual_frames": actual_frames,
"declared_frames": declared_frames,
"declared_overrun_frames": declared_overrun_frames,
"declared_overrun_fraction": overrun_fraction,
}