diff --git a/mne/fixes.py b/mne/fixes.py index 2aed20492ec..070d4125d18 100644 --- a/mne/fixes.py +++ b/mne/fixes.py @@ -720,3 +720,16 @@ def minimum_phase(h, method="homomorphic", n_fft=None, *, half=True): n_out = (n_half + len(h) % 2) if half else len(h) return h_minimum[:n_out] + + +# SciPy 1.15 deprecates sph_harm for sph_harm_y and using it will trigger a +# DeprecationWarning. This is a backport of the new function for older SciPy versions. +def sph_harm_y(n, m, theta, phi, *, diff_n=0): + """Wrap scipy.special.sph_harm for sph_harm_y.""" + # Can be removed once we no longer support scipy < 1.15.0 + from scipy import special + + if "sph_harm_y" in special.__dict__: + return special.sph_harm_y(n, m, theta, phi, diff_n=diff_n) + else: + return special.sph_harm(m, n, phi, theta) diff --git a/mne/preprocessing/maxwell.py b/mne/preprocessing/maxwell.py index 2c7e4e84b00..789c8520f05 100644 --- a/mne/preprocessing/maxwell.py +++ b/mne/preprocessing/maxwell.py @@ -10,7 +10,7 @@ import numpy as np from scipy import linalg -from scipy.special import lpmv, sph_harm_y +from scipy.special import lpmv from .. import __version__ from .._fiff.compensator import make_compensator @@ -24,7 +24,7 @@ from ..annotations import _annotations_starts_stops from ..bem import _check_origin from ..channels.channels import _get_T1T2_mag_inds, fix_mag_coil_types -from ..fixes import _safe_svd, bincount +from ..fixes import _safe_svd, bincount, sph_harm_y from ..forward import _concatenate_coils, _create_meg_coils, _prep_meg_channels from ..io import BaseRaw, RawArray from ..surface import _normalize_vectors diff --git a/mne/preprocessing/tests/test_maxwell.py b/mne/preprocessing/tests/test_maxwell.py index 0c9ec56b8c5..f5e816258f8 100644 --- a/mne/preprocessing/tests/test_maxwell.py +++ b/mne/preprocessing/tests/test_maxwell.py @@ -11,7 +11,6 @@ import pytest from numpy.testing import assert_allclose, assert_array_equal from scipy import sparse -from scipy.special import sph_harm_y import mne from mne import compute_raw_covariance, concatenate_raws, pick_info, pick_types @@ -19,6 +18,7 @@ from mne.annotations import _annotations_starts_stops from mne.chpi import filter_chpi, read_head_pos from mne.datasets import testing +from mne.fixes import sph_harm_y from mne.forward import _prep_meg_channels, use_coil_def from mne.io import ( BaseRaw, diff --git a/mne/transforms.py b/mne/transforms.py index b6632bbb41b..7072ea25124 100644 --- a/mne/transforms.py +++ b/mne/transforms.py @@ -12,14 +12,13 @@ import numpy as np from scipy import linalg from scipy.spatial.distance import cdist -from scipy.special import sph_harm_y from ._fiff.constants import FIFF from ._fiff.open import fiff_open from ._fiff.tag import read_tag from ._fiff.write import start_and_end_file, write_coord_trans from .defaults import _handle_default -from .fixes import _get_img_fdata, jit +from .fixes import _get_img_fdata, jit, sph_harm_y from .utils import ( _check_fname, _check_option,