-
Notifications
You must be signed in to change notification settings - Fork 22
/
options.py
70 lines (62 loc) · 2.49 KB
/
options.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
"""
Command-line options.
"""
def reparse(values, parser=None):
"""
Given a dict of values, construct an OptionParser and attempt to
override its values with any command-line arguments.
We return the overriden dictionary.
We also return a string contain all keys and their values that were overriden.
If parser is given, we use the values in it but update using value.
@warning: We potentially clobber existing values in parser.
Here is a common usage:
import common.hyperparameters, common.options
HYPERPARAMETERS = common.hyperparameters.read("sparse_input")
HYPERPARAMETERS, options, args, newkeystr = common.options.reparse(HYPERPARAMETERS)
"""
if parser is None:
from optparse import OptionParser
parser = OptionParser()
assert parser is not None
import re
wsre = re.compile("\s+")
# We don't want hyperparameters to be read more than once before
# reparsing them. Otherwise, other modules that read the
# hyperparameters could have stale values.
if "__suffix" in values:
import common.hyperparameters
assert common.hyperparameters._readcount[values["__suffix"]] == 1
newkey_to_key = {}
for key in values:
v = values[key]
if type(v) == type("string"):
ty = "string"
elif type(v) == type(1.0):
ty = "float"
elif type(v) == type(1):
ty = "int"
elif type(v) == type(True):
ty = "bool"
else:
import sys
print >> sys.stderr, "common.options.reparse: Skipping %s, with type %s" % (key, type(v))
continue
newkey = wsre.sub("_", key)
newkey_to_key[newkey] = key
if ty != "bool":
parser.add_option("--%s" % newkey, dest=key, default=v, action="store", type=ty)
else:
parser.add_option("--%s" % newkey, action="store_true", dest=key, default=v)
parser.add_option("--no_%s" % newkey, action="store_false", dest=key, default=v)
(options, args) = parser.parse_args()
# newkeys = {}
newkeystr = ""
for newkey in newkey_to_key:
key = newkey_to_key[newkey]
newvalue = getattr(options, key)
if newvalue != values[key]:
print >> sys.stderr, "common.options.reparse: %s %s => %s" % (key, values[key], newvalue)
values[key] = newvalue
newkeystr += ".%s=%s" % (key, newvalue)
# newkeys[key] = True
return values, options, args, newkeystr