CLMM icon indicating copy to clipboard operation
CLMM copied to clipboard

Decide if there will be an options of combining different cosmology and modeling objects

Open m-aguena opened this issue 5 years ago • 2 comments

Currently, each modeling works only with cosmologies of the same back-end. This issue is to decide if we want to add the possibility of using different cosmology objects with different modeling objects. The current behavior is:

Modeling1.set_cosmo(Cosmology2) -> ValueError

A possible implementation could be:

Modeling1.set_cosmo(Cosmology2) -> ok if possible, else convert Cosmology2 into Cosmology1

m-aguena avatar Dec 02 '20 12:12 m-aguena

A piece of code that could do that would be:

class CLMModeling:

    def _set_cosmo(self, cosmo, CosmoOutput, valid_cosmo):
        r""" Sets the cosmology to the internal cosmology object

        Parameters
        ----------
        cosmo: clmm.Comology object, None
            CLMM Cosmology object. If is None, creates a new instance of CosmoOutput().
        CosmoOutput: clmm.modbackend Cosmology class
            Cosmology Output for the output object.
        valid_cosmo: str, tuple
            Accepted type (or list of accepted types) for cosmo.backend.
        """
        if cosmo:
            if (cosmo.backend not in valid_cosmo) if hasattr(cosmo, 'backend') else True:
                warnings.warn(f'Translating {type(cosmo)} into {CosmoOutput}.')
                self.cosmo = self._import_cosmo(cosmo, CosmoOutput)
            else:
                self.cosmo = cosmo
        else:
            self.cosmo = CosmoOutput()

    def _import_cosmo(self, cosmo, CosmoOutput):
        r""" Translates a cosmology to the internal cosmology object

        Parameters
        ----------
        cosmo: clmm.Comology like
            CLMM Cosmology object
        CosmoOutput: clmm.modbackend Cosmology
            Cosmology Output for the output object

        Returns
        -------
        clmm.Cosmology like
            Cosmology object
        """
        params = {}
        for p in ('H0', 'Omega_dm0', 'Omega_b0', 'Omega_k0'):
            try:
                params[p] = cosmo[p]
            except:
                raise ValueError(f"Cosmology object {cosmo} missing parameter:{p}.")


class CTModeling(CLMModeling):

    def set_cosmo(self, cosmo):
        self._set_cosmo(cosmo, AstroPyCosmology, ('ct', 'nc'))

m-aguena avatar Dec 02 '20 12:12 m-aguena

A version of the code with this implementation can be found here.

m-aguena avatar Dec 03 '20 11:12 m-aguena