Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Circular import from utils #743

Closed
mdmould opened this issue Oct 11, 2022 · 3 comments · Fixed by #994
Closed

Circular import from utils #743

mdmould opened this issue Oct 11, 2022 · 3 comments · Fixed by #994
Assignees
Labels
bug Something isn't working hackathon

Comments

@mdmould
Copy link

mdmould commented Oct 11, 2022

Imports from sbi.neural_nets.* result in a circular import due to the interactions between sbi.utils.__init__.py and sbi.utils.get_nn_models with sbi.neural_nets. E.g., from sbi.neural_nets.flow import build_nsf gives:

>>> from sbi.neural_nets.flow import build_nsf
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/matthew/.pyenv/versions/sbi/lib/python3.8/site-packages/sbi/neural_nets/flow.py", line 14, in <module>
    from sbi.utils.sbiutils import (
  File "/home/matthew/.pyenv/versions/sbi/lib/python3.8/site-packages/sbi/utils/__init__.py", line 4, in <module>
    from sbi.utils.get_nn_models import classifier_nn, likelihood_nn, posterior_nn
  File "/home/matthew/.pyenv/versions/sbi/lib/python3.8/site-packages/sbi/utils/get_nn_models.py", line 14, in <module>
    from sbi.neural_nets.flow import build_made, build_maf, build_nsf
ImportError: cannot import name 'build_made' from partially initialized module 'sbi.neural_nets.flow' (most likely due to a circular import) (/home/matthew/.pyenv/versions/sbi/lib/python3.8/site-packages/sbi/neural_nets/flow.py)

This is because the sbi.neural_nets.* import sbi.utils, in which sbi.utils.get_nn_models then imports sbi.neural_nets.

One can instead use, e.g., from sbi.utils.get_nn_models import build_nsf or import sbi.utils; from sbi.neural_nets.flow import build_nsf, or the documented interface sbi.utils.get_nn_models.posterior_nn. But the first two options are less intuitive and the last obfuscates the parameters of the chosen model.

@michaeldeistler michaeldeistler added the bug Something isn't working label Oct 23, 2022
@janfb janfb added this to the Hackathon 2024 milestone Feb 16, 2024
@jsvetter
Copy link
Contributor

jsvetter commented Mar 5, 2024

TODO for Hackathon:

Circular import

Fix by either

  • Restructuring codebase
  • Directly importing the relevant objects and removing the circular import from __init__.py

@alvorithm
Copy link
Contributor

alvorithm commented Mar 7, 2024

@famura
Copy link
Contributor

famura commented Mar 13, 2024

I plan to have a look at it during the hackathon

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working hackathon
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants