-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathoptimizationOptions.py
executable file
·125 lines (92 loc) · 2.8 KB
/
optimizationOptions.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
114
115
116
117
118
119
120
121
122
123
124
125
#!/usr/bin/env python3
"""
Script for modifying parPE optimization options in an HDF5 file
Daniel Weindl 2017
"""
import h5py
import sys
import re
import numpy as np
def getOptionsObject(f):
"""Get h5py dataset where options are stored"""
if "/optimizationOptions" in f:
options = f["/optimizationOptions"]
return options
print("Error: file does not contain /optimizationOptions")
exit(1)
def setOption(filename, option, value):
"""
Set the given option to value
"""
value = convertValue(value)
with h5py.File(filename, "r+") as f:
options = getOptionsObject(f)
parts = option.split('/')
if len(parts) == 1:
options.attrs[option] = value
else:
g = options.require_group(parts[0])
g.attrs[parts[1]] = value
def convertValue(value):
"""
Guess type of data represented as string value and convert to the
respective type
"""
if re.match(r'^\d+$', value):
return int(value)
try:
return float(value)
except ValueError:
return np.string_(value)
def printOptions(filename):
"""
Recursively print all options in /optimizationOptions
"""
with h5py.File(filename, "r") as f:
options = getOptionsObject(f)
printAttributes(options)
for o in options:
printAttributes(options[o], o + "/")
def printAttributes(object, prefix=''):
"""
Print all attributes of the given object
"""
for o in object.attrs:
print("%40s %12s" % (prefix + o, object.attrs[o]))
def unsetOption(filename, option):
with h5py.File(filename, "r+") as f:
group = getOptionsObject(f)
parts = option.split('/')
if len(parts) > 1:
group = group.require_group(parts[0])
option = parts[1]
try:
group.attrs.__delitem__(option)
except KeyError:
pass
def printUsage():
print("""Usage:
optimizationOptions.py -> print All
optimizationOptions.py key -> print key
optimizationOptions.py -s key value -> set key to value (use e.g. optimizername/option for optimizer-specific values)
optimizationOptions.py -s key -> remove key
Options currently supported are
numStarts
maxIter
optimizer
retryOptimization""")
if __name__ == "__main__":
if len(sys.argv) < 2:
printUsage()
exit()
filename = sys.argv[1]
if len(sys.argv) == 2:
printOptions(filename)
exit()
if sys.argv[2] != "-s":
print("Unrecognized options " + sys.argv[2])
exit(1)
if len(sys.argv) == 4:
unsetOption(filename, sys.argv[3])
elif len(sys.argv) == 5:
setOption(filename, sys.argv[3], sys.argv[4])