-
Notifications
You must be signed in to change notification settings - Fork 41
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Elaborate NS run using a loop #644
Open
fnovak42
wants to merge
4
commits into
master
Choose a base branch
from
ns-loop
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,7 +38,7 @@ def _repr_html_(self): | |
name = p.name() | ||
latex = p.latex() | ||
name = latex if latex else name | ||
result += '<tr><td>{n}</td><td>{v:6.4f}</td></tr>'.format(n=name, v=v) | ||
result += f'<tr><td>{name}</td><td>{v:6.4f}</td></tr>' | ||
result += '</table>' | ||
|
||
return(result) | ||
|
@@ -87,13 +87,13 @@ def __init__(self, priors, likelihood, external_likelihood=[], global_options={} | |
eos.debug(' - {name} (constraint)'.format(name=p['constraint'])) | ||
eos.debug('constraints:') | ||
for cn in likelihood: | ||
eos.debug(' - {name}'.format(name=cn)) | ||
eos.debug(f' - {cn}') | ||
eos.debug('manual_constraints:') | ||
for cn, ce in manual_constraints.items(): | ||
eos.debug(' - {name}'.format(name=cn)) | ||
eos.debug(f' - {cn}') | ||
eos.debug('fixed_parameters:') | ||
for pn, pe in fixed_parameters.items(): | ||
eos.debug(' - {name}'.format(name=pn)) | ||
eos.debug(f' - {pn}') | ||
|
||
# collect the global options | ||
for key, value in global_options.items(): | ||
|
@@ -161,7 +161,7 @@ def __init__(self, priors, likelihood, external_likelihood=[], global_options={} | |
eos.LogPrior.Poisson(self.parameters, parameter, k), | ||
False) | ||
else: | ||
raise ValueError('Unknown prior type \'{}\''.format(prior_type)) | ||
raise ValueError(f'Unknown prior type \'{prior_type}\'') | ||
|
||
p = self.parameters[parameter] | ||
self.varied_parameters.append(p) | ||
|
@@ -216,11 +216,11 @@ def __init__(self, priors, likelihood, external_likelihood=[], global_options={} | |
|
||
used_but_unvaried = used_parameter_names - varied_parameter_names - fixed_parameter_names | ||
if (len(used_but_unvaried) > 0): | ||
eos.info('likelihood probably depends on {} parameter(s) that do not appear in the prior; check prior?'.format(len(used_but_unvaried))) | ||
eos.info(f'likelihood probably depends on {len(used_but_unvaried)} parameter(s) that do not appear in the prior; check prior?') | ||
for n in used_but_unvaried: | ||
eos.debug('used, but not included in any prior: \'{}\''.format(n)) | ||
eos.debug(f'used, but not included in any prior: \'{n}\'') | ||
for n in varied_parameter_names - used_parameter_names: | ||
eos.warn('likelihood does not depend on parameter \'{}\'; remove from prior or check options!'.format(n)) | ||
eos.warn(f'likelihood does not depend on parameter \'{n}\'; remove from prior or check options!') | ||
|
||
|
||
def _u_to_par(self, u): | ||
|
@@ -257,7 +257,7 @@ def _sanitize_manual_input(data): | |
return list(map(Analysis._sanitize_manual_input, data)) | ||
|
||
# all valid cases are covered above | ||
raise ValueError("Unexpected entry type {} in manual_constraint".format(type(data))) | ||
raise ValueError(f"Unexpected entry type {type(data)} in manual_constraint") | ||
|
||
|
||
@staticmethod | ||
|
@@ -335,7 +335,7 @@ def optimize(self, start_point=None, rng=np.random.mtrand, **kwargs): | |
eos.warn('Optimization did not succeed') | ||
eos.warn(' optimizer'' message reads: {}'.format(res.message)) | ||
else: | ||
eos.info('Optimization goal achieved after {nfev} function evaluations'.format(nfev=res.nfev)) | ||
eos.info(f'Optimization goal achieved after {res.nfev} function evaluations') | ||
|
||
bfp = self._u_to_par(res.x) | ||
|
||
|
@@ -359,9 +359,9 @@ def log_pdf(self, u, *args): | |
try: | ||
return(self._log_posterior.evaluate()) | ||
except RuntimeError as e: | ||
eos.error('encountered run time error ({e}) when evaluating log(posterior) in parameter point:'.format(e=e)) | ||
eos.error(f'encountered run time error ({e}) when evaluating log(posterior) in parameter point:') | ||
for p in self.varied_parameters: | ||
eos.error(' - {n}: {v}'.format(n=p.name(), v=p.evaluate())) | ||
eos.error(f' - {p.name()}: {p.evaluate()}') | ||
return(-np.inf) | ||
|
||
|
||
|
@@ -428,10 +428,10 @@ def sample(self, N=1000, stride=5, pre_N=150, preruns=3, cov_scale=0.1, observab | |
|
||
# pre run to adapt markov chains | ||
for i in progressbar(range(0, preruns), desc="Pre-runs", leave=False): | ||
eos.info('Prerun {} out of {}'.format(i, preruns)) | ||
eos.info(f'Prerun {i} out of {preruns}') | ||
accept_count = sampler.run(pre_N) | ||
accept_rate = accept_count / pre_N * 100 | ||
eos.info('Prerun {}: acceptance rate is {:3.0f}%'.format(i, accept_rate)) | ||
eos.info(f'Prerun {i}: acceptance rate is {accept_rate:3.0f}%') | ||
sampler.adapt() | ||
sampler.clear() | ||
|
||
|
@@ -444,7 +444,7 @@ def sample(self, N=1000, stride=5, pre_N=150, preruns=3, cov_scale=0.1, observab | |
for current_chunk in progressbar(sample_chunks, desc="Main run", leave=False): | ||
accept_count = accept_count + sampler.run(current_chunk) | ||
accept_rate = accept_count / (N * stride) * 100 | ||
eos.info('Main run: acceptance rate is {:3.0f}%'.format(accept_rate)) | ||
eos.info(f'Main run: acceptance rate is {accept_rate:3.0f}%') | ||
|
||
# Transform from generator values in u space to the parameter values | ||
u_samples = sampler.samples[:][::stride] | ||
|
@@ -621,9 +621,9 @@ def log_likelihood(self, p, *args): | |
try: | ||
return(self._log_likelihood.evaluate()) | ||
except RuntimeError as e: | ||
eos.error('encountered run time error ({e}) when evaluating log(posterior) in parameter point:'.format(e=e)) | ||
eos.error(f'encountered run time error ({e}) when evaluating log(posterior) in parameter point:') | ||
for p in self.varied_parameters: | ||
eos.error(' - {n}: {v}'.format(n=p.name(), v=p.evaluate())) | ||
eos.error(f' - {p.name()}: {p.evaluate()}') | ||
return(-np.inf) | ||
|
||
|
||
|
@@ -637,7 +637,7 @@ def _prior_transform(self, u): | |
return self._u_to_par(u) | ||
|
||
|
||
def sample_nested(self, bound='multi', nlive=250, dlogz=1.0, maxiter=None, seed=10, print_progress=True): | ||
def sample_nested(self, bound='multi', nlive=250, dlogz=1.0, maxiter=None, seed=10, checkpoint_interval=60): | ||
""" | ||
Return samples of the parameters. | ||
|
||
|
@@ -653,14 +653,53 @@ def sample_nested(self, bound='multi', nlive=250, dlogz=1.0, maxiter=None, seed= | |
:type maxiter: int, optional | ||
:param seed: The seed used to initialize the Mersenne Twister pseudo-random number generator. | ||
:type seed: {None, int, array_like[ints], SeedSequence}, optional | ||
:param checkpoint_interval: The number of seconds between checkpoints at which the intermediate dynesty sampler results are stored. If None, then no intermediate results are stored. Defaults to 60. | ||
:type checkpoint_interval: float, optional | ||
|
||
.. note:: | ||
This method requires the dynesty python module, which can be installed from PyPI. | ||
""" | ||
import dynesty | ||
import itertools | ||
from dynesty.dynamicsampler import stopping_function, weight_function | ||
sampler = dynesty.DynamicNestedSampler(self.log_likelihood, self._prior_transform, len(self.varied_parameters), bound=bound, nlive=nlive, rstate = np.random.Generator(np.random.MT19937(seed))) | ||
sampler.run_nested(dlogz_init=dlogz, maxiter=maxiter, print_progress=print_progress) | ||
return sampler.results | ||
|
||
eos.info('Drawing initial samples.') | ||
for results in sampler.sample_initial(dlogz=dlogz, maxiter=maxiter): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would enumerate this and run a log message every few seconds/every few samples. @mreboud ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Every few samples is probably simpler to implement. |
||
pass | ||
|
||
eos.info('Starting nested sampling loop.') | ||
if checkpoint_interval is not None: | ||
timer = dynesty.utils.DelayTimer(checkpoint_interval) | ||
time=0 | ||
|
||
for iter in itertools.count(): | ||
if checkpoint_interval is not None and timer.is_time(): | ||
time+=60 | ||
eos.info(f'Sampler results stored after {time} seconds.') | ||
yield sampler.results | ||
|
||
stop = stopping_function(sampler.results) | ||
|
||
if maxiter is not None and iter > maxiter: | ||
stop = True | ||
eos.info("The sampling was stopped because the maximum number of iterations was reached.") | ||
|
||
delta_logz = np.logaddexp(0, np.max(sampler.live_logl) + sampler.results.logvol - sampler.results.logz)[-1] | ||
if delta_logz < dlogz: | ||
stop = True | ||
eos.info("The sampling was stopped because the limit of the remaining evidence was reached.") | ||
|
||
if not stop: | ||
logl_bounds = weight_function(sampler.results) # derive bounds | ||
for results in sampler.sample_batch(logl_bounds=logl_bounds, dlogz=dlogz, maxiter=maxiter): | ||
pass | ||
sampler.combine_runs() # add new samples to previous results | ||
else: | ||
break | ||
|
||
yield sampler.results | ||
return | ||
|
||
|
||
def _repr_html_(self): | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All of the changes above are due to
pyupgrade
and should be moved to a separate commit.