firecrown.utils
Some utility functions for patterns common in Firecrown.
Attributes
Classes
Protocol for classes that can be serialized to and from YAML. |
Functions
|
Create a base model from a yaml string. |
|
Convert a base model to a yaml string. |
Returns the upper triangular indices for an (n x n) matrix. |
|
|
Save a data vector into a (new) SACC object, copied from sacc_data. |
|
Compare two arrays, allowing for either or both to be None. |
|
Compare two objects, allowing for either or both to be None. |
|
Wrapper for pyccl.angular_cl, with automatic caching. |
|
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.
- 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]]