sbi-dev/sbi

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

Closed this issue · 8 comments

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?

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.

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!

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

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.

Thanks for looking into the details.

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

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?

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.

I agree, thanks for clarifying.