Upload code/1027_run/tasks/lever2npz.py with huggingface_hub
Browse files
code/1027_run/tasks/lever2npz.py
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""Convert lever_sim.py output dir → ep_NNNNNN.npz in pack_zarr format.
|
| 3 |
+
|
| 4 |
+
lever_sim writes:
|
| 5 |
+
<out>/ur5_steps.json - list of {i,t,phase,q_mm,tip:[x,y,z],...}
|
| 6 |
+
<out>/ur5_cam0.mp4 - front cam (optional)
|
| 7 |
+
<out>/ur5_cam1.mp4 - wrist cam (optional)
|
| 8 |
+
|
| 9 |
+
We produce:
|
| 10 |
+
<episodes_dir>/ep_NNNNNN.npz with keys:
|
| 11 |
+
robot0_eef_pos (N,3) float32 -- tip xyz
|
| 12 |
+
robot0_eef_quat (N,3) float32 -- rotvec (constant, derived from press orientation)
|
| 13 |
+
robot0_gripper_qpos (N,2) float32 -- [0.0, -0.0] (gripper always closed)
|
| 14 |
+
action (N,7) float32 -- [eef_pos_next, eef_quat_next, gripper_next] = state[t+1]
|
| 15 |
+
timestamp (N,) float64
|
| 16 |
+
|
| 17 |
+
The episode is accepted only if q_at_press_end_mm <= press_q_mm (lever actually pressed).
|
| 18 |
+
"""
|
| 19 |
+
import argparse, json, os, sys, glob
|
| 20 |
+
import numpy as np
|
| 21 |
+
|
| 22 |
+
ap = argparse.ArgumentParser()
|
| 23 |
+
ap.add_argument('--src-dir', required=True, help='dir with ur5_steps.json from one lever_sim run')
|
| 24 |
+
ap.add_argument('--episodes-dir', required=True, help='output episodes/ dir')
|
| 25 |
+
ap.add_argument('--ep-idx', type=int, required=True, help='episode index for filename')
|
| 26 |
+
ap.add_argument('--press-q-mm', type=float, default=-50.0, help='accept if q_at_press_end <= this (mm)')
|
| 27 |
+
A = ap.parse_args()
|
| 28 |
+
|
| 29 |
+
steps_file = os.path.join(A.src_dir, 'ur5_steps.json')
|
| 30 |
+
result_file = os.path.join(A.src_dir, 'result.json')
|
| 31 |
+
|
| 32 |
+
if not os.path.exists(steps_file):
|
| 33 |
+
sys.exit(f"No ur5_steps.json in {A.src_dir}")
|
| 34 |
+
|
| 35 |
+
steps = json.load(open(steps_file))
|
| 36 |
+
if not steps:
|
| 37 |
+
sys.exit("ur5_steps.json is empty")
|
| 38 |
+
|
| 39 |
+
# Check result for success
|
| 40 |
+
if os.path.exists(result_file):
|
| 41 |
+
res = json.load(open(result_file))
|
| 42 |
+
ur5 = res.get('ur5', {})
|
| 43 |
+
q_end = ur5.get('q_at_press_end_mm', None)
|
| 44 |
+
if q_end is not None and q_end > A.press_q_mm:
|
| 45 |
+
sys.exit(f"Episode rejected: q_at_press_end_mm={q_end:.1f} > {A.press_q_mm:.1f}")
|
| 46 |
+
print(f"q_at_press_end_mm={q_end}, accepted")
|
| 47 |
+
|
| 48 |
+
tips = np.array([s['tip'] for s in steps], dtype=np.float32) # (T, 3)
|
| 49 |
+
T = len(tips)
|
| 50 |
+
if T < 2:
|
| 51 |
+
sys.exit(f"Too few steps: {T}")
|
| 52 |
+
|
| 53 |
+
# rotvec: constant (gripper always closed, orientation approximately fixed during press)
|
| 54 |
+
# Use zero rotvec as placeholder — the DP will learn from tip trajectory
|
| 55 |
+
rotvec = np.zeros((T, 3), dtype=np.float32)
|
| 56 |
+
gripper = np.zeros((T, 2), dtype=np.float32) # closed = 0 width
|
| 57 |
+
|
| 58 |
+
# state: [pos(3), rotvec(3), gripper_w(1)] -> 7D
|
| 59 |
+
state = np.concatenate([tips, rotvec, gripper[:, :1]], axis=1) # (T,7)
|
| 60 |
+
# action = state[t+1], state = state[t], both of length T-1
|
| 61 |
+
N = T - 1
|
| 62 |
+
ep = dict(
|
| 63 |
+
robot0_eef_pos=tips[:N],
|
| 64 |
+
robot0_eef_quat=rotvec[:N],
|
| 65 |
+
robot0_gripper_qpos=gripper[:N],
|
| 66 |
+
action=state[1:], # next state
|
| 67 |
+
timestamp=(np.arange(N) * 0.1).astype(np.float64),
|
| 68 |
+
)
|
| 69 |
+
|
| 70 |
+
os.makedirs(A.episodes_dir, exist_ok=True)
|
| 71 |
+
out = os.path.join(A.episodes_dir, f'ep_{A.ep_idx:06d}.npz')
|
| 72 |
+
np.savez_compressed(out, **ep)
|
| 73 |
+
print(f"Wrote {out} ({N} rows)")
|