File size: 5,932 Bytes
e0803b8 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 | #!/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()
|