Core

differentiation

class desilike.differentiation.Differentiation(calculator, getter=None, order=1, method=None, accuracy=2, delta_scale=1.0, mpicomm=None)[source]

Bases: BaseClass

Estimate derivatives of calculator quantities, with auto- or finite-differentiation.

Initialize differentiation.

Parameters:
  • calculator (BaseCalculator) – Input calculator.

  • getter (callable, default=None) – Function (without input arguments) that returns a quantity, or a list or dictionary mapping names to quantities from calculator to be differentiated. If None, defaults to derived parameters.

  • order (int, dict, default=1) – A dictionary mapping parameter name (including wildcard) to maximum derivative order. If a single value is provided, applies to all varied parameters.

  • method (str, dict, default=None) – A dictionary mapping parameter name (including wildcard) to method to use to estimate derivatives, either ‘auto’ for automatic differentiation, or ‘finite’ for finite differentiation. If None, ‘auto’ will be used if possible, else ‘finite’. If a single value is provided, applies to all varied parameters.

  • accuracy (int, dict, default=2) – A dictionary mapping parameter name (including wildcard) to derivative accuracy (number of points used to estimate it). If a single value is provided, applies to all varied parameters. Not used if method = 'auto' for this parameter.

  • delta_scale (float, default=1.) – Parameter grid ranges for the estimation of finite derivatives are inferred from parameters’ Parameter.delta. These values are then scaled by delta_scale (< 1. means smaller ranges).

  • mpicomm (mpi.COMM_WORLD, default=None) – MPI communicator. If None, defaults to calculator’s BaseCalculator.mpicomm.

desilike.differentiation.coefficients(order, acc, coords, idx)[source]

Calculate the finite difference coefficients for given derivative order and accuracy order. Assume that the underlying grid is non-uniform.

Adapted from https://github.com/maroba/findiff/blob/master/findiff/coefs.py

Parameters:
  • order (int) – The derivative order (positive integer).

  • acc (int) – The accuracy order (even positive integer).

  • coords (np.ndarray) – The coordinates of the axis for the partial derivative.

  • idx (int) – Index of the grid position where to calculate the coefficients.

Return type:

coeffs, offsets

desilike.differentiation.deriv_grid(grids, current_order=0)[source]

Return grid of points where to compute function to estimate its derivatives.

Parameters:

grids (list) – List of tuples (1D grid coordinates, array of (minimum) derivative orders corresponding to 1D grid, derivative accuracy).

Returns:

grid – List of coordinates.

Return type:

list

desilike.differentiation.deriv_ncoeffs(order, acc=2)[source]

Return number of coefficients given input derivative order and accuracy.

desilike.differentiation.deriv_nd(X, Y, orders, center=None, atol=0.0)[source]

Compute n-dimensional derivative.

Parameters:
  • X (array) – Array of shape (nsamples, ndim), with ndim the number of variables.

  • Y (array) – Array of shape (nsamples, ysize), with ysize the size of the vector to derive.

  • orders (list) – List of tuples (derivation axis between 0 and ndim - 1, derivative order, derivative accuracy).

  • center (array, default=None) – The center around which to take derivatives, of size ndim. If None, defaults to the median of input X.

  • atol (list, float) – Absolute tolerance to find the center.

Returns:

deriv – Derivative of Y, of size ysize.

Return type:

array

fisher

class desilike.fisher.Fisher(likelihood, method=None, accuracy=2, delta_scale=1.0, mpicomm=None)[source]

Bases: BaseClass

Estimate Fisher matrix. If input likelihood is a BaseGaussianLikelihood instance, or a SumLikelihood of such instances, then the Fisher matrix will be computed as:

\[F_{ij} = \frac{\partial \Delta}{\partial p_{i}} C^{-1} \frac{\partial \Delta}{\partial p_{j}}\]

where \(\Delta\) is the model (or data - model), of parameters \(p_{i}\), and \(C^{-1}\) is the data hessian matrix. If input likelihood is not Gaussian, compute the second derivatives of the log-likelihood.

Initialize Fisher estimation.

Parameters:
  • likelihood (BaseLikelihood) – Input likelihood.

  • method (str, dict, default=None) – A dictionary mapping parameter name (including wildcard) to method to use to estimate derivatives, either ‘auto’ for automatic differentiation, or ‘finite’ for finite differentiation. If None, ‘auto’ will be used if possible, else ‘finite’. If a single value is provided, applies to all varied parameters.

  • accuracy (int, dict, default=2) – A dictionary mapping parameter name (including wildcard) to derivative accuracy (number of points used to estimate it). If a single value is provided, applies to all varied parameters. Not used if method = 'auto' for this parameter.

  • delta_scale (float, default=1.) – Parameter grid ranges for the estimation of finite derivatives are inferred from parameters’ Parameter.delta. These values are then scaled by delta_scale (< 1. means smaller ranges).

  • mpicomm (mpi.COMM_WORLD, default=None) – MPI communicator. If None, defaults to likelihood’s BaseLikelihood.mpicomm.

class desilike.fisher.FisherGaussianLikelihood(*args, **kwargs)[source]

Bases: BaseGaussianLikelihood

class desilike.fisher.LikelihoodFisher(center, params=None, gradient=None, offset=None, hessian=None, with_prior=False, attrs=None)[source]

Bases: BaseClass

Class representing a Fisher representation of a likelihood.

Initialize LikelihoodFisher.

Parameters:
  • center (array, list, default=None) – Point where Fisher has been computed.

  • params (list, ParameterCollection) – Parameters corresponding to input hessian.

  • gradient (array, list, ParameterArray, default=None) – Likelihood gradient. Defaults to 0. If ParameterArray, can be used to specify hessian.

  • offset (float, default=None) – Zero-lag log-likelihood. Defaults to 0.

  • hessian (array, default=None) – Hessian, i.e. second-order derivatives of the log-likelihood. If None, taken from ParameterArray gradient.

  • with_prior (bool, default=False) – Whether input gradient, hessian and offset include parameter priors.

  • attrs (dict, default=None) – Optionally, other attributes, stored in attrs.

center(params=None, return_type='nparray')[source]

Return center, restricting to input params if provided.

Parameters:
  • params (list, ParameterCollection, default=None) – If provided, restrict to these parameters.

  • return_type (str, default='nparray') – If ‘nparray’, return a numpy array. Else, return a dictionary mapping parameter names to center values.

Returns:

center

Return type:

array, dict

property chi2min

Minimum \(\chi^{2}\).

choice(index='mean', params=None, return_type='dict', **kwargs)[source]

Return mean or center.

Parameters:
  • index (str, default='mean') – ‘argmax’ or ‘mean’ to return mean(). ‘center’ to return center().

  • params (list, ParameterCollection, default=None) – Parameters to compute mean / center for. Defaults to all parameters.

  • return_type (default='dict') – ‘dict’ to return a dictionary mapping parameter names to mean / center; ‘nparray’ to return an array of parametermean / center.

  • **kwargs (dict) – Optional arguments passed to params() to select params to return, e.g. varied=True, derived=False.

Returns:

toret

Return type:

dict, array

clone(center=None, params=None, gradient=None, offset=None, hessian=None, with_prior=None, attrs=None)[source]

Clone this Fisher i.e. copy and optionally update center, params, gradient, offset, hessian, and attrs.

Parameters:
  • center (array, list, default=None) – Point where Fisher has been computed.

  • params (list, ParameterCollection) – Parameters corresponding to input hessian.

  • gradient (array, list, default=None) – Likelihood gradient. Defaults to 0.

  • offset (float, default=None) – Zero-lag log-likelihood.

  • hessian (array, default=None) – Hessian, i.e. second-order derivatives of the log-likelihood.

  • with_prior (bool, default=None) – Whether input gradient, hessian and offset include parameter priors.

  • attrs (dict, default=None) – Optionally, other attributes, stored in attrs.

Returns:

new – A new Fisher, optionally with center, params, gradient, offset, hessian, and attrs.

Return type:

BaseParameterMatrix

corrcoef(params=None)[source]

Return correlation matrix array (optionally restricted to input parameters).

covariance(params=None, return_type='nparray')[source]

Return inverse precision matrix (covariance matrix) for input parameters params.

Parameters:
  • params (list, ParameterCollection, default=None) – If provided, restrict to these parameters. If a single parameter is provided, this parameter is a scalar, and return_type is ‘nparray’, return a scalar.

  • return_type (str, default=None) – If ‘nparray’, return a numpy array. Else, return a new ParameterCovariance.

Returns:

new

Return type:

array, float, ParameterCovariance

deepcopy()[source]

Deep copy.

mean(params=None, return_type='nparray')[source]

Return likelihood mean, restricting to input params if provided.

Parameters:
  • params (list, ParameterCollection, default=None) – If provided, restrict to these parameters.

  • return_type (str, default='nparray') – If ‘nparray’, return a numpy array. Else, return a dictionary mapping parameter names to mean values.

Returns:

mean

Return type:

array, dict

names(*args, **kwargs)[source]

Return names of parameters.

params(*args, **kwargs)[source]

Return parameters.

precision(params=None, return_type='nparray')[source]

Return inverse covariance matrix (precision matrix) for input parameters params.

Parameters:
  • params (list, ParameterCollection, default=None) – If provided, restrict to these parameters. If a single parameter is provided, this parameter is a scalar, and return_type is ‘nparray’, return a scalar.

  • return_type (str, default=None) – If ‘nparray’, return a numpy array. Else, return a new ParameterPrecision.

Returns:

new

Return type:

array, float, ParameterPrecision

classmethod read_getdist(base_fn, with_prior=True, **kwargs)[source]

Read Fisher from GetDist format.

Parameters:
  • base_fn (str) – Base CosmoMC file name. Will be appended by ‘.margestats’ for marginalized parameter mean, ‘.likestats’ for likelihood maximum and ‘.covmat’ for parameter covariance matrix.

  • with_prior (bool, default=True) – Whether input chains include parameter priors.

  • **kwargs (dict) – If params is None, optional arguments passed to ParameterCollection.select() to select parameters (e.g. varied=True). Restricting to useful parameters is relevant for the numerical accuracy of covariance inversion.

Returns:

fisher

Return type:

FisherLikelihood

select(params=None, **kwargs)[source]

Return a LikelihoodFisher restricting to input params.,

Parameters:
  • params (list, ParameterCollection, default=None) – Optionally, parameters to limit to.

  • **kwargs (dict) – If params is None, optional arguments passed to ParameterCollection.select() to select parameters (e.g. varied=True).

Returns:

new

Return type:

LikelihoodFisher

shift(mean)[source]

Shift such that new mean correspond to input mean.

Parameters:

mean (array) – New mean.

Returns:

fisher

Return type:

LikelihoodFisher

std(params=None)[source]

Return standard deviation (optionally restricted to input parameters). If a single parameter is given as input and this parameter is a scalar, return a scalar. ddof is the number of degrees of freedom.

classmethod sum(*others)[source]

Add Fisher.

to_getdist(params=None, label=None, ignore_limits=True)[source]

Return a GetDist Gaussian distribution, centered on mean(), with covariance matrix covariance().

Parameters:
  • params (list, ParameterCollection, default=None) – Parameters to share to GetDist. Defaults to all parameters.

  • label (str, default=None) – Name for GetDist to use for this distribution.

  • ignore_limits (bool, default=True) – GetDist does not seem to be able to integrate over distribution if bounded; so drop parameter limits.

Returns:

samples

Return type:

getdist.gaussian_mixtures.MixtureND

to_likelihood()[source]

Export Fisher to Gaussian likelihood.

Note

If with_prior, no prior (uniform, infinite distribution) are given to the parameters of output likelihood, to avoid double-counting prior information.

Returns:

likelihood

Return type:

FisherGaussianLikelihood

to_stats(params=None, sigfigs=2, tablefmt='latex_raw', fn=None)[source]

Export Fisher to string.

Parameters:
  • params (list, ParameterCollection, default=None) – If provided, restrict to these parameters.

  • sigfigs (int, default=2) – Number of significant digits. See utils.round_measurement().

  • tablefmt (str, default='latex_raw') – Format for summary table. See tabulate.tabulate().

  • fn (str, default=None) – If not None, file name where to save summary table.

Returns:

txt – Summary table.

Return type:

str

var(params=None)[source]

Return variance (optionally restricted to input parameters). If a single parameter is given as input and this parameter is a scalar, return a scalar. ddof is the number of degrees of freedom.

view(params=None, center=None)[source]

Return Fisher for input parameters params.

Parameters:
  • params (list, ParameterCollection, default=None) – If provided, restrict to these parameters. If a parameter in params is not in Fisher, add it, with zero precision and gradient.

  • center (str, default=None) – Center. Defaults to center().

Returns:

new

Return type:

LikelihoodFisher

class desilike.fisher.PriorCalculator(*args, **kwargs)[source]

Bases: BaseCalculator

get()[source]

Return quantity of main interest, e.g. loglikelihood + logprior if self is a likelihood.

parameter

Classes to handle parameters.

class desilike.parameter.BaseParameterCollection(data=None, attrs=None)[source]

Bases: BaseClass

Base class holding a collection of items identified by parameter.

Initialize BaseParameterCollection.

Parameters:
  • data (list, tuple, str, dict, ParameterCollection) –

    Can be:

  • attrs (dict, default=None) – Optionally, other attributes, stored in attrs.

basenames(**kwargs)[source]

Return base parameter names in collection.

clear()[source]

Empty collection.

clone(*args, **kwargs)[source]

Clone collection, i.e. (shallow) copy and update.

classmethod concatenate(*others)[source]

Concatenate input collections. Unique items only are kept.

deepcopy()[source]

Deep copy.

extend(other)[source]

Extend collection with other. Unique items only are kept.

get(name, *args, **kwargs)[source]

Return item of parameter name name in collection.

Parameters:

name (Parameter, str) – Parameter name. If Parameter instance, search for parameter with same name.

index(name)[source]

Return index of parameter name.

Parameters:

name (Parameter, str, int) – Parameter name. If Parameter instance, search for parameter with same name. If integer, index in collection.

Returns:

index

Return type:

int

items(**kwargs)[source]

Return list of tuples (parameter name, item).

keys(**kwargs)[source]

Return parameter names.

names(**kwargs)[source]

Return parameter names in collection.

params(**kwargs)[source]

Return ParameterCollection, collection of parameters corresponding to items stored in this collection.

pop(name, *args, **kwargs)[source]

Remove and return item indexed by name.

select(**kwargs)[source]

Return new collection, after selection of parameters whose attribute match input values:

collection.select(fixed=True)

returns collection of fixed parameters. If ‘name’ is provided, consider all matching parameters, e.g.:

collection.select(varied=True, name='a_[0:2]')

returns a collection of varied parameters, with name in ['a_0', 'a_1'].

set(item)[source]

Set item in collection. If there is already a parameter with same name in collection, replace this stored item by the input one. Else, append item to collection.

setdefault(item)[source]

Set item in collection if not already in it.

sort(key=None)[source]

Sort (in-place) collection, such that if follows the list of parameter names key. If None, no sorting is performed.

update(*args, **kwargs)[source]

Update collection with new one; arguments can be a BaseParameterCollection or arguments to instantiate such a class (see __init__()).

values(**kwargs)[source]

Return items.

class desilike.parameter.BaseParameterMatrix(value, params=None, attrs=None)[source]

Bases: BaseClass

Initialize BaseParameterMatrix.

Parameters:
  • value (array) – 2D array representing matrix.

  • params (list, ParameterCollection) – Parameters corresponding to input value.

  • attrs (dict, default=None) – Optionally, other attributes, stored in attrs.

classmethod bcast(value, mpicomm=None, mpiroot=0)[source]

Broadcast input samples value from rank mpiroot to other processes.

clone(value=None, params=None, attrs=None)[source]

Clone this matrix, i.e. copy and optionally update value, params and attrs.

Parameters:
  • value (array, default=None) – 2D array to replace matrix value with.

  • params (list, ParameterCollection, default=None) – New parameters.

  • attrs (dict, default=None) – Optionally, other attributes, stored in attrs.

Returns:

new – A new matrix, optionally with value and params updated.

Return type:

BaseParameterMatrix

deepcopy()[source]

Deep copy

det(params=None)[source]

Return matrix determinant, limiting to input parameters params if not None.

names(*args, **kwargs)[source]

Return names of parameters in matrix.

params(*args, **kwargs)[source]

Return parameters in matrix.

select(params=None, **kwargs)[source]

Return a sub-matrix.

Parameters:
  • params (list, ParameterCollection, default=None) – Optionally, parameters to limit to.

  • **kwargs (dict) – If params is None, optional arguments passed to ParameterCollection.select() to select parameters (e.g. varied=True).

Returns:

new – A sub-matrix.

Return type:

BaseParameterMatrix

property shape

Return matrix shape.

view(params=None, return_type=None)[source]

Return matrix for input parameters params.

Parameters:
  • params (list, ParameterCollection, default=None) – If provided, restrict to these parameters. If a single parameter is provided, and this parameter is a scalar, return a scalar. If a parameter in params is not in matrix, add it, filling in the returned matrix with zeros, except on the diagonal, which is filled with _fill_value.

  • return_type (str, default=None) – If ‘nparray’, return a numpy array. Else, return a new BaseParameterMatrix, restricting to params.

Returns:

new

Return type:

array, float, BaseParameterMatrix

class desilike.parameter.Deriv(iterable=None, /, **kwds)[source]

Bases: dict

This class encodes derivative orders. It is a modification of- Counter in https://github.com/python/cpython/blob/main/Lib/collections/__init__.py, restricting to positive elements.

Create a new, empty Deriv object.

>>> c = Deriv()                           # a new, empty derivative object, i.e. zero lag
>>> c = Deriv(['x', Parameter('x'), 'y']) # a new derivative from the list of parameters w.r.t. derivatives are taken
>>> c = Deriv({'x': 2, 'y': 1})           # a new derivative from a mapping: :math:`\partial_{x}^{2} \partial y`
>>> c = Deriv(x=2, y=1)                   # a new derivative from keyword args
elements()[source]

Iterator over derivatives repeating each as many times as its order.

>>> c = Deriv('xxyyzz')
>>> sorted(c.elements())
['x', 'x', 'y', 'y', 'z', 'z']
most_common(n=None)[source]

List the n most common derivatives and their orders from the most common to the least. If n is None, then list all derivative orders.

>>> Deriv(['x', 'x', 'x', 'y', 'y', 'z', 'z', 't']).most_common(3)
[('x', 3), ('y', 2), ('z', 2)]
setdefault(name, item)[source]

Insert key with a value of default if key is not in the dictionary.

Return the value for key if key is in the dictionary, else default.

total()[source]

Total derivative order.

update(iterable=None, /, **kwds)[source]

Like dict.update() but add derivative orders instead of replacing them. Source can be an iterable, a dictionary, or another Deriv instance.

>>> c = Deriv('xy')
>>> c.update('x')               # add derivatives from another iterable
>>> d = Deriv('xy')
>>> c.update(d)
>>> c['x']                      # 3 'x' in 'xy', 'x', 'xy'
3
class desilike.parameter.Parameter(basename, namespace='', value=None, fixed=None, derived=False, prior=None, ref=None, proposal=None, delta=None, latex=None, shape=(), drop=False)[source]

Bases: BaseClass

Class that represents a parameter.

Initialize Parameter.

Parameters:
  • basename (str) – Parameter base name (which defines parameter meaning). If Parameter, update self attributes.

  • namespace (str, default='') – Parameter namespace (to differentiate several occurences of the same parameter in the same pipeline).

  • value (float, default=None) – Default value for parameter.

  • fixed (bool, default=None) – Whether parameter is fixed. If None, defaults to True if prior or ref is not None, else False.

  • derived (bool, str, default=False) – True if parameter is taken from a calulator’s attributes (or BaseCalculator.__getstate__() at run time). ‘.best’, ‘.marg’, or ‘.auto’ to solve for this parameter (given a Gaussian likelihood), respectively taking the best-fit solution, performing analytic marginalization, or choosing between these two options depending on whether a profiler (‘.best’) or a sampler (‘.marg’) is used. When using analytic marginalization, the hessian of the loglikelihood and of the prior is stored (as ‘derived’ parameters); potentially yielding large (in terms of memory space) chains. To circumvent this, you can provide e.g. ‘.auto_not_derived’. ‘.prec’ can be used for linear parameters for which the gradient does not depend on the value of other parameters; in this case, the likelihood’s precision matrix is marginalized over this parameter at initialization, and the parameter is ignored in the profiling / sampling. One can also define the value of this parameter as a function of others (e.g. ‘a’, ‘b’), by providing e.g. the string ‘{a} + {b}’ (or any other operation; numpy is available with ‘np’, scipy with ‘sp’, and their jax version with ‘jnp’ and ‘jsp’).

  • prior (ParameterPrior, dict, default=None) – Prior distribution for parameter, arguments for ParameterPrior.

  • ref (Prior, dict, default=None) – Reference distribution for parameter, arguments for ParameterPrior. This is supposed to represent the expected posterior for this parameter. If None, defaults to prior.

  • proposal (float, default=None) – Proposal uncertainty for parameter. If None, defaults to ref.std().

  • delta (float, tuple, detault=None) – Variation for finite-differentiation, w.r.t. value. If tuple, (variation below value, variation above value), e.g.: (0.1, 0.2), with value = 1, means a variation range (0.9, 1.2).

  • latex (str, default=None) – Latex string for parameter.

  • shape (tuple, default=()) – Parameter shape; typically non-trivial when derived is True.

  • drop (bool, default=False) – If True, this parameter will not be provided to the calculator.

clone(*args, **kwargs)[source]

Clone parameter, i.e. copy and update.

deepcopy()[source]

Deep copy.

property delta

Variation for finite-differentiation; e.g.: (1., 0.1, 0.2), means a variation range (0.9, 1.2). If not specified, defaults to (value, 0.1 * proposal, 0.1 * proposal) (further limited by prior bounds if any).

property derived

If parameter is derived from others ‘a’, ‘b’, return e.g. ‘{a} * {b}’.

eval(**values)[source]

Return parameter value, given all parameter values, e.g. if derived is ‘{a} + {b}’,

>>> param.eval(a=2., b=3.)
5.
property input

Whether parameter should be fed as input to calculator.

latex(namespace=None, inline=False)[source]

Return latex string for parameter if latex is specified (i.e. not None), else name.

Parameters:
  • namespace (bool, str, default=None) – If False, no namespace is added to the latex string. If True, namespace is turned into a latex string, and added as a subscript. If string, add this subscript to the latex string. If None, and none of namespace “words” (defined as group of characters separated by ‘,’, ‘ ‘, ‘_’, ‘-‘) are in the current latex string, then same as True; else, same as False.

  • inline (bool, default=False) – If True, add ‘$’ around the latex string.

Returns:

latex – Latex string.

Return type:

str

property limits

Parameter limits.

property name

Return parameter name, as namespace.basename if namespace is not None, else basename.

property ndim

Parameter dimension, typically non-trivial when derived is True.

property proposal

Proposal uncertainty for parameter; if not specified, defaults to ref.std().

property size

Parameter size, typically non-zero when derived is True.

property solved

Whether parameter is solved, i.e. fixed at best fit or marginalized over.

update(*args, **kwargs)[source]

Update parameter attributes with new arguments kwargs.

property value

Default value for parameter; if not specified, defaults ref.center().

property varied

Whether parameter is varied (i.e. not fixed).

class desilike.parameter.ParameterArray(value, param=None, derivs=None, copy=False, dtype=None, **kwargs)[source]

Bases: NDArrayOperatorsMixin

Initalize ParameterArray.

Parameters:
  • value (array) – Array value.

  • param (Parameter, str, default=None) – Parameter.

  • derivs (list) – List of derivatives (Deriv instances).

  • copy (bool, default=False) – Whether to copy input array.

  • dtype (dtype, default=None) – If provided, enforce this dtype.

  • **kwargs (dict) – Optional arguments for np.array().

property andim

Number of dimensions of array, minus parameter dimensions and derivatives (if any).

property ashape

Array shape, removing parameter shape and derivatives (if any).

clone(**kwargs)[source]

Clone ParameterArray, optionally updating value, param or derivs.

classmethod from_state(state)[source]

Create ParameterArray for state (dictionary).

isin(deriv)[source]

Test if input deriv in array.

property pndim

Number of dimensions of stored parameter, plus 1 if derivatives.

property pshape

Parameter shape, including derivatives along first dimension (if any).

property zero

Return zero-order derivative.

class desilike.parameter.ParameterCollection(data=None, attrs=None)[source]

Bases: BaseParameterCollection

Class holding a collection of parameters. It additionally keeps track whether the collection has been updated, as used in BasePipeline.

Initialize ParameterCollection.

Parameters:
  • data (list, tuple, str, Path, dict, ParameterCollection) –

    Can be:

    • list (or tuple) of parameters (Parameter or dictionary to initialize Parameter)

    • path to yaml defining a list of parameters

    • dictionary mapping name to parameter

    • ParameterCollection instance

  • attrs (dict, default=None) – Optionally, other attributes, stored in attrs.

eval(**params)[source]

Return parameter values, given all parameter values, e.g. if c.derived is ‘{a} + {b}’,

>>> params.eval(a=2., b=3.)
{'a': 2., 'b': 3, 'c': 5}

See Parameter.eval().

params(**kwargs)[source]

Return a collection of parameters ParameterCollection, with optional selection. See select().

prior(**params)[source]

Compute total (log-)prior for input parameter values (except parameters that are solved).

set(item)[source]

Set parameter in collection. If there is already a parameter with same name in collection, replace this stored parameter by the input one. Else, append parameter to collection.

update(*args, name=None, basename=None, **kwargs)[source]

Update collection with new one. To e.g. fix parameters whose name matches the pattern ‘a*’:

>>> params.update(name='a*', fixed=True)
property updated

Whether the collection (the list of parameters itself of any of these parameters) has just been updated.

class desilike.parameter.ParameterCollectionConfig(data=None, identifier='basename', **kwargs)[source]

Bases: BaseParameterCollection

A collection of ParameterConfig objects, used internally by the code. TODO: As ParameterConfig, should be either documented and/or simplified.

Initialize BaseParameterCollection.

Parameters:
  • data (list, tuple, str, dict, ParameterCollection) –

    Can be:

  • attrs (dict, default=None) – Optionally, other attributes, stored in attrs.

select(**kwargs)[source]

Return new collection, after selection of parameters whose attribute match input values:

collection.select(fixed=True)

returns collection of fixed parameters. If ‘name’ is provided, consider all matching parameters, e.g.:

collection.select(varied=True, name='a_[0:2]')

returns a collection of varied parameters, with name in ['a_0', 'a_1'].

set(item)[source]

Set item in collection. If there is already a parameter with same name in collection, replace this stored item by the input one. Else, append item to collection.

update(*args, **kwargs)[source]

Update collection with new one; arguments can be a BaseParameterCollection or arguments to instantiate such a class (see __init__()).

class desilike.parameter.ParameterConfig(conf=None, **kwargs)[source]

Bases: NamespaceDict

A convenient object, used internally by the code, to store configuration for a given parameter.

class desilike.parameter.ParameterCovariance(value, params=None, attrs=None)[source]

Bases: BaseParameterMatrix

Class that represents a parameter covariance matrix.

Initialize BaseParameterMatrix.

Parameters:
  • value (array) – 2D array representing matrix.

  • params (list, ParameterCollection) – Parameters corresponding to input value.

  • attrs (dict, default=None) – Optionally, other attributes, stored in attrs.

corrcoef(params=None)[source]

Return correlation matrix array (optionally restricted to input parameters).

fom(**params)[source]

Figure-of-Merit, as inverse square root of the matrix determinant (optionally restricted to input parameters).

classmethod read_getdist(base_fn)[source]

Read covariance matrix from GetDist format.

Parameters:

base_fn (str) – Base CosmoMC file name. Will be appended by ‘.margestats’ for marginalized parameter mean, ‘.covmat’ for parameter covariance matrix.

Returns:

cov

Return type:

CovarianceMatrix

std(params=None)[source]

Return standard deviation (optionally restricted to input parameters). If a single parameter is given as input and this parameter is a scalar, return a scalar.

to_getdist(params=None, label=None, center=None, ignore_limits=True)[source]

Return a GetDist Gaussian distribution, with covariance matrix cov().

Parameters:
  • params (list, ParameterCollection, default=None) – Parameters to share to GetDist. Defaults to all parameters.

  • label (str, default=None) – Name for GetDist to use for this distribution.

  • center (list, array, default=None) – Optionally, override Parameter.value.

  • ignore_limits (bool, default=True) – GetDist does not seem to be able to integrate over distribution if bounded; so drop parameter limits.

Returns:

samples

Return type:

getdist.gaussian_mixtures.MixtureND

to_precision(params=None, return_type=None)[source]

Return inverse covariance matrix (precision matrix) for input parameters params.

Parameters:
  • params (list, ParameterCollection, default=None) – If provided, restrict to these parameters. If a single parameter is provided, this parameter is a scalar, and return_type is ‘nparray’, return a scalar.

  • return_type (str, default=None) – If ‘nparray’, return a numpy array. Else, return a new ParameterPrecision.

Returns:

new

Return type:

array, float, ParameterPrecision

to_stats(params=None, sigfigs=2, tablefmt='latex_raw', fn=None)[source]

Export covariance matrix to string.

Parameters:
  • params (list, ParameterCollection, default=None) – If provided, restrict to these parameters.

  • sigfigs (int, default=2) – Number of significant digits. See utils.round_measurement().

  • tablefmt (str, default='latex_raw') – Format for summary table. See tabulate.tabulate().

  • fn (str, default=None) – If not None, file name where to save summary table.

Returns:

txt – Summary table.

Return type:

str

var(params=None)[source]

Return variance (optionally restricted to input parameters). If a single parameter is given as input and this parameter is a scalar, return a scalar.

view(params=None, return_type='nparray', fill=None)[source]

Return matrix for input parameters params.

Parameters:
  • params (list, ParameterCollection, default=None) – If provided, restrict to these parameters. If a single parameter is provided, this parameter is a scalar, and return_type is ‘nparray’, return a scalar. If a parameter in params is not in matrix, add it, filling in the returned matrix with zeros, except on the diagonal, which is filled with Parameter.proposal.

  • return_type (str, default='nparray') – If ‘nparray’, return a numpy array. Else, return a new ParameterCovariance, restricting to params.

Returns:

new

Return type:

array, float, ParameterCovariance

exception desilike.parameter.ParameterError[source]

Bases: Exception

Exception raised when issue with ParameterError.

class desilike.parameter.ParameterPrecision(value, params=None, attrs=None)[source]

Bases: BaseParameterMatrix

Initialize BaseParameterMatrix.

Parameters:
  • value (array) – 2D array representing matrix.

  • params (list, ParameterCollection) – Parameters corresponding to input value.

  • attrs (dict, default=None) – Optionally, other attributes, stored in attrs.

fom(**params)[source]

Figure-of-Merit, as square root of the precision (optionally restricted to input parameters).

classmethod sum(*others)[source]

Add precision matrices.

to_covariance(params=None, return_type=None)[source]

Return inverse precision matrix (covariance matrix) for input parameters params.

Parameters:
  • params (list, ParameterCollection, default=None) – If provided, restrict to these parameters. If a single parameter is provided, this parameter is a scalar, and return_type is ‘nparray’, return a scalar.

  • return_type (str, default=None) – If ‘nparray’, return a numpy array. Else, return a new ParameterCovariance.

Returns:

new

Return type:

array, float, ParameterCovariance

class desilike.parameter.ParameterPrior(dist='uniform', limits=None, **kwargs)[source]

Bases: BaseClass

Class that describes a 1D prior distribution. TODO: make immutability explicit.

Parameters:
  • dist (str) – Distribution name.

  • rv (rv_continuous) – Random variate.

  • attrs (dict) – Arguments used to initialize rv.

Initialize ParameterPrior.

Parameters:
  • dist (str) – Distribution name in jax.scipy.stats, and as a fallback, scipy.stats.

  • limits (tuple, default=None) – Tuple corresponding to lower, upper limits. None means \(-\infty\) for lower bound and \(\infty\) for upper bound. Defaults to \(-\infty, +\infty\).

  • kwargs (dict) – Arguments for distribution, typically loc, scale (mean and standard deviation in case of a normal distribution 'dist' == 'norm').

affine_transform(loc=0.0, scale=1.0)[source]

Apply affine transform to the distribution: shifted by loc, and dispersion multiplied by scale. Useful to e.g. normalize a parameter (together with its prior).

is_limited()[source]

Whether distribution has (at least one) finite limit.

is_proper()[source]

Whether distribution is proper, i.e. has finite integral.

isin(x)[source]

Whether x is within prior, i.e. within limits - strictly positive probability.

logpdf(x, remove_zerolag=True)[source]

Return log-probability density at x. If remove_zerolag is True, remove the maximum log-probability density.

sample(size=None, random_state=None)[source]

Draw size samples from prior. Possible only if prior is proper.

Parameters:
Returns:

samples – Samples drawn from prior.

Return type:

float, array

exception desilike.parameter.ParameterPriorError[source]

Bases: Exception

Exception raised when issue with prior.

class desilike.parameter.Samples(data=None, params=None, attrs=None)[source]

Bases: BaseParameterCollection

Class that holds samples, as a collection of ParameterArray.

Initialize Samples.

Parameters:
  • data (list, dict, Samples) –

    Can be:

  • params (list, ParameterCollection) – Optionally, list of parameters.

  • attrs (dict, default=None) – Optionally, other attributes, stored in attrs.

classmethod bcast(value, mpicomm=None, mpiroot=0)[source]

Broadcast input samples value from rank mpiroot to other processes.

classmethod concatenate(*others, intersection=False)[source]

Concatenate input samples, which requires all samples to hold same parameters, except if intersection == True, in which case common parameters are selected.

classmethod load(filename)[source]

Load samples from disk.

match(other, eps=1e-07, params=None)[source]

Match other Samples against self, for parameters params.

Parameters:
  • other (Samples) – Samples to match.

  • eps (float, default=1e-7) – Distance upper bound above which samples are not considered equal. 1e-7 to handle float32/float64 conversions.

  • params (ParameterCollection, list, default=None) – Parameters to use. Defaults to all parameters that are not derived.

Return type:

index_in_other, index_in_self

property ndim

Number of dimensions.

ravel()[source]

Flatten samples.

classmethod recv(source=mpi4py.MPI.ANY_SOURCE, tag=mpi4py.MPI.ANY_TAG, mpicomm=None)[source]

Receive samples from rank source.

reshape(*args)[source]

Reshape samples (with shallow copy).

save(filename)[source]

Save samples to disk.

send(dest, tag=0, mpicomm=None)[source]

Send self to rank dest.

classmethod sendrecv(value, source=0, dest=0, tag=0, mpicomm=None)[source]

Send samples from rank source to rank dest and receive them here.

set(item)[source]

Add new ParameterArray to samples.

property shape

Shape of samples.

property size

Total number of samples.

to_array(params=None, struct=True, derivs=None)[source]

Return samples as numpy array.

Parameters:
  • params (ParameterCollection, list, default=None) – Parameters to use. Defaults to all parameters.

  • struct (bool, default=True) – Whether to return structured array, with columns accessible through e.g. array['x']. If False, numpy will attempt to cast types of different columns.

Returns:

array

Return type:

array

to_dict(params=None)[source]

Return samples as a dictionary.

Parameters:

params (ParameterCollection, list, default=None) – Parameters to use. Defaults to all parameters.

Returns:

dict – Dictionary mapping parameter name to array.

Return type:

dict

update(*args, **kwargs)[source]

Update samples with new one; arguments can be a Samples or arguments to instantiate such a class (see __init__()).

desilike.parameter.decode_name(name, default_start=0, default_stop=None, default_step=1)[source]

Split name into strings and allowed index ranges.

>>> decode_name('a_[-4:5:2]_b_[0:2]')
['a_', '_b_'], [range(-4, 5, 2), range(0, 2, 1)]
Parameters:
  • name (str) – Parameter name, e.g. a_[-4:5:2].

  • default_start (int, default=0) – Range start to use as a default.

  • default_stop (int, default=None) – Range stop to use as a default.

  • default_step (int, default=1) – Range step to use as a default.

Returns:

  • strings (list) – List of strings.

  • ranges (list) – List of ranges.

desilike.parameter.find_names(allnames, name, quiet=True)[source]

Search parameter name name in list of names allnames, matching template forms [::]; return corresponding parameter names. Contrary to find_names_latex(), it does not handle latex strings, but can take a list of parameter names as name (thus returning the concatenated list of matching names in allnames).

>>> find_names(['a_1', 'a_2', 'b_1', 'c_2'], ['a_[:]', 'b_[:]'])
['a_1', 'a_2', 'b_1']
Parameters:
  • allnames (list) – List of parameter names (strings).

  • name (list, str) – List of parameter name(s) to match in allnames.

  • quiet (bool, default=True) – If False and no match for parameter name was found is allnames, raise ParameterError.

Returns:

toret – List of parameter names (strings).

Return type:

list

desilike.parameter.yield_names_latex(name, latex=None, **kwargs)[source]

Yield parameter name and latex strings with template forms [::] replaced.

>>> yield_names_latex('a_[-4:3:2]', latex='\alpha_[-4:5:2]')
a_-4, \alpha_{-4}
a_-2, \alpha_{-2}
a_-0, \alpha_{-0}
a_2, \alpha_{-2}
Parameters:
  • name (str) – Parameter name.

  • latex (str, default=None) – Latex for parameter.

  • kwargs (dict) – Arguments for decode_name()

Returns:

  • name (str) – Parameter name with template forms [::] replaced.

  • latex (str, None) – If input latex is None, None. Else latex string with template forms [::] replaced.

installation

exception desilike.install.InstallError[source]

Bases: Exception

class desilike.install.Installer(install_dir=None, user=False, no_deps=False, force_reinstall=False, ignore_installed=False, **kwargs)[source]

Bases: BaseClass

Installer. desilike’s configuration (‘config.yaml’ and ‘profile.sh’) is saved under ‘DESILIKE_CONFIG_DIR’ environment variable if defined, else ‘~/.desilike’.

Given some calculator one would like to install, the installer is typically used as:

>>> installer = Installer(user=True)
>>> installer(calculator)

To install a profiler (e.g. MinuitProfiler), a sampler (e.g. EmceeSampler), or an emulator (e.g. MLPEmulatorEngine):

>>> installer(MinuitProfiler)
>>> installer(EmceeSampler)
>>> installer(MLPEmulatorEngine)

Initialize installer.

Parameters:
  • install_dir (str, Path, default=None) – Installation directory. Defaults to directory in config_fn if provided, else ‘DESILIKE_INSTALL_DIR’ environment variable if defined, else PIP’s default.

  • user (bool, default=False) – If True, installation directory is home directory.

  • no_deps (bool, default=False) – Does not install package’s dependencies.

  • force_reinstall (bool, default=False) – Force package’s installation.

  • ignore_installed (bool, default=False) – Ignore all (including e.g. package dependencies) previously installed packages.

property config_fn

Path to .yaml configuration file.

data_dir(section=None, ro=False)[source]

Return path to data directory, where one will typically save / install specific calculator data or code.

Parameters:
  • section (str, default=None) – Section; typically this will be calculator’s name.

  • ro (bool, default=None) – Read-only?

Returns:

data_dir – Path to data directory.

Return type:

str

get(*args, **kwargs)[source]

Get config option, e.g. install_dir.

pip(pkgindex, **kwargs)[source]

Install Python package with PIP.

Parameters:
  • pkgindex (str) – Where to find the package. A package name (if registered on pypi), or a url, if on github; e.g. git+https://github.com/cosmodesi/desilike.

  • **kwargs (dict) – Optionally, one can provide no_deps, force_reinstall, ignore_installed to override Installer attributes.

property profile_fn

Path to .sh profile to be sourced to set all paths.

setenv()[source]

Set environment (i.e. set paths). Called in desilike’s __init__.py.

write(config, update=True)[source]

Write configuration to config_fn.

Parameters:
  • config (dict) – Configuration.

  • update (bool, default=True) – If True, insert new ‘pylib_dir’, ‘bin_dir’, ‘dylib_dir’, ‘source’ entries on top of previous ones. If False, such entries are overriden.

desilike.install.download(url, target, size=None)[source]

Download file from input url.

Parameters:
  • url (str, Path) – url to download file from.

  • target (str, Path) – Path where to save the file, on disk.

  • size (int, default=None) – Expected file size, in bytes, used to show progression bar. If not provided, taken from header (if the file is larger than a couple of GBs, it may be wrong due to integer overflow). If a sensible file size is obtained, a progression bar is printed.

desilike.install.exists_package(pkgname)[source]

Check wether package with name pkgname can be imported.

desilike.install.exists_path(path)[source]

Check whether this path exists on disk.

desilike.install.extract(in_fn, out_fn, remove=True)[source]

Extract in_fn to out_fn.

Parameters:
  • in_fn (str, Path) – Path to input, compressed, filename.

  • out_fn (str, Path) – Path to output file / directory.

  • remove (bool, default=True) – If True, remove input file in_fn.

desilike.install.pip(pkgindex, pkgname=None, install_dir=None, no_deps=False, force_reinstall=False, ignore_installed=False)[source]

Install with PIP.

Parameter

pkgindexstr

Where to find the package. A package name (if registered on pypi), or a url, if on github; e.g. git+https://github.com/cosmodesi/desilike.

pkgnamestr, default=None

Package name, to check whether the package is already installed. If None, defaults to pkgindex, or the end of pkgindex, if ‘https://’ is found in it.

install_dirstr, Path, default=None

Installation directory. Defaults to PIP’s default.

no_depsbool, default=False

Does not install package’s dependencies.

force_reinstallbool, default=False

Force package’s installation.

ignore_installedbool, default=False

Ignore all (including e.g. package dependencies) previously installed packages.

desilike.install.source(fn)[source]

Source input file fn and set associated environment variables.