Bindings

class desilike.bindings.base.BaseLikelihoodGenerator(factory, dirname='.')[source]

Bases: BaseClass

Base class to write necessary files for likelihoods to be imported by external inference codes.

Initialize BaseLikelihoodGenerator.

Parameters:
  • factory (callable) – A callable that returns a likelihood object adapted to the external inference code, and takes as input a callable that returns a BaseLikelihood, a dictionary of optional arguments (passed to the latter callable), and the name of the module where it is called. and returns a likelihood object adapted to the external inference code.

  • dirname (str, Path, default='.') – Base directory for the file structure: bindings are saved in dirname/{code}_bindings/, with {code} the directory where factory is defined: ‘cobaya’, ‘cosmosis’, ‘montepython’…

get_code(likelihood, name_like=None, kw_like=None, module=None, fn=None, **kwargs)[source]

Internal method to write code to generate likelihood object to be imported by the external inference code.

Parameters:
  • likelihood (type, callable) – Callable that returns a BaseLikelihood, given some optional arguments (see kw_like).

  • name_like (str, default=None) – Likelihood name, defaults to likelihood name.

  • kw_like (dict, default=None) – Optional arguments for likelihood.

  • module (str, default=None) – Full module name where likelihood is defined. If None, the full module name is searched with find_module_from_file(); if not in a package, absolute path to file where likelihood object is defined will be used to import it in the generated code.

  • fn (Path, str, default=None) – Where to save the likelihood definition for the inference code. It is prefixed by dirname.

  • **kwargs (dict) – Other optional arguments for likelihood factory, e.g. kw_cobaya.

Returns:

cls, name, fn, code – Callable that generates BaseLikelihood, likelihood name, file name where the code is to be written, and code itself.

Return type:

callable, str, str

desilike.bindings.base.find_module_from_file(fn)[source]

Return full module name corresponding to fn. It first checks if there is an ‘__init__.py’ file in the same directory as fn, and if so, it recursively finds the module name of that directory and combines it with the basename of fn.

>>> find_module_from_file('base.py')
'desilike.bindings.base'

If there is no ‘__init__.py’ in the same directory as the given file, it returns None.

desilike.bindings.base.get_likelihood_params(likelihood, derived=False)[source]

Given a BaseLikelihood instance, return its cosmological parameters and its “nuisance” parameters.

desilike.bindings.base.load_from_file(fn, obj)[source]

Load object obj from file fn.