candl.interface

Contents

candl.interface#

Tools to help interface the likelihood code with other software.

Overview:#

Theory Code Interface Functions:

MontePython Interface:

Cobaya Interface:

Cobaya Theory Code Classes:

class candl.interface.CandlCobayaLikelihood(*args: Any, **kwargs: Any)#

Wrapper for a candl likelihood into a cobaya.likelihood.Likelihood class. Based on example likelihood provided by Cobaya (https://cobaya.readthedocs.io/en/latest/likelihoods.html), Torrado and Lewis, 2020 (https://arxiv.org/abs/2005.05290). See the different attributes below that can be set when passing the class to Cobaya.

Attributes:
data_set_filestr

Path of the data set info yaml file.

variantstr

Variant of the likelihood (if requested).

clear_internal_priorsbool

Whether to clear internal priors.

clear_specific_priorsstr or list

String of list of strings of specific priors to clear. If a given parameter appears in an internal prior, the full prior will be cleared (beware for multi-dimensional priors). Doesn’t do anything if clear_internal_priors is True.

lensingbool

Whether to use the lensing likelihood.

feedbackbool

Whether to print feedback when initialising the likelihood.

data_selectionany

Data selection to be used. String, list of string, binary mask, or path to a mask are supported.

candl_likecandl.Like or candl.LensLike

Candl likelihood.

wrapper: any

Set if wanting to access a wrapper likelihood (e.g. clipy).

additional_argsdict

Additional arguments to be passed to the likelihood at initialisation.

Methods

__init__(*args, **kwargs)

initialize()

Called from __init__ to initialise and complete the setup.

get_requirements()

Return dictionary of parameters that are needed

logp(**params)

Calculate the log-likelihood by calling candl.

get_requirements()#

Return dictionary of parameters that are needed

initialize()#

Called from __init__ to initialise and complete the setup. Loads the candl likelihood.

logp(**params)#

Calculate the log-likelihood by calling candl.

class candl.interface.CobayaTheoryBBTemplate(*args: Any, **kwargs: Any)#

Wrapper for a two template BB calculator in a cobaya theory class.

To initialise, pass ‘template_filenames’ in the relevant cobaya block: A dictionary with ‘lensing_B_modes’ and ‘r’ template file names.

This code is taken from the Cobaya example for custom theory codes and only slightly modified. See https://cobaya.readthedocs.io/en/latest/theories_and_dependencies.html. Torrado and Lewis, 2020 (https://arxiv.org/abs/2005.05290).

This is a specified version of CobayaTheoryTemplate.

Attributes:
template_filenamesdict

File names of templates.

providerProvider

Cobaya provider.

current_statedict

Dict containing current parameters and results.

templatesdict

Dict containing cropped BB spectrum templates.

ellsarray (int)

Ell range provided.

Methods

initialize()

Called from __init__ to initialise.

initialize_with_provider(provider)

Initialization after other components initialized, using Provider class instance which is used to return any dependencies (see calculate below).

get_requirements()

Return dictionary of derived parameters or other quantities that are needed by this component and should be calculated by another theory class.

must_provide(**requirements)

Return dictionary of parameters that must be provided.

get_can_provide()

Return list of quantities that can be provided.

calculate(state[, want_derived])

Calculate the CMB spectra.

get_Cl([ell_factor])

Get the Cls or Dls.

calculate(state, want_derived=True, **params_values_dict)#

Calculate the CMB spectra. Sums the two templates with their respective amplitudes.

get_Cl(ell_factor=False, **kwargs)#

Get the Cls or Dls.

get_can_provide()#

Return list of quantities that can be provided.

get_requirements()#

Return dictionary of derived parameters or other quantities that are needed by this component and should be calculated by another theory class.

initialize()#

Called from __init__ to initialise. Loads the templates requested and crops them to the right ell range.

initialize_with_provider(provider)#

Initialization after other components initialized, using Provider class instance which is used to return any dependencies (see calculate below).

must_provide(**requirements)#

Return dictionary of parameters that must be provided.

class candl.interface.CobayaTheoryCosmoPower(*args: Any, **kwargs: Any)#

Wraps regular CosmoPower model into a cobaya theory class. See A. Spurio Mancini et al. 2021 for more (https://arxiv.org/abs/2106.03846).

To initialise, pass ‘emulator_filenames’ in the relevant cobaya block: A dictionary of spectrum type (TT, TE, …) and file names of corresponding emulator models.

This code is taken from the Cobaya example for custom theory codes and only slightly modified. See https://cobaya.readthedocs.io/en/latest/theories_and_dependencies.html. Torrado and Lewis, 2020 (https://arxiv.org/abs/2005.05290)

Attributes:
emulator_filenamesdict

File names of emulators.

cp_emulatorsdict

CosmoPower emulators.

cp_parslist

List of parameters required by the emulators.

providerProvider

Cobaya provider.

current_statedict

Dict containing current parameters and results.

Methods

initialize()

Called from __init__ to initialise.

initialize_with_provider(provider)

Initialization after other components initialized, using Provider class instance which is used to return any dependencies (see calculate below).

get_requirements()

Return dictionary of derived parameters or other quantities that are needed by this component and should be calculated by another theory class.

must_provide(**requirements)

Return dictionary of parameters that must be provided.

get_can_provide()

Return list of quantities that can be provided.

calculate(state[, want_derived])

Calculate the CMB spectra.

get_Cl([ell_factor])

Get the Cls or Dls.

calculate(state, want_derived=True, **params_values_dict)#

Calculate the CMB spectra. Calls the CosmoPower emulator models.

get_Cl(ell_factor=False, **kwargs)#

Get the Cls or Dls.

get_can_provide()#

Return list of quantities that can be provided.

get_requirements()#

Return dictionary of derived parameters or other quantities that are needed by this component and should be calculated by another theory class.

initialize()#

Called from __init__ to initialise. Loads the emulator models.

initialize_with_provider(provider)#

Initialization after other components initialized, using Provider class instance which is used to return any dependencies (see calculate below).

must_provide(**requirements)#

Return dictionary of parameters that must be provided.

class candl.interface.CobayaTheoryCosmoPowerJAX(*args: Any, **kwargs: Any)#

Wraps CosmoPower-JAX model into a cobaya.theory.Theory class. See D. Piras, A. Spurio Mancini 2023 and A. Spurio Mancini et al. 2021 for more (https://arxiv.org/abs/2305.06347, https://arxiv.org/abs/2106.03846).

To initialise, pass ‘emulator_filenames’ in the relevant cobaya block: A dictionary of spectrum types and emulator file names (if they are placed in cosmopower-jax’s trained_models/ folder), or the full path of the files, including in either case the file ending ‘.pkl’ or ‘.npz’.

This code is taken from the Cobaya example for custom theory codes and only slightly modified. See https://cobaya.readthedocs.io/en/latest/theories_and_dependencies.html. Torrado and Lewis, 2020 (https://arxiv.org/abs/2005.05290)

Attributes:
emulator_filenamesdict

File names of emulators.

cp_emulatorsdict

CosmoPower emulators.

cp_parslist

List of parameters required by the emulators.

providerProvider

Cobaya provider.

current_statedict

Dict containing current parameters and results.

Methods

initialize()

Called from __init__ to initialise.

initialize_with_provider(provider)

Initialization after other components initialized, using Provider class instance which is used to return any dependencies (see calculate below).

get_requirements()

Return dictionary of derived parameters or other quantities that are needed by this component and should be calculated by another theory class.

must_provide(**requirements)

Return dictionary of parameters that must be provided.

get_can_provide()

Return list of quantities that can be provided.

calculate(state[, want_derived])

Calculate the CMB spectra.

get_Cl([ell_factor])

Return the Cls or Dls

calculate(state, want_derived=True, **params_values_dict)#

Calculate the CMB spectra. Calls the CosmoPower-JAX emulator models.

get_Cl(ell_factor=False, **kwargs)#

Return the Cls or Dls

get_can_provide()#

Return list of quantities that can be provided.

get_requirements()#

Return dictionary of derived parameters or other quantities that are needed by this component and should be calculated by another theory class.

initialize()#

Called from __init__ to initialise. Loads the emulator models.

Notes

For CosmoPower-JAX emulator models are expected to be placed in the package directory. TE emulators are loaded as PCA+NN, other spectra as NN-only models.

initialize_with_provider(provider)#

Initialization after other components initialized, using Provider class instance which is used to return any dependencies (see calculate below).

must_provide(**requirements)#

Return dictionary of parameters that must be provided.

class candl.interface.CobayaTheoryCosmoPowerJAXLensing(*args: Any, **kwargs: Any)#

Wraps CosmoPower-JAX model for lensing into a cobaya theory code. See D. Piras, A. Spurio Mancini 2023 and A. Spurio Mancini et al. 2021 for more (https://arxiv.org/abs/2305.06347, https://arxiv.org/abs/2106.03846).

To initialise, pass ‘emulator_filenames’ in the relevant cobaya block: A dictionary of spectrum types and emulator file names as placed in cosmopower-jax’s trained_models/ folder.

This code is taken from the Cobaya example for custom theory codes and only slightly modified. See https://cobaya.readthedocs.io/en/latest/theories_and_dependencies.html. Torrado and Lewis, 2020 (https://arxiv.org/abs/2005.05290)

Attributes:
emulator_filenamesdict

File names of emulators.

cp_emulatorsdict

CosmoPower emulators.

cp_parslist

List of parameters required by the emulators.

providerProvider

Cobaya provider.

descriptorstr

A short descriptor.

par_nameslist

Names of parameters involved in transformation.

current_statedict

Dict containing current parameters and results.

Methods

initialize()

Called from __init__ to initialise.

initialize_with_provider(provider)

Initialization after other components initialized, using Provider class instance which is used to return any dependencies (see calculate below).

get_requirements()

Return dictionary of derived parameters or other quantities that are needed by this component and should be calculated by another theory class.

must_provide(**requirements)

Return dictionary of parameters that must be provided.

get_can_provide()

Return list of quantities that can be provided.

calculate(state[, want_derived])

Calculate the CMB spectra.

get_Cl([ell_factor])

Get the Cls or Dls.

calculate(state, want_derived=True, **params_values_dict)#

Calculate the CMB spectra. Calls the CosmoPower-JAX emulator models.

get_Cl(ell_factor=False, **kwargs)#

Get the Cls or Dls.

get_can_provide()#

Return list of quantities that can be provided.

get_requirements()#

Return dictionary of derived parameters or other quantities that are needed by this component and should be calculated by another theory class.

initialize()#

Called from __init__ to initialise. Calls the CosmoPower-JAX emulator models.

initialize_with_provider(provider)#

Initialization after other components initialized, using Provider class instance which is used to return any dependencies (see calculate below).

must_provide(**requirements)#

Return dictionary of parameters that must be provided.

class candl.interface.CobayaTheoryCosmoPowerLensing(*args: Any, **kwargs: Any)#

Wraps CosmoPower model for lensing into a cobaya theory code. See A. Spurio Mancini et al. 2021 for more (https://arxiv.org/abs/2106.03846).

To initialise, pass ‘emulator_filenames’ in the relevant cobaya block: A dictionary of spectrum types and emulator file names as placed in cosmopower-jax’s trained_models/ folder.

This code is taken from the Cobaya example for custom theory codes and only slightly modified. See https://cobaya.readthedocs.io/en/latest/theories_and_dependencies.html. Torrado and Lewis, 2020 (https://arxiv.org/abs/2005.05290)

Attributes:
emulator_filenamesdict

File names of emulators.

cp_emulatorsdict

CosmoPower emulators.

cp_parslist

List of parameters required by the emulators.

providerProvider

Cobaya provider.

descriptorstr

A short descriptor.

par_nameslist

Names of parameters involved in transformation.

current_statedict

Dict containing current parameters and results.

Methods

__init__(*args, **kwargs)

initialize()

Called from __init__ to initialise.

initialize_with_provider(provider)

Initialization after other components initialized, using Provider class instance which is used to return any dependencies (see calculate below).

get_requirements()

Return dictionary of derived parameters or other quantities that are needed by this component and should be calculated by another theory class.

must_provide(**requirements)

Return dictionary of parameters that must be provided.

get_can_provide()

Return list of quantities that can be provided.

calculate(state[, want_derived])

Calculate the CMB spectra.

get_Cl([ell_factor])

Get the Cls or Dls.

calculate(state, want_derived=True, **params_values_dict)#

Calculate the CMB spectra. Calls the CosmoPower emulator models.

get_Cl(ell_factor=False, **kwargs)#

Get the Cls or Dls.

get_can_provide()#

Return list of quantities that can be provided.

get_requirements()#

Return dictionary of derived parameters or other quantities that are needed by this component and should be calculated by another theory class.

initialize()#

Called from __init__ to initialise. Calls the CosmoPower emulator models.

initialize_with_provider(provider)#

Initialization after other components initialized, using Provider class instance which is used to return any dependencies (see calculate below).

must_provide(**requirements)#

Return dictionary of parameters that must be provided.

class candl.interface.CobayaTheoryPyCapse(*args: Any, **kwargs: Any)#

Wraps a capse model into a cobaya theory class through the pycapse interface. See Bonici, Bianchini, Ruiz-Zapatero 2023 for more (https://arxiv.org/abs/2307.14339).

To initialise pass in the relevant Cobaya block: base_path : str

Base path where emulator files are stored.

specs_to_emulatelist

Spectrum type (TT, TE, …) of corresponding emulator models.

This code is taken from the Cobaya example for custom theory codes and only slightly modified. See https://cobaya.readthedocs.io/en/latest/theories_and_dependencies.html. Torrado and Lewis, 2020 (https://arxiv.org/abs/2005.05290)

Attributes:
base_pathstr

Path of the dictionary containing the emulator files.

specs_to_emulatelist

List of spectrum types (from TT, TE, EE, pp) indicating which emulators to load.

pc_emulatorsdict

Pycapse emulators.

pc_parslist

List of parameters required by the emulators. May not match expected parameter order of emulators, as it can be the collection of parameters for a series of emulators requiring different inputs.

pc_pars_to_reg_parslist

Dictionary translating the pycapse parameter names to more commonly used ones.

providerProvider

Cobaya provider.

current_statedict

Dict containing current parameters and results.

Methods

__init__(*args, **kwargs)

initialize()

Called from __init__ to initialise.

initialize_with_provider(provider)

Initialization after other components initialized, using Provider class instance which is used to return any dependencies (see calculate below).

get_requirements()

Return dictionary of derived parameters or other quantities that are needed by this component and should be calculated by another theory class.

must_provide(**requirements)

Return dictionary of parameters that must be provided.

get_can_provide()

Return list of quantities that can be provided.

calculate(state[, want_derived])

Calculate the CMB spectra.

get_Cl([ell_factor])

Get the Cls or Dls.

calculate(state, want_derived=True, **params_values_dict)#

Calculate the CMB spectra. Hands off to pycapse emulators.

get_Cl(ell_factor=False, **kwargs)#

Get the Cls or Dls.

get_can_provide()#

Return list of quantities that can be provided.

get_requirements()#

Return dictionary of derived parameters or other quantities that are needed by this component and should be calculated by another theory class.

initialize()#

Called from __init__ to initialise. Loads the emulator models.

initialize_with_provider(provider)#

Initialization after other components initialized, using Provider class instance which is used to return any dependencies (see calculate below).

must_provide(**requirements)#

Return dictionary of parameters that must be provided.

class candl.interface.CobayaTheoryTemplate(*args: Any, **kwargs: Any)#

Wrapper for a scaled template in a cobaya theory class.

To initialise, pass ‘template_filenames’ in the relevant cobaya block: A string with the template file name. Figures out spectrum types from the indices supplied as ix_in_file. Template is assumed to be in Dls.

This code is taken from the Cobaya example for custom theory codes and only slightly modified. See https://cobaya.readthedocs.io/en/latest/theories_and_dependencies.html. Torrado and Lewis, 2020 (https://arxiv.org/abs/2005.05290)

Attributes:
template_filenamestr

File name of the template.

ix_in_filedict

Indices of the spectrum in the template files, e.g. {“ell”: 0, “TT”: 1, “TE”:2}.

providerProvider

Cobaya provider.

current_statedict

Dict containing current parameters and results.

templatesdict

Template spectra.

ellsarray (int)

Ell range provided.

Methods

initialize()

Called from __init__ to initialise.

initialize_with_provider(provider)

Initialization after other components initialized, using Provider class instance which is used to return any dependencies (see calculate below).

get_requirements()

Return dictionary of derived parameters or other quantities that are needed by this component and should be calculated by another theory class.

must_provide(**requirements)

Return dictionary of parameters that must be provided.

get_can_provide()

Return list of quantities that can be provided.

calculate(state[, want_derived])

Calculate the CMB spectra.

get_Cl([ell_factor])

Get the Cls or Dls.

calculate(state, want_derived=True, **params_values_dict)#

Calculate the CMB spectra. Sums the two templates with their respective amplitudes.

get_Cl(ell_factor=False, **kwargs)#

Get the Cls or Dls.

get_can_provide()#

Return list of quantities that can be provided.

get_requirements()#

Return dictionary of derived parameters or other quantities that are needed by this component and should be calculated by another theory class.

initialize()#

Called from __init__ to initialise. Loads the templates requested and crops them to the right ell range.

initialize_with_provider(provider)#

Initialization after other components initialized, using Provider class instance which is used to return any dependencies (see calculate below).

must_provide(**requirements)#

Return dictionary of parameters that must be provided.

candl.interface.get_CAMB_pars_to_theory_specs_func(CAMB_pars)#

Helper that returns a simple python function that moves from parameters to spectra using CAMB.

Parameters:
CAMB_parscamb.model.CAMBparams

CAMBparams for the model. Defines accuracy, ell range, etc.

Returns:
func

Function that takes a dictionary of parameter values, ell_max, and ell_min (optional) as input and returns a dictionary of CMB spectra (Dl).

candl.interface.get_CLASS_pars_to_theory_specs_func(CLASS_cosmo)#

Helper that returns a simple python function that moves from parameters to spectra using CLASS.

Parameters:
CLASS_cosmoclassy.Class

Class for the model to be evaluated. Need to set desired output options, accuracy settings, ell range, etc.

Returns:
func

Function that takes a dictionary of parameter values, ell_max, and ell_min (optional) as input and returns a dictionary of CMB spectra (Dl).

candl.interface.get_CobayaTheory_pars_to_theory_specs_func(theory_calc)#

Helper that returns a simple python function that moves from parameters to spectra using a cobaya.theory.Theory instances.

Parameters:
theory_calccobaya.theory.Theory

Theory code to calculate theory Dls.

Returns:
func

Function that takes a dictionary of parameter values, ell_max, and ell_min (optional) as input and returns a dictionary of CMB spectra (Dl).

candl.interface.get_CosmoPowerJAX_pars_to_theory_specs_func(emulator_filenames)#

Helper that returns a simple python function that moves from parameters to spectra using CosmoPower. See D. Piras, A. Spurio Mancini 2023 and A. Spurio Mancini et al. 2021 for more (https://arxiv.org/abs/2305.06347, https://arxiv.org/abs/2106.03846). Assumes that all emulators have the same input parameters in the same order.

Parameters:
emulator_filenamesdict

Dictionary of spectrum types and emulator file names (if they are placed in cosmopower-jax’s trained_models/ folder), or the full path of the files, including in either case the file ending ‘.pkl’ or ‘.npz’.

Returns:
func

Function that takes a dictionary of parameter values, ell_max, and ell_min (optional) as input and returns a dictionary of CMB spectra (Dl).

candl.interface.get_CosmoPower_pars_to_theory_specs_func(emulator_filenames)#

Helper that returns a simple python function that moves from parameters to spectra using CosmoPower. See A. Spurio Mancini et al. 2021 for more (https://arxiv.org/abs/2106.03846).

Parameters:
emulator_filenamesdict

Dictionary of spectrum types and emulator file names.

Returns:
func

Function that takes a dictionary of parameter values, ell_max, and ell_min (optional) as input and returns a dictionary of CMB spectra (Dl).

candl.interface.get_PyCapse_pars_to_theory_specs_func(capse_base_path, specs=['TT', 'TE', 'EE'])#

Helper that returns a simple python function that moves from parameters to spectra using PyCapse. See Bonici, Bianchini, Ruiz-Zapatero 2023 for more (https://arxiv.org/abs/2307.14339).

Parameters:
capse_base_pathstr

Path where the PyCapse is located.

specslist (optional)

Which spectra (TT, TE, EE, BB) to try to load.

Returns:
func

Function that takes a dictionary of parameter values, ell_max, and ell_min (optional) as input and returns a dictionary of CMB spectra (Dl).

candl.interface.get_cobaya_info_dict_for_like(like, name='candl_like', data_selection=Ellipsis, clear_internal_priors=True, clear_specific_priors=[], feedback=True, wrapper=None, additional_args={})#

Thin wrapper for CandlCobayaLikelihood that returns the class with the requested data set in the format expected by Cobaya. Note that since Cobaya prefers to instantiate likelihoods itself, this will instantiate a new instance of the likelihood. That means, any modifications to the likelihood object you pass will not be reflected in the likelihood that Cobaya uses. Still, the wrapper will try to copy over any data selection - it’s worth double checking this with the feedback printout. Use the optional keywords to pass arguments to the new Cobaya likelihood object during initialisation. Note that candl internal priors are cleared by default, be sure to either apply these in the Cobaya parameter block or override the default behaviour.

Parameters:
like: candl.Like, candl.LensLike

A candl likelihood to be used.

name: str

Name to give the likelihood in Cobaya

data_selectionany

Data selection to be used. None, string, list of string, binary mask, or path to a mask are supported.

clear_internal_priorsbool

Whether to clear internal priors.

clear_specific_priorsstr or list

String or list of strings of specific priors to clear. If a given parameter appears in an internal prior, the full prior will be cleared (beware for multi-dimensional priors). Doesn’t do anything if clear_internal_priors is True.

feedbackbool

Whether to print feedback when initialising the likelihood.

wrapperstr

Identifier of a supported external wrapper likelihood.

additional_args: dict

Additional arguments to be passed to the likelihood at initialisation.

Returns:
dict:

Dictionary to use in Cobaya’s ‘likelihood’ entry.

candl.interface.get_montepython_nuisance_param_block_for_like(like)#

Prints out info needed by Montepython .param file for nuisance parameters.

Parameters:
like: candl.Like

Likelihood to be used.

Returns:
None