CobayaSampler / cobaya

Code for Bayesian Analysis
http://cobaya.readthedocs.io/en/latest/
Other
128 stars 127 forks source link

Cobaya cannot load info dictionary #383

Closed gcanasherrera closed 2 weeks ago

gcanasherrera commented 1 month ago

Dear @cmbant @JesusTorrado,

I have encountered for the very first time a very weird error when running cobaya in a new machine that never saw before. I pip install cobaya as usual, and it does not complain when I import it. However, when I try to simply run the gaussian example, I get the following error.

ketelmeer [203] > python
Python 3.11.5 (main, Jan  4 2024, 11:01:42) [GCC 13.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cobaya
>>> info = {
...     "likelihood": {
...         "gaussian_mixture": {
...             "means": [0.2, 0],
...             "covs": [[0.1, 0.05],
...                      [0.05, 0.2]],
...             "derived": True}},
...     "params": dict([
...         ("a", {
...             "prior": {"min": -0.5, "max": 3},
...             "latex": r"\alpha"}),
...         ("b", {
...             "prior": {"dist": "norm", "loc": 0, "scale": 1},
...             "ref": 0,
...             "proposal": 0.5,
...             "latex": r"\beta"}),
...         ("derived_a", {
...             "latex": r"\alpha^\prime"}),
...         ("derived_b", {
...             "latex": r"\beta^\prime"})]),
...     "sampler": {
...         "mcmc": None}}
>>> cobaya.run(info)
[exception handler] ---------------------------------------

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/canasherrera/.local/lib/python3.11/site-packages/cobaya/run.py", line 123, in run
    with Model(updated_info["params"], updated_info["likelihood"],
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/canasherrera/.local/lib/python3.11/site-packages/cobaya/model.py", line 247, in __init__
    self.likelihood = LikelihoodCollection(info_likelihood, theory=self.theory,
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/canasherrera/.local/lib/python3.11/site-packages/cobaya/likelihood.py", line 276, in __init__
    self.add_instance(name, like_class(info, packages_path=packages_path,
                            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/canasherrera/.local/lib/python3.11/site-packages/cobaya/likelihood.py", line 94, in __init__
    super().__init__(info, name=name, timing=timing,
  File "/home/canasherrera/.local/lib/python3.11/site-packages/cobaya/theory.py", line 65, in __init__
    super().__init__(info, name=name, timing=timing,
  File "/home/canasherrera/.local/lib/python3.11/site-packages/cobaya/component.py", line 348, in __init__
    self.packages_path = packages_path or resolve_packages_path()
                                          ^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/canasherrera/.local/lib/python3.11/site-packages/cobaya/tools.py", line 1043, in resolve_packages_path
    return os.environ.get(packages_path_env) or load_packages_path_from_config_file()
                                                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/canasherrera/.local/lib/python3.11/site-packages/cobaya/tools.py", line 1004, in load_packages_path_from_config_file
    return load_config_file().get(packages_path_input)
           ^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'get'
-------------------------------------------------------------

I'm running out of possible ideas, any suggestion for trouble-shooting?

cmbant commented 1 month ago

What's in your global config file that it just tried to read?

gcanasherrera commented 1 month ago

In principle, there shouldn't be any global config file. I haven't used cobaya-install to install anything, I literally only did pip install cobaya and tried the gaussian example in the docs.

Debug: True doesn't return any more info. What's the variable I should print within cobaya source code to figure out the config file it's trying to load?

cmbant commented 1 month ago

See load_config_file() source for the file name it constructs.

gcanasherrera commented 1 month ago

See load_config_file() source for the file name it constructs.

So, I've put some prints in the function. Here is the output:

ketelmeer [143] > cobaya-run gaussian.yaml
 2024-09-20 23:03:43,493 [output] Output to be read-from/written-into folder 'chains', with prefix 'gaussian'
 2024-09-20 23:03:43,494 [output] Found existing info files with the requested output prefix: 'chains/gaussian'
 2024-09-20 23:03:43,609 [model] Parameters were assigned as follows:
 2024-09-20 23:03:43,609 [model] - gaussian_mixture:
 2024-09-20 23:03:43,609 [model]      Input:  ['a', 'b']
 2024-09-20 23:03:43,609 [model]      Output: ['derived_a', 'derived_b']
 2024-09-20 23:03:43,609 [gaussian_mixture] Initializing
In load_config_file
get_config_path(): /home/canasherrera/.config/cobaya
packages_path_config_file: config.yaml

This file: /home/canasherrera/.config/cobaya/config.yaml does exist:

ketelmeer [162] > pwd
/home/canasherrera/.config/cobaya
ketelmeer [163] > ls
config.yaml

However, it is empty.

cmbant commented 1 month ago

Not sure how an empty file could result, looks like it must have come from the install() function being called sometime

cmbant commented 2 weeks ago

Still not sure how it happens, but changed to return empty dict in this case, which hopefully leads to more helpful error message https://github.com/CobayaSampler/cobaya/commit/8eb34eb052cf402ea9c0b86b2b61ef4cd39a036e