Download scripts/embed_perception_states.py from macrodata/WGO-Bench: direct link, hf CLI and curl.
- Browser
- Download file 5.01 kB
-
https://huggingface.co/datasets/macrodata/WGO-Bench/resolve/refs%2Fpr%2F3/scripts/embed_perception_states.py
- Command line
-
hf download hf://datasets/macrodata/WGO-Bench@refs/pr/3/scripts/embed_perception_states.py
-
curl -L -o embed_perception_states.py https://huggingface.co/datasets/macrodata/WGO-Bench/resolve/refs%2Fpr%2F3/scripts/embed_perception_states.py
5.01 kB
| from __future__ import annotations | |
| import argparse | |
| import hashlib | |
| import json | |
| from pathlib import Path | |
| from typing import Any | |
| import pyarrow as pa | |
| import pyarrow.parquet as pq | |
| from perception_states import ( | |
| LOCK_PATH, | |
| _load_episode, | |
| discover_robot_episodes, | |
| load_source_lock, | |
| normalize_episode, | |
| ) | |
| def state_type() -> pa.DataType: | |
| return pa.struct( | |
| [ | |
| ("robot_family", pa.string()), | |
| ("source_dataset", pa.string()), | |
| ("source_revision", pa.string()), | |
| ("source_episode_index", pa.int64()), | |
| ("fps", pa.float64()), | |
| ("num_frames", pa.int64()), | |
| ("frame_index", pa.list_(pa.int64())), | |
| ("timestamp_sec", pa.list_(pa.float64())), | |
| ("primary_channel", pa.string()), | |
| ( | |
| "channels", | |
| pa.list_( | |
| pa.struct( | |
| [ | |
| ("name", pa.string()), | |
| ("value_names", pa.list_(pa.string())), | |
| ("values", pa.list_(pa.list_(pa.float64()))), | |
| ] | |
| ) | |
| ), | |
| ), | |
| ] | |
| ) | |
| def collect_states(lock_path: Path, cache_dir: Path | None) -> dict[str, dict[str, Any]]: | |
| lock = load_source_lock(lock_path) | |
| states: dict[str, dict[str, Any]] = {} | |
| for episode in discover_robot_episodes(lock, cache_dir): | |
| table, feature_info = _load_episode(episode, cache_dir) | |
| row = normalize_episode(table, episode, feature_info) | |
| bench_id = row.pop("bench_id") | |
| states[bench_id] = row | |
| return states | |
| def embed_perception_states( | |
| input_path: Path, | |
| output_path: Path, | |
| *, | |
| lock_path: Path = LOCK_PATH, | |
| cache_dir: Path | None = None, | |
| ) -> dict[str, Any]: | |
| if input_path.resolve() == output_path.resolve(): | |
| raise ValueError("Input and output paths must differ") | |
| states = collect_states(lock_path, cache_dir) | |
| source = pq.ParquetFile(input_path) | |
| if "perception_state" in source.schema_arrow.names: | |
| raise ValueError("Input already contains perception_state") | |
| if source.metadata.num_rows != 100: | |
| raise ValueError(f"Expected 100 benchmark rows, found {source.metadata.num_rows}") | |
| output_path.parent.mkdir(parents=True, exist_ok=True) | |
| output_schema = source.schema_arrow.append(pa.field("perception_state", state_type())) | |
| matched: set[str] = set() | |
| video_only: list[str] = [] | |
| with pq.ParquetWriter(output_path, output_schema, compression="zstd") as writer: | |
| for batch in source.iter_batches(batch_size=1): | |
| table = pa.Table.from_batches([batch]) | |
| bench_id = str(table["id"][0].as_py()) | |
| state = states.get(bench_id) | |
| if state is None: | |
| video_only.append(bench_id) | |
| else: | |
| matched.add(bench_id) | |
| enriched = table.append_column( | |
| "perception_state", pa.array([state], type=state_type()) | |
| ) | |
| writer.write_table(enriched, row_group_size=1) | |
| missing = set(states) - matched | |
| if missing: | |
| output_path.unlink(missing_ok=True) | |
| raise ValueError(f"State IDs absent from benchmark: {sorted(missing)}") | |
| if len(matched) != 75 or len(video_only) != 25: | |
| output_path.unlink(missing_ok=True) | |
| raise ValueError( | |
| f"Expected 75 state rows and 25 video-only rows, got {len(matched)} and {len(video_only)}" | |
| ) | |
| provenance = { | |
| "schema_version": 1, | |
| "artifact": output_path.name, | |
| "input_sha256": _sha256(input_path), | |
| "output_sha256": _sha256(output_path), | |
| "source_lock": lock_path.name, | |
| "source_lock_sha256": _sha256(lock_path), | |
| "rows": source.metadata.num_rows, | |
| "robot_state_rows": len(matched), | |
| "video_only_rows": len(video_only), | |
| "video_only_ids": video_only, | |
| } | |
| provenance_path = output_path.with_suffix(output_path.suffix + ".provenance.json") | |
| provenance_path.write_text(json.dumps(provenance, indent=2) + "\n", encoding="utf-8") | |
| return provenance | |
| def _sha256(path: Path) -> str: | |
| digest = hashlib.sha256() | |
| with path.open("rb") as handle: | |
| for chunk in iter(lambda: handle.read(8 * 1024 * 1024), b""): | |
| digest.update(chunk) | |
| return digest.hexdigest() | |
| def main() -> None: | |
| parser = argparse.ArgumentParser( | |
| description="Embed pinned robot proprioception into existing WGO-Bench rows." | |
| ) | |
| parser.add_argument("input", type=Path) | |
| parser.add_argument("output", type=Path) | |
| parser.add_argument("--lock", type=Path, default=LOCK_PATH) | |
| parser.add_argument("--cache-dir", type=Path) | |
| args = parser.parse_args() | |
| result = embed_perception_states( | |
| args.input, | |
| args.output, | |
| lock_path=args.lock, | |
| cache_dir=args.cache_dir, | |
| ) | |
| print(json.dumps(result, indent=2)) | |
| if __name__ == "__main__": | |
| main() | |