Skip to content

Commit 65727ea

Browse files
committed
raise warning similar to numpy
1 parent bee4cf6 commit 65727ea

File tree

5 files changed

+36
-9
lines changed

5 files changed

+36
-9
lines changed

Diff for: README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -82,5 +82,5 @@ The package also provides `mkl_fft._numpy_fft` and `mkl_fft._scipy_fft` interfac
8282

8383
To build ``mkl_fft`` from sources on Linux:
8484
- install a recent version of MKL, if necessary;
85-
- execute ``source /path_to_oneapi/mkl/latest/env/vars.sh`` ;
85+
- execute ``source /path_to_oneapi/mkl/latest/env/vars.sh``;
8686
- execute ``python -m pip install .``

Diff for: mkl_fft/_numpy_fft.py

+32-5
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
]
7272

7373
import re
74+
import warnings
7475

7576
from numpy import array, asanyarray, conjugate, prod, sqrt, take
7677

@@ -688,22 +689,46 @@ def ihfft(a, n=None, axis=-1, norm=None):
688689
return output
689690

690691

691-
def _cook_nd_args(a, s=None, axes=None, invreal=0):
692+
# copied from: https://github.com/numpy/numpy/blob/main/numpy/fft/_pocketfft.py
693+
def _cook_nd_args(a, s=None, axes=None, invreal=False):
692694
if s is None:
693-
shapeless = 1
695+
shapeless = True
694696
if axes is None:
695697
s = list(a.shape)
696698
else:
697699
s = take(a.shape, axes)
698700
else:
699-
shapeless = 0
701+
shapeless = False
700702
s = list(s)
701703
if axes is None:
704+
if not shapeless:
705+
msg = (
706+
"`axes` should not be `None` if `s` is not `None` "
707+
"(Deprecated in NumPy 2.0). In a future version of NumPy, "
708+
"this will raise an error and `s[i]` will correspond to "
709+
"the size along the transformed axis specified by "
710+
"`axes[i]`. To retain current behaviour, pass a sequence "
711+
"[0, ..., k-1] to `axes` for an array of dimension k."
712+
)
713+
warnings.warn(msg, DeprecationWarning, stacklevel=3)
702714
axes = list(range(-len(s), 0))
703715
if len(s) != len(axes):
704716
raise ValueError("Shape and axes have different lengths.")
705717
if invreal and shapeless:
706718
s[-1] = (a.shape[axes[-1]] - 1) * 2
719+
if None in s:
720+
msg = (
721+
"Passing an array containing `None` values to `s` is "
722+
"deprecated in NumPy 2.0 and will raise an error in "
723+
"a future version of NumPy. To use the default behaviour "
724+
"of the corresponding 1-D transform, pass the value matching "
725+
"the default for its `n` parameter. To use the default "
726+
"behaviour for every axis, the `s` argument can be omitted."
727+
)
728+
warnings.warn(msg, DeprecationWarning, stacklevel=3)
729+
# use the whole input array along axis `i` if `s[i] == -1 or None`
730+
s = [a.shape[_a] if _s in [-1, None] else _s for _s, _a in zip(s, axes)]
731+
707732
return s, axes
708733

709734

@@ -808,6 +833,7 @@ def fftn(a, s=None, axes=None, norm=None):
808833
"""
809834
_check_norm(norm)
810835
x = _float_utils.__downcast_float128_array(a)
836+
s, axes = _cook_nd_args(x, s, axes)
811837

812838
if norm in (None, "backward"):
813839
fsc = 1.0
@@ -920,6 +946,7 @@ def ifftn(a, s=None, axes=None, norm=None):
920946
"""
921947
_check_norm(norm)
922948
x = _float_utils.__downcast_float128_array(a)
949+
s, axes = _cook_nd_args(x, s, axes)
923950

924951
if norm in (None, "backward"):
925952
fsc = 1.0
@@ -1215,6 +1242,7 @@ def rfftn(a, s=None, axes=None, norm=None):
12151242
"""
12161243
_check_norm(norm)
12171244
x = _float_utils.__downcast_float128_array(a)
1245+
s, axes = _cook_nd_args(x, s, axes)
12181246

12191247
if norm in (None, "backward"):
12201248
fsc = 1.0
@@ -1369,16 +1397,15 @@ def irfftn(a, s=None, axes=None, norm=None):
13691397
"""
13701398
_check_norm(norm)
13711399
x = _float_utils.__downcast_float128_array(a)
1400+
s, axes = _cook_nd_args(x, s, axes, invreal=True)
13721401

13731402
if norm in (None, "backward"):
13741403
fsc = 1.0
13751404
elif norm == "forward":
13761405
x = asanyarray(x)
1377-
s, axes = _cook_nd_args(x, s, axes, invreal=1)
13781406
fsc = frwd_sc_nd(s, x.shape)
13791407
else:
13801408
x = asanyarray(x)
1381-
s, axes = _cook_nd_args(x, s, axes, invreal=1)
13821409
fsc = sqrt(frwd_sc_nd(s, x.shape))
13831410

13841411
return trycall(

Diff for: mkl_fft/_pydfti.pyx

+2-2
Original file line numberDiff line numberDiff line change
@@ -1100,7 +1100,7 @@ def _fftnd_impl(x, s=None, axes=None, overwrite_x=False, direction=+1, double fs
11001100
_direct_fftnd,
11011101
{'overwrite_x': overwrite_x, 'direction': direction, 'fsc': fsc},
11021102
res
1103-
)
1103+
)
11041104
else:
11051105
sc = <object> fsc
11061106
return _iter_fftnd(x, s=s, axes=axes,
@@ -1200,7 +1200,7 @@ def rfftn(x, s=None, axes=None, fwd_scale=1.0):
12001200
a = _fix_dimensions(a, tuple(ss), axes)
12011201
if len(set(axes)) == len(axes) and len(axes) == a.ndim and len(axes) > 2:
12021202
ss, aa = _remove_axis(s, axes, -1)
1203-
ind = [slice(None,None,1),] * len(s)
1203+
ind = [slice(None, None, 1),] * len(s)
12041204
for ii in range(a.shape[la]):
12051205
ind[la] = ii
12061206
tind = tuple(ind)

Diff for: mkl_fft/tests/test_interfaces.py

+1
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ def test_scipy_rfftn(norm, dtype):
121121
assert np.allclose(x, xx, atol=tol, rtol=tol)
122122

123123

124+
@pytest.mark.filterwarnings("ignore::DeprecationWarning")
124125
@pytest.mark.parametrize("norm", [None, "forward", "backward", "ortho"])
125126
@pytest.mark.parametrize("dtype", [np.float32, np.float64])
126127
def test_numpy_rfftn(norm, dtype):

Diff for: mkl_fft/tests/test_pocketfft.py

-1
Original file line numberDiff line numberDiff line change
@@ -519,7 +519,6 @@ def test_s_axes_none(self, op):
519519
with pytest.warns(match="`axes` should not be `None` if `s`"):
520520
op(x, s=(-1, 5))
521521

522-
@pytest.mark.skip("no warning is raised in mkl_ftt")
523522
@pytest.mark.parametrize("op", [mkl_fft.fft2, mkl_fft.ifft2])
524523
def test_s_axes_none_2D(self, op):
525524
x = np.arange(100).reshape(10, 10)

0 commit comments

Comments
 (0)