elephant icon indicating copy to clipboard operation
elephant copied to clipboard

[MAIN] add support for numpy > 2.0

Open Moritz-Alexander-Kern opened this issue 11 months ago • 3 comments

This pull request ensures compatibility with NumPy >2.0.

This adresses #652

Moritz-Alexander-Kern avatar Feb 12 '25 14:02 Moritz-Alexander-Kern

Coverage Status

coverage: 88.303%. remained the same when pulling 36d441010c18137caccebe7a78d8305dc84e797e on INM-6:support_numpy_2 into febc7ce6b30ce35f3a72c40103f3f79a76547f62 on NeuralEnsemble:master.

coveralls avatar Feb 13 '25 04:02 coveralls

A comment regarding the changes, as several are due to the change in the string representation of NumPy types (obtained by the built-in repr). An alternative to avoid having the types in the docstring examples is to set NumPy to use the legacy formatters available, e.g., np.set_printoptions(legacy='1.25'). This affects only the formatting in NumPy and could be set by each module in Elephant.

We can also wrap the pprint to use the np.printoptions context manager locally, avoiding setting a global NumPy formatter. We could add the wrapped version to the utils module. However, this would work only for the examples with explicit pprint statements, not with examples where a variable or return is used directly (e.g., line 48 in elephant/kernels.py).

A third option would be to reconfigure Sphinx and DocTest instantiation to include the formatter in the initialization. The doctest_global_setup directive (details here). This seems the cleanest solution as it only affects the documentation generation and testing, not the imported Elephant code.

kohlerca avatar May 26 '25 09:05 kohlerca

The last changes fix the doctests to work with NumPy 2.0 while keeping the old-style NumPy formatting in the docstrings (NumPy 1.25).

The solution was to create a conftest.py dynamically when running the doctests job in the CI, which sets the formatter behavior for those tests.

This solution was chosen as doctest_global_setup was used only if running doctests as a specific Sphinx build, which is not the case (the CI uses pytest for the doctests). Also, disabled the doctest extension in Sphinx's conf.py to make it explicit that the runner is pytest.

kohlerca avatar Jun 02 '25 16:06 kohlerca