Utilities¶
Provides a collection of functions useful for using/mixing PRISM with other applications.
-
prism.utils.
get_hybrid_lnpost_fn
(lnpost_fn, pipeline_obj, *, emul_i=None, unit_space=False, impl_prior=True, par_dict=False)[source]¶ Returns a function definition
hybrid_lnpost(par_set, *args, **kwargs)
.This hybrid_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 lnpost_fn if it is plausible.
This function needs to be called by all MPI ranks.
Parameters: - lnpost_fn (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
lnpost_fn(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: False) – Bool determining whether or not par_set will be given in unit space.
- impl_prior (bool. Default: True) – Bool determining whether or not the hybrid_lnpost() function should use the implausibility values of a given par_set as an additional prior.
- par_dict (bool. Default: False) – Bool determining whether or not par_set will be an array_like (False) or a dict (True).
Returns: hybrid_lnpost (function) – Definition of the function
hybrid_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.
Note
The input arguments unit_space and par_dict state in what form par_set will be provided to the hybrid_lnpost() function, such that it can be properly converted to the format used in
Pipeline
. The par_set that is passed to lnpost_fn is unchanged.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 hybrid_lnpost() is called.- lnpost_fn (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=False, lnpost_fn=None, **kwargs)[source]¶ Analyzes proposed init_walkers and returns plausible 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 plausibility 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, dict, 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: False) – Bool determining whether or not the provided samples and returned walkers are given in unit space.
- lnpost_fn (function or None. Default: None) – If function, call
get_hybrid_lnpost_fn()
using lnpost_fn and the same values for pipeline_obj, emul_i and unit_space, and return the resulting function definition hybrid_lnpost(). Any additionally provided kwargs are also passed to it.
Returns: - n_walkers (int) – Number of returned MCMC walkers.
- p0_walkers (2D
ndarray
object or dict) – Array containing plausible starting positions of valid MCMC walkers. If init_walkers was provided as a dict, p0_walkers will be a dict. - hybrid_lnpost (function (if lnpost_fn is a function)) – The function returned by
get_hybrid_lnpost_fn()
using lnpost_fn, pipeline_obj, emul_i, unit_space and kwargs as the input values.
See also
get_hybrid_lnpost_fn()
- Returns a function definition
hybrid_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.
-
prism.utils.
get_lnpost_fn
(ext_lnpost, pipeline_obj, *, emul_i=None, unit_space=True, hybrid=True, par_dict=False)[source]¶ Deprecated since version 1.1.3.
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 par_set 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.
- par_dict (bool. Default: False) – Bool determining whether or not par_set will be an array_like (False) or a dict (True).
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.
Note
The input arguments unit_space and par_dict state in what form par_set will be provided to the
get_lnpost()
function, such that it can be properly converted to the format used inPipeline
. The par_set that is passed to theext_lnpost()
function is unchanged.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