Utilities¶
Provides a collection of functions useful for using/mixing PRISM with other applications.
-
prism.utils.
get_lnpost_fn
(ext_lnpost, pipeline_obj, *, emul_i=None, unit_space=True, hybrid=True)[source]¶ Returns a function definition
get_lnpost(par_set, *args, **kwargs)
.This get_lnpost function can be used to calculate the natural logarithm of the posterior probability, which analyzes a given par_set first in the provided pipeline_obj at iteration emul_i and passes it to the ext_lnpost function if it is plausible.
This function needs to be called by all MPI ranks.
Parameters: - ext_lnpost (function) – Function definition that needs to be called if the provided par_set
is plausible in iteration emul_i of pipeline_obj. The used call
signature is
ext_lnpost(par_set, *args, **kwargs)
. All MPI ranks will call this function unless called within theworker_mode
context manager. - pipeline_obj (
Pipeline
object) – The instance of thePipeline
class that needs to be used for determining the validity of the proposed sampling step.
Other Parameters: - emul_i (int or None. Default: None) – If int, number indicating the requested emulator iteration. If None, the last iteration of the emulator will be used.
- unit_space (bool. Default: True) – Bool determining whether or not the provided sample will be given in unit space.
- hybrid (bool. Default: True) – Bool determining whether or not the get_lnpost function should use the implausibility values of a given par_set as an additional prior.
Returns: get_lnpost (function) – Definition of the function
get_lnpost(par_set, *args, **kwargs)
.See also
get_walkers()
: Analyzes proposed init_walkers and returns valid p0_walkers.worker_mode
: Special context manager within which all code is executed in worker mode.Warning
Calling this function factory will disable all regular logging in pipeline_obj (
do_logging
set to False), in order to avoid having the same message being logged every time get_lnpost is called.- ext_lnpost (function) – Function definition that needs to be called if the provided par_set
is plausible in iteration emul_i of pipeline_obj. The used call
signature is
-
prism.utils.
get_walkers
(pipeline_obj, *, emul_i=None, init_walkers=None, unit_space=True, ext_lnpost=None, **kwargs)[source]¶ Analyzes proposed init_walkers and returns valid p0_walkers.
Analyzes sample set init_walkers in the provided pipeline_obj at iteration emul_i and returns all samples that are plausible to be used as MCMC walkers. The provided samples and returned walkers should be/are given in unit space if unit_space is True.
If init_walkers is None, returns
impl_sam
instead if it is available.This function needs to be called by all MPI ranks.
Parameters: pipeline_obj (
Pipeline
object) – The instance of thePipeline
class that needs to be used for determining the validity of the proposed walkers.Other Parameters: - emul_i (int or None. Default: None) – If int, number indicating the requested emulator iteration. If None, the last iteration of the emulator will be used.
- init_walkers (2D array_like, int or None. Default: None) – Sample set of proposed initial MCMC walker positions. All plausible
samples in init_walkers will be returned.
If int, generate an LHD of provided size and return all plausible
samples.
If None, return
impl_sam
corresponding to iteration emul_i instead. - unit_space (bool. Default: True) – Bool determining whether or not the provided samples and returned walkers are given in unit space.
- ext_lnpost (function or None. Default: None) – If function, call
get_lnpost_fn()
function factory using ext_lnpost and the same values for pipeline_obj, emul_i and unit_space, and return the resulting function definition get_lnpost. Any additionally provided kwargs are also passed to it.
Returns: - n_walkers (int) – Number of returned MCMC walkers.
- p0_walkers (2D
ndarray
object) – Array containing starting positions of valid MCMC walkers. - get_lnpost (function (if ext_lnpost is a function)) – The function returned by
get_lnpost_fn()
function factory using ext_lnpost, pipeline_obj, emul_i, unit_space and kwargs as the input values.
See also
get_lnpost_fn()
: Returns a function definitionget_lnpost(par_set, *args, **kwargs)
.worker_mode
: Special context manager within which all code is executed in worker mode.Notes
If init_walkers is None and emulator iteration emul_i has not been analyzed yet, a
RequestError
will be raised.