BaryonForge.utils.misc module

BaryonForge.utils.misc.generate_operator_method(op, reflect=False)[source]

Defines a method for generating simple arithmetic operations for the Profile classes.

The generate_operator_method function dynamically creates methods that can be used to perform arithmetic operations (such as addition, subtraction, multiplication, etc.) on instances of the HaloProfile classes or similar. This function alters the _real() routine in the HaloProfile classes so that the new result is equivalent to computing the _real() of the individual classes and then performing the specified arithmetic operation.

Parameters:
  • op (function) – The arithmetic operation to be applied. It should be one of the Python arithmetic operators like add, mul, sub, pow, truediv, abs, neg, or pos from the operator module.

  • reflect (bool, optional) – If True, the order of the operands is reversed (i.e., the operation is performed with the second operand as the first). Used to handle right-handed/left-handed operations. Default is False.

Returns:

operator_method – A function that defines how the arithmetic operation should be performed on the HaloProfile classes. This function can handle operations with other HaloProfile instances, integers, or floats.

Return type:

function

Examples

To use generate_operator_method for addition, you might do:

>>> from operator import add
>>> add_method = generate_operator_method(add)
>>> profile_sum = add_method(profile1, profile2)

Notes

  • The method returned can handle both unary and binary operations depending on the operator specified.

BaryonForge.utils.misc.destory_Pk(cosmo)[source]

Removes some SwigPyObject from cosmo that are unable to be pickled and therefore caused the multiprocessing to break.

Parameters:

cosmo (object) – A ccl Cosmology object

Returns:

cosmo – The ccl Cosmology object but with some attributes (that are necessarily SwigPyObjects) destoryed in order to allow pickling.

Return type:

object

Notes

  • For efficiency in pipeline, this function should be used on a Cosmology object

only once the main profile/cosmology operations are finished. Otherwise this forces the recalculation of the power spectrum (which may not be an issue depending on your use-case, but something to be aware of).

BaryonForge.utils.misc.build_cosmodict(cosmo)[source]

Generate a dictionary containing a subset of cosmological parameters from a pyccl Cosmology object.

This function extracts key cosmological parameter values from a pyccl Cosmology object and stores them in a dictionary. If the sigma8 parameter is not already computed (NaN), it triggers its computation.

Parameters:

cosmo (pyccl.core.Cosmology) – A pyccl Cosmology object containing the cosmological model parameters.

Returns:

A dictionary with the following keys: - ‘Omega_m’ : float

The total matter density parameter.

  • ’Omega_b’float

    The baryonic matter density parameter.

  • ’sigma8’float

    The normalization of the power spectrum.

  • ’h’float

    The dimensionless Hubble constant.

  • ’n_s’float

    The spectral index of the primordial power spectrum.

  • ’w0’float

    The equation-of-state parameter for dark energy (constant term).

  • ’wa’float

    The equation-of-state parameter for dark energy (time-dependent term).

Return type:

dict

Notes

If sigma8 is not already computed in the Cosmology object, this function invokes cosmo.compute_sigma() to compute and update its value before returning the dictionary.

BaryonForge.utils.misc.safe_Pchip_minimize(x, y)[source]
BaryonForge.utils.misc.combine_fftpars(setA, setB)[source]

Combine two dictionaries of FFT-related parameters using predefined merge rules.

This function merges two parameter dictionaries (setA and setB) by iterating over a global list of merge rules (_fft_precision_logic). Each entry in _fft_precision_logic specifies:

  • a key k expected in both dictionaries, and

  • a merge rule rule which is either a callable or None.

If rule is a callable, it is applied to the corresponding entries setA[k] and setB[k] to compute the merged value.

If rule is None and the values in setA and setB differ, the value from setA is taken as the default and a warning is issued. If the values match, that value is used.

Parameters:
  • setA (dict) – First and second dictionary of FFT parameter values. All keys appearing in _fft_precision_logic must be present.

  • setB (dict) – First and second dictionary of FFT parameter values. All keys appearing in _fft_precision_logic must be present.

Returns:

A new dictionary containing the merged FFT parameter values.

Return type:

dict

Warns:

UserWarning – If a merge rule is None and the values for a parameter differ between setA and setB. In this case, the value from setA is used.