bilby.core.sampler.base_sampler.Sampler

class bilby.core.sampler.base_sampler.Sampler(likelihood, priors, outdir='outdir', label='label', use_ratio=False, plot=False, skip_import_verification=False, injection_parameters=None, meta_data=None, result_class=None, likelihood_benchmark=False, soft_init=False, exit_code=130, npool=1, **kwargs)[source]

Bases: object

A sampler object to aid in setting up an inference run

Parameters:
likelihood: likelihood.Likelihood

A object with a log_l method

priors: bilby.core.prior.PriorDict, dict

Priors to be used in the search. This has attributes for each parameter to be sampled.

external_sampler: str, Sampler, optional

A string containing the module name of the sampler or an instance of this class

outdir: str, optional

Name of the output directory

label: str, optional

Naming scheme of the output files

use_ratio: bool, optional

Switch to set whether or not you want to use the log-likelihood ratio or just the log-likelihood

plot: bool, optional

Switch to set whether or not you want to create traceplots

injection_parameters:

A dictionary of the injection parameters

meta_data:

A dictionary of extra meta data to store in the result

result_class: bilby.core.result.Result, or child of

The result class to use. By default, bilby.core.result.Result is used, but objects which inherit from this class can be given providing additional methods.

soft_init: bool, optional

Switch to enable a soft initialization that prevents the likelihood from being tested before running the sampler. This is relevant when using custom likelihoods that must NOT be initialized on the main thread when using multiprocessing, e.g. when using tensorflow in the likelihood.

**kwargs: dict

Additional keyword arguments

Raises:
TypeError:

If external_sampler is neither a string nor an instance of this class If not all likelihood.parameters have been defined

ImportError:

If the external_sampler string does not refer to a sampler that is installed on this system

AttributeError:

If some of the priors can’t be sampled

Attributes:
likelihood: likelihood.Likelihood

A object with a log_l method

priors: bilby.core.prior.PriorDict

Priors to be used in the search. This has attributes for each parameter to be sampled.

external_sampler: Module

An external module containing an implementation of a sampler.

outdir: str

Name of the output directory

label: str

Naming scheme of the output files

use_ratio: bool

Switch to set whether or not you want to use the log-likelihood ratio or just the log-likelihood

plot: bool

Switch to set whether or not you want to create traceplots

skip_import_verification: bool

Skips the check if the sampler is installed if true. This is only advisable for testing environments

result: bilby.core.result.Result

Container for the results of the sampling run

exit_code: int

System exit code to return on interrupt

kwargs: dict

Dictionary of keyword arguments that can be used in the external sampler

hard_exit: bool

Whether the implemented sampler exits hard (os._exit rather than sys.exit). The latter can be escaped as SystemExit. The former cannot.

sampler_namestr

Name of the sampler. This is used when creating the output directory for the sampler.

abbreviationstr

Abbreviated name of the sampler. Does not have to be specified in child classes. If set to a value other than None, this will be used instead of sampler_name when creating the output directory.

__init__(likelihood, priors, outdir='outdir', label='label', use_ratio=False, plot=False, skip_import_verification=False, injection_parameters=None, meta_data=None, result_class=None, likelihood_benchmark=False, soft_init=False, exit_code=130, npool=1, **kwargs)[source]
__call__(*args, **kwargs)

Call self as a function.

Methods

__init__(likelihood, priors[, outdir, ...])

calc_likelihood_count()

check_draw(theta[, warning])

Checks if the draw will generate an infinite prior or likelihood

get_expected_outputs([outdir, label])

Get lists of the expected outputs directories and files.

get_initial_points_from_prior([npoints])

Method to draw a set of live points from the prior

get_random_draw_from_prior()

Get a random draw from the prior distribution

log_likelihood(theta)

Parameters:

log_prior(theta)

Parameters:

prior_transform(theta)

Prior transform method that is passed into the external sampler.

run_sampler()

A template method to run in subclasses

write_current_state()

write_current_state_and_exit([signum, frame])

Make sure that if a pool of jobs is running only the parent tries to checkpoint and exit.

Attributes

abbreviation

check_point_equiv_kwargs

constraint_parameter_keys

list: List of parameters providing prior constraints

default_kwargs

external_sampler_name

fixed_parameter_keys

list: List of parameter keys that are not being sampled

hard_exit

kwargs

dict: Container for the kwargs.

ndim

int: Number of dimensions of the search parameter space

npool

npool_equiv_kwargs

sampler_name

sampling_seed_equiv_kwargs

sampling_seed_key

Name of keyword argument for setting the sampling for the specific sampler.

search_parameter_keys

list: List of parameter keys that are being sampled

check_draw(theta, warning=True)[source]

Checks if the draw will generate an infinite prior or likelihood

Also catches the output of numpy.nan_to_num.

Parameters:
theta: array_like

Parameter values at which to evaluate likelihood

warning: bool

Whether or not to print a warning

Returns:
bool, cube (nlive,

True if the likelihood and prior are finite, false otherwise

property constraint_parameter_keys

list: List of parameters providing prior constraints

property fixed_parameter_keys

list: List of parameter keys that are not being sampled

classmethod get_expected_outputs(outdir=None, label=None)[source]

Get lists of the expected outputs directories and files.

These are used by bilby_pipe when transferring files via HTCondor. Both can be empty. Defaults to a single directory: "{outdir}/{name}_{label}/", where name is abbreviation if it is defined for the sampler class, otherwise it defaults to sampler_name.

Parameters:
outdirstr

The output directory.

labelstr

The label for the run.

Returns:
list

List of file names.

list

List of directory names.

get_initial_points_from_prior(npoints=1)[source]

Method to draw a set of live points from the prior

This iterates over draws from the prior until all the samples have a finite prior and likelihood (relevant for constrained priors).

Parameters:
npoints: int

The number of values to return

Returns:
unit_cube, parameters, likelihood: tuple of array_like

unit_cube (nlive, ndim) is an array of the prior samples from the unit cube, parameters (nlive, ndim) is the unit_cube array transformed to the target space, while likelihood (nlive) are the likelihood evaluations.

get_random_draw_from_prior()[source]

Get a random draw from the prior distribution

Returns:
draw: array_like

An ndim-length array of values drawn from the prior. Parameters with delta-function (or fixed) priors are not returned

property kwargs

dict: Container for the kwargs. Has more sophisticated logic in subclasses

log_likelihood(theta)[source]
Parameters:
theta: list

List of values for the likelihood parameters

Returns:
float: Log-likelihood or log-likelihood-ratio given the current

likelihood.parameter values

log_prior(theta)[source]
Parameters:
theta: list

List of sampled values on a unit interval

Returns:
float: Joint ln prior probability of theta
property ndim

int: Number of dimensions of the search parameter space

prior_transform(theta)[source]

Prior transform method that is passed into the external sampler.

Parameters:
theta: list

List of sampled values on a unit interval

Returns:
list: Properly rescaled sampled values
run_sampler()[source]

A template method to run in subclasses

sampling_seed_key = None

Name of keyword argument for setting the sampling for the specific sampler. If a specific sampler does not have a sampling seed option, then it should be left as None.

property search_parameter_keys

list: List of parameter keys that are being sampled

write_current_state_and_exit(signum=None, frame=None)[source]

Make sure that if a pool of jobs is running only the parent tries to checkpoint and exit. Only the parent has a ‘pool’ attribute.

For samplers that must hard exit (typically due to non-Python process) use os._exit that cannot be excepted. Other samplers exiting can be caught as a SystemExit.