-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathparams_util.py
More file actions
50 lines (42 loc) · 1.47 KB
/
params_util.py
File metadata and controls
50 lines (42 loc) · 1.47 KB
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
import os
import pickle
def check_same_params(param_filename, current_params):
ofile = open(param_filename)
loaded_params = pickle.load(ofile)
ofile.close()
same = True
loaded_vars = vars(loaded_params)
current_vars = vars(current_params)
for attribute, value in loaded_vars.items():
if attribute not in current_vars:
print(
"Loaded attribute " +
str(attribute) +
" not defined in current parameters")
same = False
elif value != current_vars[attribute]:
print("Attribute " +
str(attribute) +
" has different value in loaded and current parameters (" +
str(value) +
" vs. " +
str(current_vars[attribute]) +
")")
same = False
for attribute in current_vars:
if attribute not in loaded_vars:
print(
"Current attribute " +
str(attribute) +
" not defined in loaded parameters (filename: " +
str(param_filename) +
")")
same = False
assert same, "loaded and current parameters are not the same"
def write_params(param_filename, params):
if os.path.exists(param_filename):
check_same_params(param_filename, params)
else:
outfile = open(param_filename, "w")
pickle.dump(params, outfile)
outfile.close()