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

sbi.analysis seems broken because np.string_ was removed in the NumPy 2.0 release. #1189

Closed
danielmk opened this issue Jul 9, 2024 · 8 comments · Fixed by #1233
Closed
Labels
blocked Something is in the way of fixing this. Refer to it in the issue bug Something isn't working

Comments

@danielmk
Copy link
Contributor

danielmk commented Jul 9, 2024

Describe the bug
import sbi.analysis causes the following error:
AttributeError: `np.string_` was removed in the NumPy 2.0 release. Use `np.bytes_` instead.

To Reproduce
I just made a fresh sbi environment and pip installed sbi

  1. Name Python version and SBI version
    Python 3.10.14
    sbi 0.22.0
    numpy 2.0.0
  2. Minimal code example
    import sbi.analysis
  3. [Optional]: error message
    AttributeError: np.string_was removed in the NumPy 2.0 release. Usenp.bytes_ instead.

Additional context
I guess this means that sbi (at least the analysis module) is currently not compatible with the recent NumPy release. I could produce a fix or alternatively numpy<2.0.0 could be added to the pyproject file, in case the new numpy release requires some broader changes?

@danielmk danielmk added the bug Something isn't working label Jul 9, 2024
@danielmk
Copy link
Contributor Author

danielmk commented Jul 9, 2024

I just installed the sbi-main branch directly, since I noticed that it differs from the one I got from pip, and sbi.analysis problem exists there too and furthermore, for example import sbi.diagnostics also produces a NumPy 2.0 related error:
AttributeError: `np.obj2sctype` was removed in the NumPy 2.0 release. Use `np.dtype(obj).type` instead.

So I guess the new NumPy version might require some research into the changelog to make sbi compatible with it.

@janfb
Copy link
Contributor

janfb commented Jul 9, 2024

Hi @danielmk thanks for reporting this!

I think we would rather try to adopt the required changes coming with Numpy 2.0 rather than requiring numpy<2.0.0.

If you would be willing to make a PR with the fixes that would be great!

@danielmk
Copy link
Contributor Author

Ok, I will work on a PR according to the SciPy migration guide https://numpy.org/devdocs/numpy_2_0_migration_guide.html

@danielmk
Copy link
Contributor Author

danielmk commented Jul 10, 2024

I just ran ruff according to the migration guide and the sbi code itself appears to be compatible with NumPy 2.0.0, so no changes required there.

The errors I have been getting come from other packages. So far I have seen pytensor and tensorboard show up. Not a lot we can do there. Will just have to stay below numpy 2.0.0 until other packages have caught up.

EDIT: It was Brian2 that installed NumPy 2.0.0 in the first place.

@janfb
Copy link
Contributor

janfb commented Jul 18, 2024

Thanks for looking into the details.

For the upcoming release I suggest we pin numpy<2.0 until the dependencies have fixed the issues.

@janfb
Copy link
Contributor

janfb commented Jul 18, 2024

Double checking, pytensor is still working on an alignment with numpy 2.0, but they made a release this week to pin numpy<2 for now - fixing the problems.

tensorboard is fixed already.

So overall, this seems to be fixed and we do not need to pin numpy<2.0, because it will be pinned indirectly by pytensor via our pymc dependency.

For me from sbi.analysis import sbc_rand_plot worked. Do you have a minimal code example of your initially failing code?

@danielmk
Copy link
Contributor Author

The normal pip installation of sbi installs numpy 1.26.4 and sbi is working in that case. However, when I manually install numpy=2.0, tensorboard and pytensor are still broken (the tensorboard fix you linked is not in the release version yet, I guess).

This is not a big issue, since there is no good reason to upgrade to numpy 2.0 right now. You even have to explicitly go through the conda-forge channel to get it. It was only an issue for me because I installed Brian2, which installed numpy 2.0, which in turn broke sbi for me. My fix was to manually downgrade numpy to 1.26.4 with conda install numpy==1.26.4. Brian2 docs confirm that Brian2 is still backwards compatible with numpy 1.26.

So for me the issue is resolved, but maybe it is worth keeping the issue open until pytensor and tensorboard both have their fixes in the stable release, in case other people run into the same problem.

@janfb
Copy link
Contributor

janfb commented Jul 19, 2024

I agree, thanks for clarifying.

@janfb janfb added the blocked Something is in the way of fixing this. Refer to it in the issue label Jul 22, 2024
@janfb janfb linked a pull request Aug 26, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
blocked Something is in the way of fixing this. Refer to it in the issue bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants