Cobaya: unit tests of code using cobaya

Use of Cobaya. camb, CLASS, cosmomc, compilers, etc.
Post Reply
Chen Heinrich
Posts: 5
Joined: October 06 2018
Affiliation: Caltech

Cobaya: unit tests of code using cobaya

Post by Chen Heinrich » September 22 2020

Hi Antony and Jesus, I wanted to ask about how to set up unit testing of code that uses Cobaya. It seems that unit testing any code that calls cobaya has the following challenges:

1) Using pytest at least, any code that calls cobaya gets stopped at these lines in cobaya/log.py, in the function `logger_setup`

Code: Select all

if not any(h.stream == sys.stdout for h in logging.root.handlers):
        logging.root.addHandler(handle_stdout)
If these lines are commented out, the test would run fine. However that doesn't seem like a great solution in the long run, for I'm sure the lines are doing something useful. Would you please let me know what your proposed solution would be?

2) When I write a new theory or likelihood class, I can only write a unit test for functionalities that do not need things like `state` and `**params_values_dict`, which depends on other theory modules and how cobaya initializes the sampled parameters. So as a result I cannot test separately ingredients of `calculate` function. Is there anyway to generate a sample `state`, `params_values_dict` and `provider` within cobaya to do the unit test for a theory module? Or would there be pre-written test classes for Theory and Likelihood that users can inherit? Please let us know what's the best way of doing this! Thank you!

Antony Lewis
Posts: 1936
Joined: September 23 2004
Affiliation: University of Sussex
Contact:

Re: Cobaya: unit tests of code using cobaya

Post by Antony Lewis » September 23 2020

There's an example external likelihood using Cobaya at

https://github.com/CobayaSampler/planck_lensing_external

This has a test, which may help serve as an example.

Chen Heinrich
Posts: 5
Joined: October 06 2018
Affiliation: Caltech

Re: Cobaya: unit tests of code using cobaya

Post by Chen Heinrich » October 13 2020

Thank you so much! I just noticed that the lines that were causing problems were dealt with in the latest update in git, so I pulled the latest code and 1) was solved . The example you pointed me to was also helpful for 2). Thank you!

Post Reply