Skip to content

Commit 844bccd

Browse files
committed
rename M to mapper
remove old_stopping_function
1 parent d73c645 commit 844bccd

4 files changed

Lines changed: 22 additions & 153 deletions

File tree

py/dynesty/dynamicsampler.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ def _get_update_interval_ratio(update_interval, sample, bound, ndim, nlive,
204204
def stopping_function(results,
205205
args=None,
206206
rstate=None,
207-
M=None,
207+
mapper=None,
208208
return_vals=False):
209209
"""
210210
The default stopping function utilized by :class:`DynamicSampler`.
@@ -245,7 +245,7 @@ def stopping_function(results,
245245
rstate : `~numpy.random.Generator`, optional
246246
`~numpy.random.Generator` instance.
247247
248-
M : `map` function, optional
248+
mapper : `map` function, optional
249249
An alias to a `map`-like function. This allows users to pass
250250
functions from pools (e.g., `pool.map`) to compute realizations in
251251
parallel. By default the standard `map` function is used.
@@ -269,8 +269,8 @@ def stopping_function(results,
269269
# Initialize values.
270270
if args is None:
271271
args = {}
272-
if M is None:
273-
M = map
272+
if mapper is None:
273+
mapper = map
274274

275275
# Initialize hyperparameters.
276276
pfrac = args.get('pfrac', 1.0)
@@ -307,7 +307,7 @@ def stopping_function(results,
307307
approx_list = [approx for i in range(n_mc)]
308308
seeds = get_seed_sequence(rstate, n_mc)
309309
args = zip(rlist, error_list, approx_list, seeds)
310-
outputs = list(M(_kld_error, args))
310+
outputs = list(mapper(_kld_error, args))
311311
lnz_arr = np.array([res[1].logz[-1] for res in outputs])
312312
# Evidence stopping value.
313313
lnz_std = np.std(lnz_arr)
@@ -645,7 +645,7 @@ def _configure_batch_sampler(main_sampler,
645645
None,
646646
main_sampler.prior_transform,
647647
main_sampler.loglikelihood,
648-
main_sampler.M,
648+
main_sampler.mapper,
649649
nlive=nlive_new,
650650
ndim=main_sampler.ndim,
651651
rstate=main_sampler.rstate,
@@ -938,9 +938,9 @@ def __init__(self, loglikelihood, prior_transform, ndim, bound, method,
938938
self.queue_size = queue_size
939939
self.pool = pool
940940
if self.pool is None:
941-
self.M = map
941+
self.mapper = map
942942
else:
943-
self.M = pool.map
943+
self.mapper = pool.map
944944

945945
self.use_pool = use_pool # provided flags for when to use the pool
946946
self.use_pool_ptform = use_pool.get('prior_transform', True)
@@ -983,7 +983,7 @@ def __init__(self, loglikelihood, prior_transform, ndim, bound, method,
983983
def __setstate__(self, state):
984984
self.__dict__ = state
985985
self.pool = None
986-
self.M = map
986+
self.mapper = map
987987

988988
def __getstate__(self):
989989
"""Get state information for pickling."""
@@ -992,7 +992,7 @@ def __getstate__(self):
992992

993993
# deal with pool
994994
del state['pool'] # remove pool
995-
del state['M'] # remove `pool.map` function hook
995+
del state['mapper'] # remove `pool.map` function hook
996996

997997
return state
998998

@@ -1264,7 +1264,7 @@ def sample_initial(self,
12641264
live_points,
12651265
self.prior_transform,
12661266
self.loglikelihood,
1267-
self.M,
1267+
self.mapper,
12681268
nlive=nlive,
12691269
ndim=self.ndim,
12701270
rstate=self.rstate,
@@ -2041,13 +2041,13 @@ def run_nested(self,
20412041
miter = min(maxiter - niter, maxiter_batch)
20422042
if mcall > 0 and miter > 0 and use_stop:
20432043
if self.use_pool_stopfn:
2044-
M = self.M
2044+
mapper = self.mapper
20452045
else:
2046-
M = map
2046+
mapper = map
20472047
stop, stop_vals = stop_function(res,
20482048
stop_kwargs,
20492049
rstate=self.rstate,
2050-
M=M,
2050+
mapper=mapper,
20512051
return_vals=True)
20522052
stop_val = stop_vals[2]
20532053
else:

py/dynesty/sampler.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -157,9 +157,9 @@ def __init__(self,
157157
# parallelism
158158
self.pool = pool # provided pool
159159
if self.pool is None:
160-
self.M = map
160+
self.mapper = map
161161
else:
162-
self.M = pool.map
162+
self.mapper = pool.map
163163
self.use_pool = use_pool or {
164164
} # provided flags for when to use the pool
165165
self.use_pool_ptform = use_pool.get('prior_transform', True)
@@ -318,13 +318,13 @@ def update_bound(self, subset=slice(None)):
318318
def __setstate__(self, state):
319319
self.__dict__ = state
320320
self.pool = None
321-
self.M = map
321+
self.mapper = map
322322

323323
def __getstate__(self):
324324
"""Get state information for pickling."""
325325

326326
state = self.__dict__.copy()
327-
for k in ['M', 'pool']:
327+
for k in ['mapper', 'pool']:
328328
if k in state:
329329
del state[k]
330330
return state
@@ -337,7 +337,8 @@ def reset(self):
337337
if self.use_pool_ptform:
338338
# Use the pool to compute the prior transform.
339339
self.live_v = np.array(
340-
list(self.M(self.prior_transform, np.asarray(self.live_u))))
340+
list(self.mapper(self.prior_transform,
341+
np.asarray(self.live_u))))
341342
else:
342343
# Compute the prior transform using the default `map` function.
343344
self.live_v = np.array(
@@ -502,7 +503,7 @@ def _fill_queue(self, loglstar):
502503

503504
if self.use_pool_evolve:
504505
# Use the pool to propose ("evolve") a new live point.
505-
mapper = self.M
506+
mapper = self.mapper
506507
else:
507508
# Propose ("evolve") a new live point using the default `map`
508509
# function.

py/dynesty/utils.py

Lines changed: 1 addition & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -2125,121 +2125,6 @@ def _kld_error(args):
21252125
approx=approx)
21262126

21272127

2128-
def old_stopping_function(results,
2129-
args=None,
2130-
rstate=None,
2131-
M=None,
2132-
return_vals=False):
2133-
"""
2134-
The old stopping function utilized by :class:`DynamicSampler`.
2135-
Zipped parameters are passed to the function via :data:`args`.
2136-
Assigns the run a stopping value based on a weighted average of the
2137-
stopping values for the posterior and evidence::
2138-
stop = pfrac * stop_post + (1.- pfrac) * stop_evid
2139-
The evidence stopping value is based on the estimated evidence error
2140-
(i.e. standard deviation) relative to a given threshold::
2141-
stop_evid = evid_std / evid_thresh
2142-
The posterior stopping value is based on the fractional error (i.e.
2143-
standard deviation / mean) in the Kullback-Leibler (KL) divergence
2144-
relative to a given threshold::
2145-
stop_post = (kld_std / kld_mean) / post_thresh
2146-
Estimates of the mean and standard deviation are computed using `n_mc`
2147-
realizations of the input using a provided `'error'` keyword (either
2148-
`'jitter'` or `'resample'`).
2149-
Returns the boolean `stop <= 1`. If `True`, the :class:`DynamicSampler`
2150-
will stop adding new samples to our results.
2151-
Parameters
2152-
----------
2153-
results : :class:`Results` instance
2154-
:class:`Results` instance.
2155-
args : dictionary of keyword arguments, optional
2156-
Arguments used to set the stopping values. Default values are
2157-
`pfrac = 1.0`, `evid_thresh = 0.1`, `post_thresh = 0.02`,
2158-
`n_mc = 128`, `error = 'jitter'`, and `approx = True`.
2159-
rstate : `~numpy.random.Generator`, optional
2160-
`~numpy.random.Generator` instance.
2161-
M : `map` function, optional
2162-
An alias to a `map`-like function. This allows users to pass
2163-
functions from pools (e.g., `pool.map`) to compute realizations in
2164-
parallel. By default the standard `map` function is used.
2165-
return_vals : bool, optional
2166-
Whether to return the stopping value (and its components). Default
2167-
is `False`.
2168-
Returns
2169-
-------
2170-
stop_flag : bool
2171-
Boolean flag indicating whether we have passed the desired stopping
2172-
criteria.
2173-
stop_vals : tuple of shape (3,), optional
2174-
The individual stopping values `(stop_post, stop_evid, stop)` used
2175-
to determine the stopping criteria.
2176-
"""
2177-
2178-
with warnings.catch_warnings():
2179-
warnings.filterwarnings("once")
2180-
warnings.warn(
2181-
"This an old stopping function that will "
2182-
"be removed in future releases", DeprecationWarning)
2183-
# Initialize values.
2184-
if args is None:
2185-
args = {}
2186-
if M is None:
2187-
M = map
2188-
2189-
# Initialize hyperparameters.
2190-
pfrac = args.get('pfrac', 1.0)
2191-
if not 0. <= pfrac <= 1.:
2192-
raise ValueError(
2193-
f"The provided `pfrac` {pfrac} is not between 0. and 1.")
2194-
evid_thresh = args.get('evid_thresh', 0.1)
2195-
if pfrac < 1. and evid_thresh < 0.:
2196-
raise ValueError(
2197-
f"The provided `evid_thresh` {evid_thresh} is not non-negative "
2198-
f"even though `pfrac` is {pfrac}.")
2199-
post_thresh = args.get('post_thresh', 0.02)
2200-
if pfrac > 0. and post_thresh < 0.:
2201-
raise ValueError(
2202-
f"The provided `post_thresh` {post_thresh} is not non-negative "
2203-
f"even though `pfrac` is {pfrac}.")
2204-
n_mc = args.get('n_mc', 128)
2205-
if n_mc <= 1:
2206-
raise ValueError(f"The number of realizations {n_mc} must be greater "
2207-
"than 1.")
2208-
if n_mc < 20:
2209-
warnings.warn("Using a small number of realizations might result in "
2210-
"excessively noisy stopping value estimates.")
2211-
error = args.get('error', 'jitter')
2212-
if error not in {'jitter', 'resample'}:
2213-
raise ValueError(f"The chosen `'error'` option {error} is not valid.")
2214-
approx = args.get('approx', True)
2215-
2216-
# Compute realizations of ln(evidence) and the KL divergence.
2217-
rlist = [results for i in range(n_mc)]
2218-
error_list = [error for i in range(n_mc)]
2219-
approx_list = [approx for i in range(n_mc)]
2220-
seeds = get_seed_sequence(rstate, n_mc)
2221-
args = zip(rlist, error_list, approx_list, seeds)
2222-
outputs = list(M(_kld_error, args))
2223-
kld_arr, lnz_arr = np.array([(kld[-1], res.logz[-1])
2224-
for kld, res in outputs]).T
2225-
2226-
# Evidence stopping value.
2227-
lnz_std = np.std(lnz_arr)
2228-
stop_evid = lnz_std / evid_thresh
2229-
2230-
# Posterior stopping value.
2231-
kld_mean, kld_std = np.mean(kld_arr), np.std(kld_arr)
2232-
stop_post = (kld_std / kld_mean) / post_thresh
2233-
2234-
# Effective stopping value.
2235-
stop = pfrac * stop_post + (1. - pfrac) * stop_evid
2236-
2237-
if return_vals:
2238-
return stop <= 1., (stop_post, stop_evid, stop)
2239-
else:
2240-
return stop <= 1.
2241-
2242-
22432128
def restore_sampler(fname, pool=None):
22442129
"""
22452130
Restore the dynamic sampler from a file.
@@ -2293,7 +2178,7 @@ def restore_sampler(fname, pool=None):
22932178
samplers = [sampler]
22942179

22952180
for cursamp in samplers:
2296-
cursamp.M = mapper
2181+
cursamp.mapper = mapper
22972182
cursamp.pool = pool
22982183
cursamp.loglikelihood.pool = pool
22992184
return sampler

tests/test_misc.py

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -277,23 +277,6 @@ def test_neff():
277277
assert sampler.n_effective > 10000
278278

279279

280-
def test_oldstop():
281-
# test of old stopping function functionality
282-
ndim = 2
283-
rstate = get_rstate()
284-
import dynesty.utils as dyutil
285-
stopfn = dyutil.old_stopping_function
286-
sampler = dynesty.DynamicNestedSampler(loglike,
287-
prior_transform,
288-
ndim,
289-
nlive=nlive,
290-
rstate=rstate)
291-
sampler.run_nested(dlogz_init=1,
292-
n_effective=None,
293-
stop_function=stopfn,
294-
print_progress=printing)
295-
296-
297280
def test_stop_nmc():
298281
# test stopping relying in n_mc
299282
ndim = 2

0 commit comments

Comments
 (0)