Repository: nilearn/nilearn

Issue:
numpy 2 test workflow failures
~See here:~
~https://github.com/nilearn/nilearn/actions/runs/8137823574/job/22237131770?pr=4306#step:6:5109~

~seem to be a more of a pandas 3.0~

~FAILED nilearn/datasets/tests/test_func.py::test_fetch_surf_nki_enhanced~
~FAILED nilearn/plotting/tests/test_matrix_plotting.py::test_show_event_plot~

~Errors:~

~("'DataFrame' object has no attribute '_mgr'")~
~ValueError: Unable to avoid copy while creating an array from given array.~

See [below for an update](https://github.com/nilearn/nilearn/issues/4309#issuecomment-1989166590)


Discussion:
Seems that one of the runs of the numpy2 workflow passed recently on main:

https://github.com/nilearn/nilearn/actions/runs/8168896123

Rerunning some of the failed ones to see...
It still fails from time to time.
It mostly fails though.

Will try to see what combination of versions of our "bleeing-edge" dependencies makes those tests fail.

Also realised that the workflow is set to run in scheduled manner on main, but not with push on main (not a big problem) but it is weird that all the other tests are run when merging a PR on main but not those...
When I last checked, it seemed like the failures had migrated and were not specifically on the two tests linked in the top comment. [As of three days ago](https://github.com/nilearn/nilearn/actions/runs/8208651542/job/22452496683?pr=4313), it now looks like this:

```
test_numpy2: commands[3]> pytest nilearn
ImportError while loading conftest '/home/runner/work/nilearn/nilearn/nilearn/conftest.py'.
nilearn/conftest.py:10: in <module>
    from nilearn import image
nilearn/image/__init__.py:6: in <module>
    from .image import (
nilearn/image/image.py:20: in <module>
    from .. import signal
nilearn/signal.py:13: in <module>
    import pandas as pd
.tox/test_numpy2/lib/python3.12/site-packages/pandas/__init__.py:46: in <module>
    from pandas.core.api import (
.tox/test_numpy2/lib/python3.12/site-packages/pandas/core/api.py:1: in <module>
    from pandas._libs import (
.tox/test_numpy2/lib/python3.12/site-packages/pandas/_libs/__init__.py:18: in <module>
    from pandas._libs.interval import Interval
interval.pyx:1: in init pandas._libs.interval
    ???
E   ValueError: numpy.dtype size changed, may indicate binary incompatibility. Expected 96 from C header, got 88 from PyObject
test_numpy2: exit 4 (1.31 seconds) /home/runner/work/nilearn/nilearn> pytest nilearn pid=2301
```

The [explanations I've found online](https://softhints.com/python-warning-numpy-dtype-size-changed-may-indicate-binary-incompatibility/) suggest that this is because the module pandas is build on different version of numpy. It looks like this is [something pandas is already tracking](https://github.com/pandas-dev/pandas/issues/55519) and it's just a matter of syncing up the releases.

EDIT: And in case we want to track it, [here's the current numpy 2.0 timelines](https://github.com/numpy/numpy/issues/24300#issuecomment-1986815755).
Thanks for tracking this @emdupre !!!

Will update the top message.

Resolving patch:
diff --git a/nilearn/glm/first_level/first_level.py b/nilearn/glm/first_level/first_level.py
--- a/nilearn/glm/first_level/first_level.py
+++ b/nilearn/glm/first_level/first_level.py
@@ -113,7 +113,12 @@ def _yule_walker(x, order):
         r[:, k] += (y[:, np.newaxis, 0:-k] @ y[:, k:, np.newaxis])[:, 0, 0]
     r /= denom * x.shape[-1]
     rt = np.array([toeplitz(rr[:-1]) for rr in r], np.float64)
-    rho = np.linalg.solve(rt, r[:, 1:])
+
+    # extra dimension added to r for compatibility with numpy <2 and >2
+    # see https://numpy.org/devdocs/release/2.0.0-notes.html
+    # section removed-ambiguity-when-broadcasting-in-np-solve
+    rho = np.linalg.solve(rt, r[:, 1:, None])[..., 0]
+
     rho.shape = x.shape[:-1] + (order,)
     return rho
 


Test patch: