Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

get_parameters_dict() method possibility #1150

Open
nikosarcevic opened this issue Dec 22, 2023 · 10 comments
Open

get_parameters_dict() method possibility #1150

nikosarcevic opened this issue Dec 22, 2023 · 10 comments

Comments

@nikosarcevic
Copy link
Contributor

nikosarcevic commented Dec 22, 2023

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

@hsinfan1996
Copy link
Contributor

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

@fjaviersanchez
Copy link
Collaborator

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

@fjaviersanchez
Copy link
Collaborator

Oh, @hsinfan1996 beat me to it :)

@nikosarcevic
Copy link
Contributor Author

@hsinfan1996 @fjaviersanchez brill! much obliged!!

@tilmantroester
Copy link
Contributor

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

@nikosarcevic
Copy link
Contributor Author

@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)

@tilmantroester
Copy link
Contributor

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.

@nikosarcevic
Copy link
Contributor Author

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.

@tilmantroester
Copy link
Contributor

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.

@arthurmloureiro
Copy link

@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...)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants