Nurec_eval_v2 / inject_mads_map.py
luuuulinnnn's picture
Add 59 OOD-longtail USDZ scenes with MADS HD-map (trajdata-compatible)
e23bb81 verified
"""Inject MADS-format map_data into a USDZ for alpasim consumption.
Reads from <mads_dir>:
clip.parquet, lane.parquet, road_boundary.parquet,
association.parquet, wait_line.parquet, traffic_sign.parquet
Writes them into the USDZ at `map_data/<name>.parquet` (alpasim's loader scans
for any file under `map_data/` and feeds the directory to trajdata).
"""
from __future__ import annotations
import argparse
import shutil
import sys
import zipfile
from pathlib import Path
MADS_FILES = [
"clip.parquet",
"lane.parquet",
"road_boundary.parquet",
"association.parquet",
"wait_line.parquet",
"traffic_sign.parquet",
]
def inject(src_usdz: Path, mads_dir: Path, dst_usdz: Path) -> None:
# Copy the source USDZ to destination, then append MADS files
if src_usdz != dst_usdz:
shutil.copy2(src_usdz, dst_usdz)
# Remove any existing map_data entries (from the old CF-format inject)
# by rebuilding the zip without them, then appending fresh MADS files.
if dst_usdz != src_usdz:
# Already a copy; check if it has stale map_data entries
with zipfile.ZipFile(dst_usdz, "r") as zin:
existing = set(zin.namelist())
has_old_map = any(n.startswith("map_data/") for n in existing)
else:
has_old_map = False
if has_old_map:
# Rebuild without the map_data/ entries
tmp = dst_usdz.with_suffix(".tmp.usdz")
with zipfile.ZipFile(dst_usdz, "r") as zin, \
zipfile.ZipFile(tmp, "w", compression=zipfile.ZIP_STORED) as zout:
for info in zin.infolist():
if info.filename.startswith("map_data/"):
continue
zout.writestr(info, zin.read(info.filename))
tmp.replace(dst_usdz)
# Now append the MADS files
with zipfile.ZipFile(dst_usdz, "a", compression=zipfile.ZIP_STORED) as zf:
for name in MADS_FILES:
src_file = mads_dir / name
if not src_file.exists():
raise FileNotFoundError(src_file)
arc = f"map_data/{name}"
zf.write(src_file, arcname=arc)
print(f" added: {arc} ({src_file.stat().st_size} bytes)")
def main() -> int:
p = argparse.ArgumentParser()
p.add_argument("src", type=Path, help="Source USDZ")
p.add_argument("mads_dir", type=Path,
help="Dir with MADS files (clip/lane/road_boundary/association/wait_line/traffic_sign.parquet)")
p.add_argument("dst", type=Path, help="Destination USDZ")
args = p.parse_args()
print(f"inject {args.mads_dir} -> {args.src} => {args.dst}")
inject(args.src, args.mads_dir, args.dst)
print(f"wrote {args.dst} ({args.dst.stat().st_size} bytes)")
return 0
if __name__ == "__main__":
sys.exit(main())