forked from TomaszGolan/hdf5_manipulator
-
Notifications
You must be signed in to change notification settings - Fork 1
/
hdf5.py
92 lines (58 loc) · 1.81 KB
/
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
"""
HDF5 files tools for HDF5 Manipulator
"""
import h5py
def load(filename):
"""Load hdf5 file to data dictionary and return it.
Keyword arguments:
filename -- the full path to hdf5 file
"""
f = h5py.File(filename, 'r')
data = {}
attrs = {}
for key in f:
data[key] = f[key][...]
for key in f.attrs:
attrs[key] = f.attrs[key]
f.close()
return data, attrs
def save(filename, data, attrs):
"""Create hdf5 file with given data.
Keyword arguments:
filename -- the full path to hdf5 file
data -- dictionary with data
"""
f = h5py.File(filename, 'w')
for key in data:
f.create_dataset(key, data[key].shape, dtype=data[key].dtype,
compression='gzip')[...] = data[key]
for key in attrs:
f.attrs[key] = attrs[key]
f.close()
def save_subset(filename, data, begin, end):
"""Create hdf5 file with subset [begin, end) of given data.
Keyword arguments:
filename -- the full path to hdf5 file
data -- dictionary with data
begin -- start saving from index=i
end -- finish savin at index=end
"""
subset = {}
for key in data:
subset[key] = data[key][begin:end]
save(filename, subset)
def save_subset_big(filename, data, begin, end):
"""Create hdf5 file with subset [begin, end) of given data.
Keyword arguments:
filename -- the full path to hdf5 file
data -- input file
begin -- start saving from index=i
end -- finish savin at index=end
"""
o = h5py.File(filename, 'w')
for key in data:
shape = list(data[key].shape)
shape[0] = end - begin
o.create_dataset(key, shape, dtype=data[key].dtype,
compression='gzip')[...] = data[key][begin:end]
o.close()