Skip to content
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

[Draft] Support for converting from dynesty and emcee samples #158

Draft
wants to merge 13 commits into
base: master
Choose a base branch
from
Draft
95 changes: 95 additions & 0 deletions anesthetic/convert.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Tools for converting to other outputs."""
from anesthetic.samples import NestedSamples, MCMCSamples
import numpy as np


Expand All @@ -25,3 +26,97 @@ def to_getdist(samples):
names=samples.columns,
ranges=ranges,
labels=labels)


def from_anesthetic(dynesty_sampler, columns=None, tex=None, limits=None):
"""Convert from dynesty to anesthetic samples.

Parameters
----------
dynesty_sampler: dynesty.dynesty.NestedSampler or dynesty.results.Results
dynesty NestedSampler instance to copy results from
(must have been run already), or results of a dynesty run.

columns: list, optional
List of (python) parameter names
default: None

tex: list, optional
List of LaTeX parameter names
default: None

limits: list, optional
List of parameter limits
default: None

Returns
-------
nested_samples: NestedSamples
anesthetic nested samples
"""
import dynesty.results
import dynesty.dynesty

if isinstance(dynesty_sampler, dynesty.dynesty.NestedSampler):
dynesty_results = dynesty_sampler.results
elif isinstance(dynesty_sampler, dynesty.results.Results):
dynesty_results = dynesty_sampler
else:
raise ValueError("Unknown dynesty input type")

data = dynesty_results['samples']
weights = np.exp(dynesty_results['logwt'])
logl = dynesty_results['logl']
# dynesty_results['logz']
# logL_birth
# label
# beta
# logzero
return NestedSamples(data=data,
weights=weights,
logL=logl,
columns=columns,
tex=tex,
limits=limits)


def from_emcee(emcee_sampler, columns=None, tex=None, limits=None):
"""Convert from emcee to anesthetic samples.

Parameters
----------
emcee_sampler: emcee.ensemble.EnsembleSampler
emcee sampler to copy results from

columns: list, optional
List of (python) parameter names
default: None

tex: list, optional
List of LaTeX parameter names
default: None

limits: list, optional
List of parameter limits
default: None

Returns
-------
mcmc_samples: MCMCSamples
anesthetic MCMC samples
"""
import emcee.ensemble
if not isinstance(emcee_sampler, emcee.ensemble.EnsembleSampler):
raise ValueError("Wrong input type, please pass\
emcee.ensemble.EnsembleSampler")

data = emcee_sampler.flatchain
# weights = 1
# logL
# label
# logzero
# burn_in
return MCMCSamples(data=data,
columns=columns,
tex=tex,
limits=limits)