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:
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:
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.combine_fftpars(setA, setB)[source]
Combine two dictionaries of FFT-related parameters using predefined merge rules.
This function merges two parameter dictionaries (
setAandsetB) by iterating over a global list of merge rules (_fft_precision_logic). Each entry in_fft_precision_logicspecifies:a key
kexpected in both dictionaries, anda merge rule
rulewhich is either a callable orNone.
If
ruleis a callable, it is applied to the corresponding entriessetA[k]andsetB[k]to compute the merged value.If
ruleisNoneand the values insetAandsetBdiffer, the value fromsetAis taken as the default and a warning is issued. If the values match, that value is used.- Parameters:
- Returns:
A new dictionary containing the merged FFT parameter values.
- Return type:
- Warns:
UserWarning – If a merge rule is
Noneand the values for a parameter differ betweensetAandsetB. In this case, the value fromsetAis used.