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

Improve tests to detect circular imports and resolve all of them #1357

Merged
merged 9 commits into from
Jan 29, 2025

Conversation

manuelgloeckler
Copy link
Contributor

The previous test to detect circular imports only checked a small subset of imports for main modules like "inference." and similars.

I made this test now much more comprehensive in the sense that it crawls through all Python modules XXX present in SBI (i.e.sbi.neural_nets.estimators) and tests if it is possible to do

import XXX
# Reset the python env (i.e. deletes all sbi-related imports)
from XXX import FIRST_ELEMENT
# Resets the python env

This revealed around 20 previously undetected circular imports, which I will aim to resolve here, if possible.

@manuelgloeckler manuelgloeckler linked an issue Jan 10, 2025 that may be closed by this pull request
@manuelgloeckler manuelgloeckler self-assigned this Jan 10, 2025
@manuelgloeckler
Copy link
Contributor Author

manuelgloeckler commented Jan 13, 2025

So, I removed all circular import errors the test detected (which I think should be all). It sometimes required me to type with "strings" (to avoid an import), for which lines I had to disable ruff (F821) and pyright.

Edit: Not sure why this test is failing, this is not happening locally

@manuelgloeckler manuelgloeckler requested a review from janfb January 13, 2025 14:59
Copy link
Contributor

@janfb janfb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks a lot for fixing this endless circle of imports 🙏

All tests are passing both on local MacOS and remote Ubuntu, so I have no idea what's the matter with the pickling error during parallelization of the serial slice sampler.

tests/circular_import_test.py Outdated Show resolved Hide resolved
tests/circular_import_test.py Outdated Show resolved Hide resolved
@manuelgloeckler
Copy link
Contributor Author

Mhh I fixed the modifications. However also not able to reproduce the error :/

Copy link

codecov bot commented Jan 28, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 78.45%. Comparing base (d3f22b5) to head (b5a35de).
Report is 15 commits behind head on main.

Additional details and impacted files
@@             Coverage Diff             @@
##             main    #1357       +/-   ##
===========================================
- Coverage   89.40%   78.45%   -10.96%     
===========================================
  Files         118      119        +1     
  Lines        8715     8786       +71     
===========================================
- Hits         7792     6893      -899     
- Misses        923     1893      +970     
Flag Coverage Δ
unittests 78.45% <100.00%> (-10.96%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
sbi/inference/posteriors/score_posterior.py 95.52% <100.00%> (-1.41%) ⬇️
sbi/inference/posteriors/vi_posterior.py 82.42% <100.00%> (-9.56%) ⬇️
sbi/inference/potentials/score_based_potential.py 96.96% <100.00%> (-0.05%) ⬇️
sbi/samplers/rejection/__init__.py 100.00% <100.00%> (ø)
sbi/samplers/score/__init__.py 100.00% <100.00%> (ø)
sbi/samplers/score/diffuser.py 84.90% <100.00%> (ø)
sbi/samplers/score/predictors.py 97.43% <ø> (-0.07%) ⬇️
sbi/samplers/vi/__init__.py 100.00% <ø> (ø)
sbi/samplers/vi/vi_divergence_optimizers.py 80.19% <ø> (-6.98%) ⬇️
sbi/utils/restriction_estimator.py 76.31% <100.00%> (-8.59%) ⬇️

... and 41 files with indirect coverage changes

@manuelgloeckler manuelgloeckler requested a review from janfb January 28, 2025 14:07
@manuelgloeckler
Copy link
Contributor Author

I fixed the issue. Also, see #1377 when reviewing. Added a note for that (just implemented it differently now to avoid pickle conflicts).

The test should still work as expected, I did test it by introducing a circular import which it detected.

Copy link
Contributor

@janfb janfb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! Thanks for digging into this and fixing it 🪨

tests/circular_import_test.py Outdated Show resolved Hide resolved
@manuelgloeckler
Copy link
Contributor Author

I guess this seems to be a issue with a new pytorch 2.6 release?

@janfb
Copy link
Contributor

janfb commented Jan 28, 2025

I guess this seems to be a issue with a new pytorch 2.6 release?

Yes, and changing weights_only=False would fix it here, no?

@manuelgloeckler
Copy link
Contributor Author

Yeah, the failing test should be fixed. But not sure about the pytype errors. Moving these to #1380

@manuelgloeckler manuelgloeckler merged commit 6d527f7 into main Jan 29, 2025
6 checks passed
@janfb janfb deleted the 1348-order-of-imports-matters branch January 29, 2025 10:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

order of imports matters?
2 participants