-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathutils.py
113 lines (83 loc) · 2.42 KB
/
utils.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# utils.py ---
#
# Filename: utils.py
# Description:
# Author: Kwang
# Maintainer:
# Created: Mon Jan 18 16:52:58 2016 (+0100)
# Version:
# Package-Requires: ()
# URL:
# Doc URL:
# Keywords:
# Compatibility:
#
#
# Commentary:
#
#
#
#
# Change Log:
#
#
#
#
# Copyright (C)
# Visual Computing Group @ University of Victoria
# Computer Vision Lab @ EPFL
# Code:
import gzip
import pickle
import h5py
def savepklz(data_to_dump, dump_file_full_name, force_run=False):
''' Saves a pickle object and gzip it '''
if not force_run:
raise RuntimeError("This function should no longer be used!")
with gzip.open(dump_file_full_name, 'wb') as out_file:
pickle.dump(data_to_dump, out_file)
def loadpklz(dump_file_full_name, force_run=False):
''' Loads a gziped pickle object '''
if not force_run:
raise RuntimeError("This function should no longer be used!")
with gzip.open(dump_file_full_name, 'rb') as in_file:
dump_data = pickle.load(in_file)
return dump_data
def saveh5(dict_to_dump, dump_file_full_name):
''' Saves a dictionary as h5 file '''
with h5py.File(dump_file_full_name, 'w') as h5file:
if isinstance(dict_to_dump, list):
for i, d in enumerate(dict_to_dump):
newdict = {'dict' + str(i): d}
writeh5(newdict, h5file)
else:
writeh5(dict_to_dump, h5file)
def writeh5(dict_to_dump, h5node):
''' Recursive function to write dictionary to h5 nodes '''
for _key in dict_to_dump.keys():
if isinstance(dict_to_dump[_key], dict):
h5node.create_group(_key)
cur_grp = h5node[_key]
writeh5(dict_to_dump[_key], cur_grp)
else:
h5node[_key] = dict_to_dump[_key]
def loadh5(dump_file_full_name):
''' Loads a h5 file as dictionary '''
try:
with h5py.File(dump_file_full_name, 'r') as h5file:
dict_from_file = readh5(h5file)
except Exception as e:
print("Error while loading {}".format(dump_file_full_name))
raise e
return dict_from_file
def readh5(h5node):
''' Recursive function to read h5 nodes as dictionary '''
dict_from_file = {}
for _key in h5node.keys():
if isinstance(h5node[_key], h5py._hl.group.Group):
dict_from_file[_key] = readh5(h5node[_key])
else:
dict_from_file[_key] = h5node[_key].value
return dict_from_file
#
# utils.py ends here