Skip to content

include a test #155

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 13 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/conda-package-cf.yml
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ jobs:
- name: Install mkl_fft
run: |
CHANNELS="-c $GITHUB_WORKSPACE/channel ${{ env.CHANNELS }}"
conda create -n ${{ env.TEST_ENV_NAME }} python=${{ matrix.python_ver }} ${{ matrix.numpy }} $PACKAGE_NAME pytest $CHANNELS
conda create -n ${{ env.TEST_ENV_NAME }} python=${{ matrix.python_ver }} ${{ matrix.numpy }} $PACKAGE_NAME pytest scipy $CHANNELS
# Test installed packages
conda list -n ${{ env.TEST_ENV_NAME }}
- name: Run tests
Expand Down Expand Up @@ -268,7 +268,7 @@ jobs:
SET PACKAGE_VERSION=%%F
)
SET "TEST_DEPENDENCIES=pytest pytest-cov"
conda install -n ${{ env.TEST_ENV_NAME }} ${{ env.PACKAGE_NAME }}=%PACKAGE_VERSION% %TEST_DEPENDENCIES% python=${{ matrix.python }} ${{ matrix.numpy }} -c ${{ env.workdir }}/channel ${{ env.CHANNELS }}
conda install -n ${{ env.TEST_ENV_NAME }} ${{ env.PACKAGE_NAME }}=%PACKAGE_VERSION% %TEST_DEPENDENCIES% python=${{ matrix.python }} ${{ matrix.numpy }} scipy -c ${{ env.workdir }}/channel ${{ env.CHANNELS }}
- name: Report content of test environment
shell: cmd /C CALL {0}
run: |
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/conda-package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ jobs:
- name: Install mkl_fft
run: |
CHANNELS="-c $GITHUB_WORKSPACE/channel ${{ env.CHANNELS }}"
conda create -n ${{ env.TEST_ENV_NAME }} python=${{ matrix.python }} $PACKAGE_NAME pytest $CHANNELS
conda create -n ${{ env.TEST_ENV_NAME }} python=${{ matrix.python }} $PACKAGE_NAME scipy pytest $CHANNELS
# Test installed packages
conda list -n ${{ env.TEST_ENV_NAME }}
- name: Run tests
Expand Down Expand Up @@ -267,7 +267,7 @@ jobs:
SET PACKAGE_VERSION=%%F
)
SET "TEST_DEPENDENCIES=pytest pytest-cov"
conda install -n ${{ env.TEST_ENV_NAME }} ${{ env.PACKAGE_NAME }}=%PACKAGE_VERSION% %TEST_DEPENDENCIES% python=${{ matrix.python }} -c ${{ env.workdir }}/channel ${{ env.CHANNELS }}
conda install -n ${{ env.TEST_ENV_NAME }} ${{ env.PACKAGE_NAME }}=%PACKAGE_VERSION% %TEST_DEPENDENCIES% python=${{ matrix.python }} scipy -c ${{ env.workdir }}/channel ${{ env.CHANNELS }}
- name: Report content of test environment
shell: cmd /C CALL {0}
run: |
Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ build/
mkl_fft.egg-info/
mkl_fft/__pycache__/
mkl_fft/_pydfti.c
mkl_fft/_pydfti.cpython-310-x86_64-linux-gnu.so
mkl_fft/_pydfti.cpython*.so
mkl_fft/interfaces/__pycache__/
mkl_fft/src/mklfft.c
mkl_fft/tests/__pycache__/
mkl_fft/tests/from_scipy/__pycache__/
1 change: 1 addition & 0 deletions conda-recipe-cf/meta.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ test:
- pytest -v --pyargs mkl_fft
requires:
- pytest
- scipy
imports:
- mkl_fft
- mkl_fft.interfaces
Expand Down
1 change: 1 addition & 0 deletions conda-recipe/meta.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ test:
- pytest -v --pyargs mkl_fft
requires:
- pytest
- scipy
imports:
- mkl_fft
- mkl_fft.interfaces
Expand Down
1 change: 1 addition & 0 deletions mkl_fft/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


import mkl_fft.interfaces

from . import _init_helper
Expand Down
51 changes: 51 additions & 0 deletions mkl_fft/_fft_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#!/usr/bin/env python
# Copyright (c) 2025, Intel Corporation
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# * Neither the name of Intel Corporation nor the names of its contributors
# may be used to endorse or promote products derived from this software
# without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

import numpy as np

__all__ = ["check_norm", "compute_fwd_scale"]


def check_norm(norm):
if norm not in (None, "ortho", "forward", "backward"):
raise ValueError(
f"Invalid norm value {norm} should be None, 'ortho', 'forward', "
"or 'backward'."
)


def compute_fwd_scale(norm, n, shape):
check_norm(norm)
if norm in (None, "backward"):
return 1.0

ss = n if n is not None else shape
nn = np.prod(ss)
fsc = 1 / nn if nn != 0 else 1
if norm == "forward":
return fsc
else: # norm == "ortho"
return np.sqrt(fsc)
2 changes: 1 addition & 1 deletion mkl_fft/_float_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,5 +91,5 @@ def __supported_array_or_not_implemented(x):
if hasattr(np, "complex256"):
black_list.append(np.complex256)
if __x.dtype in black_list:
return NotImplemented
raise NotImplementedError
return __x
158 changes: 69 additions & 89 deletions mkl_fft/_numpy_fft.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,36 +74,59 @@
import warnings

import numpy as np
from numpy import array, conjugate, prod, sqrt, take

from . import _float_utils
from . import _pydfti as mkl_fft # pylint: disable=no-name-in-module
import mkl_fft

from ._fft_utils import check_norm, compute_fwd_scale
from ._float_utils import __downcast_float128_array

def _compute_fwd_scale(norm, n, shape):
_check_norm(norm)
if norm in (None, "backward"):
return 1.0

ss = n if n is not None else shape
nn = prod(ss)
fsc = 1 / nn if nn != 0 else 1
if norm == "forward":
return fsc
else: # norm == "ortho"
return sqrt(fsc)


def _check_norm(norm):
if norm not in (None, "ortho", "forward", "backward"):
raise ValueError(
f"Invalid norm value {norm} should be None, 'ortho', 'forward', "
"or 'backward'."
# copied with modifications from:
# https://github.com/numpy/numpy/blob/main/numpy/fft/_pocketfft.py
def _cook_nd_args(a, s=None, axes=None, invreal=False):
if s is None:
shapeless = True
if axes is None:
s = list(a.shape)
else:
s = np.take(a.shape, axes)
else:
shapeless = False
s = list(s)
if axes is None:
if not shapeless and np.__version__ >= "2.0":
msg = (
"`axes` should not be `None` if `s` is not `None` "
"(Deprecated in NumPy 2.0). In a future version of NumPy, "
"this will raise an error and `s[i]` will correspond to "
"the size along the transformed axis specified by "
"`axes[i]`. To retain current behaviour, pass a sequence "
"[0, ..., k-1] to `axes` for an array of dimension k."
)
warnings.warn(msg, DeprecationWarning, stacklevel=3)
axes = list(range(-len(s), 0))
if len(s) != len(axes):
raise ValueError("Shape and axes have different lengths.")
if invreal and shapeless:
s[-1] = (a.shape[axes[-1]] - 1) * 2
if None in s and np.__version__ >= "2.0":
msg = (
"Passing an array containing `None` values to `s` is "
"deprecated in NumPy 2.0 and will raise an error in "
"a future version of NumPy. To use the default behaviour "
"of the corresponding 1-D transform, pass the value matching "
"the default for its `n` parameter. To use the default "
"behaviour for every axis, the `s` argument can be omitted."
)
warnings.warn(msg, DeprecationWarning, stacklevel=3)
# use the whole input array along axis `i` if `s[i] == -1 or None`
s = [a.shape[_a] if _s in [-1, None] else _s for _s, _a in zip(s, axes)]

return s, axes


def _swap_direction(norm):
_check_norm(norm)
check_norm(norm)
_swap_direction_map = {
"backward": "forward",
None: "forward",
Expand Down Expand Up @@ -218,8 +241,8 @@ def fft(a, n=None, axis=-1, norm=None):

"""

x = _float_utils.__downcast_float128_array(a)
fsc = _compute_fwd_scale(norm, n, x.shape[axis])
x = __downcast_float128_array(a)
fsc = compute_fwd_scale(norm, n, x.shape[axis])

return trycall(mkl_fft.fft, (x,), {"n": n, "axis": axis, "fwd_scale": fsc})

Expand Down Expand Up @@ -311,8 +334,8 @@ def ifft(a, n=None, axis=-1, norm=None):

"""

x = _float_utils.__downcast_float128_array(a)
fsc = _compute_fwd_scale(norm, n, x.shape[axis])
x = __downcast_float128_array(a)
fsc = compute_fwd_scale(norm, n, x.shape[axis])

return trycall(mkl_fft.ifft, (x,), {"n": n, "axis": axis, "fwd_scale": fsc})

Expand Down Expand Up @@ -402,8 +425,8 @@ def rfft(a, n=None, axis=-1, norm=None):

"""

x = _float_utils.__downcast_float128_array(a)
fsc = _compute_fwd_scale(norm, n, x.shape[axis])
x = __downcast_float128_array(a)
fsc = compute_fwd_scale(norm, n, x.shape[axis])

return trycall(mkl_fft.rfft, (x,), {"n": n, "axis": axis, "fwd_scale": fsc})

Expand Down Expand Up @@ -495,8 +518,8 @@ def irfft(a, n=None, axis=-1, norm=None):

"""

x = _float_utils.__downcast_float128_array(a)
fsc = _compute_fwd_scale(norm, n, 2 * (x.shape[axis] - 1))
x = __downcast_float128_array(a)
fsc = compute_fwd_scale(norm, n, 2 * (x.shape[axis] - 1))

return trycall(
mkl_fft.irfft, (x,), {"n": n, "axis": axis, "fwd_scale": fsc}
Expand Down Expand Up @@ -581,10 +604,10 @@ def hfft(a, n=None, axis=-1, norm=None):
"""

norm = _swap_direction(norm)
x = _float_utils.__downcast_float128_array(a)
x = array(x, copy=True, dtype=complex)
conjugate(x, out=x)
fsc = _compute_fwd_scale(norm, n, 2 * (x.shape[axis] - 1))
x = __downcast_float128_array(a)
x = np.array(x, copy=True, dtype=complex)
np.conjugate(x, out=x)
fsc = compute_fwd_scale(norm, n, 2 * (x.shape[axis] - 1))

return trycall(
mkl_fft.irfft, (x,), {"n": n, "axis": axis, "fwd_scale": fsc}
Expand Down Expand Up @@ -651,61 +674,18 @@ def ihfft(a, n=None, axis=-1, norm=None):

# The copy may be required for multithreading.
norm = _swap_direction(norm)
x = _float_utils.__downcast_float128_array(a)
x = array(x, copy=True, dtype=float)
fsc = _compute_fwd_scale(norm, n, x.shape[axis])
x = __downcast_float128_array(a)
x = np.array(x, copy=True, dtype=float)
fsc = compute_fwd_scale(norm, n, x.shape[axis])

output = trycall(
mkl_fft.rfft, (x,), {"n": n, "axis": axis, "fwd_scale": fsc}
)

conjugate(output, out=output)
np.conjugate(output, out=output)
return output


# copied from: https://github.com/numpy/numpy/blob/main/numpy/fft/_pocketfft.py
def _cook_nd_args(a, s=None, axes=None, invreal=False):
if s is None:
shapeless = True
if axes is None:
s = list(a.shape)
else:
s = take(a.shape, axes)
else:
shapeless = False
s = list(s)
if axes is None:
if not shapeless and np.__version__ >= "2.0":
msg = (
"`axes` should not be `None` if `s` is not `None` "
"(Deprecated in NumPy 2.0). In a future version of NumPy, "
"this will raise an error and `s[i]` will correspond to "
"the size along the transformed axis specified by "
"`axes[i]`. To retain current behaviour, pass a sequence "
"[0, ..., k-1] to `axes` for an array of dimension k."
)
warnings.warn(msg, DeprecationWarning, stacklevel=3)
axes = list(range(-len(s), 0))
if len(s) != len(axes):
raise ValueError("Shape and axes have different lengths.")
if invreal and shapeless:
s[-1] = (a.shape[axes[-1]] - 1) * 2
if None in s and np.__version__ >= "2.0":
msg = (
"Passing an array containing `None` values to `s` is "
"deprecated in NumPy 2.0 and will raise an error in "
"a future version of NumPy. To use the default behaviour "
"of the corresponding 1-D transform, pass the value matching "
"the default for its `n` parameter. To use the default "
"behaviour for every axis, the `s` argument can be omitted."
)
warnings.warn(msg, DeprecationWarning, stacklevel=3)
# use the whole input array along axis `i` if `s[i] == -1 or None`
s = [a.shape[_a] if _s in [-1, None] else _s for _s, _a in zip(s, axes)]

return s, axes


def fftn(a, s=None, axes=None, norm=None):
"""
Compute the N-dimensional discrete Fourier Transform.
Expand Down Expand Up @@ -806,9 +786,9 @@ def fftn(a, s=None, axes=None, norm=None):

"""

x = _float_utils.__downcast_float128_array(a)
x = __downcast_float128_array(a)
s, axes = _cook_nd_args(x, s, axes)
fsc = _compute_fwd_scale(norm, s, x.shape)
fsc = compute_fwd_scale(norm, s, x.shape)

return trycall(mkl_fft.fftn, (x,), {"s": s, "axes": axes, "fwd_scale": fsc})

Expand Down Expand Up @@ -913,9 +893,9 @@ def ifftn(a, s=None, axes=None, norm=None):

"""

x = _float_utils.__downcast_float128_array(a)
x = __downcast_float128_array(a)
s, axes = _cook_nd_args(x, s, axes)
fsc = _compute_fwd_scale(norm, s, x.shape)
fsc = compute_fwd_scale(norm, s, x.shape)

return trycall(
mkl_fft.ifftn, (x,), {"s": s, "axes": axes, "fwd_scale": fsc}
Expand Down Expand Up @@ -1201,9 +1181,9 @@ def rfftn(a, s=None, axes=None, norm=None):

"""

x = _float_utils.__downcast_float128_array(a)
x = __downcast_float128_array(a)
s, axes = _cook_nd_args(x, s, axes)
fsc = _compute_fwd_scale(norm, s, x.shape)
fsc = compute_fwd_scale(norm, s, x.shape)

return trycall(
mkl_fft.rfftn, (x,), {"s": s, "axes": axes, "fwd_scale": fsc}
Expand Down Expand Up @@ -1345,9 +1325,9 @@ def irfftn(a, s=None, axes=None, norm=None):

"""

x = _float_utils.__downcast_float128_array(a)
x = __downcast_float128_array(a)
s, axes = _cook_nd_args(x, s, axes, invreal=True)
fsc = _compute_fwd_scale(norm, s, x.shape)
fsc = compute_fwd_scale(norm, s, x.shape)

return trycall(
mkl_fft.irfftn, (x,), {"s": s, "axes": axes, "fwd_scale": fsc}
Expand Down
4 changes: 2 additions & 2 deletions mkl_fft/_pydfti.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1335,11 +1335,11 @@ def ifftn(x, s=None, axes=None, overwrite_x=False, fwd_scale=1.0):


def rfft2(x, s=None, axes=(-2, -1), fwd_scale=1.0):
return rfftn(x, s=s, axes=axes, fsc=fwd_scale)
return rfftn(x, s=s, axes=axes, fwd_scale=fwd_scale)


def irfft2(x, s=None, axes=(-2, -1), fwd_scale=1.0):
return irfftn(x, s=s, axes=axes, fsc=fwd_scale)
return irfftn(x, s=s, axes=axes, fwd_scale=fwd_scale)


def _remove_axis(s, axes, axis_to_remove):
Expand Down
Loading
Loading