#!/usr/bin/env python3 """Dataset cleanup & re-index tool. Usage: python scripts/cleanup_reindex.py This script performs the following: 1. Delete episodes 000000-000009 from data & videos. 2. Rename remaining episodes (000010-000049 → 000000-000039). 3. Update meta JSONL / JSON files accordingly. Designed for one-off use; idempotency is *not* guaranteed. """ from __future__ import annotations import json import shutil from pathlib import Path from typing import Dict, List ROOT = Path(__file__).resolve().parent.parent # project root DATA_DIR = ROOT / "data" / "chunk-000" VIDEO_DIR = ROOT / "videos" / "chunk-000" META_DIR = ROOT / "meta" DELETE_RANGE = range(0, 10) # episodes 0-9, inclusive OLD_START = 10 # first episode kept TOTAL_EPISODES_OLD = 50 TOTAL_EPISODES_NEW = 40 # ----------------------------------------------------------------------------- # Helper functions # ----------------------------------------------------------------------------- def delete_episode_files(idx: int) -> None: """Remove parquet & mp4 files for a given episode index.""" ep_str = f"episode_{idx:06d}" parquet = DATA_DIR / f"{ep_str}.parquet" for p in [parquet]: if p.exists(): p.unlink() print(f"[DEL] {p.relative_to(ROOT)}") for cam in ["observation.images.laptop", "observation.images.phone"]: mp4 = VIDEO_DIR / cam / f"{ep_str}.mp4" if mp4.exists(): mp4.unlink() print(f"[DEL] {mp4.relative_to(ROOT)}") def rename_episode_files(old_idx: int, new_idx: int) -> None: """Rename parquet & mp4 files old_idx → new_idx.""" old_ep = f"episode_{old_idx:06d}" new_ep = f"episode_{new_idx:06d}" # Parquet src_parquet = DATA_DIR / f"{old_ep}.parquet" dst_parquet = DATA_DIR / f"{new_ep}.parquet" if src_parquet.exists(): src_parquet.rename(dst_parquet) print(f"[MV ] {src_parquet.relative_to(ROOT)} → {dst_parquet.relative_to(ROOT)}") # Videos for cam in ["observation.images.laptop", "observation.images.phone"]: src_mp4 = VIDEO_DIR / cam / f"{old_ep}.mp4" dst_mp4 = VIDEO_DIR / cam / f"{new_ep}.mp4" if src_mp4.exists(): src_mp4.rename(dst_mp4) print(f"[MV ] {src_mp4.relative_to(ROOT)} → {dst_mp4.relative_to(ROOT)}") def update_episodes_jsonl() -> int: """Trim first 10 lines & re-index episode_index. Returns ------- int Total frames across remaining episodes (sum of `length`). """ path = META_DIR / "episodes.jsonl" new_lines: List[str] = [] total_frames = 0 with path.open() as fh: for line in fh: data = json.loads(line) idx = data["episode_index"] if idx in DELETE_RANGE: continue # skip new_idx = idx - OLD_START data["episode_index"] = new_idx total_frames += data.get("length", 0) new_lines.append(json.dumps(data, ensure_ascii=False)) # Overwrite path.write_text("\n".join(new_lines) + "\n") print(f"[OK ] episodes.jsonl updated (total_frames={total_frames})") return total_frames def update_episodes_stats_jsonl() -> None: """Trim & re-index episode_index field only.""" path = META_DIR / "episodes_stats.jsonl" new_lines: List[str] = [] with path.open() as fh: for line in fh: data = json.loads(line) idx = data.get("episode_index") if idx in DELETE_RANGE: continue new_idx = idx - OLD_START data["episode_index"] = new_idx new_lines.append(json.dumps(data, ensure_ascii=False)) path.write_text("\n".join(new_lines) + "\n") print("[OK ] episodes_stats.jsonl updated") def update_info_json(total_frames: int) -> None: path = META_DIR / "info.json" info: Dict = json.loads(path.read_text()) info["total_episodes"] = TOTAL_EPISODES_NEW info["total_videos"] = TOTAL_EPISODES_NEW * 2 info["total_frames"] = total_frames # Update splits if "splits" in info and "train" in info["splits"]: info["splits"]["train"] = "0:40" path.write_text(json.dumps(info, ensure_ascii=False, indent=4) + "\n") print("[OK ] info.json updated") # ----------------------------------------------------------------------------- # Main # ----------------------------------------------------------------------------- def main() -> None: # 1. Delete unwanted episodes print("=== Deleting episodes 0-9 ===") for idx in DELETE_RANGE: delete_episode_files(idx) # 2. Rename remaining episodes print("\n=== Renaming remaining episodes ===") for old_idx in range(OLD_START, TOTAL_EPISODES_OLD): rename_episode_files(old_idx, old_idx - OLD_START) # 3. Update meta files print("\n=== Updating meta files ===") total_frames = update_episodes_jsonl() update_episodes_stats_jsonl() update_info_json(total_frames) print("\nAll tasks completed.") if __name__ == "__main__": main()