firecrown.utils
Some utility functions for patterns common in Firecrown.
Attributes
Classes
Protocol for classes that can be serialized to and from YAML. |
|
This class defines Cl limber methods. |
|
This class defines Cl integration methods. |
|
Options for angular power spectrum integration. |
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:
collections.abc.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
- class firecrown.utils.ClLimberMethod[source]
Bases:
YAMLSerializable,str,enum.Enum
This class defines Cl limber methods.
- GSL_QAG_QUAD
- GSL_SPLINE
- class firecrown.utils.ClIntegrationMethod[source]
Bases:
YAMLSerializable,str,enum.Enum
This class defines Cl integration methods.
- LIMBER
- FKEM_AUTO
- FKEM_L_LIMBER
- class firecrown.utils.ClIntegrationOptions(/, **data)[source]
Bases:
pydantic.BaseModel
Options for angular power spectrum integration.
- Parameters:
data (Any)
- model_config
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- method: Annotated[ClIntegrationMethod, BeforeValidator(_validate_cl_integration_method)]
- limber_method: Annotated[ClLimberMethod, BeforeValidator(_validate_cl_limber_method)]
- l_limber: int | None = None
- limber_max_error: float | None = None
- fkem_chi_min: float | None = None
- fkem_Nchi: int | None = None
- classmethod serialize_method(value)[source]
Serialize the method parameter.
- Parameters:
value (ClIntegrationMethod)
- Return type:
str
- classmethod serialize_limber_method(value)[source]
Serialize the limber_method parameter.
- Parameters:
value (ClLimberMethod)
- Return type:
str
- firecrown.utils.cached_angular_cl(cosmo, tracers, ells, p_of_k_a=None | Callable[[npt.NDArray[np.int64]], npt.NDArray[np.float64]], p_of_k_a_lin=None | pyccl.Pk2D | str, int_options=None)[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
l_limber – the maximum ell for the non-limber integration
p_of_k_a_lin – function that returns the linear power spectrum
int_options (ClIntegrationOptions | None)
- 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:
collections.abc.Callable[[numpy.typing.NDArray[numpy.int64]], numpy.typing.NDArray[numpy.float64]]