-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathtests.py
359 lines (309 loc) · 10.3 KB
/
tests.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
import logging
import tempfile
import unittest
import yacs.config
import yaml
from yacs.config import CfgNode as CN
try:
_ignore = unicode # noqa: F821
PY2 = True
except Exception as _ignore:
PY2 = False
class SubCN(CN):
pass
def get_cfg(cls=CN):
cfg = cls()
cfg.NUM_GPUS = 8
cfg.TRAIN = cls()
cfg.TRAIN.HYPERPARAMETER_1 = 0.1
cfg.TRAIN.SCALES = (2, 4, 8, 16)
cfg.MODEL = cls()
cfg.MODEL.TYPE = "a_foo_model"
# Some extra stuff to test CfgNode.__str__
cfg.STR = cls()
cfg.STR.KEY1 = 1
cfg.STR.KEY2 = 2
cfg.STR.FOO = cls()
cfg.STR.FOO.KEY1 = 1
cfg.STR.FOO.KEY2 = 2
cfg.STR.FOO.BAR = cls()
cfg.STR.FOO.BAR.KEY1 = 1
cfg.STR.FOO.BAR.KEY2 = 2
cfg.register_deprecated_key("FINAL_MSG")
cfg.register_deprecated_key("MODEL.DILATION")
cfg.register_renamed_key(
"EXAMPLE.OLD.KEY",
"EXAMPLE.NEW.KEY",
message="Please update your config fil config file.",
)
cfg.KWARGS = cls(new_allowed=True)
cfg.KWARGS.z = 0
cfg.KWARGS.Y = cls()
cfg.KWARGS.Y.X = 1
return cfg
class TestCfgNode(unittest.TestCase):
def test_immutability(self):
# Top level immutable
a = CN()
a.foo = 0
a.freeze()
with self.assertRaises(AttributeError):
a.foo = 1
a.bar = 1
assert a.is_frozen()
assert a.foo == 0
a.defrost()
assert not a.is_frozen()
a.foo = 1
assert a.foo == 1
# Recursively immutable
a.level1 = CN()
a.level1.foo = 0
a.level1.level2 = CN()
a.level1.level2.foo = 0
a.freeze()
assert a.is_frozen()
with self.assertRaises(AttributeError):
a.level1.level2.foo = 1
a.level1.bar = 1
assert a.level1.level2.foo == 0
class TestCfg(unittest.TestCase):
def test_copy_cfg(self):
cfg = get_cfg()
cfg2 = cfg.clone()
s = cfg.MODEL.TYPE
cfg2.MODEL.TYPE = "dummy"
assert cfg.MODEL.TYPE == s
def test_merge_cfg_from_cfg(self):
# Test: merge from clone
cfg = get_cfg()
s = "dummy0"
cfg2 = cfg.clone()
cfg2.MODEL.TYPE = s
cfg.merge_from_other_cfg(cfg2)
assert cfg.MODEL.TYPE == s
# Test: merge from yaml
s = "dummy1"
cfg2 = CN.load_cfg(cfg.dump())
cfg2.MODEL.TYPE = s
cfg.merge_from_other_cfg(cfg2)
assert cfg.MODEL.TYPE == s
# Test: merge with a valid key
s = "dummy2"
cfg2 = CN()
cfg2.MODEL = CN()
cfg2.MODEL.TYPE = s
cfg.merge_from_other_cfg(cfg2)
assert cfg.MODEL.TYPE == s
# Test: merge with an invalid key
s = "dummy3"
cfg2 = CN()
cfg2.FOO = CN()
cfg2.FOO.BAR = s
with self.assertRaises(KeyError):
cfg.merge_from_other_cfg(cfg2)
# Test: merge with converted type
cfg2 = CN()
cfg2.TRAIN = CN()
cfg2.TRAIN.SCALES = [1]
cfg.merge_from_other_cfg(cfg2)
assert type(cfg.TRAIN.SCALES) is tuple
assert cfg.TRAIN.SCALES[0] == 1
# Test str (bytes) <-> unicode conversion for py2
if PY2:
cfg.A_UNICODE_KEY = u"foo"
cfg2 = CN()
cfg2.A_UNICODE_KEY = b"bar"
cfg.merge_from_other_cfg(cfg2)
assert type(cfg.A_UNICODE_KEY) == unicode # noqa: F821
assert cfg.A_UNICODE_KEY == u"bar"
# Test: merge with invalid type
cfg2 = CN()
cfg2.TRAIN = CN()
cfg2.TRAIN.SCALES = 1
with self.assertRaises(ValueError):
cfg.merge_from_other_cfg(cfg2)
def test_merge_cfg_from_file(self):
with tempfile.NamedTemporaryFile(mode="wt") as f:
cfg = get_cfg()
f.write(cfg.dump())
f.flush()
s = cfg.MODEL.TYPE
cfg.MODEL.TYPE = "dummy"
assert cfg.MODEL.TYPE != s
cfg.merge_from_file(f.name)
assert cfg.MODEL.TYPE == s
def test_merge_cfg_from_list(self):
cfg = get_cfg()
opts = ["TRAIN.SCALES", "(100, )", "MODEL.TYPE", "foobar", "NUM_GPUS", 2]
assert len(cfg.TRAIN.SCALES) > 0
assert cfg.TRAIN.SCALES[0] != 100
assert cfg.MODEL.TYPE != "foobar"
assert cfg.NUM_GPUS != 2
cfg.merge_from_list(opts)
assert type(cfg.TRAIN.SCALES) is tuple
assert len(cfg.TRAIN.SCALES) == 1
assert cfg.TRAIN.SCALES[0] == 100
assert cfg.MODEL.TYPE == "foobar"
assert cfg.NUM_GPUS == 2
def test_deprecated_key_from_list(self):
# You should see logger messages like:
# "Deprecated config key (ignoring): MODEL.DILATION"
cfg = get_cfg()
opts = ["FINAL_MSG", "foobar", "MODEL.DILATION", 2]
with self.assertRaises(AttributeError):
_ = cfg.FINAL_MSG # noqa
with self.assertRaises(AttributeError):
_ = cfg.MODEL.DILATION # noqa
cfg.merge_from_list(opts)
with self.assertRaises(AttributeError):
_ = cfg.FINAL_MSG # noqa
with self.assertRaises(AttributeError):
_ = cfg.MODEL.DILATION # noqa
def test_nonexistant_key_from_list(self):
cfg = get_cfg()
opts = ["MODEL.DOES_NOT_EXIST", "IGNORE"]
with self.assertRaises(AssertionError):
cfg.merge_from_list(opts)
def test_load_cfg_invalid_type(self):
class CustomClass(yaml.YAMLObject):
"""A custom class that yaml.safe_load can load."""
yaml_loader = yaml.SafeLoader
yaml_tag = u"!CustomClass"
# FOO.BAR.QUUX will have type CustomClass, which is not allowed
cfg_string = "FOO:\n BAR:\n QUUX: !CustomClass {}"
with self.assertRaises(AssertionError):
yacs.config.load_cfg(cfg_string)
def test_deprecated_key_from_file(self):
# You should see logger messages like:
# "Deprecated config key (ignoring): MODEL.DILATION"
cfg = get_cfg()
with tempfile.NamedTemporaryFile("wt") as f:
cfg2 = cfg.clone()
cfg2.MODEL.DILATION = 2
f.write(cfg2.dump())
f.flush()
with self.assertRaises(AttributeError):
_ = cfg.MODEL.DILATION # noqa
cfg.merge_from_file(f.name)
with self.assertRaises(AttributeError):
_ = cfg.MODEL.DILATION # noqa
def test_renamed_key_from_list(self):
cfg = get_cfg()
opts = ["EXAMPLE.OLD.KEY", "foobar"]
with self.assertRaises(AttributeError):
_ = cfg.EXAMPLE.OLD.KEY # noqa
with self.assertRaises(KeyError):
cfg.merge_from_list(opts)
def test_renamed_key_from_file(self):
cfg = get_cfg()
with tempfile.NamedTemporaryFile("wt") as f:
cfg2 = cfg.clone()
cfg2.EXAMPLE = CN()
cfg2.EXAMPLE.RENAMED = CN()
cfg2.EXAMPLE.RENAMED.KEY = "foobar"
f.write(cfg2.dump())
f.flush()
with self.assertRaises(AttributeError):
_ = cfg.EXAMPLE.RENAMED.KEY # noqa
with self.assertRaises(KeyError):
cfg.merge_from_file(f.name)
def test_load_cfg_from_file(self):
cfg = get_cfg()
with tempfile.NamedTemporaryFile("wt") as f:
f.write(cfg.dump())
f.flush()
with open(f.name, "rt") as f_read:
yacs.config.load_cfg(f_read)
def test_load_from_python_file(self):
# Case 1: exports CfgNode
cfg = get_cfg()
cfg.merge_from_file("example/config_override.py")
assert cfg.TRAIN.HYPERPARAMETER_1 == 0.9
# Case 2: exports dict
cfg = get_cfg()
cfg.merge_from_file("example/config_override_from_dict.py")
assert cfg.TRAIN.HYPERPARAMETER_1 == 0.9
def test_invalid_type(self):
cfg = get_cfg()
with self.assertRaises(AssertionError):
cfg.INVALID_KEY_TYPE = object()
def test__str__(self):
expected_str = """
KWARGS:
Y:
X: 1
z: 0
MODEL:
TYPE: a_foo_model
NUM_GPUS: 8
STR:
FOO:
BAR:
KEY1: 1
KEY2: 2
KEY1: 1
KEY2: 2
KEY1: 1
KEY2: 2
TRAIN:
HYPERPARAMETER_1: 0.1
SCALES: (2, 4, 8, 16)
""".strip()
cfg = get_cfg()
assert str(cfg) == expected_str
def test_new_allowed(self):
cfg = get_cfg()
cfg.merge_from_file("example/config_new_allowed.yaml")
assert cfg.KWARGS.a == 1
assert cfg.KWARGS.B.c == 2
assert cfg.KWARGS.B.D.e == "3"
def test_new_allowed_bad(self):
cfg = get_cfg()
with self.assertRaises(KeyError):
cfg.merge_from_file("example/config_new_allowed_bad.yaml")
cfg.set_new_allowed(True)
cfg.merge_from_file("example/config_new_allowed_bad.yaml")
assert cfg.KWARGS.Y.f == 4
class TestCfgNodeSubclass(unittest.TestCase):
def test_merge_cfg_from_file(self):
with tempfile.NamedTemporaryFile(mode="wt") as f:
cfg = get_cfg(SubCN)
f.write(cfg.dump())
f.flush()
s = cfg.MODEL.TYPE
cfg.MODEL.TYPE = "dummy"
assert cfg.MODEL.TYPE != s
cfg.merge_from_file(f.name)
assert cfg.MODEL.TYPE == s
def test_merge_cfg_from_list(self):
cfg = get_cfg(SubCN)
opts = ["TRAIN.SCALES", "(100, )", "MODEL.TYPE", "foobar", "NUM_GPUS", 2]
assert len(cfg.TRAIN.SCALES) > 0
assert cfg.TRAIN.SCALES[0] != 100
assert cfg.MODEL.TYPE != "foobar"
assert cfg.NUM_GPUS != 2
cfg.merge_from_list(opts)
assert type(cfg.TRAIN.SCALES) is tuple
assert len(cfg.TRAIN.SCALES) == 1
assert cfg.TRAIN.SCALES[0] == 100
assert cfg.MODEL.TYPE == "foobar"
assert cfg.NUM_GPUS == 2
def test_merge_cfg_from_cfg(self):
cfg = get_cfg(SubCN)
cfg2 = get_cfg(SubCN)
s = "dummy0"
cfg2.MODEL.TYPE = s
cfg.merge_from_other_cfg(cfg2)
assert cfg.MODEL.TYPE == s
# Test: merge from yaml
s = "dummy1"
cfg2 = SubCN.load_cfg(cfg.dump())
cfg2.MODEL.TYPE = s
cfg.merge_from_other_cfg(cfg2)
assert cfg.MODEL.TYPE == s
if __name__ == "__main__":
logging.basicConfig()
yacs_logger = logging.getLogger("yacs.config")
yacs_logger.setLevel(logging.DEBUG)
unittest.main()