1
|
|
|
#!/usr/bin/env python |
2
|
|
|
# coding=utf-8 |
3
|
|
|
from __future__ import division, print_function, unicode_literals |
4
|
|
|
|
5
|
|
|
from sacred.config.config_scope import ConfigScope |
6
|
|
|
from sacred.experiment import Experiment, Ingredient |
7
|
|
|
|
8
|
|
|
|
9
|
|
|
def test_ingredient_config(): |
10
|
|
|
m = Ingredient("somemod") |
11
|
|
|
|
12
|
|
|
@m.config |
13
|
|
|
def cfg(): |
14
|
|
|
a = 5 |
15
|
|
|
b = 'foo' |
16
|
|
|
|
17
|
|
|
assert len(m.configurations) == 1 |
18
|
|
|
cfg = m.configurations[0] |
19
|
|
|
assert isinstance(cfg, ConfigScope) |
20
|
|
|
assert cfg() == {'a': 5, 'b': 'foo'} |
21
|
|
|
|
22
|
|
|
|
23
|
|
|
def test_ingredient_captured_functions(): |
24
|
|
|
m = Ingredient("somemod") |
25
|
|
|
|
26
|
|
|
@m.capture |
27
|
|
|
def get_answer(b): |
28
|
|
|
return b |
29
|
|
|
|
30
|
|
|
assert len(m.captured_functions) == 1 |
31
|
|
|
f = m.captured_functions[0] |
32
|
|
|
assert f == get_answer |
33
|
|
|
|
34
|
|
|
|
35
|
|
|
def test_ingredient_command(): |
36
|
|
|
m = Ingredient("somemod") |
37
|
|
|
|
38
|
|
|
m.add_config(a=42, b='foo{}') |
39
|
|
|
|
40
|
|
|
@m.command |
41
|
|
|
def transmogrify(a, b): |
42
|
|
|
return b.format(a) |
43
|
|
|
|
44
|
|
|
assert 'transmogrify' in m.commands |
45
|
|
|
assert m.commands['transmogrify'] == transmogrify |
46
|
|
|
ex = Experiment('foo', ingredients=[m]) |
47
|
|
|
|
48
|
|
|
assert ex.run_command('somemod.transmogrify').result == 'foo42' |
49
|
|
|
|
50
|
|
|
|
51
|
|
|
# ############# Experiment #################################################### |
52
|
|
|
|
53
|
|
|
def test_experiment_run(): |
54
|
|
|
ex = Experiment("some_experiment") |
55
|
|
|
|
56
|
|
|
@ex.main |
57
|
|
|
def main(): |
58
|
|
|
return 12 |
59
|
|
|
|
60
|
|
|
assert ex.run().result == 12 |
61
|
|
|
|
62
|
|
|
|
63
|
|
|
def test_experiment_run_access_subingredient(): |
64
|
|
|
somemod = Ingredient("somemod") |
65
|
|
|
|
66
|
|
|
@somemod.config |
67
|
|
|
def cfg(): |
68
|
|
|
a = 5 |
69
|
|
|
b = 'foo' |
70
|
|
|
|
71
|
|
|
ex = Experiment("some_experiment", ingredients=[somemod]) |
72
|
|
|
|
73
|
|
|
@ex.main |
74
|
|
|
def main(somemod): |
75
|
|
|
return somemod |
76
|
|
|
|
77
|
|
|
r = ex.run().result |
78
|
|
|
assert r['a'] == 5 |
79
|
|
|
assert r['b'] == 'foo' |
80
|
|
|
|
81
|
|
|
|
82
|
|
|
def test_experiment_run_subingredient_function(): |
83
|
|
|
somemod = Ingredient("somemod") |
84
|
|
|
|
85
|
|
|
@somemod.config |
86
|
|
|
def cfg(): |
87
|
|
|
a = 5 |
88
|
|
|
b = 'foo' |
89
|
|
|
|
90
|
|
|
@somemod.capture |
91
|
|
|
def get_answer(b): |
92
|
|
|
return b |
93
|
|
|
|
94
|
|
|
ex = Experiment("some_experiment", ingredients=[somemod]) |
95
|
|
|
|
96
|
|
|
@ex.main |
97
|
|
|
def main(): |
98
|
|
|
return get_answer() |
99
|
|
|
|
100
|
|
|
assert ex.run().result == 'foo' |
101
|
|
|
|
102
|
|
|
|
103
|
|
|
def test_experiment_named_config_subingredient(): |
104
|
|
|
somemod = Ingredient("somemod") |
105
|
|
|
|
106
|
|
|
@somemod.config |
107
|
|
|
def sub_cfg(): |
108
|
|
|
a = 15 |
109
|
|
|
|
110
|
|
|
@somemod.capture |
111
|
|
|
def get_answer(a): |
112
|
|
|
return a |
113
|
|
|
|
114
|
|
|
@somemod.named_config |
115
|
|
|
def nsubcfg(): |
116
|
|
|
a = 16 |
117
|
|
|
|
118
|
|
|
ex = Experiment("some_experiment", ingredients=[somemod]) |
119
|
|
|
|
120
|
|
|
@ex.config |
121
|
|
|
def cfg(): |
122
|
|
|
a = 1 |
123
|
|
|
|
124
|
|
|
@ex.named_config |
125
|
|
|
def ncfg(): |
126
|
|
|
a = 2 |
127
|
|
|
somemod = {'a': 25} |
128
|
|
|
|
129
|
|
|
@ex.main |
130
|
|
|
def main(a): |
131
|
|
|
return a, get_answer() |
132
|
|
|
|
133
|
|
|
assert ex.run().result == (1, 15) |
134
|
|
|
assert ex.run(named_configs=['somemod.nsubcfg']).result == (1, 16) |
135
|
|
|
assert ex.run(named_configs=['ncfg']).result == (2, 25) |
136
|
|
|
assert ex.run(named_configs=['ncfg', 'somemod.nsubcfg']).result == (2, 16) |
137
|
|
|
assert ex.run(named_configs=['somemod.nsubcfg', 'ncfg']).result == (2, 25) |
138
|
|
|
|
139
|
|
|
|
140
|
|
|
def test_experiment_named_config_subingredient_overwrite(): |
141
|
|
|
somemod = Ingredient("somemod") |
142
|
|
|
|
143
|
|
|
@somemod.capture |
144
|
|
|
def get_answer(a): |
145
|
|
|
return a |
146
|
|
|
|
147
|
|
|
ex = Experiment("some_experiment", ingredients=[somemod]) |
148
|
|
|
|
149
|
|
|
@ex.named_config |
150
|
|
|
def ncfg(): |
151
|
|
|
somemod = {'a': 1} |
152
|
|
|
|
153
|
|
|
@ex.main |
154
|
|
|
def main(): |
155
|
|
|
return get_answer() |
156
|
|
|
|
157
|
|
|
assert ex.run(named_configs=['ncfg']).result == 1 |
158
|
|
|
assert ex.run(config_updates={'somemod': {'a': 2}}).result == 2 |
159
|
|
|
assert ex.run(named_configs=['ncfg'], |
160
|
|
|
config_updates={'somemod': {'a': 2}} |
161
|
|
|
).result == 2 |
162
|
|
|
|
163
|
|
|
|
164
|
|
|
def test_experiment_double_named_config(): |
165
|
|
|
ex = Experiment() |
166
|
|
|
|
167
|
|
|
@ex.config |
168
|
|
|
def config(): |
169
|
|
|
a = 0 |
170
|
|
|
d = { |
171
|
|
|
'e': 0, |
172
|
|
|
'f': 0 |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
@ex.named_config |
176
|
|
|
def A(): |
177
|
|
|
a = 2 |
178
|
|
|
d = { |
179
|
|
|
'e': 2, |
180
|
|
|
'f': 2 |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
@ex.named_config |
184
|
|
|
def B(): |
185
|
|
|
d = {'f': -1} |
186
|
|
|
|
187
|
|
|
@ex.main |
188
|
|
|
def run(a, d): |
189
|
|
|
return a, d['e'], d['f'] |
190
|
|
|
|
191
|
|
|
assert ex.run().result == (0, 0, 0) |
192
|
|
|
assert ex.run(named_configs=['A']).result == (2, 2, 2) |
193
|
|
|
assert ex.run(named_configs=['B']).result == (0, 0, -1) |
194
|
|
|
assert ex.run(named_configs=['A', 'B']).result == (2, 2, -1) |
195
|
|
|
assert ex.run(named_configs=['B', 'A']).result == (2, 2, 2) |
196
|
|
|
|
197
|
|
|
|
198
|
|
|
def test_double_nested_config(): |
199
|
|
|
sub_sub_ing = Ingredient('sub_sub_ing') |
200
|
|
|
sub_ing = Ingredient('sub_ing', [sub_sub_ing]) |
201
|
|
|
ing = Ingredient('ing', [sub_ing]) |
202
|
|
|
ex = Experiment('ex', [ing]) |
203
|
|
|
|
204
|
|
|
@ex.config |
205
|
|
|
def config(): |
206
|
|
|
a = 1 |
207
|
|
|
|
208
|
|
|
@ing.config |
209
|
|
|
def config(): |
210
|
|
|
b = 1 |
211
|
|
|
|
212
|
|
|
@sub_ing.config |
213
|
|
|
def config(): |
214
|
|
|
c = 2 |
215
|
|
|
|
216
|
|
|
@sub_sub_ing.config |
217
|
|
|
def config(): |
218
|
|
|
d = 3 |
219
|
|
|
|
220
|
|
|
@sub_sub_ing.capture |
221
|
|
|
def sub_sub_ing_main(_config): |
222
|
|
|
assert _config == { |
223
|
|
|
'd': 3 |
224
|
|
|
}, _config |
225
|
|
|
|
226
|
|
|
@sub_ing.capture |
227
|
|
|
def sub_ing_main(_config): |
228
|
|
|
assert _config == { |
229
|
|
|
'c': 2, |
230
|
|
|
'sub_sub_ing': {'d': 3} |
231
|
|
|
}, _config |
232
|
|
|
|
233
|
|
|
@ing.capture |
234
|
|
|
def ing_main(_config): |
235
|
|
|
assert _config == { |
236
|
|
|
'b': 1, |
237
|
|
|
'sub_sub_ing': {'d': 3}, |
238
|
|
|
'sub_ing': {'c': 2} |
239
|
|
|
}, _config |
240
|
|
|
|
241
|
|
|
@ex.main |
242
|
|
|
def main(_config): |
243
|
|
|
_config.pop('seed') |
244
|
|
|
assert _config == { |
245
|
|
|
'a': 1, |
246
|
|
|
'sub_sub_ing': {'d': 3}, |
247
|
|
|
'sub_ing': {'c': 2}, |
248
|
|
|
'ing': {'b': 1} |
249
|
|
|
}, _config |
250
|
|
|
|
251
|
|
|
ing_main() |
252
|
|
|
sub_ing_main() |
253
|
|
|
sub_sub_ing_main() |
254
|
|
|
|
255
|
|
|
ex.run() |
256
|
|
|
|