LSSTDESC / CCL

DESC Core Cosmology Library: cosmology routines with validated numerical accuracy
BSD 3-Clause "New" or "Revised" License
133 stars 64 forks source link

Add to_dict utility method #1160

Closed tilmantroester closed 3 months ago

tilmantroester commented 4 months ago

This adds a Cosmology.to_dict method and addresses #1159, as well as #1150. It allows round-tripping, even when the arguments are complex types:

    cosmo = ccl.CosmologyVanillaLCDM(
        transfer_function=ccl.emulators.EmulatorPk(),
        matter_power_spectrum=ccl.emulators.CosmicemuMTIIPk(),
        baryonic_effects=ccl.baryons.BaryonsvanDaalen19(),
        mg_parametrization=ccl.modified_gravity.MuSigmaMG()
    )

    assert cosmo == ccl.Cosmology(**cosmo.to_dict())

Currently this dumps everything into the yaml file when calling write_yaml, which can be quite awkward for all the internal data of the emulators. The upside is that the write/read yaml works for round trips but I think throwing an error until we have a cleaner solution for yaml serialisation for complex types might be cleaner.

Now raises an error when trying to use write_yaml with complex types.

coveralls commented 4 months ago

Pull Request Test Coverage Report for Build 8048774551

Details


Totals Coverage Status
Change from base Build 7986188385: 0.02%
Covered Lines: 6547
Relevant Lines: 6716

💛 - Coveralls