from __future__ import annotations import csv import json import tempfile import unittest from pathlib import Path from types import SimpleNamespace import numpy as np from psi.data.seed.motion_frame_index import read_motion_frame_count, valid_sample_count from psi.data.seed.seed_audio_motion_dataset import SeedAudioMotionDataset from psi.data.seed.seed_motion_dataset import SeedMotionDataset from psi.config.data_seed_text_audio_video_mix import SeedTextAudioVideoMixDataConfig def _write_csv(path: Path, columns: int, frames: int) -> None: path.parent.mkdir(parents=True, exist_ok=True) with path.open("w", newline="", encoding="utf-8") as handle: writer = csv.writer(handle) writer.writerow([f"c{column}" for column in range(columns)]) for frame in range(frames): writer.writerow([float(frame + column) for column in range(columns)]) def _write_motion(path: Path, frames: int) -> None: _write_csv(path / "joint_pos.csv", 29, frames) _write_csv(path / "body_pos.csv", 3, frames) _write_csv(path / "body_quat.csv", 4, frames) class MotionFrameIndexTest(unittest.TestCase): def test_real_motion_rows_override_text_metadata(self) -> None: with tempfile.TemporaryDirectory() as tmp: root = Path(tmp) motion_dir = root / "g1" / "csv_sonic" / "example" _write_motion(motion_dir, frames=4) metadata_path = root / "metadata.csv" with metadata_path.open("w", newline="", encoding="utf-8") as handle: writer = csv.DictWriter( handle, fieldnames=["move_g1_path", "move_name", "move_duration_frames", "prompt"], ) writer.writeheader() writer.writerow( { "move_g1_path": "g1/csv/example.csv", "move_name": "example", "move_duration_frames": 10, "prompt": "move", } ) dataset = SeedMotionDataset( root_dir=str(root), metadata_csv=str(metadata_path), split="all", action_chunk_size=2, use_delta_actions=False, text_fields=["prompt"], records_cache_dir=str(root / "cache"), ) self.assertEqual(dataset.motion_records[0]["n_frames"], 4) self.assertEqual(dataset.motion_records[0]["declared_n_frames"], 10) # Three starts (0, 1, 2) have a real next-frame target; start 3 is # intentionally absent instead of becoming an all-zero-loss item. self.assertEqual(len(dataset), 3) self.assertGreater(float(dataset[2]["action_mask"].sum()), 0.0) rtc_dataset = SeedMotionDataset( root_dir=str(root), metadata_csv=str(metadata_path), split="all", action_chunk_size=2, use_delta_actions=False, supervision_prefix_frames=1, text_fields=["prompt"], records_cache_dir=str(root / "cache"), ) self.assertEqual(len(rtc_dataset), 2) self.assertGreater(float(rtc_dataset[1]["action_mask"][1:].sum()), 0.0) def test_real_motion_rows_override_audio_manifest(self) -> None: with tempfile.TemporaryDirectory() as tmp: root = Path(tmp) motion_dir = root / "motion" _write_motion(motion_dir, frames=4) np.save(root / "audio.npy", np.zeros(160, dtype=np.float32)) manifest_path = root / "manifest.jsonl" manifest_path.write_text( json.dumps( { "id": "example", "converted_path": "motion", "audio_path": "audio.npy", "n_frames": 10, } ) + "\n", encoding="utf-8", ) dataset = SeedAudioMotionDataset( dataset_root=str(root), metadata_jsonl="manifest.jsonl", split="all", action_chunk_size=2, use_delta_actions=False, full_audio=True, audio_path_key="audio_path", source_name="test_audio", records_cache_dir=str(root / "cache"), ) self.assertEqual(dataset.motion_records[0]["n_frames"], 4) self.assertEqual(dataset.motion_records[0]["declared_n_frames"], 10) self.assertEqual(len(dataset), 3) self.assertGreater(float(dataset[2]["action_mask"].sum()), 0.0) rtc_dataset = SeedAudioMotionDataset( dataset_root=str(root), metadata_jsonl="manifest.jsonl", split="all", action_chunk_size=2, use_delta_actions=False, supervision_prefix_frames=1, full_audio=True, audio_path_key="audio_path", source_name="test_audio", records_cache_dir=str(root / "cache"), ) self.assertEqual(len(rtc_dataset), 2) self.assertGreater(float(rtc_dataset[1]["action_mask"][1:].sum()), 0.0) def test_motion_csv_lengths_must_agree(self) -> None: with tempfile.TemporaryDirectory() as tmp: motion_dir = Path(tmp) _write_motion(motion_dir, frames=4) _write_csv(motion_dir / "body_pos.csv", 3, frames=3) with self.assertRaisesRegex(ValueError, "Frame length mismatch"): read_motion_frame_count(motion_dir) def test_non_delta_index_never_adds_zero_target_start(self) -> None: self.assertEqual( valid_sample_count( 4, index_step_frames=1, action_step_frames=1, use_delta_actions=False, ), 3, ) self.assertEqual( valid_sample_count( 4, index_step_frames=1, action_step_frames=1, use_delta_actions=True, ), 3, ) def test_rtc_prefix_requires_a_real_postfix_target(self) -> None: # With a ten-token condition prefix, the first supervised token targets # start+11. Four starts (0..3) are valid in a 15-frame motion. for use_delta_actions in (False, True): self.assertEqual( valid_sample_count( 15, index_step_frames=1, action_step_frames=1, use_delta_actions=use_delta_actions, supervision_prefix_frames=10, ), 4, ) with self.assertRaisesRegex(ValueError, "non-negative"): valid_sample_count( 15, index_step_frames=1, action_step_frames=1, use_delta_actions=False, supervision_prefix_frames=-1, ) def test_data_mix_reads_rtc_prefix_from_repack_config(self) -> None: config = SimpleNamespace( transform=SimpleNamespace( repack=SimpleNamespace( action_chunk_size=30, use_delta_actions=False, num_past_frames=0, rtc_prefix_frames=10, ) ) ) self.assertEqual( SeedTextAudioVideoMixDataConfig._repack_args(config), (30, False, 0, 10), ) if __name__ == "__main__": unittest.main()