sbi-dev / sbi

Simulation-based inference toolkit
https://sbi-dev.github.io/sbi/
Apache License 2.0
546 stars 133 forks source link

unable to import `posterior_nn` from `sbi.neural_nets` in v0.22.0 #1158

Closed psteinb closed 2 weeks ago

psteinb commented 1 month ago

Describe the bug I'd like to do from sbi.neural_nets import posterior_nn and I get

---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
Cell In[9], line 1
----> 1 from sbi.neural_nets import posterior_nn

ImportError: cannot import name 'posterior_nn' from 'sbi.neural_nets' ([/home/steinb95/development/sbi-dev/tutorial_paper/paper/fig_4_examples/py312/lib64/python3.12/site-packages/sbi/neural_nets/__init__.py](http://localhost:8888/lab/tree/py312/lib64/python3.12/site-packages/sbi/neural_nets/__init__.py))

To Reproduce Please add a minimal code example that reproduces the problem:

  1. python 3.12.3 and sbi 0.22.0
  2. see above

The same happens from the CLI:

$ source py312/bin/activate
$ python -c 'from sbi.neural_nets import posterior_nn'
Traceback (most recent call last):
  File "<string>", line 1, in <module>
ImportError: cannot import name 'posterior_nn' from 'sbi.neural_nets' (/home/steinb95/development/sbi-dev/tutorial_paper/paper/fig_4_examples/py312/lib64/python3.12/site-packages/sbi/neural_nets/__init__.py)

Expected behavior I can import posterior_nn.

Additional context I tried the same with HEAD.

$ git slog -1        
* 3c1e7250 (HEAD -> main, origin/main, origin/HEAD) feat: local c2st metric  (#1109)

and then:

$ python -c 'from sbi.neural_nets import posterior_nn'
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/home/steinb95/development/sbi-dev/vanilla/sbi/neural_nets/__init__.py", line 1, in <module>
    from sbi.neural_nets.classifier import (
  File "/home/steinb95/development/sbi-dev/vanilla/sbi/neural_nets/classifier.py", line 10, in <module>
    from sbi.utils.sbiutils import standardizing_net, z_score_parser
  File "/home/steinb95/development/sbi-dev/vanilla/sbi/utils/__init__.py", line 73, in <module>
    from sbi.utils.get_nn_models import posterior_nn, likelihood_nn, classifier_nn
  File "/home/steinb95/development/sbi-dev/vanilla/sbi/utils/get_nn_models.py", line 9, in <module>
    from sbi.neural_nets import classifier_nn as classifier_nn_moved_to_neural_nets
ImportError: cannot import name 'classifier_nn' from partially initialized module 'sbi.neural_nets' (most likely due to a circular import) (/home/steinb95/development/sbi-dev/vanilla/sbi/neural_nets/__init__.py)

Fun thing, if you run the tests in the same fresh environment of sbi, as in python -m pytest ./tests/embedding_net_test.py they do pass!

psteinb commented 1 month ago

Note for HEAD (i.e. 3c1e7250), the following appears to work:

$ python -c 'from sbi import utils; from sbi.neural_nets import posterior_nn'

and gives no error! So it appears to be again caused by circular imports.

michaeldeistler commented 1 month ago

Hi Peter, thanks for reporting this! On the pypi version it should be from sbi.utils import posterior_nn.

psteinb commented 1 month ago

Totally true and well spotted! I'll go with that for the time being. What I find a bit irritating (if not risky) is that in HEAD the problem of circular imports is hidden due to the way, the ./tests/embedding_net_test.py imports things.

michaeldeistler commented 1 month ago

Fully agreed, we should fix this.

psteinb commented 1 month ago

Note also, the current version of the embedding net tutorial points to the syntax valid in HEAD of sbi (which doesn't work neither in 0.22.0 nor in HEAD). :frowning_face:

michaeldeistler commented 1 month ago

Thanks! See #1159

manuelgloeckler commented 2 weeks ago

packages

Just made a dependency graph using pyreverse :O

Seems like a big source of circular imports is sbi.utils.sbiutils and sbi.utils (and probably many more)

manuelgloeckler commented 2 weeks ago

Fixed by #1179