Skip to content
1 change: 1 addition & 0 deletions doc/changes/dev/13585.other.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix deprecation of setting a shape on an array directly in ``numpy`` 2.5+, by `Mathieu Scheltienne`_.
3 changes: 2 additions & 1 deletion mne/_fiff/_digitization.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import numpy as np

from ..fixes import _reshape_view
from ..utils import Bunch, _check_fname, _validate_type, logger, verbose, warn
from .constants import FIFF, _coord_frame_named
from .tag import read_tag
Expand Down Expand Up @@ -335,7 +336,7 @@ def _get_data_as_dict_from_dig(dig, exclude_ref_channel=True):
f"Only single coordinate frame in dig is supported, got {dig_coord_frames}"
)
dig_ch_pos_location = np.array(dig_ch_pos_location)
dig_ch_pos_location.shape = (-1, 3) # empty will be (0, 3)
dig_ch_pos_location = _reshape_view(dig_ch_pos_location, (-1, 3))
return Bunch(
nasion=fids.get("nasion", None),
lpa=fids.get("lpa", None),
Expand Down
3 changes: 2 additions & 1 deletion mne/_fiff/tag.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import numpy as np
from scipy.sparse import csc_array, csr_array

from ..fixes import _reshape_view
from ..utils import _check_option, warn
from ..utils.numerics import _julian_to_date
from .constants import (
Expand Down Expand Up @@ -177,7 +178,7 @@ def _read_matrix(fid, tag, shape, rlims):
data = data.view(">c8")
elif matrix_type == FIFF.FIFFT_COMPLEX_DOUBLE:
data = data.view(">c16")
data.shape = dims
data = _reshape_view(data, dims)
else:
# Find dimensions and return to the beginning of tag data
ndim = int(np.frombuffer(fid.read(4), dtype=">i4").item())
Expand Down
8 changes: 4 additions & 4 deletions mne/beamformer/_rap_music.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from scipy import linalg

from .._fiff.pick import pick_channels_forward, pick_info
from ..fixes import _safe_svd
from ..fixes import _reshape_view, _safe_svd
from ..forward import convert_forward_solution, is_fixed_orient
from ..inverse_sparse.mxne_inverse import _make_dipoles_sparse
from ..minimum_norm.inverse import _log_exp_var
Expand Down Expand Up @@ -68,9 +68,9 @@ def _apply_rap_music(
phi_sig = eig_vectors[:, -n_dipoles:]

n_orient = 3 if is_free_ori else 1
G.shape = (G.shape[0], -1, n_orient)
G = _reshape_view(G, (G.shape[0], -1, n_orient))
gain = forward["sol"]["data"].copy()
gain.shape = G.shape
gain = _reshape_view(gain, G.shape)
n_channels = G.shape[0]
A = np.empty((n_channels, n_dipoles))
gain_dip = np.empty((n_channels, n_dipoles))
Expand Down Expand Up @@ -122,7 +122,7 @@ def _apply_rap_music(
sol = linalg.lstsq(A, M)[0]
if n_orient == 3:
X = sol[:, np.newaxis] * oris[:, :, np.newaxis]
X.shape = (-1, len(times))
X = _reshape_view(X, (-1, len(times)))
else:
X = sol

Expand Down
3 changes: 2 additions & 1 deletion mne/beamformer/tests/test_dics.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from mne.beamformer._dics import _prepare_noise_csd
from mne.beamformer.tests.test_lcmv import _assert_weight_norm
from mne.datasets import testing
from mne.fixes import _reshape_view
from mne.io import read_info
from mne.proj import compute_proj_evoked, make_projector
from mne.surface import _compute_nearest
Expand Down Expand Up @@ -269,7 +270,7 @@ def test_make_dics(tmp_path, _load_forward, idx, whiten):
exp=None,
noise_cov=noise_cov,
)
G.shape = (n_channels, n_verts, n_orient)
G = _reshape_view(G, (n_channels, n_verts, n_orient))
G = G.transpose(1, 2, 0).conj() # verts, orient, ch
_assert_weight_norm(filters, G)

Expand Down
3 changes: 2 additions & 1 deletion mne/beamformer/tests/test_external.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from mne.beamformer import apply_lcmv, apply_lcmv_cov, make_lcmv
from mne.beamformer.tests.test_lcmv import _get_data
from mne.datasets import testing
from mne.fixes import _reshape_view

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

if stc_mne.data.ndim == 2:
signs = np.sign((stc_mne.data * stc_ft_data).sum(-1, keepdims=True))
Expand Down
3 changes: 2 additions & 1 deletion mne/beamformer/tests/test_lcmv.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
)
from mne.beamformer._compute_beamformer import _prepare_beamformer_input
from mne.datasets import testing
from mne.fixes import _reshape_view
from mne.minimum_norm import apply_inverse, make_inverse_operator
from mne.minimum_norm.tests.test_inverse import _assert_free_ori_match
from mne.simulation import simulate_evoked
Expand Down Expand Up @@ -1185,7 +1186,7 @@ def test_unit_noise_gain_formula(pick_ori, weight_norm, reg, inversion):
)
n_channels, n_sources = G.shape
n_sources //= 3
G.shape = (n_channels, n_sources, 3)
G = _reshape_view(G, (n_channels, n_sources, 3))
G = G.transpose(1, 2, 0) # verts, orient, ch
_assert_weight_norm(filters, G)

Expand Down
5 changes: 3 additions & 2 deletions mne/channels/montage.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
from .._fiff.pick import _picks_to_idx, channel_type, pick_types
from .._freesurfer import get_mni_fiducials
from ..defaults import HEAD_SIZE_DEFAULT
from ..fixes import _reshape_view
from ..transforms import (
Transform,
_ensure_trans,
Expand Down Expand Up @@ -973,9 +974,9 @@ def read_dig_hpts(fname, unit="mm"):
label[ii]: this_xyz for ii, this_xyz in enumerate(xyz) if kind[ii] == "eeg"
}
hpi = np.array([this_xyz for ii, this_xyz in enumerate(xyz) if kind[ii] == "hpi"])
hpi.shape = (-1, 3) # in case it's empty
hpi = _reshape_view(hpi, (-1, 3)) # in case it's empty
hsp = np.array([this_xyz for ii, this_xyz in enumerate(xyz) if kind[ii] == "extra"])
hsp.shape = (-1, 3) # in case it's empty
hsp = _reshape_view(hsp, (-1, 3)) # in case it's empty
return make_dig_montage(ch_pos=ch_pos, **fid, hpi=hpi, hsp=hsp)


Expand Down
6 changes: 3 additions & 3 deletions mne/chpi.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
from .cov import compute_whitener, make_ad_hoc_cov
from .dipole import _make_guesses
from .event import find_events
from .fixes import jit
from .fixes import _reshape_view, jit
from .forward import _concatenate_coils, _create_meg_coils, _magnetic_dipole_field_vec
from .io import BaseRaw, RawArray
from .io.ctf.trans import _make_ctf_coord_trans_set
Expand Down Expand Up @@ -117,7 +117,7 @@ def read_head_pos(fname):
"""
_check_fname(fname, must_exist=True, overwrite="read")
data = np.loadtxt(fname, skiprows=1) # first line is header, skip it
data.shape = (-1, 10) # ensure it's the right size even if empty
data = _reshape_view(data, (-1, 10)) # ensure it's the right size even if empty
if np.isnan(data).any(): # make sure we didn't do something dumb
raise RuntimeError(f"positions could not be read properly from {fname}")
return data
Expand Down Expand Up @@ -1390,7 +1390,7 @@ def compute_chpi_locs(
)
fwd = _magnetic_dipole_field_vec(guesses, meg_coils, too_close)
fwd = np.dot(fwd, whitener.T)
fwd.shape = (guesses.shape[0], 3, -1)
fwd = _reshape_view(fwd, (guesses.shape[0], 3, -1))
fwd = np.linalg.svd(fwd, full_matrices=False)[2]
guesses = dict(rr=guesses, whitened_fwd_svd=fwd)
del fwd, R
Expand Down
3 changes: 3 additions & 0 deletions mne/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,9 @@ def pytest_configure(config: pytest.Config):
ignore:^'.*' deprecated - use '.*'$:DeprecationWarning
# dipy
ignore:'where' used without 'out', expect .*:UserWarning
# VTK <-> NumPy 2.5 (https://gitlab.kitware.com/vtk/vtk/-/merge_requests/12796)
# nitime <-> NumPy 2.5 (https://github.com/nipy/nitime/pull/236)
ignore:Setting the shape on a NumPy array has been deprecated.*:DeprecationWarning
""" # noqa: E501
for warning_line in warning_lines.split("\n"):
warning_line = warning_line.strip()
Expand Down
3 changes: 2 additions & 1 deletion mne/decoding/receptive_field.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from sklearn.exceptions import NotFittedError
from sklearn.metrics import r2_score

from ..fixes import _reshape_view
from ..utils import _validate_type, fill_doc, pinv
from ._fixes import _check_n_features_3d, validate_data
from .base import _check_estimator, get_coef
Expand Down Expand Up @@ -361,7 +362,7 @@ def predict(self, X):
else:
extra = 1
shape = shape[: self._y_dim + extra]
y_pred.shape = shape
y_pred = _reshape_view(y_pred, shape)
return y_pred

def score(self, X, y):
Expand Down
15 changes: 8 additions & 7 deletions mne/decoding/tests/test_receptive_field.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
_times_to_delays,
)
from mne.decoding.time_delaying_ridge import _compute_corrs, _compute_reg_neighbors
from mne.fixes import _reshape_view

data_dir = Path(__file__).parents[2] / "io" / "tests" / "data"
raw_fname = data_dir / "test_raw.fif"
Expand Down Expand Up @@ -271,7 +272,7 @@ def test_time_delaying_fast_calc(n_jobs):
smin, smax = 1, 2
X_del = _delay_time_series(X, smin, smax, 1.0)
# (n_times, n_features, n_delays) -> (n_times, n_features * n_delays)
X_del.shape = (X.shape[0], -1)
X_del = _reshape_view(X_del, (X.shape[0], -1))
expected = np.array([[0, 1, 2], [0, 0, 1], [0, 5, 7], [0, 0, 5]]).T
assert_allclose(X_del, expected)
Xt_X = np.dot(X_del.T, X_del)
Expand All @@ -282,7 +283,7 @@ def test_time_delaying_fast_calc(n_jobs):
# all positive
smin, smax = -2, -1
X_del = _delay_time_series(X, smin, smax, 1.0)
X_del.shape = (X.shape[0], -1)
X_del = _reshape_view(X_del, (X.shape[0], -1))
expected = np.array([[3, 0, 0], [2, 3, 0], [11, 0, 0], [7, 11, 0]]).T
assert_allclose(X_del, expected)
Xt_X = np.dot(X_del.T, X_del)
Expand All @@ -293,7 +294,7 @@ def test_time_delaying_fast_calc(n_jobs):
# both sides
smin, smax = -1, 1
X_del = _delay_time_series(X, smin, smax, 1.0)
X_del.shape = (X.shape[0], -1)
X_del = _reshape_view(X_del, (X.shape[0], -1))
expected = np.array(
[[2, 3, 0], [1, 2, 3], [0, 1, 2], [7, 11, 0], [5, 7, 11], [0, 5, 7]]
).T
Expand All @@ -315,7 +316,7 @@ def test_time_delaying_fast_calc(n_jobs):
X = np.array([[1, 2, 3, 5]]).T
smin, smax = 0, 3
X_del = _delay_time_series(X, smin, smax, 1.0)
X_del.shape = (X.shape[0], -1)
X_del = _reshape_view(X_del, (X.shape[0], -1))
expected = np.array([[1, 2, 3, 5], [0, 1, 2, 3], [0, 0, 1, 2], [0, 0, 0, 1]]).T
assert_allclose(X_del, expected)
Xt_X = np.dot(X_del.T, X_del)
Expand All @@ -328,7 +329,7 @@ def test_time_delaying_fast_calc(n_jobs):
X = np.array([[1, 2, 3], [5, 7, 11]]).T
smin, smax = 0, 2
X_del = _delay_time_series(X, smin, smax, 1.0)
X_del.shape = (X.shape[0], -1)
X_del = _reshape_view(X_del, (X.shape[0], -1))
expected = np.array(
[[1, 2, 3], [0, 1, 2], [0, 0, 1], [5, 7, 11], [0, 5, 7], [0, 0, 5]]
).T
Expand Down Expand Up @@ -366,7 +367,7 @@ def test_time_delaying_fast_calc(n_jobs):
x_yt_true = einsum("tfd,to->ofd", X_del, y)
x_yt_true = np.reshape(x_yt_true, (x_yt_true.shape[0], -1)).T
assert_allclose(x_yt, x_yt_true, atol=1e-7, err_msg=(smin, smax))
X_del.shape = (X.shape[0], -1)
X_del = _reshape_view(X_del, (X.shape[0], -1))
x_xt_true = np.dot(X_del.T, X_del).T
assert_allclose(x_xt, x_xt_true, atol=1e-7, err_msg=(smin, smax))

Expand All @@ -388,7 +389,7 @@ def test_receptive_field_1d(n_jobs):
y[delay:] = x[:-delay, 0]
slims += [(1, 2)]
for ndim in (1, 2):
y.shape = (y.shape[0],) + (1,) * (ndim - 1)
y = _reshape_view(y, (y.shape[0],) + (1,) * (ndim - 1))
for slim in slims:
smin, smax = slim
lap = TimeDelayingRidge(
Expand Down
3 changes: 2 additions & 1 deletion mne/decoding/transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from ..cov import _check_scalings_user
from ..epochs import BaseEpochs
from ..filter import filter_data
from ..fixes import _reshape_view
from ..time_frequency import psd_array_multitaper
from ..utils import _check_option, _validate_type, check_version, fill_doc
from ._fixes import validate_data # TODO VERSION remove with sklearn 1.4+
Expand Down Expand Up @@ -118,7 +119,7 @@ def _sklearn_reshape_apply(func, return_result, X, *args, **kwargs):
X = np.reshape(X.transpose(0, 2, 1), (-1, orig_shape[1]))
X = func(X, *args, **kwargs)
if return_result:
X.shape = (orig_shape[0], orig_shape[2], orig_shape[1])
X = _reshape_view(X, (orig_shape[0], orig_shape[2], orig_shape[1]))
X = X.transpose(0, 2, 1)
return X

Expand Down
4 changes: 2 additions & 2 deletions mne/epochs.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
from .event import _read_events_fif, make_fixed_length_events, match_event_names
from .evoked import EvokedArray
from .filter import FilterMixin, _check_fun, detrend
from .fixes import rng_uniform
from .fixes import _reshape_view, rng_uniform
from .html_templates import _get_html_template
from .parallel import parallel_func
from .time_frequency.spectrum import EpochsSpectrum, SpectrumMixin, _validate_method
Expand Down Expand Up @@ -4479,7 +4479,7 @@ def _get_epoch_from_raw(self, idx, verbose=None):
else:
data = data.astype(np.float64)

data.shape = raw.epoch_shape
data = _reshape_view(data, raw.epoch_shape)
data *= raw.cals
return data

Expand Down
3 changes: 2 additions & 1 deletion mne/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from ._fiff.tag import read_tag
from ._fiff.tree import dir_tree_find
from ._fiff.write import end_block, start_and_end_file, start_block, write_int
from .fixes import _reshape_view
from .utils import (
_check_fname,
_check_integer_or_list,
Expand Down Expand Up @@ -181,7 +182,7 @@ def _read_events_fif(fid, tree):
if event_list is None:
raise ValueError("Could not find any events")
else:
event_list.shape = (-1, 3)
event_list = _reshape_view(event_list, (-1, 3))
for d in events["directory"]:
kind = d.kind
pos = d.pos
Expand Down
12 changes: 6 additions & 6 deletions mne/filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
_setup_cuda_fft_resample,
_smart_pad,
)
from .fixes import minimum_phase
from .fixes import _reshape_view, minimum_phase
from .parallel import parallel_func
from .utils import (
_check_option,
Expand Down Expand Up @@ -349,7 +349,7 @@ def _overlap_add_filter(
for pp, p in enumerate(picks):
x[p] = data_new[pp]

x.shape = orig_shape
x = _reshape_view(x, orig_shape)
return x


Expand Down Expand Up @@ -404,7 +404,7 @@ def _prep_for_filtering(x, copy, picks=None):
orig_shape = x.shape
x = np.atleast_2d(x)
picks = _picks_to_idx(x.shape[-2], picks)
x.shape = (np.prod(x.shape[:-1]), x.shape[-1])
x = _reshape_view(x, (np.prod(x.shape[:-1]), x.shape[-1]))
if len(orig_shape) == 3:
n_epochs, n_channels, n_times = orig_shape
offset = np.repeat(np.arange(0, n_channels * n_epochs, n_channels), len(picks))
Expand Down Expand Up @@ -577,7 +577,7 @@ def _iir_filter(x, iir_params, picks, n_jobs, copy, phase="zero"):
data_new = parallel(p_fun(x=x[p]) for p in picks)
for pp, p in enumerate(picks):
x[p] = data_new[pp]
x.shape = orig_shape
x = _reshape_view(x, orig_shape)
return x


Expand Down Expand Up @@ -1657,7 +1657,7 @@ def _mt_spectrum_proc(
)
logger.info(f"{kind} notch frequencies (Hz):\n{found_freqs}")

x.shape = orig_shape
x = _reshape_view(x, orig_shape)
return x


Expand Down Expand Up @@ -2952,5 +2952,5 @@ def _iir_pad_apply_unpad(x, *, func, padlen, padtype, **kwargs):
x_ext = _smart_pad(x_ext, (padlen, padlen), padtype)
x_ext = func(x=x_ext, axis=-1, padlen=0, **kwargs)
this_x[:] = x_ext[padlen : len(x_ext) - padlen]
x_out.shape = x.shape
x_out = _reshape_view(x_out, x.shape)
return x_out
Loading