|
a |
|
b/utils/file_utils.py |
|
|
1 |
import pickle |
|
|
2 |
import h5py |
|
|
3 |
|
|
|
4 |
def save_pkl(filename, save_object): |
|
|
5 |
writer = open(filename,'wb') |
|
|
6 |
pickle.dump(save_object, writer) |
|
|
7 |
writer.close() |
|
|
8 |
|
|
|
9 |
def load_pkl(filename): |
|
|
10 |
loader = open(filename,'rb') |
|
|
11 |
file = pickle.load(loader) |
|
|
12 |
loader.close() |
|
|
13 |
return file |
|
|
14 |
|
|
|
15 |
def save_hdf5(output_path, asset_dict, attr_dict= None, mode='a'): |
|
|
16 |
file = h5py.File(output_path, mode) |
|
|
17 |
for key, val in asset_dict.items(): |
|
|
18 |
data_shape = val.shape |
|
|
19 |
if key not in file: |
|
|
20 |
data_type = val.dtype |
|
|
21 |
chunk_shape = (1, ) + data_shape[1:] |
|
|
22 |
maxshape = (None, ) + data_shape[1:] |
|
|
23 |
dset = file.create_dataset(key, shape=data_shape, maxshape=maxshape, chunks=chunk_shape, dtype=data_type) |
|
|
24 |
dset[:] = val |
|
|
25 |
if attr_dict is not None: |
|
|
26 |
if key in attr_dict.keys(): |
|
|
27 |
for attr_key, attr_val in attr_dict[key].items(): |
|
|
28 |
dset.attrs[attr_key] = attr_val |
|
|
29 |
else: |
|
|
30 |
dset = file[key] |
|
|
31 |
dset.resize(len(dset) + data_shape[0], axis=0) |
|
|
32 |
dset[-data_shape[0]:] = val |
|
|
33 |
file.close() |
|
|
34 |
return output_path |