CCL icon indicating copy to clipboard operation
CCL copied to clipboard

get_parameters_dict() method possibility

Open nikosarcevic opened this issue 1 year ago • 10 comments

I was wondering, can we have an option to draw a param dict from the cosmology object?

cosmology = ccl.Cosmology(Omega_m=0.3, h=0.67...)

params = cosmology.get_parameters_dict() params = { 'Omega_c': cosmology['Omega_c'], 'Omega_b': cosmology['Omega_b'], 'h': cosmology['h'], 'sigma8': cosmology['sigma8'], 'n_s': cosmology['n_s'], # Add any other relevant parameters here }

I would find this beyond useful as I work with dicts a lot
    

@hsinfan1996

nikosarcevic avatar Dec 22 '23 17:12 nikosarcevic

For now you can retrieve the two private attributes cosmo._params_init_kwargs and cosmo._config_init_kwargs.

hsinfan1996 avatar Dec 22 '23 17:12 hsinfan1996

This kind of exists but it's ocult: If cosmo is a ccl.Cosmology object you can do: cosmo.__dict__['_params_init_kwargs']

fjaviersanchez avatar Dec 22 '23 17:12 fjaviersanchez

Oh, @hsinfan1996 beat me to it :)

fjaviersanchez avatar Dec 22 '23 17:12 fjaviersanchez

@hsinfan1996 @fjaviersanchez brill! much obliged!!

nikosarcevic avatar Dec 22 '23 17:12 nikosarcevic

Cosmology.write_yaml does that internally. We should probably factor that out into a to_dict method or similar.

tilmantroester avatar Dec 22 '23 17:12 tilmantroester

@tilmantroester ah I see you also prefer to have a method rather then calling cosmology._params_init_kwargs

yeah actually makes sense (going back to my initial request)

nikosarcevic avatar Dec 22 '23 17:12 nikosarcevic

You shouldn't use private APIs (eg anything with a leading underscore) outside of CCL, since they can change at any point. Better to add a proper API than hack something together.

tilmantroester avatar Dec 22 '23 17:12 tilmantroester

You shouldn't use private APIs (eg anything with a leading underscore) outside of CCL, since they can change at any point. Better to add a proper API than hack something together.

Yeah totally makes sense, you are right. And this is not some elaborate code that requires 3 days (it's a few min in Python at least) and it is beyond useful to have. I have my own version of it and use it constantly.

nikosarcevic avatar Dec 22 '23 17:12 nikosarcevic

We should look into how to make to_dict and from_dict methods that allow round trip generation of Cosmology objects. That's useful for serialisation (#1159), as well as for the use case where we want the same cosmology setup but with a few parameters changed, e.g. for blinding (@arthurmloureiro). We might be able to factor this functionality out of the __repr__ machinery in https://github.com/LSSTDESC/CCL/blob/master/pyccl/core/repr.py#L103.

tilmantroester avatar Feb 20 '24 11:02 tilmantroester

@tilmantroester @nikosarcevic

I am having precisely that difficulty with the blinding library. I found a very dirty temporary solution that is similar to what has been suggested in this issue: https://github.com/LSSTDESC/Blinding/blob/1920d195ca182f3625d5bab0018dea0db925bffc/src/blinding/smokescreen.py#L156

    def _create_blinded_cosmo(self):
        """
        Creates a blinded cosmology object with the shifts applied.

        FIXME: Unsure this is the best way of doing this but it is similar to what is done in Augur.
        """
        blinded_cosmo_dict = deepcopy(self.cosmo._params_init_kwargs)
        # sometimes we have this extra paramters that can cause problems:
        try:
            del blinded_cosmo_dict['extra_parameters']
        except KeyError:
            pass
        for k in self.__shifts.keys():
            blinded_cosmo_dict[k] = self.__shifts[k]
        blinded_cosmo = ccl.Cosmology(**blinded_cosmo_dict)
        return blinded_cosmo

A safer, more "off the shelf" solution would be great. I am not super happy with the solution I found.

(Note that HORRIBLE, inexcusable try/except which is begging to cause trouble... I couldn't find a better way of doing this...)

arthurmloureiro avatar Feb 20 '24 13:02 arthurmloureiro