firecrown.utils

Some utility functions for patterns common in Firecrown.

Attributes

ST

Classes

YAMLSerializable

Protocol for classes that can be serialized to and from YAML.

Functions

base_model_from_yaml(cls, yaml_str)

Create a base model from a yaml string.

base_model_to_yaml(model)

Convert a base model to a yaml string.

upper_triangle_indices(n)

Returns the upper triangular indices for an (n x n) matrix.

save_to_sacc(sacc_data, data_vector, indices[, strict])

Save a data vector into a (new) SACC object, copied from sacc_data.

compare_optional_arrays(x, y)

Compare two arrays, allowing for either or both to be None.

compare_optionals(x, y)

Compare two objects, allowing for either or both to be None.

cached_angular_cl(cosmo, tracers, ells[, p_of_k_a])

Wrapper for pyccl.angular_cl, with automatic caching.

make_log_interpolator(x, y)

Return a function object that does 1D spline interpolation.

Module Contents

firecrown.utils.ST
class firecrown.utils.YAMLSerializable[source]

Protocol for classes that can be serialized to and from YAML.

to_yaml()[source]

Return the YAML representation of the object.

Return type:

str

classmethod from_yaml(yaml_str)[source]

Load the object from YAML.

Parameters:

yaml_str (str)

Return type:

ST

firecrown.utils.base_model_from_yaml(cls, yaml_str)[source]

Create a base model from a yaml string.

Parameters:
  • cls (type)

  • yaml_str (str)

firecrown.utils.base_model_to_yaml(model)[source]

Convert a base model to a yaml string.

Parameters:

model (pydantic.BaseModel)

Return type:

str

firecrown.utils.upper_triangle_indices(n)[source]

Returns the upper triangular indices for an (n x n) matrix.

generator that yields a sequence of tuples that carry the indices for an (n x n) upper-triangular matrix. This is a replacement for the nested loops:

for i in range(n):
for j in range(i, n):

Parameters:

n (int) – the size of the matrix

Returns:

the generator

Return type:

Generator[tuple[int, int], None, None]

firecrown.utils.save_to_sacc(sacc_data, data_vector, indices, strict=True)[source]

Save a data vector into a (new) SACC object, copied from sacc_data.

Note that the original object sacc_data is not modified. Its contents are copied into a new object, and the new information is put into that copy, which is returned by this method.

If strict is True (the default), then we must overwrite the entire data vector. If strict is False, then we only overwrite the data at the specified indices.

Parameters:
  • sacc_data (sacc.Sacc) – SACC object to be copied. It is not modified.

  • data_vector (numpy.typing.NDArray[numpy.float64]) – Data vector to be saved to the new copy of sacc_data.

  • indices (numpy.typing.NDArray[numpy.int64]) – SACC indices where the data vector should be written.

  • strict (bool) – Whether to check if the data vector covers all the data already present in the sacc_data.

Returns:

A copy of sacc_data, with data at indices replaced with data_vector.

Return type:

sacc.Sacc

firecrown.utils.compare_optional_arrays(x, y)[source]

Compare two arrays, allowing for either or both to be None.

Parameters:
  • x (None | numpy.typing.NDArray) – first array

  • y (None | numpy.typing.NDArray) – second array

Returns:

whether the arrays are equal

Return type:

bool

firecrown.utils.compare_optionals(x, y)[source]

Compare two objects, allowing for either or both to be None.

Parameters:
  • x (None | object) – first object

  • y (None | object) – second object

Returns:

whether the objects are equal

Return type:

bool

firecrown.utils.cached_angular_cl(cosmo, tracers, ells, p_of_k_a=None | Callable[[npt.NDArray[np.int64]], npt.NDArray[np.float64]])[source]

Wrapper for pyccl.angular_cl, with automatic caching.

Parameters:
  • cosmo (pyccl.Cosmology) – the current cosmology

  • tracers (tuple[pyccl.Tracer, pyccl.Tracer]) – tracers indicating the measurements to be correlated

  • ells (numpy.typing.NDArray[numpy.int64]) – ell values at which to calculate the power spectrum

  • p_of_k_a – function that computes the power spectrum

firecrown.utils.make_log_interpolator(x, y)[source]

Return a function object that does 1D spline interpolation.

If all the y values are greater than 0, the function interpolates log(y) as a function of log(x). Otherwise, the function interpolates y as a function of log(x). The resulting interpolater will not extrapolate; if called with an out-of-range argument it will raise a ValueError.

Parameters:
  • x (numpy.typing.NDArray[numpy.int64])

  • y (numpy.typing.NDArray[numpy.float64])

Return type:

Callable[[numpy.typing.NDArray[numpy.int64]], numpy.typing.NDArray[numpy.float64]]