CMB-HD / hdlike

CMB-HD Likelihood
BSD 3-Clause "New" or "Revised" License
1 stars 1 forks source link

HDLike auto-install in Cobaya #1

Open JesusTorrado opened 4 weeks ago

JesusTorrado commented 4 weeks ago

Hi,

We are reimplementing the way Cobaya deals with external likelihoods, to gain some new features, such as automatic installation with a simple mention in the input file. I was starting by adding the external likelihoods which are already in https://cobaya.readthedocs.io/en/latest/likelihood_external.html#list-of-external-packages

Among them, there is a reference to HDLike. The installation in this case works, but it throws an error when checking if the class is correctly installed by importing it: it does not find the hd_mock_data package. I have checked in the README that this package is indeed a requisite, and I reckon that since the package is not in PyPI it does not make much sense to add it as a requisite in the package definition.

It would make our life easier, and I think would also be clearer for users, to handle it in a different way: throw a warning at import instead of an error, and and error at instantiation of a likelihood class. This way we can have the cobaya installer pull the data package automatically after installing the main one. Is that something you would consider implementing?

amandamacinnis commented 4 weeks ago

Hi, thank you for getting in touch about this! We are happy to make it easier for anyone to use HDLike.

I've updated the likelihood to just issue a warning when hdlike is imported, and raise an error when initializing the likelihood class, if the hd_mock_data package is not installed. Below, I've pasted an example of what the user will see in this case. Please let me know if this is what you had in mind!

This way we can have the cobaya installer pull the data package automatically after installing the main one.

That sounds great! Is there anything we need to do to make this possible?


Here is the warning that occurs when importinghdlike without the hd_mock_data package installed:

/home/amacinnis/.local/lib/python3.9/site-packages/hdlike/hdlike.py:11: UserWarning: Unable to import the `hd_mock_data` package. The `hd_mock_data` package must be installed in order to use the CMB-HD likelihood.
  warnings.warn(hd_mock_data_warn_msg)

And here is the output of trying to use hdlike with Cobaya, using the example YAML file we provide and running cobaya-run example_cmbhd.yaml --test:

[output] Output to be read-from/written-into folder '.', with prefix 'example_cmbhd'
/home/amacinnis/.local/lib/python3.9/site-packages/hdlike/hdlike.py:11: UserWarning: Unable to import the `hd_mock_data` package. The `hd_mock_data` package must be installed in order to use the CMB-HD likelihood.
  warnings.warn(hd_mock_data_warn_msg)
[camb] `camb` module loaded successfully from /home/amacinnis/.local/lib/python3.9/site-packages/camb
ERROR while initializing the CMB-HD likelihood: Unable to import the `hd_mock_data` package. You must install the `hd_mock_data` package (github.com/CMB-HD/hdMockData) in order to use the CMB-HD likelihood.
[exception handler] ---------------------------------------

Traceback (most recent call last):
  File "/home/amacinnis/.local/bin/cobaya-run", line 8, in <module>
    sys.exit(run_script())
  File "/home/amacinnis/.local/lib/python3.9/site-packages/cobaya/run.py", line 203, in run_script
    run(info, **arguments.__dict__)
  File "/home/amacinnis/.local/lib/python3.9/site-packages/cobaya/run.py", line 132, in run
    with Model(updated_info["params"], updated_info["likelihood"],
  File "/home/amacinnis/.local/lib/python3.9/site-packages/cobaya/model.py", line 247, in __init__
    self.likelihood = LikelihoodCollection(info_likelihood, theory=self.theory,
  File "/home/amacinnis/.local/lib/python3.9/site-packages/cobaya/likelihood.py", line 266, in __init__
    self.add_instance(name, like_class(info, packages_path=packages_path,
  File "/home/amacinnis/.local/lib/python3.9/site-packages/cobaya/likelihood.py", line 89, in __init__
    super().__init__(info, name=name, timing=timing,
  File "/home/amacinnis/.local/lib/python3.9/site-packages/cobaya/theory.py", line 65, in __init__
    super().__init__(info, name=name, timing=timing,
  File "/home/amacinnis/.local/lib/python3.9/site-packages/cobaya/component.py", line 337, in __init__
    self.initialize()
  File "/home/amacinnis/.local/lib/python3.9/site-packages/hdlike/hdlike.py", line 686, in initialize
    self.hd_data = HDData(lmin=self.lmin, lmax=self.lmax, Lmax=self.Lmax,
  File "/home/amacinnis/.local/lib/python3.9/site-packages/hdlike/hdlike.py", line 359, in __init__
    from hd_mock_data import hd_data
ModuleNotFoundError: No module named 'hd_mock_data'
-------------------------------------------------------------
JesusTorrado commented 4 weeks ago

Hi @amandamacinnis

Thanks for the quick answer! Great that you could implement that! I just submitted a PR to allow the Cobaya wrapper to get the data package, so that both packages are installed by cobaya-install.

The new framework will also allow for a richer list of external likelihoods. You can see a preliminary version at https://cobaya.readthedocs.io/en/cosmo_package/likelihood_external_list.html (you need to unfold the "cmb" list at the bottom).

This list includes new proposed aliases for all likelihoods, so that they can be installed just by mention. In your case, we propose cmb.mock_hdlike. Does it sound OK for you? Would you propose an alternative, preserving the cmb.mock_ prefix? NB: you do not need to change anything on your side regarding this. You will be able to use your likelihood as before, and ALSO with the new alias.

The list will also include a 1 line description and a small number (1-2) of links to papers describing the likelihood (independent of the get_bib Cobaya functionality). For HDLike, please let me know if this looks accurate:

    "desc": "Mock CMB-HD likelihood including lensed and delensed TT/TE/EE/BB CMB + lensing spectra from multipoles 30 to 20,000.",
    "url": "https://github.com/CMB-HD/hdlike",
    "biburl": "https://arxiv.org/abs/2309.03021",
amandamacinnis commented 4 weeks ago

Hi, for the alias name, could we use cmb.mock_cmb_CMBHD? Otherwise, everything looks perfect, thank you!