Download scripts/batch_convert.py from honglei-code/bones-seed-g1: direct link, hf CLI and curl.
- Browser
- Download file 5.93 kB
-
https://huggingface.co/datasets/honglei-code/bones-seed-g1/resolve/main/scripts/batch_convert.py
- Command line
-
hf download hf://datasets/honglei-code/bones-seed-g1/scripts/batch_convert.py
-
curl -L -o batch_convert.py https://huggingface.co/datasets/honglei-code/bones-seed-g1/resolve/main/scripts/batch_convert.py
5.93 kB
| #!/usr/bin/env python3 | |
| """Batch-convert BONES-SEED G1 CSV files to MT, Isaac, or both.""" | |
| from __future__ import annotations | |
| import argparse | |
| import multiprocessing | |
| import time | |
| from functools import partial | |
| from pathlib import Path | |
| from csv_to_isaac import convert_csv_to_isaac | |
| from csv_to_mt import convert_csv_to_mt | |
| def convert_one( | |
| csv_path: Path, | |
| *, | |
| input_dir: Path, | |
| output_dir: Path, | |
| formats: tuple[str, ...], | |
| g1_xml: Path, | |
| source_fps: int, | |
| target_fps: int, | |
| overwrite: bool, | |
| ) -> tuple[int, int, list[str]]: | |
| """Convert one source file. Return completed, skipped, and error messages.""" | |
| relative_path = csv_path.relative_to(input_dir).with_suffix(".npz") | |
| completed = 0 | |
| skipped = 0 | |
| errors = [] | |
| for output_format in formats: | |
| output_path = output_dir / output_format / relative_path | |
| if output_path.exists() and not overwrite: | |
| skipped += 1 | |
| continue | |
| try: | |
| if output_format == "mt": | |
| convert_csv_to_mt( | |
| csv_path=csv_path, | |
| output_path=output_path, | |
| g1_xml=g1_xml, | |
| source_fps=source_fps, | |
| target_fps=target_fps, | |
| verbose=False, | |
| ) | |
| else: | |
| convert_csv_to_isaac( | |
| csv_path=csv_path, | |
| output_path=output_path, | |
| g1_xml=g1_xml, | |
| source_fps=source_fps, | |
| target_fps=target_fps, | |
| verbose=False, | |
| ) | |
| completed += 1 | |
| except Exception as error: # keep the batch running and report failures | |
| errors.append(f"{output_format}\t{csv_path}\t{error}") | |
| return completed, skipped, errors | |
| def parse_args() -> argparse.Namespace: | |
| parser = argparse.ArgumentParser( | |
| description="Batch-convert BONES-SEED G1 CSV files" | |
| ) | |
| parser.add_argument( | |
| "--input-dir", | |
| required=True, | |
| type=Path, | |
| help="Directory recursively containing G1 CSV files", | |
| ) | |
| parser.add_argument( | |
| "--output-dir", | |
| required=True, | |
| type=Path, | |
| help="Output root; results are written under mt/ and/or isaac/", | |
| ) | |
| parser.add_argument( | |
| "--g1-xml", | |
| required=True, | |
| type=Path, | |
| help="Compatible Unitree G1 MJCF XML", | |
| ) | |
| parser.add_argument( | |
| "--format", | |
| choices=("mt", "isaac", "both"), | |
| default="both", | |
| help="Output format (default: both)", | |
| ) | |
| parser.add_argument("--source-fps", type=int, default=120) | |
| parser.add_argument("--target-fps", type=int, default=50) | |
| parser.add_argument( | |
| "--workers", | |
| type=int, | |
| default=max(1, multiprocessing.cpu_count() - 2), | |
| help="Worker processes (default: CPU count minus two)", | |
| ) | |
| parser.add_argument( | |
| "--overwrite", | |
| action="store_true", | |
| help="Replace output files that already exist", | |
| ) | |
| parser.add_argument( | |
| "--limit", | |
| type=int, | |
| help="Convert only the first N files (useful for smoke tests)", | |
| ) | |
| return parser.parse_args() | |
| def main() -> None: | |
| args = parse_args() | |
| input_dir = args.input_dir.expanduser().resolve() | |
| output_dir = args.output_dir.expanduser().resolve() | |
| g1_xml = args.g1_xml.expanduser().resolve() | |
| if not input_dir.is_dir(): | |
| raise SystemExit(f"Input directory not found: {input_dir}") | |
| if not g1_xml.is_file(): | |
| raise SystemExit(f"G1 MJCF file not found: {g1_xml}") | |
| if args.workers <= 0: | |
| raise SystemExit("--workers must be positive") | |
| if args.limit is not None and args.limit <= 0: | |
| raise SystemExit("--limit must be positive") | |
| csv_files = sorted(input_dir.rglob("*.csv")) | |
| if args.limit is not None: | |
| csv_files = csv_files[: args.limit] | |
| if not csv_files: | |
| raise SystemExit(f"No CSV files found under: {input_dir}") | |
| formats = ("mt", "isaac") if args.format == "both" else (args.format,) | |
| output_dir.mkdir(parents=True, exist_ok=True) | |
| worker = partial( | |
| convert_one, | |
| input_dir=input_dir, | |
| output_dir=output_dir, | |
| formats=formats, | |
| g1_xml=g1_xml, | |
| source_fps=args.source_fps, | |
| target_fps=args.target_fps, | |
| overwrite=args.overwrite, | |
| ) | |
| total_outputs = len(csv_files) * len(formats) | |
| print(f"CSV files: {len(csv_files)}") | |
| print(f"Formats: {', '.join(formats)}") | |
| print(f"Expected outputs: {total_outputs}") | |
| print(f"Workers: {args.workers}") | |
| print(f"Output root: {output_dir}") | |
| start_time = time.time() | |
| completed = 0 | |
| skipped = 0 | |
| failures = [] | |
| with multiprocessing.Pool(args.workers) as pool: | |
| for index, result in enumerate( | |
| pool.imap_unordered(worker, csv_files), start=1 | |
| ): | |
| done_count, skipped_count, errors = result | |
| completed += done_count | |
| skipped += skipped_count | |
| failures.extend(errors) | |
| if index % 1000 == 0 or index == len(csv_files): | |
| elapsed = max(time.time() - start_time, 1e-6) | |
| rate = index / elapsed | |
| print( | |
| f"[{index}/{len(csv_files)}] " | |
| f"done={completed} skipped={skipped} " | |
| f"failed={len(failures)} files={rate:.1f}/s" | |
| ) | |
| if failures: | |
| failure_log = output_dir / "conversion_failures.log" | |
| failure_log.write_text("\n".join(failures) + "\n", encoding="utf-8") | |
| print(f"Failure details: {failure_log}") | |
| elapsed = time.time() - start_time | |
| print( | |
| f"Finished in {elapsed / 60:.1f} min: " | |
| f"done={completed}, skipped={skipped}, failed={len(failures)}" | |
| ) | |
| if failures: | |
| raise SystemExit(1) | |
| if __name__ == "__main__": | |
| main() | |