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
16
def save_hdf5(output_path, asset_dict, attr_dict= None, mode='a'):
17
    file = h5py.File(output_path, mode)
18
    for key, val in asset_dict.items():
19
        data_shape = val.shape
20
        if key not in file:
21
            data_type = val.dtype
22
            chunk_shape = (1, ) + data_shape[1:]
23
            maxshape = (None, ) + data_shape[1:]
24
            dset = file.create_dataset(key, shape=data_shape, maxshape=maxshape, chunks=chunk_shape, dtype=data_type)
25
            dset[:] = val
26
            if attr_dict is not None:
27
                if key in attr_dict.keys():
28
                    for attr_key, attr_val in attr_dict[key].items():
29
                        dset.attrs[attr_key] = attr_val
30
        else:
31
            dset = file[key]
32
            dset.resize(len(dset) + data_shape[0], axis=0)
33
            dset[-data_shape[0]:] = val
34
    file.close()
35
    return output_path