Skip to content

Commit a16b2bf

Browse files
committed
when restoring the sampler, try to update the queue_size of the sampler
when using the pool
1 parent 8812f7e commit a16b2bf

3 files changed

Lines changed: 40 additions & 28 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
77
[Unreleased]
88
### Added
99
### Changed
10+
- When restoring the sampler with the pool, try to use an updated value of queue_size based on pool size
1011
### Fixed
1112

1213
[3.0.0 - 2025-10-04]

py/dynesty/dynesty.py

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
from .bounding import BOUND_LIST
1919
from . import bounding
2020
from .dynamicsampler import DynamicSampler
21-
from .utils import (LogLikelihood, get_random_generator, get_nonbounded)
21+
from .utils import (LogLikelihood, get_random_generator, get_nonbounded,
22+
_parse_pool_queue)
2223

2324
__all__ = ["NestedSampler", "DynamicNestedSampler", "_function_wrapper"]
2425

@@ -199,32 +200,6 @@ def _get_enlarge_bootstrap(sample, enlarge, bootstrap):
199200
'sense unless bootstrap=0 or enlarge = 1')
200201

201202

202-
def _parse_pool_queue(pool, queue_size):
203-
"""
204-
Common functionality of interpretign the pool and queue_size
205-
arguments to Dynamic and static nested samplers
206-
"""
207-
if queue_size is not None and queue_size < 1:
208-
raise ValueError("The queue must contain at least one element!")
209-
elif (queue_size == 1) or (pool is None and queue_size is None):
210-
mapper = map
211-
queue_size = 1
212-
elif pool is not None:
213-
mapper = pool.map
214-
if queue_size is None:
215-
try:
216-
queue_size = pool.size
217-
except AttributeError as e:
218-
raise ValueError("Cannot initialize `queue_size` because "
219-
"`pool.size` has not been provided. Please"
220-
"define `pool.size` or specify `queue_size` "
221-
"explicitly.") from e
222-
else:
223-
raise ValueError("`queue_size > 1` but no `pool` provided.")
224-
225-
return mapper, queue_size
226-
227-
228203
def _check_first_update(first_update):
229204
"""
230205
Verify that the first_update dictionary is valid

py/dynesty/utils.py

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2147,7 +2147,18 @@ def restore_sampler(fname, pool=None):
21472147
f'does not match the current dynesty version'
21482148
f'({DYNESTY_VERSION}). That is *NOT* guaranteed to work')
21492149
if pool is not None:
2150-
mapper = pool.map
2150+
queue_size_old = getattr(sampler, 'queue_size')
2151+
try:
2152+
# we first try to get the new queue_size
2153+
# that may fail if the pool has no information about the size
2154+
mapper, queue_size_new = _parse_pool_queue(pool, None)
2155+
except ValueError:
2156+
# if first failed we are using the new queue_size
2157+
mapper, queue_size_new = _parse_pool_queue(pool, queue_size_old)
2158+
2159+
if queue_size_new is not None and queue_size_new > queue_size_old:
2160+
warnings.warn(
2161+
f'Restoring the sampler with queue_size {queue_size_old}')
21512162
else:
21522163
mapper = map
21532164
if hasattr(sampler, 'sampler'):
@@ -2206,3 +2217,28 @@ def save_sampler(sampler, fname):
22062217
except: # noqa
22072218
pass
22082219
raise
2220+
2221+
2222+
def _parse_pool_queue(pool, queue_size):
2223+
"""
2224+
Common functionality of interpreting the pool and queue_size
2225+
arguments to Dynamic and static nested samplers
2226+
"""
2227+
if queue_size is not None and queue_size < 1:
2228+
raise ValueError("The queue must contain at least one element!")
2229+
if pool is None:
2230+
if queue_size > 1:
2231+
raise ValueError("`queue_size > 1` but no `pool` provided.")
2232+
mapper = map
2233+
queue_size = 1
2234+
elif pool is not None:
2235+
mapper = pool.map
2236+
if queue_size is None:
2237+
queue_size = getattr(pool, '_processes') or getattr(pool, 'size')
2238+
if queue_size is None:
2239+
raise ValueError(
2240+
"Cannot initialize `queue_size` because "
2241+
"`pool.size` or pool._processes has not been provided. "
2242+
"Please `define `pool.size` or specify `queue_size` "
2243+
"explicitly.")
2244+
return mapper, queue_size

0 commit comments

Comments
 (0)