Skip to content

Add to_dict utility method#1160

Merged
damonge merged 5 commits intomasterfrom
to_dict
Mar 8, 2024
Merged

Add to_dict utility method#1160
damonge merged 5 commits intomasterfrom
to_dict

Conversation

@tilmantroester
Copy link
Member

@tilmantroester tilmantroester commented Feb 21, 2024

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
Copy link

coveralls commented Feb 21, 2024

Pull Request Test Coverage Report for Build 8048774551

Details

  • 0 of 32 (100.0%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.02%) to 97.484%

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

💛 - Coveralls

Copy link
Collaborator

@damonge damonge left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@damonge damonge merged commit 652cc67 into master Mar 8, 2024
@damonge damonge deleted the to_dict branch March 8, 2024 15:48
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

Successfully merging this pull request may close these issues.

3 participants