Skip to content

Commit f133bea

Browse files
Fix deprecation of setting shape on a numpy array in 2.5+ (#13585)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent ff1092b commit f133bea

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+158
-83
lines changed

doc/changes/dev/13585.other.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix deprecation of setting a shape on an array directly in ``numpy`` 2.5+, by `Mathieu Scheltienne`_.

mne/_fiff/_digitization.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import numpy as np
99

10+
from ..fixes import _reshape_view
1011
from ..utils import Bunch, _check_fname, _validate_type, logger, verbose, warn
1112
from .constants import FIFF, _coord_frame_named
1213
from .tag import read_tag
@@ -335,7 +336,7 @@ def _get_data_as_dict_from_dig(dig, exclude_ref_channel=True):
335336
f"Only single coordinate frame in dig is supported, got {dig_coord_frames}"
336337
)
337338
dig_ch_pos_location = np.array(dig_ch_pos_location)
338-
dig_ch_pos_location.shape = (-1, 3) # empty will be (0, 3)
339+
dig_ch_pos_location = _reshape_view(dig_ch_pos_location, (-1, 3))
339340
return Bunch(
340341
nasion=fids.get("nasion", None),
341342
lpa=fids.get("lpa", None),

mne/_fiff/tag.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import numpy as np
1313
from scipy.sparse import csc_array, csr_array
1414

15+
from ..fixes import _reshape_view
1516
from ..utils import _check_option, warn
1617
from ..utils.numerics import _julian_to_date
1718
from .constants import (
@@ -177,7 +178,7 @@ def _read_matrix(fid, tag, shape, rlims):
177178
data = data.view(">c8")
178179
elif matrix_type == FIFF.FIFFT_COMPLEX_DOUBLE:
179180
data = data.view(">c16")
180-
data.shape = dims
181+
data = _reshape_view(data, dims)
181182
else:
182183
# Find dimensions and return to the beginning of tag data
183184
ndim = int(np.frombuffer(fid.read(4), dtype=">i4").item())

mne/beamformer/_rap_music.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from scipy import linalg
99

1010
from .._fiff.pick import pick_channels_forward, pick_info
11-
from ..fixes import _safe_svd
11+
from ..fixes import _reshape_view, _safe_svd
1212
from ..forward import convert_forward_solution, is_fixed_orient
1313
from ..inverse_sparse.mxne_inverse import _make_dipoles_sparse
1414
from ..minimum_norm.inverse import _log_exp_var
@@ -68,9 +68,9 @@ def _apply_rap_music(
6868
phi_sig = eig_vectors[:, -n_dipoles:]
6969

7070
n_orient = 3 if is_free_ori else 1
71-
G.shape = (G.shape[0], -1, n_orient)
71+
G = _reshape_view(G, (G.shape[0], -1, n_orient))
7272
gain = forward["sol"]["data"].copy()
73-
gain.shape = G.shape
73+
gain = _reshape_view(gain, G.shape)
7474
n_channels = G.shape[0]
7575
A = np.empty((n_channels, n_dipoles))
7676
gain_dip = np.empty((n_channels, n_dipoles))
@@ -122,7 +122,7 @@ def _apply_rap_music(
122122
sol = linalg.lstsq(A, M)[0]
123123
if n_orient == 3:
124124
X = sol[:, np.newaxis] * oris[:, :, np.newaxis]
125-
X.shape = (-1, len(times))
125+
X = _reshape_view(X, (-1, len(times)))
126126
else:
127127
X = sol
128128

mne/beamformer/tests/test_dics.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
from mne.beamformer._dics import _prepare_noise_csd
2626
from mne.beamformer.tests.test_lcmv import _assert_weight_norm
2727
from mne.datasets import testing
28+
from mne.fixes import _reshape_view
2829
from mne.io import read_info
2930
from mne.proj import compute_proj_evoked, make_projector
3031
from mne.surface import _compute_nearest
@@ -269,7 +270,7 @@ def test_make_dics(tmp_path, _load_forward, idx, whiten):
269270
exp=None,
270271
noise_cov=noise_cov,
271272
)
272-
G.shape = (n_channels, n_verts, n_orient)
273+
G = _reshape_view(G, (n_channels, n_verts, n_orient))
273274
G = G.transpose(1, 2, 0).conj() # verts, orient, ch
274275
_assert_weight_norm(filters, G)
275276

mne/beamformer/tests/test_external.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from mne.beamformer import apply_lcmv, apply_lcmv_cov, make_lcmv
1212
from mne.beamformer.tests.test_lcmv import _get_data
1313
from mne.datasets import testing
14+
from mne.fixes import _reshape_view
1415

1516
data_path = testing.data_path(download=False)
1617
ft_data_path = data_path / "fieldtrip" / "beamformer"
@@ -98,7 +99,7 @@ def test_lcmv_fieldtrip(_get_bf_data, bf_type, weight_norm, pick_ori, pwr):
9899
ft_fname = ft_data_path / ("ft_source_" + bf_type + "-vol.mat")
99100
stc_ft_data = pymatreader.read_mat(ft_fname)["stc"]
100101
if stc_ft_data.ndim == 1:
101-
stc_ft_data.shape = (stc_ft_data.size, 1)
102+
stc_ft_data = _reshape_view(stc_ft_data, (stc_ft_data.size, 1))
102103

103104
if stc_mne.data.ndim == 2:
104105
signs = np.sign((stc_mne.data * stc_ft_data).sum(-1, keepdims=True))

mne/beamformer/tests/test_lcmv.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
)
4343
from mne.beamformer._compute_beamformer import _prepare_beamformer_input
4444
from mne.datasets import testing
45+
from mne.fixes import _reshape_view
4546
from mne.minimum_norm import apply_inverse, make_inverse_operator
4647
from mne.minimum_norm.tests.test_inverse import _assert_free_ori_match
4748
from mne.simulation import simulate_evoked
@@ -1185,7 +1186,7 @@ def test_unit_noise_gain_formula(pick_ori, weight_norm, reg, inversion):
11851186
)
11861187
n_channels, n_sources = G.shape
11871188
n_sources //= 3
1188-
G.shape = (n_channels, n_sources, 3)
1189+
G = _reshape_view(G, (n_channels, n_sources, 3))
11891190
G = G.transpose(1, 2, 0) # verts, orient, ch
11901191
_assert_weight_norm(filters, G)
11911192

mne/channels/montage.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
from .._fiff.pick import _picks_to_idx, channel_type, pick_types
2929
from .._freesurfer import get_mni_fiducials
3030
from ..defaults import HEAD_SIZE_DEFAULT
31+
from ..fixes import _reshape_view
3132
from ..transforms import (
3233
Transform,
3334
_ensure_trans,
@@ -973,9 +974,9 @@ def read_dig_hpts(fname, unit="mm"):
973974
label[ii]: this_xyz for ii, this_xyz in enumerate(xyz) if kind[ii] == "eeg"
974975
}
975976
hpi = np.array([this_xyz for ii, this_xyz in enumerate(xyz) if kind[ii] == "hpi"])
976-
hpi.shape = (-1, 3) # in case it's empty
977+
hpi = _reshape_view(hpi, (-1, 3)) # in case it's empty
977978
hsp = np.array([this_xyz for ii, this_xyz in enumerate(xyz) if kind[ii] == "extra"])
978-
hsp.shape = (-1, 3) # in case it's empty
979+
hsp = _reshape_view(hsp, (-1, 3)) # in case it's empty
979980
return make_dig_montage(ch_pos=ch_pos, **fid, hpi=hpi, hsp=hsp)
980981

981982

mne/chpi.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
from .cov import compute_whitener, make_ad_hoc_cov
4444
from .dipole import _make_guesses
4545
from .event import find_events
46-
from .fixes import jit
46+
from .fixes import _reshape_view, jit
4747
from .forward import _concatenate_coils, _create_meg_coils, _magnetic_dipole_field_vec
4848
from .io import BaseRaw, RawArray
4949
from .io.ctf.trans import _make_ctf_coord_trans_set
@@ -117,7 +117,7 @@ def read_head_pos(fname):
117117
"""
118118
_check_fname(fname, must_exist=True, overwrite="read")
119119
data = np.loadtxt(fname, skiprows=1) # first line is header, skip it
120-
data.shape = (-1, 10) # ensure it's the right size even if empty
120+
data = _reshape_view(data, (-1, 10)) # ensure it's the right size even if empty
121121
if np.isnan(data).any(): # make sure we didn't do something dumb
122122
raise RuntimeError(f"positions could not be read properly from {fname}")
123123
return data
@@ -1390,7 +1390,7 @@ def compute_chpi_locs(
13901390
)
13911391
fwd = _magnetic_dipole_field_vec(guesses, meg_coils, too_close)
13921392
fwd = np.dot(fwd, whitener.T)
1393-
fwd.shape = (guesses.shape[0], 3, -1)
1393+
fwd = _reshape_view(fwd, (guesses.shape[0], 3, -1))
13941394
fwd = np.linalg.svd(fwd, full_matrices=False)[2]
13951395
guesses = dict(rr=guesses, whitened_fwd_svd=fwd)
13961396
del fwd, R

mne/conftest.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,9 @@ def pytest_configure(config: pytest.Config):
207207
ignore:^'.*' deprecated - use '.*'$:DeprecationWarning
208208
# dipy
209209
ignore:'where' used without 'out', expect .*:UserWarning
210+
# VTK <-> NumPy 2.5 (https://gitlab.kitware.com/vtk/vtk/-/merge_requests/12796)
211+
# nitime <-> NumPy 2.5 (https://github.com/nipy/nitime/pull/236)
212+
ignore:Setting the shape on a NumPy array has been deprecated.*:DeprecationWarning
210213
""" # noqa: E501
211214
for warning_line in warning_lines.split("\n"):
212215
warning_line = warning_line.strip()

0 commit comments

Comments
 (0)