-
Notifications
You must be signed in to change notification settings - Fork 1
/
parse_hdf5.py
71 lines (55 loc) · 1.84 KB
/
parse_hdf5.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import argparse
import os
import glob
from pathlib import Path
import h5py
import numpy as np
# example usage: python parse_hdf5.py --folder RoboTurkPilot/bins-Bread/
def separate_joint_csv(demo_path):
# data_path = os.path.join(demo_path, 'jointdata')
# data_path = os.path.join(demo_path, 'observations')
data_path = demo_path
os.chdir(data_path)
files = glob.glob('*.npy')
for file in files:
fname_base = Path(file).stem
try:
os.mkdir(fname_base)
except FileExistsError as error:
pass
dat = np.load(file, allow_pickle=True)
for i in range(0, dat.shape[0]):
np.save(os.path.join(fname_base, 'frame_{:04d}.npy'.format(i)), dat[i])
def get_joint_csv(demo_path):
hdf5_path = os.path.join(demo_path, "demo.hdf5")
video_path = os.path.join(demo_path, 'videos')
data_path = os.path.join(demo_path, 'jointdata')
try:
os.mkdir(data_path)
except FileExistsError as error:
pass
f = h5py.File(hdf5_path, "r")
demos = list(f["data"].keys())
for i in range(0, len(demos)):
ep = demos[i]
states = f["data/{}/states".format(ep)][()]
joint_vel = np.array(f["data/{}/joint_velocities".format(ep)][()])
gripper_act = np.array(f["data/{}/gripper_actuations".format(ep)][()])
actions = np.concatenate((joint_vel, gripper_act), axis=1)
np.save(os.path.join(data_path, '{}_jointdata.npy'.format(ep)), actions)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
"--folder",
type=str
)
parser.add_argument(
'--separate',
action='store_true',
)
args = parser.parse_args()
demo_path = args.folder
if (args.separate):
separate_joint_csv(demo_path)
else:
get_joint_csv(demo_path)