Skip to content

update _scipy_fft.py #145

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

Merged
merged 5 commits into from
Apr 11, 2025
Merged
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 pytest scipy $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
11 changes: 6 additions & 5 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
_vendored/__pycache__/
# CMake build and local install directory
build/
mkl_fft.egg-info/
mkl_fft/__pycache__/

# Byte-compiled / optimized / DLL files
__pycache__/

mkl_fft/_pydfti.c
mkl_fft/_pydfti.cpython-310-x86_64-linux-gnu.so
mkl_fft/interfaces/__pycache__/
mkl_fft/_pydfti.cpython*.so
mkl_fft/src/mklfft.c
mkl_fft/tests/__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
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)
36 changes: 18 additions & 18 deletions mkl_fft/_float_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@
import numpy as np

__all__ = [
"__upcast_float16_array",
"__downcast_float128_array",
"__supported_array_or_not_implemented",
"_upcast_float16_array",
"_downcast_float128_array",
"_supported_array_or_not_implemented",
]


def __upcast_float16_array(x):
def _upcast_float16_array(x):
"""
Used in _scipy_fft to upcast float16 to float32,
instead of float64, as mkl_fft would do
Expand All @@ -46,18 +46,18 @@ def __upcast_float16_array(x):
if xdt == np.longdouble and not xdt == np.float64:
raise ValueError("type %s is not supported" % xdt)
if not isinstance(x, np.ndarray):
__x = np.asarray(x)
xdt = __x.dtype
_x = np.asarray(x)
xdt = _x.dtype
if xdt == np.half:
# no half-precision routines, so convert to single precision
return np.asarray(__x, dtype=np.float32)
return np.asarray(_x, dtype=np.float32)
if xdt == np.longdouble and not xdt == np.float64:
raise ValueError("type %s is not supported" % xdt)
return __x
return _x
return x


def __downcast_float128_array(x):
def _downcast_float128_array(x):
"""
Used in _numpy_fft to unsafely downcast float128/complex256 to
complex128, instead of raising an error
Expand All @@ -69,27 +69,27 @@ def __downcast_float128_array(x):
elif xdt == np.clongdouble and not xdt == np.complex128:
return np.asarray(x, dtype=np.complex128)
if not isinstance(x, np.ndarray):
__x = np.asarray(x)
xdt = __x.dtype
_x = np.asarray(x)
xdt = _x.dtype
if xdt == np.longdouble and not xdt == np.float64:
return np.asarray(x, dtype=np.float64)
elif xdt == np.clongdouble and not xdt == np.complex128:
return np.asarray(x, dtype=np.complex128)
return __x
return _x
return x


def __supported_array_or_not_implemented(x):
def _supported_array_or_not_implemented(x):
"""
Used in _scipy_fft to convert array to float32,
float64, complex64, or complex128 type or return NotImplemented
float64, complex64, or complex128 type or raise NotImplementedError
"""
__x = np.asarray(x)
_x = np.asarray(x)
black_list = [np.half]
if hasattr(np, "float128"):
black_list.append(np.float128)
if hasattr(np, "complex256"):
black_list.append(np.complex256)
if __x.dtype in black_list:
return NotImplemented
return __x
if _x.dtype in black_list:
raise NotImplementedError
return _x
Loading
Loading