Base components

CobayaComponent class

class component.CobayaComponent(info=mappingproxy({}), name=None, timing=None, packages_path=None, initialize=True, standalone=True)

Bases: HasLogger, HasDefaults

Base class for a theory, likelihood or sampler with associated .yaml parameter file that can set attributes.

get_name()

Get the name. This is usually set by the name used in the input .yaml, but otherwise defaults to the fully-qualified class name.

Return type:

str

Returns:

name string

close(*args)

Finalizes the class, if something needs to be cleaned up.

set_instance_defaults()

Can use this to define any default instance attributes before setting to the input dictionary (from inputs or defaults)

initialize()

Initializes the class (called from __init__, before other initializations).

get_version()

Get version information for this component.

Return type:

Union[None, str, Dict[str, Any]]

Returns:

string or dict of values or None

has_version()

Whether to track version information for this component

validate_info(k, value, annotations)

Does any validation on parameter k read from an input dictionary or yaml file, before setting the corresponding class attribute. You could enforce consistency with annotations here, but does not by default.

Parameters:
  • k (str) – name of parameter

  • value (Any) – value

  • annotations (dict) – resolved inherited dictionary of attributes for this class

classmethod get_kind()

Return, as a string, the kind of this component.

classmethod compare_versions(version_a, version_b, equal=True)

Checks whether version_a is equal or higher than version_b.

For strictly higher, pass equal=False (default: True).

Returns:

bool

ComponentCollection class

class component.ComponentCollection

Base class for a dictionary of components (e.g. likelihoods or theories)

get_versions(add_version_field=False)

Get version dictionary :rtype: Dict[str, Any] :return: dictionary of versions for all components

get_speeds(ignore_sub=False)

Get speeds dictionary :rtype: Dict[str, Any] :return: dictionary of versions for all components

Provider class

class theory.Provider(model, requirement_providers)

Class used to retrieve computed requirements. Just passes on get_X and get_param methods to the component assigned to compute them.

For get_param it will also take values directly from the current sampling parameters if the parameter is defined there.

get_param(param)

Returns the value of a derived (or sampled) parameter. If it is not a sampled parameter it calls Theory.get_param() on component assigned to compute this derived parameter.

Parameters:

param (Union[str, Iterable[str]]) – parameter name, or a list of parameter names

Return type:

Union[float, List[float]]

Returns:

value of parameter, or list of parameter values

HasDefaults class

class component.HasDefaults

Base class for components that can read settings from a .yaml file. Class methods provide the methods needed to get the defaults information and associated data.

classmethod get_qualified_class_name()

Get the distinct shortest reference name for the class of the form module.ClassName or module.submodule.ClassName etc. For Cobaya components the name is relative to subpackage for the relevant kind of class (e.g. Likelihood names are relative to cobaya.likelihoods).

For external classes it loads the shortest fully qualified name of the form package.ClassName or package.module.ClassName or package.subpackage.module.ClassName, etc.

Return type:

str

classmethod get_class_path()

Get the file path for the class.

Return type:

str

classmethod get_file_base_name()

Gets the string used as the name for .yaml, .bib files, typically the class name or an un-CamelCased class name

Return type:

str

classmethod get_yaml_file()

Gets the file name of the .yaml file for this component if it exists on file (otherwise None).

Return type:

Optional[str]

get_desc()

Returns a short description of the class. By default, returns the class’ docstring.

You can redefine this method to dynamically generate the description based on the class initialisation info (see e.g. the source code of MCMC’s class method _get_desc()).

classmethod get_bibtex()

Get the content of .bibtex file for this component. If no specific bibtex from this class, it will return the result from an inherited class if that provides bibtex.

Return type:

Optional[str]

classmethod get_associated_file_content(ext, file_root=None)

Return the content of the associated file, if it exists.

This function handles extracting package files when they may be inside a zipped package and thus not directly accessible.

Return type:

Optional[str]

Returns:

The content of the file as a string, if it exists and can be read. None otherwise.

classmethod get_text_file_content(file_name)

Return the content of a file in the directory of the module, if it exists.

Return type:

Optional[str]

classmethod get_class_options(input_options=mappingproxy({}))

Returns dictionary of names and values for class variables that can also be input and output in yaml files, by default it takes all the (non-inherited and non-private) attributes of the class excluding known specials.

Could be overridden using input_options to dynamically generate defaults, e.g. a set of input parameters generated depending on the input_options.

Parameters:

input_options – optional dictionary of input parameters

Return type:

Dict[str, Any]

Returns:

dict of names and values

classmethod get_defaults(return_yaml=False, yaml_expand_defaults=True, input_options=mappingproxy({}))

Return defaults for this component_or_class, with syntax:

option: value
[...]

params:
  [...]  # if required

prior:
  [...]  # if required

If keyword return_yaml is set to True, it returns literally that, whereas if False (default), it returns the corresponding Python dict.

Note that in external components installed as zip_safe=True packages files cannot be accessed directly. In this case using !default .yaml includes currently does not work.

Also note that if you return a dictionary it may be modified (return a deep copy if you want to keep it).

if yaml_expand_defaults then !default: file includes will be expanded

input_options may be a dictionary of input options, e.g. in case default params are dynamically dependent on an input variable