Skip to content

Commit eb1ab83

Browse files
committed
raise warning similar to numpy
1 parent 69defde commit eb1ab83

File tree

5 files changed

+32
-14
lines changed

5 files changed

+32
-14
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

+28-8
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
from . import _pydfti as mkl_fft
6363
from . import _float_utils
6464
import re
65+
import warnings
6566

6667
def _check_norm(norm):
6768
if norm not in (None, "ortho", "forward", "backward"):
@@ -691,22 +692,42 @@ def ihfft(a, n=None, axis=-1, norm=None):
691692
return output
692693

693694

694-
def _cook_nd_args(a, s=None, axes=None, invreal=0):
695+
# copied from: https://github.com/numpy/numpy/blob/main/numpy/fft/_pocketfft.py
696+
def _cook_nd_args(a, s=None, axes=None, invreal=False):
695697
if s is None:
696-
shapeless = 1
698+
shapeless = True
697699
if axes is None:
698700
s = list(a.shape)
699701
else:
700702
s = take(a.shape, axes)
701703
else:
702-
shapeless = 0
704+
shapeless = False
703705
s = list(s)
704706
if axes is None:
707+
if not shapeless:
708+
msg = ("`axes` should not be `None` if `s` is not `None` "
709+
"(Deprecated in NumPy 2.0). In a future version of NumPy, "
710+
"this will raise an error and `s[i]` will correspond to "
711+
"the size along the transformed axis specified by "
712+
"`axes[i]`. To retain current behaviour, pass a sequence "
713+
"[0, ..., k-1] to `axes` for an array of dimension k.")
714+
warnings.warn(msg, DeprecationWarning, stacklevel=3)
705715
axes = list(range(-len(s), 0))
706716
if len(s) != len(axes):
707717
raise ValueError("Shape and axes have different lengths.")
708718
if invreal and shapeless:
709719
s[-1] = (a.shape[axes[-1]] - 1) * 2
720+
if None in s:
721+
msg = ("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+
warnings.warn(msg, DeprecationWarning, stacklevel=3)
728+
# use the whole input array along axis `i` if `s[i] == -1 or None`
729+
s = [a.shape[_a] if _s in [-1, None] else _s for _s, _a in zip(s, axes)]
730+
710731
return s, axes
711732

712733

@@ -811,6 +832,7 @@ def fftn(a, s=None, axes=None, norm=None):
811832
"""
812833
_check_norm(norm)
813834
x = _float_utils.__downcast_float128_array(a)
835+
s, axes = _cook_nd_args(x, s, axes)
814836

815837
if norm in (None, "backward"):
816838
fsc = 1.0
@@ -927,6 +949,7 @@ def ifftn(a, s=None, axes=None, norm=None):
927949
"""
928950
_check_norm(norm)
929951
x = _float_utils.__downcast_float128_array(a)
952+
s, axes = _cook_nd_args(x, s, axes)
930953

931954
if norm in (None, "backward"):
932955
fsc = 1.0
@@ -1224,16 +1247,15 @@ def rfftn(a, s=None, axes=None, norm=None):
12241247
"""
12251248
_check_norm(norm)
12261249
x = _float_utils.__downcast_float128_array(a)
1250+
s, axes = _cook_nd_args(x, s, axes)
12271251

12281252
if (norm in (None, "backward")):
12291253
fsc = 1.0
12301254
elif norm == "forward":
12311255
x = asanyarray(x)
1232-
s, axes = _cook_nd_args(x, s, axes)
12331256
fsc = frwd_sc_nd(s, axes, x.shape)
12341257
else:
12351258
x = asanyarray(x)
1236-
s, axes = _cook_nd_args(x, s, axes)
12371259
fsc = sqrt(frwd_sc_nd(s, axes, x.shape))
12381260

12391261
return trycall(
@@ -1380,17 +1402,15 @@ def irfftn(a, s=None, axes=None, norm=None):
13801402
"""
13811403
_check_norm(norm)
13821404
x = _float_utils.__downcast_float128_array(a)
1383-
1405+
s, axes = _cook_nd_args(x, s, axes, invreal=True)
13841406

13851407
if (norm in (None, "backward")):
13861408
fsc = 1.0
13871409
elif norm == "forward":
13881410
x = asanyarray(x)
1389-
s, axes = _cook_nd_args(x, s, axes, invreal=1)
13901411
fsc = frwd_sc_nd(s, axes, x.shape)
13911412
else:
13921413
x = asanyarray(x)
1393-
s, axes = _cook_nd_args(x, s, axes, invreal=1)
13941414
fsc = sqrt(frwd_sc_nd(s, axes, x.shape))
13951415

13961416
return trycall(

Diff for: mkl_fft/_pydfti.pyx

+2-2
Original file line numberDiff line numberDiff line change
@@ -1097,7 +1097,7 @@ def _fftnd_impl(x, shape=None, axes=None, overwrite_x=False, direction=+1, doubl
10971097
_direct_fftnd,
10981098
{'overwrite_arg': overwrite_x, 'direction': direction, 'fsc': fsc},
10991099
res
1100-
)
1100+
)
11011101
else:
11021102
sc = <object> fsc
11031103
return _iter_fftnd(x, s=shape, axes=axes,
@@ -1197,7 +1197,7 @@ def rfftn(x, s=None, axes=None, fwd_scale=1.0):
11971197
a = _fix_dimensions(a, tuple(ss), axes)
11981198
if len(set(axes)) == len(axes) and len(axes) == a.ndim and len(axes) > 2:
11991199
ss, aa = _remove_axis(s, axes, -1)
1200-
ind = [slice(None,None,1),] * len(s)
1200+
ind = [slice(None, None, 1),] * len(s)
12011201
for ii in range(a.shape[la]):
12021202
ind[la] = ii
12031203
tind = tuple(ind)

Diff for: mkl_fft/tests/test_interfaces.py

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

121121

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

Diff for: mkl_fft/tests/test_pocketfft.py

-3
Original file line numberDiff line numberDiff line change
@@ -378,22 +378,19 @@ def test_s_negative_1(self, op):
378378
# should use the whole input array along the first axis
379379
assert op(x, s=(-1, 5), axes=(0, 1)).shape == (10, 5)
380380

381-
@pytest.mark.skip("no warning is raised in mkl_ftt")
382381
@pytest.mark.parametrize("op", [mkl_fft.fftn, mkl_fft.ifftn,
383382
mkl_fft.rfftn, mkl_fft.irfftn])
384383
def test_s_axes_none(self, op):
385384
x = np.arange(100).reshape(10, 10)
386385
with pytest.warns(match='`axes` should not be `None` if `s`'):
387386
op(x, s=(-1, 5))
388387

389-
@pytest.mark.skip("no warning is raised in mkl_ftt")
390388
@pytest.mark.parametrize("op", [mkl_fft.fft2, mkl_fft.ifft2])
391389
def test_s_axes_none_2D(self, op):
392390
x = np.arange(100).reshape(10, 10)
393391
with pytest.warns(match='`axes` should not be `None` if `s`'):
394392
op(x, s=(-1, 5), axes=None)
395393

396-
@pytest.mark.skip("no warning is raised in mkl_ftt")
397394
@pytest.mark.parametrize("op", [mkl_fft.fftn, mkl_fft.ifftn,
398395
mkl_fft.rfftn, mkl_fft.irfftn,
399396
mkl_fft.fft2, mkl_fft.ifft2])

0 commit comments

Comments
 (0)