-
Notifications
You must be signed in to change notification settings - Fork 87
Expand file tree
/
Copy pathtest_pool.py
More file actions
244 lines (192 loc) · 8.45 KB
/
test_pool.py
File metadata and controls
244 lines (192 loc) · 8.45 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
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
import numpy as np
import pytest
import dynesty
import multiprocessing as mp
import dynesty.pool as dypool
from utils import get_rstate, get_printing
"""
Run a series of basic tests to check whether anything huge is broken.
"""
nlive = 1000
printing = get_printing()
ndim = 2
gau_s = 0.01
def loglike_gau(x):
return (-0.5 * np.log(2 * np.pi) * ndim - np.log(gau_s) * ndim -
0.5 * np.sum((x - 0.5)**2) / gau_s**2)
def prior_transform_gau(x):
return x
# EGGBOX
# see 1306.2144
def loglike_egg(x):
logl = ((2 + np.cos(x[0] / 2) * np.cos(x[1] / 2))**5)
return logl
def prior_transform_egg(x):
return x * 10 * np.pi
LOGZ_TRUTH_GAU = 0
LOGZ_TRUTH_EGG = 235.856
def terminator(pool):
# Because of https://github.com/nedbat/coveragepy/issues/1310
# I have to close join and can't fully rely on contexts that
# do send SIGTERMS
pool.close()
pool.join()
def test_pool():
# test pool on egg problem
rstate = get_rstate()
# i specify large queue_size here, otherwise it is too slow
with dypool.Pool(2, loglike_egg, prior_transform_egg) as pool:
sampler = dynesty.NestedSampler(pool.loglike,
pool.prior_transform,
ndim,
nlive=nlive,
pool=pool,
queue_size=100,
rstate=rstate)
sampler.run_nested(dlogz=0.1, print_progress=printing)
assert (abs(LOGZ_TRUTH_EGG - sampler.results['logz'][-1])
< 5. * sampler.results['logzerr'][-1])
terminator(pool)
def test_pool_x():
# check without specifying queue_size
rstate = get_rstate()
with dypool.Pool(2, loglike_egg, prior_transform_egg) as pool:
sampler = dynesty.NestedSampler(pool.loglike,
pool.prior_transform,
ndim,
nlive=50,
pool=pool,
rstate=rstate)
sampler.run_nested(print_progress=printing, maxiter=100)
# not checking the results since I'm interrupting
terminator(pool)
def test_pool_dynamic():
# test pool on gau problem
# i specify large queue_size here, otherwise it is too slow
rstate = get_rstate()
with dypool.Pool(2, loglike_gau, prior_transform_gau) as pool:
sampler = dynesty.DynamicNestedSampler(pool.loglike,
pool.prior_transform,
ndim,
nlive=nlive,
pool=pool,
queue_size=100,
rstate=rstate)
sampler.run_nested(dlogz_init=1, print_progress=printing)
assert (abs(LOGZ_TRUTH_GAU - sampler.results['logz'][-1])
< 5. * sampler.results['logzerr'][-1])
terminator(pool)
def loglike_gau_args(x, y, z=None, a=0, b=0):
return (-0.5 * np.log(2 * np.pi) * ndim - np.log(gau_s) * ndim -
0.5 * np.sum((x - 0.5)**2) / gau_s**2) + y + z + a + b
def prior_transform_gau_args(x, y, z=None, a=0, b=0):
return x + y + z + a + b
def test_pool_args():
# test pool on gau problem
# i specify large queue_size here, otherwise it is too slow
rstate = get_rstate()
with dypool.Pool(2,
loglike_gau_args,
prior_transform_gau_args,
logl_args=(1, ),
ptform_args=(1, ),
logl_kwargs=dict(z=-1),
ptform_kwargs=dict(z=-1)) as pool:
sampler = dynesty.DynamicNestedSampler(pool.loglike,
pool.prior_transform,
ndim,
nlive=nlive,
pool=pool,
queue_size=100,
rstate=rstate)
sampler.run_nested(maxiter=300, print_progress=printing)
assert (abs(LOGZ_TRUTH_GAU - sampler.results['logz'][-1])
< 5. * sampler.results['logzerr'][-1])
# to ensure we get coverage
terminator(pool)
def test_pool_args2():
# test pool on gau problem
# i specify large queue_size here, otherwise it is too slow
# Here I am testing that args from pool and Nested sampler are
# properly concatenated
rstate = get_rstate()
with dypool.Pool(
2,
loglike_gau_args,
prior_transform_gau_args,
logl_args=(1, ),
ptform_args=(1, ),
logl_kwargs={'a': 2},
ptform_kwargs={'a': 2},
) as pool:
sampler = dynesty.DynamicNestedSampler(pool.loglike,
pool.prior_transform,
ndim,
nlive=nlive,
pool=pool,
logl_args=(-1, ),
ptform_args=(-1, ),
logl_kwargs={'b': -2},
ptform_kwargs={'b': -2},
queue_size=100,
rstate=rstate)
sampler.run_nested(maxiter=300, print_progress=printing)
assert (abs(LOGZ_TRUTH_GAU - sampler.results['logz'][-1])
< 5. * sampler.results['logzerr'][-1])
# to ensure we get coverage
terminator(pool)
@pytest.mark.parametrize('sample', ['slice', 'rwalk', 'rslice', 'unif'])
def test_pool_samplers(sample):
# this is to test how the samplers are dealing with queue_size>1
rstate = get_rstate()
ctx = mp.get_context('spawn')
with ctx.Pool(2) as pool:
sampler = dynesty.NestedSampler(loglike_gau,
prior_transform_gau,
ndim,
nlive=nlive,
sample=sample,
pool=pool,
queue_size=100,
rstate=rstate)
sampler.run_nested(print_progress=printing)
assert (abs(LOGZ_TRUTH_GAU - sampler.results['logz'][-1])
< 5. * sampler.results['logzerr'][-1])
terminator(pool)
POOL_KW = ['prior_transform', 'loglikelihood', 'propose_point', 'update_bound']
@pytest.mark.parametrize('func', POOL_KW)
def test_usepool(func):
# test all the use_pool options, toggle them one by one
rstate = get_rstate()
use_pool = {}
for k in POOL_KW:
use_pool[k] = False
use_pool[func] = True
ctx = mp.get_context('spawn')
with ctx.Pool(2) as pool:
sampler = dynesty.DynamicNestedSampler(loglike_gau,
prior_transform_gau,
ndim,
nlive=nlive,
rstate=rstate,
use_pool=use_pool,
pool=pool,
queue_size=100)
sampler.run_nested(maxiter=10000, print_progress=printing)
terminator(pool)
@pytest.mark.parametrize('queue_size', [None, 2])
def test_pool_queue_size(queue_size):
# this is to test how the samplers are dealing with specified
# or unspecified queue_size
rstate = get_rstate()
ctx = mp.get_context('spawn')
with ctx.Pool(2) as pool:
sampler = dynesty.NestedSampler(loglike_gau,
prior_transform_gau,
ndim,
nlive=nlive,
sample='rslice',
pool=pool,
queue_size=queue_size,
rstate=rstate)
sampler.run_nested(print_progress=printing, dlogz=10)