| 1 |
# Copyright 2015 Knowledge Economy Developments Ltd |
| 2 |
# |
| 3 |
# Henry Gomersall |
| 4 |
# [email protected] |
| 5 |
# |
| 6 |
# All rights reserved. |
| 7 |
# |
| 8 |
# Redistribution and use in source and binary forms, with or without |
| 9 |
# modification, are permitted provided that the following conditions are met: |
| 10 |
# |
| 11 |
# * Redistributions of source code must retain the above copyright notice, this |
| 12 |
# list of conditions and the following disclaimer. |
| 13 |
# |
| 14 |
# * Redistributions in binary form must reproduce the above copyright notice, |
| 15 |
# this list of conditions and the following disclaimer in the documentation |
| 16 |
# and/or other materials provided with the distribution. |
| 17 |
# |
| 18 |
# * Neither the name of the copyright holder nor the names of its contributors |
| 19 |
# may be used to endorse or promote products derived from this software without |
| 20 |
# specific prior written permission. |
| 21 |
# |
| 22 |
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 23 |
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 24 |
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 25 |
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 26 |
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 27 |
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 28 |
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 29 |
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 30 |
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 31 |
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 32 |
# POSSIBILITY OF SUCH DAMAGE. |
| 33 |
# |
| 34 |
|
| 35 |
from pyfftw import interfaces, _supported_types, _all_types_np |
| 36 |
|
| 37 |
from .test_pyfftw_base import run_test_suites, np_fft |
| 38 |
from ._get_default_args import get_default_args |
| 39 |
|
| 40 |
from packaging.version import Version |
| 41 |
import unittest |
| 42 |
import numpy |
| 43 |
import warnings |
| 44 |
import copy |
| 45 |
warnings.filterwarnings('always') |
| 46 |
|
| 47 |
if Version(numpy.version.version) <= Version('1.6.2'): |
| 48 |
# We overwrite the broken _cook_nd_args with a fixed version. |
| 49 |
from ._cook_nd_args import _cook_nd_args |
| 50 |
numpy.fft.fftpack._cook_nd_args = _cook_nd_args |
| 51 |
|
| 52 |
complex_dtypes = [] |
| 53 |
real_dtypes = [] |
| 54 |
if '32' in _supported_types: |
| 55 |
complex_dtypes.extend([numpy.complex64]*2) |
| 56 |
real_dtypes.extend([numpy.float16, numpy.float32]) |
| 57 |
if '64' in _supported_types: |
| 58 |
complex_dtypes.append(numpy.complex128) |
| 59 |
real_dtypes.append(numpy.float64) |
| 60 |
if 'ld' in _supported_types: |
| 61 |
complex_dtypes.append(numpy.clongdouble) |
| 62 |
real_dtypes.append(numpy.longdouble) |
| 63 |
|
| 64 |
def make_complex_data(shape, dtype): |
| 65 |
ar, ai = dtype(numpy.random.randn(2, *shape)) |
| 66 |
return ar + 1j*ai |
| 67 |
|
| 68 |
def make_real_data(shape, dtype): |
| 69 |
return dtype(numpy.random.randn(*shape)) |
| 70 |
|
| 71 |
def _numpy_fft_has_norm_kwarg(): |
| 72 |
"""returns True if numpy's fft supports the norm keyword argument |
| 73 |
|
| 74 |
This should be true for numpy >= 1.10 |
| 75 |
""" |
| 76 |
# return LooseVersion(numpy.version.version) >= LooseVersion('1.10') |
| 77 |
try: |
| 78 |
np_fft.fft(numpy.ones(4), norm=None) |
| 79 |
return True |
| 80 |
except TypeError: |
| 81 |
return False |
| 82 |
|
| 83 |
if _numpy_fft_has_norm_kwarg() and numpy.__version__ < '1.13': |
| 84 |
# use version of numpy.fft.rfft* with normalisation bug fixed |
| 85 |
# The patched version here, corresponds to the following bugfix PR: |
| 86 |
# https://github.com/numpy/numpy/pull/8445 |
| 87 |
from numpy.fft import fftpack as fftpk |
| 88 |
|
| 89 |
def rfft_fix(a, n=None, axis=-1, norm=None): |
| 90 |
# from numpy.fft import fftpack_lite as fftpack |
| 91 |
# from numpy.fft.fftpack import _raw_fft, _unitary, _real_fft_cache |
| 92 |
a = numpy.array(a, copy=True, dtype=float) |
| 93 |
output = fftpk._raw_fft(a, n, axis, fftpk.fftpack.rffti, |
| 94 |
fftpk.fftpack.rfftf, fftpk._real_fft_cache) |
| 95 |
if fftpk._unitary(norm): |
| 96 |
if n is None: |
| 97 |
n = a.shape[axis] |
| 98 |
output *= 1 / numpy.sqrt(n) |
| 99 |
return output |
| 100 |
|
| 101 |
def rfftn_fix(a, s=None, axes=None, norm=None): |
| 102 |
a = numpy.array(a, copy=True, dtype=float) |
| 103 |
s, axes = fftpk._cook_nd_args(a, s, axes) |
| 104 |
a = rfft_fix(a, s[-1], axes[-1], norm) |
| 105 |
for ii in range(len(axes)-1): |
| 106 |
a = fftpk.fft(a, s[ii], axes[ii], norm) |
| 107 |
return a |
| 108 |
|
| 109 |
def rfft2_fix(a, s=None, axes=(-2, -1), norm=None): |
| 110 |
return rfftn_fix(a, s, axes, norm) |
| 111 |
|
| 112 |
np_fft.rfft = rfft_fix |
| 113 |
np_fft.rfft2 = rfft2_fix |
| 114 |
np_fft.rfftn = rfftn_fix |
| 115 |
|
| 116 |
functions = { |
| 117 |
'fft': 'complex', |
| 118 |
'ifft': 'complex', |
| 119 |
'rfft': 'r2c', |
| 120 |
'irfft': 'c2r', |
| 121 |
'rfftn': 'r2c', |
| 122 |
'hfft': 'c2r', |
| 123 |
'ihfft': 'r2c', |
| 124 |
'irfftn': 'c2r', |
| 125 |
'rfft2': 'r2c', |
| 126 |
'irfft2': 'c2r', |
| 127 |
'fft2': 'complex', |
| 128 |
'ifft2': 'complex', |
| 129 |
'fftn': 'complex', |
| 130 |
'ifftn': 'complex'} |
| 131 |
|
| 132 |
acquired_names = ('fftfreq', 'fftshift', 'ifftshift') |
| 133 |
|
| 134 |
if Version(numpy.version.version) >= Version('1.8'): |
| 135 |
acquired_names += ('rfftfreq', ) |
| 136 |
|
| 137 |
|
| 138 |
class InterfacesNumpyFFTTestModule(unittest.TestCase): |
| 139 |
''' A really simple test suite to check the module works as expected. |
| 140 |
''' |
| 141 |
|
| 142 |
def test_acquired_names(self): |
| 143 |
for each_name in acquired_names: |
| 144 |
|
| 145 |
numpy_fft_attr = getattr(numpy.fft, each_name) |
| 146 |
acquired_attr = getattr(interfaces.numpy_fft, each_name) |
| 147 |
|
| 148 |
self.assertIs(numpy_fft_attr, acquired_attr) |
| 149 |
|
| 150 |
|
| 151 |
class InterfacesNumpyFFTTestFFT(unittest.TestCase): |
| 152 |
|
| 153 |
io_dtypes = { |
| 154 |
'complex': (complex_dtypes, make_complex_data), |
| 155 |
'r2c': (real_dtypes, make_real_data), |
| 156 |
'c2r': (complex_dtypes, make_complex_data)} |
| 157 |
|
| 158 |
validator_module = np_fft |
| 159 |
test_interface = interfaces.numpy_fft |
| 160 |
func = 'fft' |
| 161 |
axes_kw = 'axis' |
| 162 |
threads_arg_name = 'threads' |
| 163 |
overwrite_input_flag = 'overwrite_input' |
| 164 |
default_s_from_shape_slicer = slice(-1, None) |
| 165 |
|
| 166 |
if numpy.__version__ >= '1.20.0': |
| 167 |
test_shapes = ( |
| 168 |
((100,), {}), |
| 169 |
((128, 64), {'axis': 0}), |
| 170 |
((128, 32), {'axis': -1}), |
| 171 |
((59, 100), {}), |
| 172 |
((59, 99), {'axis': -1}), |
| 173 |
((59, 99), {'axis': 0}), |
| 174 |
((32, 32, 4), {'axis': 1}), |
| 175 |
((32, 32, 2), {'axis': 1, 'norm': 'ortho'}), |
| 176 |
((32, 32, 2), {'axis': 1, 'norm': None}), |
| 177 |
((32, 32, 2), {'axis': 1, 'norm': 'backward'}), |
| 178 |
((32, 32, 2), {'axis': 1, 'norm': 'forward'}), |
| 179 |
((64, 128, 16), {}), |
| 180 |
) |
| 181 |
else: |
| 182 |
test_shapes = ( |
| 183 |
((100,), {}), |
| 184 |
((128, 64), {'axis': 0}), |
| 185 |
((128, 32), {'axis': -1}), |
| 186 |
((59, 100), {}), |
| 187 |
((59, 99), {'axis': -1}), |
| 188 |
((59, 99), {'axis': 0}), |
| 189 |
((32, 32, 4), {'axis': 1}), |
| 190 |
((32, 32, 2), {'axis': 1, 'norm': 'ortho'}), |
| 191 |
((32, 32, 2), {'axis': 1, 'norm': None}), |
| 192 |
((64, 128, 16), {}), |
| 193 |
) |
| 194 |
|
| 195 |
# invalid_s_shapes is: |
| 196 |
# (size, invalid_args, error_type, error_string) |
| 197 |
invalid_args = ( |
| 198 |
((100,), ((100, 200),), TypeError, ''), |
| 199 |
((100, 200), ((100, 200),), TypeError, ''), |
| 200 |
((100,), (100, (-2, -1)), TypeError, ''), |
| 201 |
((100,), (100, -20), IndexError, '')) |
| 202 |
|
| 203 |
realinv = False |
| 204 |
has_norm_kwarg = _numpy_fft_has_norm_kwarg() |
| 205 |
|
| 206 |
@property |
| 207 |
def test_data(self): |
| 208 |
for test_shape, kwargs in self.test_shapes: |
| 209 |
axes = self.axes_from_kwargs(kwargs) |
| 210 |
s = self.s_from_kwargs(test_shape, kwargs) |
| 211 |
|
| 212 |
if not self.has_norm_kwarg and 'norm' in kwargs: |
| 213 |
kwargs.pop('norm') |
| 214 |
|
| 215 |
if self.realinv: |
| 216 |
test_shape = list(test_shape) |
| 217 |
test_shape[axes[-1]] = test_shape[axes[-1]]//2 + 1 |
| 218 |
test_shape = tuple(test_shape) |
| 219 |
|
| 220 |
yield test_shape, s, kwargs |
| 221 |
|
| 222 |
def __init__(self, *args, **kwargs): |
| 223 |
|
| 224 |
super(InterfacesNumpyFFTTestFFT, self).__init__(*args, **kwargs) |
| 225 |
|
| 226 |
# Assume python 3, but keep backwards compatibility |
| 227 |
if not hasattr(self, 'assertRaisesRegex'): |
| 228 |
self.assertRaisesRegex = self.assertRaisesRegexp |
| 229 |
|
| 230 |
def validate(self, array_type, test_shape, dtype, |
| 231 |
s, kwargs, copy_func=copy.copy): |
| 232 |
|
| 233 |
# Do it without the cache |
| 234 |
|
| 235 |
# without: |
| 236 |
interfaces.cache.disable() |
| 237 |
self._validate(array_type, test_shape, dtype, s, kwargs, |
| 238 |
copy_func=copy_func) |
| 239 |
|
| 240 |
def munge_input_array(self, array, kwargs): |
| 241 |
return array |
| 242 |
|
| 243 |
def _validate(self, array_type, test_shape, dtype, |
| 244 |
s, kwargs, copy_func=copy.copy): |
| 245 |
|
| 246 |
input_array = self.munge_input_array( |
| 247 |
array_type(test_shape, dtype), kwargs) |
| 248 |
|
| 249 |
orig_input_array = copy_func(input_array) |
| 250 |
|
| 251 |
np_input_array = numpy.asarray(input_array) |
| 252 |
|
| 253 |
# Why are long double inputs copied to double precision? It's what |
| 254 |
# numpy silently does anyways as of v1.10 but helps with backward |
| 255 |
# compatibility and scipy. |
| 256 |
# https://github.com/pyFFTW/pyFFTW/pull/189#issuecomment-356449731 |
| 257 |
if np_input_array.dtype == 'clongdouble': |
| 258 |
np_input_array = numpy.complex128(input_array) |
| 259 |
|
| 260 |
elif np_input_array.dtype == 'longdouble': |
| 261 |
np_input_array = numpy.float64(input_array) |
| 262 |
|
| 263 |
with warnings.catch_warnings(record=True) as w: |
| 264 |
# We catch the warnings so as to pick up on when |
| 265 |
# a complex array is turned into a real array |
| 266 |
|
| 267 |
if 'axes' in kwargs: |
| 268 |
validator_kwargs = {'axes': kwargs['axes']} |
| 269 |
elif 'axis' in kwargs: |
| 270 |
validator_kwargs = {'axis': kwargs['axis']} |
| 271 |
else: |
| 272 |
validator_kwargs = {} |
| 273 |
|
| 274 |
if self.has_norm_kwarg and 'norm' in kwargs: |
| 275 |
validator_kwargs['norm'] = kwargs['norm'] |
| 276 |
|
| 277 |
try: |
| 278 |
test_out_array = getattr(self.validator_module, self.func)( |
| 279 |
copy_func(np_input_array), s, **validator_kwargs) |
| 280 |
|
| 281 |
except Exception as e: |
| 282 |
interface_exception = None |
| 283 |
try: |
| 284 |
getattr(self.test_interface, self.func)( |
| 285 |
copy_func(input_array), s, **kwargs) |
| 286 |
except Exception as _interface_exception: |
| 287 |
# It's necessary to assign the exception to the |
| 288 |
# already defined variable in Python 3. |
| 289 |
# See http://www.python.org/dev/peps/pep-3110/#semantic-changes |
| 290 |
interface_exception = _interface_exception |
| 291 |
|
| 292 |
# If the test interface raised, so must this. |
| 293 |
self.assertEqual(type(interface_exception), type(e), |
| 294 |
msg='Interface exception raised. ' + |
| 295 |
'Testing for: ' + repr(e)) |
| 296 |
return |
| 297 |
try: |
| 298 |
output_array = getattr(self.test_interface, self.func)( |
| 299 |
copy_func(np_input_array), s, **kwargs) |
| 300 |
except NotImplementedError as e: |
| 301 |
# check if exception due to missing precision |
| 302 |
msg = repr(e) |
| 303 |
if 'Rebuild pyFFTW with support for' in msg: |
| 304 |
self.skipTest(msg) |
| 305 |
else: |
| 306 |
raise |
| 307 |
|
| 308 |
if (functions[self.func] == 'r2c'): |
| 309 |
if numpy.iscomplexobj(input_array): |
| 310 |
if len(w) > 0: |
| 311 |
# Make sure a warning is raised |
| 312 |
self.assertIs( |
| 313 |
w[-1].category, numpy.ComplexWarning) |
| 314 |
|
| 315 |
self.assertTrue( |
| 316 |
numpy.allclose(output_array, test_out_array, |
| 317 |
rtol=1e-2, atol=1e-4)) |
| 318 |
|
| 319 |
if _all_types_np.get(np_input_array.real.dtype, "") in _supported_types: |
| 320 |
# supported precisions should not be converted |
| 321 |
self.assertEqual(np_input_array.real.dtype, |
| 322 |
output_array.real.dtype) |
| 323 |
|
| 324 |
if (not self.overwrite_input_flag in kwargs or |
| 325 |
not kwargs[self.overwrite_input_flag]): |
| 326 |
self.assertTrue(numpy.allclose(input_array, |
| 327 |
orig_input_array)) |
| 328 |
|
| 329 |
return output_array |
| 330 |
|
| 331 |
def axes_from_kwargs(self, kwargs): |
| 332 |
default_args = get_default_args( |
| 333 |
getattr(self.test_interface, self.func)) |
| 334 |
|
| 335 |
if 'axis' in kwargs: |
| 336 |
axes = (kwargs['axis'],) |
| 337 |
|
| 338 |
elif 'axes' in kwargs: |
| 339 |
axes = kwargs['axes'] |
| 340 |
if axes is None: |
| 341 |
axes = default_args['axes'] |
| 342 |
|
| 343 |
else: |
| 344 |
if 'axis' in default_args: |
| 345 |
# default 1D |
| 346 |
axes = (default_args['axis'],) |
| 347 |
else: |
| 348 |
# default nD |
| 349 |
axes = default_args['axes'] |
| 350 |
|
| 351 |
if axes is None: |
| 352 |
axes = (-1,) |
| 353 |
|
| 354 |
return axes |
| 355 |
|
| 356 |
def s_from_kwargs(self, test_shape, kwargs): |
| 357 |
''' Return either a scalar s or a tuple depending on |
| 358 |
whether axis or axes is specified |
| 359 |
''' |
| 360 |
default_args = get_default_args( |
| 361 |
getattr(self.test_interface, self.func)) |
| 362 |
|
| 363 |
if 'axis' in kwargs: |
| 364 |
s = test_shape[kwargs['axis']] |
| 365 |
|
| 366 |
elif 'axes' in kwargs: |
| 367 |
axes = kwargs['axes'] |
| 368 |
if axes is not None: |
| 369 |
s = [] |
| 370 |
for each_axis in axes: |
| 371 |
s.append(test_shape[each_axis]) |
| 372 |
else: |
| 373 |
# default nD |
| 374 |
s = [] |
| 375 |
try: |
| 376 |
for each_axis in default_args['axes']: |
| 377 |
s.append(test_shape[each_axis]) |
| 378 |
except TypeError: |
| 379 |
try: |
| 380 |
s = list(test_shape[ |
| 381 |
self.default_s_from_shape_slicer]) |
| 382 |
except TypeError: |
| 383 |
# We had an integer as the default, so force |
| 384 |
# it to be a list |
| 385 |
s = [test_shape[self.default_s_from_shape_slicer]] |
| 386 |
|
| 387 |
else: |
| 388 |
if 'axis' in default_args: |
| 389 |
# default 1D |
| 390 |
s = test_shape[default_args['axis']] |
| 391 |
else: |
| 392 |
# default nD |
| 393 |
s = [] |
| 394 |
try: |
| 395 |
for each_axis in default_args['axes']: |
| 396 |
s.append(test_shape[each_axis]) |
| 397 |
except TypeError: |
| 398 |
s = None |
| 399 |
|
| 400 |
return s |
| 401 |
|
| 402 |
def test_valid(self): |
| 403 |
dtype_tuple = self.io_dtypes[functions[self.func]] |
| 404 |
|
| 405 |
for dtype in dtype_tuple[0]: |
| 406 |
for test_shape, s, kwargs in self.test_data: |
| 407 |
s = None |
| 408 |
|
| 409 |
self.validate(dtype_tuple[1], |
| 410 |
test_shape, dtype, s, kwargs) |
| 411 |
|
| 412 |
def test_on_non_numpy_array(self): |
| 413 |
dtype_tuple = self.io_dtypes[functions[self.func]] |
| 414 |
|
| 415 |
array_type = (lambda test_shape, dtype: |
| 416 |
dtype_tuple[1](test_shape, dtype).tolist()) |
| 417 |
|
| 418 |
for dtype in dtype_tuple[0]: |
| 419 |
for test_shape, s, kwargs in self.test_data: |
| 420 |
s = None |
| 421 |
|
| 422 |
self.validate(array_type, |
| 423 |
test_shape, dtype, s, kwargs) |
| 424 |
|
| 425 |
|
| 426 |
def test_fail_on_invalid_s_or_axes_or_norm(self): |
| 427 |
dtype_tuple = self.io_dtypes[functions[self.func]] |
| 428 |
|
| 429 |
for dtype in dtype_tuple[0]: |
| 430 |
|
| 431 |
for test_shape, args, exception, e_str in self.invalid_args: |
| 432 |
input_array = dtype_tuple[1](test_shape, dtype) |
| 433 |
|
| 434 |
if len(args) > 2 and not self.has_norm_kwarg: |
| 435 |
# skip tests involving norm argument if it isn't available |
| 436 |
continue |
| 437 |
|
| 438 |
self.assertRaisesRegex(exception, e_str, |
| 439 |
getattr(self.test_interface, self.func), |
| 440 |
*((input_array,) + args)) |
| 441 |
|
| 442 |
|
| 443 |
def test_same_sized_s(self): |
| 444 |
dtype_tuple = self.io_dtypes[functions[self.func]] |
| 445 |
for dtype in dtype_tuple[0]: |
| 446 |
for test_shape, s, kwargs in self.test_data: |
| 447 |
|
| 448 |
self.validate(dtype_tuple[1], |
| 449 |
test_shape, dtype, s, kwargs) |
| 450 |
|
| 451 |
def test_bigger_s(self): |
| 452 |
dtype_tuple = self.io_dtypes[functions[self.func]] |
| 453 |
for dtype in dtype_tuple[0]: |
| 454 |
for test_shape, s, kwargs in self.test_data: |
| 455 |
|
| 456 |
try: |
| 457 |
for each_axis, length in enumerate(s): |
| 458 |
s[each_axis] += 2 |
| 459 |
except TypeError: |
| 460 |
s += 2 |
| 461 |
|
| 462 |
self.validate(dtype_tuple[1], |
| 463 |
test_shape, dtype, s, kwargs) |
| 464 |
|
| 465 |
|
| 466 |
def test_smaller_s(self): |
| 467 |
dtype_tuple = self.io_dtypes[functions[self.func]] |
| 468 |
for dtype in dtype_tuple[0]: |
| 469 |
for test_shape, s, kwargs in self.test_data: |
| 470 |
|
| 471 |
try: |
| 472 |
for each_axis, length in enumerate(s): |
| 473 |
s[each_axis] -= 2 |
| 474 |
except TypeError: |
| 475 |
s -= 2 |
| 476 |
|
| 477 |
self.validate(dtype_tuple[1], |
| 478 |
test_shape, dtype, s, kwargs) |
| 479 |
|
| 480 |
def check_arg(self, arg, arg_test_values, array_type, test_shape, |
| 481 |
dtype, s, kwargs): |
| 482 |
'''Check that the correct arg is passed to the builder''' |
| 483 |
# We trust the builders to work as expected when passed |
| 484 |
# the correct arg (the builders have their own unittests). |
| 485 |
|
| 486 |
return_values = [] |
| 487 |
input_array = array_type(test_shape, dtype) |
| 488 |
|
| 489 |
def fake_fft(*args, **kwargs): |
| 490 |
return_values.append((args, kwargs)) |
| 491 |
return (args, kwargs) |
| 492 |
|
| 493 |
try: |
| 494 |
|
| 495 |
# Replace the function that is to be used |
| 496 |
real_fft = getattr(self.test_interface, self.func) |
| 497 |
setattr(self.test_interface, self.func, fake_fft) |
| 498 |
|
| 499 |
_kwargs = kwargs.copy() |
| 500 |
|
| 501 |
for each_value in arg_test_values: |
| 502 |
_kwargs[arg] = each_value |
| 503 |
builder_args = getattr(self.test_interface, self.func)( |
| 504 |
input_array.copy(), s, **_kwargs) |
| 505 |
|
| 506 |
self.assertTrue(builder_args[1][arg] == each_value) |
| 507 |
|
| 508 |
# make sure it was called |
| 509 |
self.assertTrue(len(return_values) > 0) |
| 510 |
except: |
| 511 |
raise |
| 512 |
|
| 513 |
finally: |
| 514 |
# Make sure we set it back |
| 515 |
setattr(self.test_interface, self.func, real_fft) |
| 516 |
|
| 517 |
# Validate it aswell |
| 518 |
for each_value in arg_test_values: |
| 519 |
_kwargs[arg] = each_value |
| 520 |
builder_args = getattr(self.test_interface, self.func)( |
| 521 |
input_array.copy(), s, **_kwargs) |
| 522 |
|
| 523 |
self.validate(array_type, test_shape, dtype, s, _kwargs) |
| 524 |
|
| 525 |
def test_auto_align_input(self): |
| 526 |
dtype_tuple = self.io_dtypes[functions[self.func]] |
| 527 |
|
| 528 |
for dtype in dtype_tuple[0]: |
| 529 |
for test_shape, s, kwargs in self.test_data: |
| 530 |
self.check_arg('auto_align_input', (True, False), |
| 531 |
dtype_tuple[1], test_shape, dtype, s, kwargs) |
| 532 |
|
| 533 |
def test_auto_contiguous_input(self): |
| 534 |
dtype_tuple = self.io_dtypes[functions[self.func]] |
| 535 |
|
| 536 |
for dtype in dtype_tuple[0]: |
| 537 |
for test_shape, s, kwargs in self.test_data: |
| 538 |
self.check_arg('auto_contiguous', (True, False), |
| 539 |
dtype_tuple[1], test_shape, dtype, s, kwargs) |
| 540 |
|
| 541 |
def test_bigger_and_smaller_s(self): |
| 542 |
dtype_tuple = self.io_dtypes[functions[self.func]] |
| 543 |
for dtype in dtype_tuple[0]: |
| 544 |
i = -1 |
| 545 |
for test_shape, s, kwargs in self.test_data: |
| 546 |
|
| 547 |
try: |
| 548 |
for each_axis, length in enumerate(s): |
| 549 |
s[each_axis] += i * 2 |
| 550 |
i *= i |
| 551 |
except TypeError: |
| 552 |
s += i * 2 |
| 553 |
i *= i |
| 554 |
|
| 555 |
self.validate(dtype_tuple[1], |
| 556 |
test_shape, dtype, s, kwargs) |
| 557 |
|
| 558 |
@unittest.skipIf( |
| 559 |
Version(numpy.version.version) >= Version("2.0"), |
| 560 |
reason="Unsupported for Numpy >=2.0" |
| 561 |
) |
| 562 |
def test_dtype_coercian(self): |
| 563 |
# Make sure we input a dtype that needs to be coerced |
| 564 |
if functions[self.func] == 'r2c': |
| 565 |
dtype_tuple = self.io_dtypes['complex'] |
| 566 |
else: |
| 567 |
dtype_tuple = self.io_dtypes['r2c'] |
| 568 |
|
| 569 |
for dtype in dtype_tuple[0]: |
| 570 |
for test_shape, s, kwargs in self.test_data: |
| 571 |
s = None |
| 572 |
|
| 573 |
self.validate(dtype_tuple[1], |
| 574 |
test_shape, dtype, s, kwargs) |
| 575 |
|
| 576 |
|
| 577 |
def test_planner_effort(self): |
| 578 |
'''Test the planner effort arg |
| 579 |
''' |
| 580 |
dtype_tuple = self.io_dtypes[functions[self.func]] |
| 581 |
test_shape = (16,) |
| 582 |
|
| 583 |
for dtype in dtype_tuple[0]: |
| 584 |
s = None |
| 585 |
if self.axes_kw == 'axis': |
| 586 |
kwargs = {'axis': -1} |
| 587 |
else: |
| 588 |
kwargs = {'axes': (-1,)} |
| 589 |
|
| 590 |
for each_effort in ('FFTW_ESTIMATE', 'FFTW_MEASURE', |
| 591 |
'FFTW_PATIENT', 'FFTW_EXHAUSTIVE'): |
| 592 |
|
| 593 |
kwargs['planner_effort'] = each_effort |
| 594 |
|
| 595 |
self.validate( |
| 596 |
dtype_tuple[1], test_shape, dtype, s, kwargs) |
| 597 |
|
| 598 |
kwargs['planner_effort'] = 'garbage' |
| 599 |
|
| 600 |
self.assertRaisesRegex(ValueError, 'Invalid planner effort', |
| 601 |
self.validate, |
| 602 |
*(dtype_tuple[1], test_shape, dtype, s, kwargs)) |
| 603 |
|
| 604 |
def test_threads_arg(self): |
| 605 |
'''Test the threads argument |
| 606 |
''' |
| 607 |
dtype_tuple = self.io_dtypes[functions[self.func]] |
| 608 |
test_shape = (16,) |
| 609 |
|
| 610 |
for dtype in dtype_tuple[0]: |
| 611 |
s = None |
| 612 |
if self.axes_kw == 'axis': |
| 613 |
kwargs = {'axis': -1} |
| 614 |
else: |
| 615 |
kwargs = {'axes': (-1,)} |
| 616 |
|
| 617 |
self.check_arg(self.threads_arg_name, (1, 2, 5, 10), |
| 618 |
dtype_tuple[1], test_shape, dtype, s, kwargs) |
| 619 |
|
| 620 |
kwargs[self.threads_arg_name] = 'bleh' |
| 621 |
|
| 622 |
# Should not work |
| 623 |
self.assertRaises(TypeError, |
| 624 |
self.validate, |
| 625 |
*(dtype_tuple[1], test_shape, dtype, s, kwargs)) |
| 626 |
|
| 627 |
|
| 628 |
def test_overwrite_input(self): |
| 629 |
'''Test the overwrite_input flag |
| 630 |
''' |
| 631 |
dtype_tuple = self.io_dtypes[functions[self.func]] |
| 632 |
|
| 633 |
for dtype in dtype_tuple[0]: |
| 634 |
for test_shape, s, _kwargs in self.test_data: |
| 635 |
s = None |
| 636 |
|
| 637 |
kwargs = _kwargs.copy() |
| 638 |
self.validate(dtype_tuple[1], test_shape, dtype, s, kwargs) |
| 639 |
|
| 640 |
self.check_arg(self.overwrite_input_flag, (True, False), |
| 641 |
dtype_tuple[1], test_shape, dtype, s, kwargs) |
| 642 |
|
| 643 |
def test_input_maintained(self): |
| 644 |
'''Test to make sure the input is maintained by default. |
| 645 |
''' |
| 646 |
dtype_tuple = self.io_dtypes[functions[self.func]] |
| 647 |
for dtype in dtype_tuple[0]: |
| 648 |
for test_shape, s, kwargs in self.test_data: |
| 649 |
|
| 650 |
input_array = dtype_tuple[1](test_shape, dtype) |
| 651 |
|
| 652 |
orig_input_array = input_array.copy() |
| 653 |
|
| 654 |
getattr(self.test_interface, self.func)( |
| 655 |
input_array, s, **kwargs) |
| 656 |
|
| 657 |
self.assertTrue( |
| 658 |
numpy.all(input_array == orig_input_array)) |
| 659 |
|
| 660 |
def test_on_non_writeable_array_issue_92(self): |
| 661 |
'''Test to make sure that locked arrays work. |
| 662 |
|
| 663 |
Regression test for issue 92. |
| 664 |
''' |
| 665 |
def copy_with_writeable(array_to_copy): |
| 666 |
array_copy = array_to_copy.copy() |
| 667 |
array_copy.flags.writeable = array_to_copy.flags.writeable |
| 668 |
return array_copy |
| 669 |
|
| 670 |
dtype_tuple = self.io_dtypes[functions[self.func]] |
| 671 |
|
| 672 |
def array_type(test_shape, dtype): |
| 673 |
a = dtype_tuple[1](test_shape, dtype) |
| 674 |
a.flags.writeable = False |
| 675 |
return a |
| 676 |
|
| 677 |
for dtype in dtype_tuple[0]: |
| 678 |
for test_shape, s, kwargs in self.test_data: |
| 679 |
s = None |
| 680 |
|
| 681 |
self.validate(array_type, |
| 682 |
test_shape, dtype, s, kwargs, |
| 683 |
copy_func=copy_with_writeable) |
| 684 |
|
| 685 |
def test_overwrite_input_for_issue_92(self): |
| 686 |
'''Tests that trying to overwrite a locked array fails. |
| 687 |
''' |
| 688 |
a = numpy.zeros((4,)) |
| 689 |
a.flags.writeable = False |
| 690 |
self.assertRaisesRegex( |
| 691 |
ValueError, |
| 692 |
'overwrite_input cannot be True when the ' + |
| 693 |
'input array flags.writeable is False', |
| 694 |
interfaces.numpy_fft.fft, |
| 695 |
a, overwrite_input=True) |
| 696 |
|
| 697 |
|
| 698 |
class InterfacesNumpyFFTTestIFFT(InterfacesNumpyFFTTestFFT): |
| 699 |
func = 'ifft' |
| 700 |
|
| 701 |
class InterfacesNumpyFFTTestRFFT(InterfacesNumpyFFTTestFFT): |
| 702 |
func = 'rfft' |
| 703 |
|
| 704 |
class InterfacesNumpyFFTTestIRFFT(InterfacesNumpyFFTTestFFT): |
| 705 |
func = 'irfft' |
| 706 |
realinv = True |
| 707 |
|
| 708 |
class InterfacesNumpyFFTTestHFFT(InterfacesNumpyFFTTestFFT): |
| 709 |
func = 'hfft' |
| 710 |
realinv = True |
| 711 |
|
| 712 |
class InterfacesNumpyFFTTestIHFFT(InterfacesNumpyFFTTestFFT): |
| 713 |
func = 'ihfft' |
| 714 |
|
| 715 |
class InterfacesNumpyFFTTestFFT2(InterfacesNumpyFFTTestFFT): |
| 716 |
axes_kw = 'axes' |
| 717 |
func = 'ifft2' |
| 718 |
if numpy.__version__ >= '1.20.0': |
| 719 |
test_shapes = ( |
| 720 |
((128, 64), {'axes': None}), |
| 721 |
((128, 32), {'axes': None}), |
| 722 |
((128, 32, 4), {'axes': (0, 2)}), |
| 723 |
((59, 100), {'axes': (-2, -1)}), |
| 724 |
((32, 32), {'axes': (-2, -1), 'norm': 'ortho'}), |
| 725 |
((32, 32), {'axes': (-2, -1), 'norm': None}), |
| 726 |
((32, 32), {'axes': (-2, -1), 'norm': 'backward'}), |
| 727 |
((32, 32), {'axes': (-2, -1), 'norm': 'forward'}), |
| 728 |
((64, 128, 16), {'axes': (0, 2)}), |
| 729 |
((4, 6, 8, 4), {'axes': (0, 3)}), |
| 730 |
) |
| 731 |
else: |
| 732 |
test_shapes = ( |
| 733 |
((128, 64), {'axes': None}), |
| 734 |
((128, 32), {'axes': None}), |
| 735 |
((128, 32, 4), {'axes': (0, 2)}), |
| 736 |
((59, 100), {'axes': (-2, -1)}), |
| 737 |
((32, 32), {'axes': (-2, -1), 'norm': 'ortho'}), |
| 738 |
((32, 32), {'axes': (-2, -1), 'norm': None}), |
| 739 |
((64, 128, 16), {'axes': (0, 2)}), |
| 740 |
((4, 6, 8, 4), {'axes': (0, 3)}), |
| 741 |
) |
| 742 |
|
| 743 |
invalid_args = ( |
| 744 |
((100,), ((100, 200),), ValueError, ''), |
| 745 |
((100, 200), ((100, 200, 100),), ValueError, ''), |
| 746 |
((100,), ((100, 200), (-3, -2, -1)), ValueError, ''), |
| 747 |
((100, 200), (100, -1), TypeError, ''), |
| 748 |
((100, 200), ((100, 200), (-3, -2)), IndexError, 'Invalid axes'), |
| 749 |
((100, 200), ((100,), (-3,)), IndexError, 'Invalid axes'), |
| 750 |
# pass invalid normalisation string |
| 751 |
((100, 200), ((100,), (-3,), 'invalid_norm'), ValueError, '')) |
| 752 |
|
| 753 |
def test_shape_and_s_different_lengths(self): |
| 754 |
dtype_tuple = self.io_dtypes[functions[self.func]] |
| 755 |
for dtype in dtype_tuple[0]: |
| 756 |
for test_shape, s, _kwargs in self.test_data: |
| 757 |
kwargs = copy.copy(_kwargs) |
| 758 |
try: |
| 759 |
s = s[1:] |
| 760 |
except TypeError: |
| 761 |
self.skipTest('Not meaningful test on 1d arrays.') |
| 762 |
|
| 763 |
del kwargs['axes'] |
| 764 |
self.validate(dtype_tuple[1], |
| 765 |
test_shape, dtype, s, kwargs) |
| 766 |
|
| 767 |
|
| 768 |
class InterfacesNumpyFFTTestIFFT2(InterfacesNumpyFFTTestFFT2): |
| 769 |
func = 'ifft2' |
| 770 |
|
| 771 |
class InterfacesNumpyFFTTestRFFT2(InterfacesNumpyFFTTestFFT2): |
| 772 |
func = 'rfft2' |
| 773 |
|
| 774 |
class InterfacesNumpyFFTTestIRFFT2(InterfacesNumpyFFTTestFFT2): |
| 775 |
func = 'irfft2' |
| 776 |
realinv = True |
| 777 |
|
| 778 |
class InterfacesNumpyFFTTestFFTN(InterfacesNumpyFFTTestFFT2): |
| 779 |
func = 'ifftn' |
| 780 |
if numpy.__version__ >= '1.20.0': |
| 781 |
test_shapes = ( |
| 782 |
((128, 32, 4), {'axes': None}), |
| 783 |
((64, 128, 16), {'axes': (0, 1, 2)}), |
| 784 |
((4, 6, 8, 4), {'axes': (0, 3, 1)}), |
| 785 |
((4, 6, 4, 4), {'axes': (0, 3, 1), 'norm': 'ortho'}), |
| 786 |
((4, 6, 4, 4), {'axes': (0, 3, 1), 'norm': None}), |
| 787 |
((4, 6, 4, 4), {'axes': (0, 3, 1), 'norm': 'backward'}), |
| 788 |
((4, 6, 4, 4), {'axes': (0, 3, 1), 'norm': 'forward'}), |
| 789 |
((4, 6, 8, 4), {'axes': (0, 3, 1, 2)}), |
| 790 |
) |
| 791 |
else: |
| 792 |
test_shapes = ( |
| 793 |
((128, 32, 4), {'axes': None}), |
| 794 |
((64, 128, 16), {'axes': (0, 1, 2)}), |
| 795 |
((4, 6, 8, 4), {'axes': (0, 3, 1)}), |
| 796 |
((4, 6, 4, 4), {'axes': (0, 3, 1), 'norm': 'ortho'}), |
| 797 |
((4, 6, 4, 4), {'axes': (0, 3, 1), 'norm': None}), |
| 798 |
((4, 6, 8, 4), {'axes': (0, 3, 1, 2)}), |
| 799 |
) |
| 800 |
|
| 801 |
class InterfacesNumpyFFTTestIFFTN(InterfacesNumpyFFTTestFFTN): |
| 802 |
func = 'ifftn' |
| 803 |
|
| 804 |
class InterfacesNumpyFFTTestRFFTN(InterfacesNumpyFFTTestFFTN): |
| 805 |
func = 'rfftn' |
| 806 |
|
| 807 |
class InterfacesNumpyFFTTestIRFFTN(InterfacesNumpyFFTTestFFTN): |
| 808 |
func = 'irfftn' |
| 809 |
realinv = True |
| 810 |
|
| 811 |
test_cases = ( |
| 812 |
InterfacesNumpyFFTTestModule, |
| 813 |
InterfacesNumpyFFTTestFFT, |
| 814 |
InterfacesNumpyFFTTestIFFT, |
| 815 |
InterfacesNumpyFFTTestRFFT, |
| 816 |
InterfacesNumpyFFTTestIRFFT, |
| 817 |
InterfacesNumpyFFTTestHFFT, |
| 818 |
InterfacesNumpyFFTTestIHFFT, |
| 819 |
InterfacesNumpyFFTTestFFT2, |
| 820 |
InterfacesNumpyFFTTestIFFT2, |
| 821 |
InterfacesNumpyFFTTestRFFT2, |
| 822 |
InterfacesNumpyFFTTestIRFFT2, |
| 823 |
InterfacesNumpyFFTTestFFTN, |
| 824 |
InterfacesNumpyFFTTestIFFTN, |
| 825 |
InterfacesNumpyFFTTestRFFTN, |
| 826 |
InterfacesNumpyFFTTestIRFFTN,) |
| 827 |
|
| 828 |
#test_set = {'InterfacesNumpyFFTTestHFFT': ('test_valid',)} |
| 829 |
test_set = None |
| 830 |
|
| 831 |
if __name__ == '__main__': |
| 832 |
|
| 833 |
run_test_suites(test_cases, test_set) |