Upload strict640x480-v2/code/merge_strict_lpd_sources.py with huggingface_hub
Browse files
strict640x480-v2/code/merge_strict_lpd_sources.py
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Build the exact formal 100-demo LPD corpus from validated sources.
|
| 2 |
+
|
| 3 |
+
The main collector is deliberately sampled at six completed trajectories from
|
| 4 |
+
each of its eight workers (48 total); the acceleration collector supplies the
|
| 5 |
+
remaining 52. Output is newly written HDF5 rather than an in-place edit, so a
|
| 6 |
+
failed merge cannot contaminate the official strict corpus.
|
| 7 |
+
"""
|
| 8 |
+
from __future__ import annotations
|
| 9 |
+
|
| 10 |
+
import argparse
|
| 11 |
+
import json
|
| 12 |
+
import shutil
|
| 13 |
+
from pathlib import Path
|
| 14 |
+
|
| 15 |
+
import h5py
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
def trajectory_names(path: Path) -> list[str]:
|
| 19 |
+
with h5py.File(path, "r") as source:
|
| 20 |
+
return sorted(key for key in source if key.startswith("traj_"))
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
def main() -> None:
|
| 24 |
+
parser = argparse.ArgumentParser()
|
| 25 |
+
parser.add_argument("--main", type=Path, required=True)
|
| 26 |
+
parser.add_argument("--accel", type=Path, required=True)
|
| 27 |
+
parser.add_argument("--output", type=Path, required=True)
|
| 28 |
+
args = parser.parse_args()
|
| 29 |
+
|
| 30 |
+
if args.output.exists():
|
| 31 |
+
raise FileExistsError(args.output)
|
| 32 |
+
main_files = sorted(args.main.glob("worker_*.h5"))
|
| 33 |
+
if len(main_files) != 8:
|
| 34 |
+
raise AssertionError(f"expected eight main workers, found {len(main_files)}")
|
| 35 |
+
|
| 36 |
+
selected: list[tuple[Path, str, str]] = []
|
| 37 |
+
for source in main_files:
|
| 38 |
+
names = trajectory_names(source)
|
| 39 |
+
if len(names) < 6:
|
| 40 |
+
raise AssertionError(f"{source} has fewer than six completed trajectories")
|
| 41 |
+
selected.extend((source, name, "main") for name in names[:6])
|
| 42 |
+
|
| 43 |
+
accel_files = sorted(args.accel.glob("gpu*/worker_*.h5"))
|
| 44 |
+
for source in accel_files:
|
| 45 |
+
selected.extend((source, name, "accel") for name in trajectory_names(source))
|
| 46 |
+
if len(selected) != 100:
|
| 47 |
+
raise AssertionError(f"expected 48 + 52 selected trajectories, found {len(selected)}")
|
| 48 |
+
|
| 49 |
+
args.output.mkdir(parents=True)
|
| 50 |
+
provenance: list[dict[str, str | int]] = []
|
| 51 |
+
for shard_index in range(4):
|
| 52 |
+
start, end = shard_index * 25, (shard_index + 1) * 25
|
| 53 |
+
target = args.output / f"worker_{shard_index:02d}.h5"
|
| 54 |
+
with h5py.File(target, "w") as destination:
|
| 55 |
+
for local_index, (source_path, name, source_kind) in enumerate(selected[start:end]):
|
| 56 |
+
with h5py.File(source_path, "r") as source:
|
| 57 |
+
destination.copy(source[name], f"traj_{local_index:05d}")
|
| 58 |
+
provenance.append({
|
| 59 |
+
"output_file": target.name,
|
| 60 |
+
"output_trajectory": f"traj_{local_index:05d}",
|
| 61 |
+
"source_kind": source_kind,
|
| 62 |
+
"source_file": str(source_path),
|
| 63 |
+
"source_trajectory": name,
|
| 64 |
+
})
|
| 65 |
+
(args.output / "manifest.json").write_text(json.dumps({
|
| 66 |
+
"task": "long_pipeline_delivery",
|
| 67 |
+
"protocol": "strict640x480_v2",
|
| 68 |
+
"episodes": 100,
|
| 69 |
+
"selection": {"main": 48, "accelerated": 52},
|
| 70 |
+
"provenance": provenance,
|
| 71 |
+
}, indent=2) + "\n", encoding="utf-8")
|
| 72 |
+
print("MERGED_STRICT_LPD_100")
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
if __name__ == "__main__":
|
| 76 |
+
main()
|