| 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 builders, empty_aligned, byte_align, FFTW |
| 36 |
from pyfftw import _supported_nptypes_complex, _supported_nptypes_real |
| 37 |
from pyfftw.builders import _utils as utils |
| 38 |
from .test_pyfftw_base import run_test_suites, require |
| 39 |
from ._get_default_args import get_default_args |
| 40 |
|
| 41 |
import unittest |
| 42 |
import numpy |
| 43 |
import numpy as np |
| 44 |
# import the numpy fft routines having the rfft normalization bug fix |
| 45 |
from .test_pyfftw_numpy_interface import np_fft, _numpy_fft_has_norm_kwarg |
| 46 |
import copy |
| 47 |
import warnings |
| 48 |
warnings.filterwarnings('always') |
| 49 |
|
| 50 |
from packaging.version import Version |
| 51 |
|
| 52 |
complex_dtypes = _supported_nptypes_complex |
| 53 |
real_dtypes = _supported_nptypes_real |
| 54 |
|
| 55 |
def make_complex_data(shape, dtype): |
| 56 |
ar, ai = numpy.random.randn(2, *shape).astype(dtype) |
| 57 |
return ar + 1j*ai |
| 58 |
|
| 59 |
def make_real_data(shape, dtype): |
| 60 |
return numpy.random.randn(*shape).astype(dtype) |
| 61 |
|
| 62 |
|
| 63 |
input_dtypes = { |
| 64 |
'complex': (complex_dtypes, make_complex_data), |
| 65 |
'r2c': (real_dtypes, make_real_data), |
| 66 |
'c2r': (complex_dtypes, make_complex_data)} |
| 67 |
|
| 68 |
output_dtypes = { |
| 69 |
'complex': complex_dtypes, |
| 70 |
'r2c': complex_dtypes, |
| 71 |
'c2r': real_dtypes} |
| 72 |
|
| 73 |
functions = { |
| 74 |
'fft': 'complex', |
| 75 |
'ifft': 'complex', |
| 76 |
'rfft': 'r2c', |
| 77 |
'irfft': 'c2r', |
| 78 |
'rfftn': 'r2c', |
| 79 |
'irfftn': 'c2r', |
| 80 |
'rfft2': 'r2c', |
| 81 |
'irfft2': 'c2r', |
| 82 |
'fft2': 'complex', |
| 83 |
'ifft2': 'complex', |
| 84 |
'fftn': 'complex', |
| 85 |
'ifftn': 'complex'} |
| 86 |
|
| 87 |
|
| 88 |
class BuildersTestFFT(unittest.TestCase): |
| 89 |
|
| 90 |
func = 'fft' |
| 91 |
axes_kw = 'axis' |
| 92 |
if numpy.__version__ >= '1.20.0': |
| 93 |
test_shapes = ( |
| 94 |
((100,), {}), |
| 95 |
((128, 64), {'axis': 0}), |
| 96 |
((128, 32), {'axis': -1}), |
| 97 |
((59, 100), {}), |
| 98 |
((32, 32, 4), {'axis': 1}), |
| 99 |
((32, 32, 4), {'axis': 1, 'norm': 'ortho'}), |
| 100 |
((32, 32, 4), {'axis': 1, 'norm': None}), |
| 101 |
((32, 32, 4), {'axis': 1, 'norm': 'backward'}), |
| 102 |
((32, 32, 4), {'axis': 1, 'norm': 'forward'}), |
| 103 |
((64, 128, 16), {}), |
| 104 |
) |
| 105 |
else: |
| 106 |
test_shapes = ( |
| 107 |
((100,), {}), |
| 108 |
((128, 64), {'axis': 0}), |
| 109 |
((128, 32), {'axis': -1}), |
| 110 |
((59, 100), {}), |
| 111 |
((32, 32, 4), {'axis': 1}), |
| 112 |
((32, 32, 4), {'axis': 1, 'norm': 'ortho'}), |
| 113 |
((32, 32, 4), {'axis': 1, 'norm': None}), |
| 114 |
((64, 128, 16), {}), |
| 115 |
) |
| 116 |
|
| 117 |
# invalid_s_shapes is: |
| 118 |
# (size, invalid_args, error_type, error_string) |
| 119 |
invalid_args = ( |
| 120 |
((100,), ((100, 200),), TypeError, ''), |
| 121 |
((100, 200), ((100, 200),), TypeError, ''), |
| 122 |
((100,), (100, (-2, -1)), TypeError, ''), |
| 123 |
((100,), (100, -20), IndexError, '')) |
| 124 |
|
| 125 |
realinv = False |
| 126 |
has_norm_kwarg = _numpy_fft_has_norm_kwarg() |
| 127 |
|
| 128 |
def __init__(self, *args, **kwargs): |
| 129 |
|
| 130 |
super(BuildersTestFFT, self).__init__(*args, **kwargs) |
| 131 |
|
| 132 |
if not hasattr(self, 'assertRaisesRegex'): |
| 133 |
self.assertRaisesRegex = self.assertRaisesRegexp |
| 134 |
|
| 135 |
@property |
| 136 |
def test_data(self): |
| 137 |
for test_shape, kwargs in self.test_shapes: |
| 138 |
axes = self.axes_from_kwargs(kwargs) |
| 139 |
s = self.s_from_kwargs(test_shape, kwargs) |
| 140 |
|
| 141 |
if not self.has_norm_kwarg and 'norm' in kwargs: |
| 142 |
kwargs.pop('norm') |
| 143 |
|
| 144 |
if self.realinv: |
| 145 |
test_shape = list(test_shape) |
| 146 |
test_shape[axes[-1]] = test_shape[axes[-1]]//2 + 1 |
| 147 |
test_shape = tuple(test_shape) |
| 148 |
|
| 149 |
yield test_shape, s, kwargs |
| 150 |
|
| 151 |
def validate_pyfftw_object(self, array_type, test_shape, dtype, |
| 152 |
s, kwargs): |
| 153 |
|
| 154 |
input_array = array_type(test_shape, dtype) |
| 155 |
|
| 156 |
# Use char because of potential MSVC related bug. |
| 157 |
if input_array.dtype.char == np.dtype('clongdouble').char: |
| 158 |
np_input_array = numpy.complex128(input_array) |
| 159 |
|
| 160 |
elif input_array.dtype.char == np.dtype('longdouble').char: |
| 161 |
np_input_array = numpy.float64(input_array) |
| 162 |
|
| 163 |
else: |
| 164 |
np_input_array = input_array |
| 165 |
|
| 166 |
with warnings.catch_warnings(record=True) as w: |
| 167 |
# We catch the warnings so as to pick up on when |
| 168 |
# a complex array is turned into a real array |
| 169 |
|
| 170 |
FFTW_object = getattr(builders, self.func)( |
| 171 |
input_array.copy(), s, **kwargs) |
| 172 |
|
| 173 |
# We run FFT twice to check two operations don't |
| 174 |
# yield different results (which they might if |
| 175 |
# the state is buggered up). |
| 176 |
output_array = FFTW_object(input_array.copy()) |
| 177 |
output_array_2 = FFTW_object(input_array.copy()) |
| 178 |
|
| 179 |
if 'axes' in kwargs: |
| 180 |
axes = {'axes': kwargs['axes']} |
| 181 |
elif 'axis' in kwargs: |
| 182 |
axes = {'axis': kwargs['axis']} |
| 183 |
else: |
| 184 |
axes = {} |
| 185 |
|
| 186 |
if self.has_norm_kwarg and 'norm' in kwargs: |
| 187 |
axes['norm'] = kwargs['norm'] |
| 188 |
|
| 189 |
test_out_array = getattr(np_fft, self.func)( |
| 190 |
np_input_array.copy(), s, **axes) |
| 191 |
|
| 192 |
if (functions[self.func] == 'r2c'): |
| 193 |
if numpy.iscomplexobj(input_array): |
| 194 |
if len(w) > 0: |
| 195 |
# Make sure a warning is raised |
| 196 |
self.assertIs( |
| 197 |
w[-1].category, numpy.ComplexWarning) |
| 198 |
|
| 199 |
self.assertTrue( |
| 200 |
numpy.allclose(output_array, test_out_array, |
| 201 |
rtol=1e-2, atol=1e-4)) |
| 202 |
|
| 203 |
self.assertTrue( |
| 204 |
numpy.allclose(output_array_2, test_out_array, |
| 205 |
rtol=1e-2, atol=1e-4)) |
| 206 |
|
| 207 |
return FFTW_object |
| 208 |
|
| 209 |
def axes_from_kwargs(self, kwargs): |
| 210 |
|
| 211 |
default_args = get_default_args(getattr(builders, self.func)) |
| 212 |
|
| 213 |
if 'axis' in kwargs: |
| 214 |
axes = (kwargs['axis'],) |
| 215 |
|
| 216 |
elif 'axes' in kwargs: |
| 217 |
axes = kwargs['axes'] |
| 218 |
if axes is None: |
| 219 |
axes = default_args['axes'] |
| 220 |
|
| 221 |
else: |
| 222 |
if 'axis' in default_args: |
| 223 |
# default 1D |
| 224 |
axes = (default_args['axis'],) |
| 225 |
else: |
| 226 |
# default nD |
| 227 |
axes = default_args['axes'] |
| 228 |
|
| 229 |
if axes is None: |
| 230 |
axes = (-1,) |
| 231 |
|
| 232 |
return axes |
| 233 |
|
| 234 |
def s_from_kwargs(self, test_shape, kwargs): |
| 235 |
''' Return either a scalar s or a tuple depending on |
| 236 |
whether axis or axes is specified |
| 237 |
''' |
| 238 |
default_args = get_default_args(getattr(builders, self.func)) |
| 239 |
|
| 240 |
if 'axis' in kwargs: |
| 241 |
s = test_shape[kwargs['axis']] |
| 242 |
|
| 243 |
elif 'axes' in kwargs: |
| 244 |
axes = kwargs['axes'] |
| 245 |
if axes is not None: |
| 246 |
s = [] |
| 247 |
for each_axis in axes: |
| 248 |
s.append(test_shape[each_axis]) |
| 249 |
else: |
| 250 |
# default nD |
| 251 |
s = [] |
| 252 |
try: |
| 253 |
for each_axis in default_args['axes']: |
| 254 |
s.append(test_shape[each_axis]) |
| 255 |
except TypeError: |
| 256 |
s = [test_shape[-1]] |
| 257 |
|
| 258 |
else: |
| 259 |
if 'axis' in default_args: |
| 260 |
# default 1D |
| 261 |
s = test_shape[default_args['axis']] |
| 262 |
else: |
| 263 |
# default nD |
| 264 |
s = [] |
| 265 |
try: |
| 266 |
for each_axis in default_args['axes']: |
| 267 |
s.append(test_shape[each_axis]) |
| 268 |
except TypeError: |
| 269 |
s = None |
| 270 |
|
| 271 |
return s |
| 272 |
|
| 273 |
def test_valid(self): |
| 274 |
dtype_tuple = input_dtypes[functions[self.func]] |
| 275 |
|
| 276 |
for dtype in dtype_tuple[0]: |
| 277 |
for test_shape, s, kwargs in self.test_data: |
| 278 |
s = None |
| 279 |
|
| 280 |
FFTW_object = self.validate_pyfftw_object(dtype_tuple[1], |
| 281 |
test_shape, dtype, s, kwargs) |
| 282 |
|
| 283 |
self.assertTrue(type(FFTW_object) == FFTW) |
| 284 |
|
| 285 |
def test_output_dtype_correct(self): |
| 286 |
'''The output dtype should be correct given the input dtype. |
| 287 |
|
| 288 |
It was noted that this is a particular problem on windows 64 |
| 289 |
due longdouble being mapped to double, but the dtype().char attribute |
| 290 |
still being different. |
| 291 |
''' |
| 292 |
inp_dtype_tuple = input_dtypes[functions[self.func]] |
| 293 |
output_dtype_tuple = output_dtypes[functions[self.func]] |
| 294 |
|
| 295 |
for input_dtype, output_dtype in zip(inp_dtype_tuple[0], |
| 296 |
output_dtype_tuple): |
| 297 |
|
| 298 |
for test_shape, s, kwargs in self.test_data: |
| 299 |
s = None |
| 300 |
|
| 301 |
FFTW_object = self.validate_pyfftw_object(inp_dtype_tuple[1], |
| 302 |
test_shape, input_dtype, s, kwargs) |
| 303 |
|
| 304 |
self.assertTrue( |
| 305 |
FFTW_object.output_array.dtype.char == |
| 306 |
np.dtype(output_dtype).char) |
| 307 |
|
| 308 |
def test_fail_on_invalid_s_or_axes(self): |
| 309 |
dtype_tuple = input_dtypes[functions[self.func]] |
| 310 |
|
| 311 |
for dtype in dtype_tuple[0]: |
| 312 |
|
| 313 |
for test_shape, args, exception, e_str in self.invalid_args: |
| 314 |
input_array = dtype_tuple[1](test_shape, dtype) |
| 315 |
|
| 316 |
self.assertRaisesRegex(exception, e_str, |
| 317 |
getattr(builders, self.func), |
| 318 |
*((input_array,) + args)) |
| 319 |
|
| 320 |
|
| 321 |
def test_same_sized_s(self): |
| 322 |
dtype_tuple = input_dtypes[functions[self.func]] |
| 323 |
for dtype in dtype_tuple[0]: |
| 324 |
for test_shape, s, kwargs in self.test_data: |
| 325 |
|
| 326 |
FFTW_object = self.validate_pyfftw_object(dtype_tuple[1], |
| 327 |
test_shape, dtype, s, kwargs) |
| 328 |
|
| 329 |
self.assertTrue(type(FFTW_object) == FFTW) |
| 330 |
|
| 331 |
def test_bigger_s_overwrite_input(self): |
| 332 |
'''Test that FFTWWrapper deals with a destroyed input properly. |
| 333 |
''' |
| 334 |
dtype_tuple = input_dtypes[functions[self.func]] |
| 335 |
for dtype in dtype_tuple[0]: |
| 336 |
for test_shape, s, kwargs in self.test_data: |
| 337 |
|
| 338 |
try: |
| 339 |
for each_axis, length in enumerate(s): |
| 340 |
s[each_axis] += 2 |
| 341 |
except TypeError: |
| 342 |
s += 2 |
| 343 |
|
| 344 |
_kwargs = kwargs.copy() |
| 345 |
|
| 346 |
if self.func not in ('irfft2', 'irfftn'): |
| 347 |
# They implicitly overwrite the input anyway |
| 348 |
_kwargs['overwrite_input'] = True |
| 349 |
|
| 350 |
FFTW_object = self.validate_pyfftw_object(dtype_tuple[1], |
| 351 |
test_shape, dtype, s, _kwargs) |
| 352 |
|
| 353 |
self.assertTrue( |
| 354 |
type(FFTW_object) == utils._FFTWWrapper) |
| 355 |
|
| 356 |
def test_bigger_s(self): |
| 357 |
dtype_tuple = input_dtypes[functions[self.func]] |
| 358 |
for dtype in dtype_tuple[0]: |
| 359 |
for test_shape, s, kwargs in self.test_data: |
| 360 |
|
| 361 |
try: |
| 362 |
for each_axis, length in enumerate(s): |
| 363 |
s[each_axis] += 2 |
| 364 |
except TypeError: |
| 365 |
s += 2 |
| 366 |
|
| 367 |
FFTW_object = self.validate_pyfftw_object(dtype_tuple[1], |
| 368 |
test_shape, dtype, s, kwargs) |
| 369 |
|
| 370 |
self.assertTrue( |
| 371 |
type(FFTW_object) == utils._FFTWWrapper) |
| 372 |
|
| 373 |
def test_smaller_s(self): |
| 374 |
dtype_tuple = input_dtypes[functions[self.func]] |
| 375 |
for dtype in dtype_tuple[0]: |
| 376 |
for test_shape, s, kwargs in self.test_data: |
| 377 |
|
| 378 |
try: |
| 379 |
for each_axis, length in enumerate(s): |
| 380 |
s[each_axis] -= 2 |
| 381 |
except TypeError: |
| 382 |
s -= 2 |
| 383 |
|
| 384 |
FFTW_object = self.validate_pyfftw_object(dtype_tuple[1], |
| 385 |
test_shape, dtype, s, kwargs) |
| 386 |
|
| 387 |
self.assertTrue( |
| 388 |
type(FFTW_object) == utils._FFTWWrapper) |
| 389 |
|
| 390 |
def test_bigger_and_smaller_s(self): |
| 391 |
dtype_tuple = input_dtypes[functions[self.func]] |
| 392 |
for dtype in dtype_tuple[0]: |
| 393 |
i = -1 |
| 394 |
for test_shape, s, kwargs in self.test_data: |
| 395 |
|
| 396 |
try: |
| 397 |
for each_axis, length in enumerate(s): |
| 398 |
s[each_axis] += i * 2 |
| 399 |
i *= i |
| 400 |
except TypeError: |
| 401 |
s += i * 2 |
| 402 |
i *= i |
| 403 |
|
| 404 |
FFTW_object = self.validate_pyfftw_object(dtype_tuple[1], |
| 405 |
test_shape, dtype, s, kwargs) |
| 406 |
|
| 407 |
self.assertTrue( |
| 408 |
type(FFTW_object) == utils._FFTWWrapper) |
| 409 |
|
| 410 |
def test_auto_contiguous_input(self): |
| 411 |
dtype_tuple = input_dtypes[functions[self.func]] |
| 412 |
|
| 413 |
for dtype in dtype_tuple[0]: |
| 414 |
for test_shape, s, kwargs in self.test_data: |
| 415 |
_kwargs = kwargs.copy() |
| 416 |
s1 = None |
| 417 |
s2 = copy.copy(s) |
| 418 |
try: |
| 419 |
for each_axis, length in enumerate(s): |
| 420 |
s2[each_axis] += 2 |
| 421 |
except TypeError: |
| 422 |
s2 += 2 |
| 423 |
|
| 424 |
_test_shape = [] |
| 425 |
slices = [] |
| 426 |
for each_dim in test_shape: |
| 427 |
_test_shape.append(each_dim*2) |
| 428 |
slices.append(slice(None, None, 2)) |
| 429 |
slices = tuple(slices) |
| 430 |
|
| 431 |
input_array = dtype_tuple[1](_test_shape, dtype)[slices] |
| 432 |
# check the input is non contiguous |
| 433 |
self.assertFalse(input_array.flags['C_CONTIGUOUS'] or |
| 434 |
input_array.flags['F_CONTIGUOUS']) |
| 435 |
|
| 436 |
|
| 437 |
# Firstly check the non-contiguous case (for both |
| 438 |
# FFTW and _FFTWWrapper) |
| 439 |
_kwargs['auto_contiguous'] = False |
| 440 |
|
| 441 |
# We also need to make sure we're not copying due |
| 442 |
# to a trivial misalignment |
| 443 |
_kwargs['auto_align_input'] = False |
| 444 |
|
| 445 |
FFTW_object = getattr(builders, self.func)( |
| 446 |
input_array, s1, **_kwargs) |
| 447 |
|
| 448 |
internal_input_array = FFTW_object.input_array |
| 449 |
flags = internal_input_array.flags |
| 450 |
self.assertTrue(input_array is internal_input_array) |
| 451 |
self.assertFalse(flags['C_CONTIGUOUS'] or |
| 452 |
flags['F_CONTIGUOUS']) |
| 453 |
|
| 454 |
FFTW_object = getattr(builders, self.func)( |
| 455 |
input_array, s2, **_kwargs) |
| 456 |
|
| 457 |
internal_input_array = FFTW_object.input_array |
| 458 |
flags = internal_input_array.flags |
| 459 |
# We actually expect the _FFTWWrapper to be C_CONTIGUOUS |
| 460 |
self.assertTrue(flags['C_CONTIGUOUS']) |
| 461 |
|
| 462 |
# Now for the contiguous case (for both |
| 463 |
# FFTW and _FFTWWrapper) |
| 464 |
_kwargs['auto_contiguous'] = True |
| 465 |
FFTW_object = getattr(builders, self.func)( |
| 466 |
input_array, s1, **_kwargs) |
| 467 |
|
| 468 |
internal_input_array = FFTW_object.input_array |
| 469 |
flags = internal_input_array.flags |
| 470 |
self.assertTrue(flags['C_CONTIGUOUS'] or |
| 471 |
flags['F_CONTIGUOUS']) |
| 472 |
|
| 473 |
FFTW_object = getattr(builders, self.func)( |
| 474 |
input_array, s2, **_kwargs) |
| 475 |
|
| 476 |
internal_input_array = FFTW_object.input_array |
| 477 |
flags = internal_input_array.flags |
| 478 |
# as above |
| 479 |
self.assertTrue(flags['C_CONTIGUOUS']) |
| 480 |
|
| 481 |
|
| 482 |
def test_auto_align_input(self): |
| 483 |
dtype_tuple = input_dtypes[functions[self.func]] |
| 484 |
|
| 485 |
for dtype in dtype_tuple[0]: |
| 486 |
for test_shape, s, kwargs in self.test_data: |
| 487 |
_kwargs = kwargs.copy() |
| 488 |
s1 = None |
| 489 |
s2 = copy.copy(s) |
| 490 |
try: |
| 491 |
for each_axis, length in enumerate(s): |
| 492 |
s2[each_axis] += 2 |
| 493 |
except TypeError: |
| 494 |
s2 += 2 |
| 495 |
|
| 496 |
input_array = dtype_tuple[1](test_shape, dtype) |
| 497 |
|
| 498 |
# Firstly check the unaligned case (for both |
| 499 |
# FFTW and _FFTWWrapper) |
| 500 |
_kwargs['auto_align_input'] = False |
| 501 |
FFTW_object = getattr(builders, self.func)( |
| 502 |
input_array.copy(), s1, **_kwargs) |
| 503 |
|
| 504 |
self.assertFalse(FFTW_object.simd_aligned) |
| 505 |
|
| 506 |
FFTW_object = getattr(builders, self.func)( |
| 507 |
input_array.copy(), s2, **_kwargs) |
| 508 |
|
| 509 |
self.assertFalse(FFTW_object.simd_aligned) |
| 510 |
|
| 511 |
# Now for the aligned case (for both |
| 512 |
# FFTW and _FFTWWrapper) |
| 513 |
_kwargs['auto_align_input'] = True |
| 514 |
FFTW_object = getattr(builders, self.func)( |
| 515 |
input_array.copy(), s1, **_kwargs) |
| 516 |
|
| 517 |
self.assertTrue(FFTW_object.simd_aligned) |
| 518 |
|
| 519 |
self.assertTrue('FFTW_UNALIGNED' not in FFTW_object.flags) |
| 520 |
FFTW_object = getattr(builders, self.func)( |
| 521 |
input_array.copy(), s2, **_kwargs) |
| 522 |
|
| 523 |
self.assertTrue(FFTW_object.simd_aligned) |
| 524 |
|
| 525 |
self.assertTrue('FFTW_UNALIGNED' not in FFTW_object.flags) |
| 526 |
|
| 527 |
@unittest.skipIf( |
| 528 |
Version(numpy.version.version) >= Version("2.0"), |
| 529 |
reason="Unsupported for Numpy >=2.0" |
| 530 |
) |
| 531 |
def test_dtype_coercian(self): |
| 532 |
# Make sure we input a dtype that needs to be coerced |
| 533 |
if functions[self.func] == 'r2c': |
| 534 |
dtype_tuple = input_dtypes['complex'] |
| 535 |
else: |
| 536 |
dtype_tuple = input_dtypes['r2c'] |
| 537 |
|
| 538 |
for dtype in dtype_tuple[0]: |
| 539 |
for test_shape, s, kwargs in self.test_data: |
| 540 |
s = None |
| 541 |
|
| 542 |
FFTW_object = self.validate_pyfftw_object(dtype_tuple[1], |
| 543 |
test_shape, dtype, s, kwargs) |
| 544 |
|
| 545 |
self.assertTrue(type(FFTW_object) == FFTW) |
| 546 |
|
| 547 |
def test_persistent_padding(self): |
| 548 |
'''Test to confirm the padding it not touched after creation. |
| 549 |
''' |
| 550 |
dtype_tuple = input_dtypes[functions[self.func]] |
| 551 |
for dtype in dtype_tuple[0]: |
| 552 |
for test_shape, s, kwargs in self.test_data: |
| 553 |
|
| 554 |
n_add = 2 |
| 555 |
# these slicers get the padding |
| 556 |
# from the internal input array |
| 557 |
padding_slicer = [slice(None)] * len(test_shape) |
| 558 |
axes = self.axes_from_kwargs(kwargs) |
| 559 |
try: |
| 560 |
for each_axis, length in enumerate(s): |
| 561 |
s[each_axis] += n_add |
| 562 |
padding_slicer[axes[each_axis]] = ( |
| 563 |
slice(s[each_axis], None)) |
| 564 |
|
| 565 |
except TypeError: |
| 566 |
s += n_add |
| 567 |
padding_slicer[axes[0]] = slice(s, None) |
| 568 |
padding_slicer = tuple(padding_slicer) |
| 569 |
# Get a valid object |
| 570 |
FFTW_object = self.validate_pyfftw_object(dtype_tuple[1], |
| 571 |
test_shape, dtype, s, kwargs) |
| 572 |
|
| 573 |
internal_array = FFTW_object.input_array |
| 574 |
padding = internal_array[padding_slicer] |
| 575 |
|
| 576 |
# Fill the padding with garbage |
| 577 |
initial_padding = dtype_tuple[1](padding.shape, dtype) |
| 578 |
|
| 579 |
padding[:] = initial_padding |
| 580 |
|
| 581 |
# Now confirm that nothing is done to the padding |
| 582 |
FFTW_object() |
| 583 |
|
| 584 |
final_padding = FFTW_object.input_array[padding_slicer] |
| 585 |
|
| 586 |
self.assertTrue(numpy.all(final_padding == initial_padding)) |
| 587 |
|
| 588 |
def test_planner_effort(self): |
| 589 |
'''Test the planner effort arg |
| 590 |
''' |
| 591 |
dtype_tuple = input_dtypes[functions[self.func]] |
| 592 |
test_shape = (16,) |
| 593 |
|
| 594 |
for dtype in dtype_tuple[0]: |
| 595 |
s = None |
| 596 |
if self.axes_kw == 'axis': |
| 597 |
kwargs = {'axis': -1} |
| 598 |
else: |
| 599 |
kwargs = {'axes': (-1,)} |
| 600 |
|
| 601 |
for each_effort in ('FFTW_ESTIMATE', 'FFTW_MEASURE', |
| 602 |
'FFTW_PATIENT', 'FFTW_EXHAUSTIVE'): |
| 603 |
|
| 604 |
kwargs['planner_effort'] = each_effort |
| 605 |
|
| 606 |
FFTW_object = self.validate_pyfftw_object( |
| 607 |
dtype_tuple[1], test_shape, dtype, s, kwargs) |
| 608 |
|
| 609 |
self.assertTrue(each_effort in FFTW_object.flags) |
| 610 |
|
| 611 |
kwargs['planner_effort'] = 'garbage' |
| 612 |
|
| 613 |
self.assertRaisesRegex(ValueError, 'Invalid planner effort', |
| 614 |
self.validate_pyfftw_object, |
| 615 |
*(dtype_tuple[1], test_shape, dtype, s, kwargs)) |
| 616 |
|
| 617 |
def test_threads_arg(self): |
| 618 |
'''Test the threads argument |
| 619 |
''' |
| 620 |
dtype_tuple = input_dtypes[functions[self.func]] |
| 621 |
test_shape = (16,) |
| 622 |
|
| 623 |
for dtype in dtype_tuple[0]: |
| 624 |
s = None |
| 625 |
if self.axes_kw == 'axis': |
| 626 |
kwargs = {'axis': -1} |
| 627 |
else: |
| 628 |
kwargs = {'axes': (-1,)} |
| 629 |
|
| 630 |
kwargs['threads'] = 2 |
| 631 |
|
| 632 |
# Should just work |
| 633 |
FFTW_object = self.validate_pyfftw_object( |
| 634 |
dtype_tuple[1], test_shape, dtype, s, kwargs) |
| 635 |
|
| 636 |
kwargs['threads'] = 'bleh' |
| 637 |
|
| 638 |
# Should not work |
| 639 |
self.assertRaises(TypeError, |
| 640 |
self.validate_pyfftw_object, |
| 641 |
*(dtype_tuple[1], test_shape, dtype, s, kwargs)) |
| 642 |
|
| 643 |
|
| 644 |
def test_overwrite_input(self): |
| 645 |
'''Test the overwrite_input flag |
| 646 |
''' |
| 647 |
dtype_tuple = input_dtypes[functions[self.func]] |
| 648 |
|
| 649 |
for dtype in dtype_tuple[0]: |
| 650 |
for test_shape, s, _kwargs in self.test_data: |
| 651 |
s = None |
| 652 |
|
| 653 |
kwargs = _kwargs.copy() |
| 654 |
FFTW_object = self.validate_pyfftw_object(dtype_tuple[1], |
| 655 |
test_shape, dtype, s, kwargs) |
| 656 |
|
| 657 |
if self.func not in ('irfft2', 'irfftn'): |
| 658 |
self.assertTrue( |
| 659 |
'FFTW_DESTROY_INPUT' not in FFTW_object.flags) |
| 660 |
|
| 661 |
kwargs['overwrite_input'] = True |
| 662 |
|
| 663 |
FFTW_object = self.validate_pyfftw_object( |
| 664 |
dtype_tuple[1], test_shape, dtype, s, kwargs) |
| 665 |
|
| 666 |
self.assertTrue('FFTW_DESTROY_INPUT' in FFTW_object.flags) |
| 667 |
|
| 668 |
|
| 669 |
def test_input_maintained(self): |
| 670 |
'''Test to make sure the input is maintained |
| 671 |
''' |
| 672 |
dtype_tuple = input_dtypes[functions[self.func]] |
| 673 |
for dtype in dtype_tuple[0]: |
| 674 |
for test_shape, s, kwargs in self.test_data: |
| 675 |
|
| 676 |
input_array = dtype_tuple[1](test_shape, dtype) |
| 677 |
|
| 678 |
FFTW_object = getattr( |
| 679 |
builders, self.func)(input_array, s, **kwargs) |
| 680 |
|
| 681 |
final_input_array = FFTW_object.input_array |
| 682 |
|
| 683 |
self.assertTrue( |
| 684 |
numpy.all(input_array == final_input_array)) |
| 685 |
|
| 686 |
def test_avoid_copy(self): |
| 687 |
'''Test the avoid_copy flag |
| 688 |
''' |
| 689 |
dtype_tuple = input_dtypes[functions[self.func]] |
| 690 |
|
| 691 |
for dtype in dtype_tuple[0]: |
| 692 |
for test_shape, s, kwargs in self.test_data: |
| 693 |
_kwargs = kwargs.copy() |
| 694 |
|
| 695 |
_kwargs['avoid_copy'] = True |
| 696 |
|
| 697 |
s2 = copy.copy(s) |
| 698 |
try: |
| 699 |
for each_axis, length in enumerate(s): |
| 700 |
s2[each_axis] += 2 |
| 701 |
except TypeError: |
| 702 |
s2 += 2 |
| 703 |
|
| 704 |
input_array = dtype_tuple[1](test_shape, dtype) |
| 705 |
|
| 706 |
self.assertRaisesRegex(ValueError, |
| 707 |
'Cannot avoid copy.*transform shape.*', |
| 708 |
getattr(builders, self.func), |
| 709 |
input_array, s2, **_kwargs) |
| 710 |
|
| 711 |
non_contiguous_shape = [ |
| 712 |
each_dim * 2 for each_dim in test_shape] |
| 713 |
non_contiguous_slices = tuple( |
| 714 |
[slice(None, None, 2)] * len(test_shape)) |
| 715 |
|
| 716 |
misaligned_input_array = dtype_tuple[1]( |
| 717 |
non_contiguous_shape, dtype)[non_contiguous_slices] |
| 718 |
|
| 719 |
self.assertRaisesRegex(ValueError, |
| 720 |
'Cannot avoid copy.*not contiguous.*', |
| 721 |
getattr(builders, self.func), |
| 722 |
misaligned_input_array, s, **_kwargs) |
| 723 |
|
| 724 |
# Offset by one from 16 byte aligned to guarantee it's not |
| 725 |
# 16 byte aligned |
| 726 |
_input_array = empty_aligned( |
| 727 |
numpy.prod(test_shape)*input_array.itemsize+1, |
| 728 |
dtype='int8', n=16) |
| 729 |
|
| 730 |
misaligned_input_array = _input_array[1:].view( |
| 731 |
dtype=input_array.dtype).reshape(*test_shape) |
| 732 |
|
| 733 |
self.assertRaisesRegex(ValueError, |
| 734 |
'Cannot avoid copy.*not aligned.*', |
| 735 |
getattr(builders, self.func), |
| 736 |
misaligned_input_array, s, **_kwargs) |
| 737 |
|
| 738 |
_input_array = byte_align(input_array.copy()) |
| 739 |
FFTW_object = getattr(builders, self.func)( |
| 740 |
_input_array, s, **_kwargs) |
| 741 |
|
| 742 |
# A catch all to make sure the internal array |
| 743 |
# is not a copy |
| 744 |
self.assertTrue(FFTW_object.input_array is |
| 745 |
_input_array) |
| 746 |
|
| 747 |
|
| 748 |
class BuildersTestIFFT(BuildersTestFFT): |
| 749 |
func = 'ifft' |
| 750 |
|
| 751 |
class BuildersTestRFFT(BuildersTestFFT): |
| 752 |
func = 'rfft' |
| 753 |
|
| 754 |
class BuildersTestIRFFT(BuildersTestFFT): |
| 755 |
func = 'irfft' |
| 756 |
realinv = True |
| 757 |
|
| 758 |
class BuildersTestFFT2(BuildersTestFFT): |
| 759 |
axes_kw = 'axes' |
| 760 |
func = 'ifft2' |
| 761 |
if numpy.__version__ >= '1.20.0': |
| 762 |
test_shapes = ( |
| 763 |
((128, 64), {'axes': None}), |
| 764 |
((128, 32), {'axes': None}), |
| 765 |
((128, 32, 4), {'axes': (0, 2)}), |
| 766 |
((59, 100), {'axes': (-2, -1)}), |
| 767 |
((59, 100), {'axes': (-2, -1), 'norm': 'ortho'}), |
| 768 |
((59, 100), {'axes': (-2, -1), 'norm': None}), |
| 769 |
((59, 100), {'axes': (-2, -1), 'norm': 'backward'}), |
| 770 |
((59, 100), {'axes': (-2, -1), 'norm': 'forward'}), |
| 771 |
((64, 128, 16), {'axes': (0, 2)}), |
| 772 |
((4, 6, 8, 4), {'axes': (0, 3)}), |
| 773 |
) |
| 774 |
else: |
| 775 |
test_shapes = ( |
| 776 |
((128, 64), {'axes': None}), |
| 777 |
((128, 32), {'axes': None}), |
| 778 |
((128, 32, 4), {'axes': (0, 2)}), |
| 779 |
((59, 100), {'axes': (-2, -1)}), |
| 780 |
((59, 100), {'axes': (-2, -1), 'norm': 'ortho'}), |
| 781 |
((59, 100), {'axes': (-2, -1), 'norm': None}), |
| 782 |
((64, 128, 16), {'axes': (0, 2)}), |
| 783 |
((4, 6, 8, 4), {'axes': (0, 3)}), |
| 784 |
) |
| 785 |
|
| 786 |
invalid_args = ( |
| 787 |
((100,), ((100, 200),), ValueError, 'Shape error'), |
| 788 |
((100, 200), ((100, 200, 100),), ValueError, 'Shape error'), |
| 789 |
((100,), ((100, 200), (-3, -2, -1)), ValueError, 'Shape error'), |
| 790 |
((100, 200), (100, -1), TypeError, ''), |
| 791 |
((100, 200), ((100, 200), (-3, -2)), IndexError, 'Invalid axes'), |
| 792 |
((100, 200), ((100,), (-3,)), IndexError, 'Invalid axes')) |
| 793 |
|
| 794 |
|
| 795 |
class BuildersTestIFFT2(BuildersTestFFT2): |
| 796 |
func = 'ifft2' |
| 797 |
|
| 798 |
class BuildersTestRFFT2(BuildersTestFFT2): |
| 799 |
func = 'rfft2' |
| 800 |
|
| 801 |
class BuildersTestIRFFT2(BuildersTestFFT2): |
| 802 |
func = 'irfft2' |
| 803 |
realinv = True |
| 804 |
|
| 805 |
class BuildersTestFFTN(BuildersTestFFT2): |
| 806 |
func = 'ifftn' |
| 807 |
if numpy.__version__ >= '1.20.0': |
| 808 |
test_shapes = ( |
| 809 |
((128, 32, 4), {'axes': None}), |
| 810 |
((64, 128, 16), {'axes': (0, 1, 2)}), |
| 811 |
((4, 6, 8, 4), {'axes': (0, 3, 1)}), |
| 812 |
((4, 6, 8, 4), {'axes': (0, 3, 1), 'norm': 'ortho'}), |
| 813 |
((4, 6, 8, 4), {'axes': (0, 3, 1), 'norm': None}), |
| 814 |
((4, 6, 8, 4), {'axes': (0, 3, 1), 'norm': 'backward'}), |
| 815 |
((4, 6, 8, 4), {'axes': (0, 3, 1), 'norm': 'forward'}), |
| 816 |
((4, 6, 8, 4), {'axes': (0, 3, 1, 2)}), |
| 817 |
) |
| 818 |
else: |
| 819 |
test_shapes = ( |
| 820 |
((128, 32, 4), {'axes': None}), |
| 821 |
((64, 128, 16), {'axes': (0, 1, 2)}), |
| 822 |
((4, 6, 8, 4), {'axes': (0, 3, 1)}), |
| 823 |
((4, 6, 8, 4), {'axes': (0, 3, 1), 'norm': 'ortho'}), |
| 824 |
((4, 6, 8, 4), {'axes': (0, 3, 1), 'norm': None}), |
| 825 |
((4, 6, 8, 4), {'axes': (0, 3, 1, 2)}), |
| 826 |
) |
| 827 |
|
| 828 |
class BuildersTestIFFTN(BuildersTestFFTN): |
| 829 |
func = 'ifftn' |
| 830 |
|
| 831 |
class BuildersTestRFFTN(BuildersTestFFTN): |
| 832 |
func = 'rfftn' |
| 833 |
|
| 834 |
class BuildersTestIRFFTN(BuildersTestFFTN): |
| 835 |
func = 'irfftn' |
| 836 |
realinv = True |
| 837 |
|
| 838 |
|
| 839 |
class BuildersTestFFTWWrapper(unittest.TestCase): |
| 840 |
'''This basically reimplements the FFTW.__call__ tests, with |
| 841 |
a few tweaks. |
| 842 |
''' |
| 843 |
|
| 844 |
def __init__(self, *args, **kwargs): |
| 845 |
|
| 846 |
super(BuildersTestFFTWWrapper, self).__init__(*args, **kwargs) |
| 847 |
|
| 848 |
if not hasattr(self, 'assertRaisesRegex'): |
| 849 |
self.assertRaisesRegex = self.assertRaisesRegexp |
| 850 |
|
| 851 |
def setUp(self): |
| 852 |
|
| 853 |
require(self, '64') |
| 854 |
|
| 855 |
self.input_array_slicer = tuple([slice(None), slice(256)]) |
| 856 |
self.FFTW_array_slicer = tuple([slice(128), slice(None)]) |
| 857 |
|
| 858 |
self.input_array = empty_aligned((128, 512), dtype='complex128') |
| 859 |
self.output_array = empty_aligned((256, 256), dtype='complex128') |
| 860 |
|
| 861 |
self.internal_array = empty_aligned((256, 256), dtype='complex128') |
| 862 |
|
| 863 |
self.fft = utils._FFTWWrapper(self.internal_array, |
| 864 |
self.output_array, |
| 865 |
input_array_slicer=self.input_array_slicer, |
| 866 |
FFTW_array_slicer=self.FFTW_array_slicer) |
| 867 |
|
| 868 |
self.input_array[:] = (numpy.random.randn(*self.input_array.shape) |
| 869 |
+ 1j*numpy.random.randn(*self.input_array.shape)) |
| 870 |
|
| 871 |
self.internal_array[:] = 0 |
| 872 |
self.internal_array[self.FFTW_array_slicer] = ( |
| 873 |
self.input_array[self.input_array_slicer]) |
| 874 |
|
| 875 |
def update_arrays(self, input_array, output_array): |
| 876 |
'''Does what the internal update arrays does for an FFTW |
| 877 |
object but with a reslicing. |
| 878 |
''' |
| 879 |
internal_input_array = self.fft.input_array |
| 880 |
internal_output_array = self.fft.output_array |
| 881 |
|
| 882 |
internal_input_array[self.FFTW_array_slicer] = ( |
| 883 |
input_array[self.input_array_slicer]) |
| 884 |
|
| 885 |
self.fft(output_array=output_array) |
| 886 |
|
| 887 |
def test_call(self): |
| 888 |
'''Test a call to an instance of the class. |
| 889 |
''' |
| 890 |
|
| 891 |
self.input_array[:] = (numpy.random.randn(*self.input_array.shape) |
| 892 |
+ 1j*numpy.random.randn(*self.input_array.shape)) |
| 893 |
|
| 894 |
output_array = self.fft() |
| 895 |
|
| 896 |
self.assertTrue(numpy.all(output_array == self.output_array)) |
| 897 |
|
| 898 |
|
| 899 |
def test_call_with_positional_input_update(self): |
| 900 |
'''Test the class call with a positional input update. |
| 901 |
''' |
| 902 |
|
| 903 |
input_array = byte_align( |
| 904 |
(numpy.random.randn(*self.input_array.shape) |
| 905 |
+ 1j*numpy.random.randn(*self.input_array.shape))) |
| 906 |
|
| 907 |
output_array = self.fft( |
| 908 |
byte_align(input_array.copy())).copy() |
| 909 |
|
| 910 |
self.update_arrays(input_array, self.output_array) |
| 911 |
self.fft.execute() |
| 912 |
|
| 913 |
self.assertTrue(numpy.all(output_array == self.output_array)) |
| 914 |
|
| 915 |
|
| 916 |
def test_call_with_keyword_input_update(self): |
| 917 |
'''Test the class call with a keyword input update. |
| 918 |
''' |
| 919 |
input_array = byte_align( |
| 920 |
numpy.random.randn(*self.input_array.shape) |
| 921 |
+ 1j*numpy.random.randn(*self.input_array.shape)) |
| 922 |
|
| 923 |
output_array = self.fft( |
| 924 |
input_array=byte_align(input_array.copy())).copy() |
| 925 |
|
| 926 |
self.update_arrays(input_array, self.output_array) |
| 927 |
self.fft.execute() |
| 928 |
|
| 929 |
self.assertTrue(numpy.all(output_array == self.output_array)) |
| 930 |
|
| 931 |
|
| 932 |
def test_call_with_keyword_output_update(self): |
| 933 |
'''Test the class call with a keyword output update. |
| 934 |
''' |
| 935 |
output_array = byte_align( |
| 936 |
(numpy.random.randn(*self.output_array.shape) |
| 937 |
+ 1j*numpy.random.randn(*self.output_array.shape))) |
| 938 |
|
| 939 |
returned_output_array = self.fft( |
| 940 |
output_array=byte_align(output_array.copy())).copy() |
| 941 |
|
| 942 |
|
| 943 |
self.update_arrays(self.input_array, output_array) |
| 944 |
self.fft.execute() |
| 945 |
|
| 946 |
self.assertTrue( |
| 947 |
numpy.all(returned_output_array == output_array)) |
| 948 |
|
| 949 |
def test_call_with_positional_updates(self): |
| 950 |
'''Test the class call with a positional array updates. |
| 951 |
''' |
| 952 |
|
| 953 |
input_array = byte_align((numpy.random.randn(*self.input_array.shape) |
| 954 |
+ 1j*numpy.random.randn(*self.input_array.shape))) |
| 955 |
|
| 956 |
output_array = byte_align((numpy.random.randn(*self.output_array.shape) |
| 957 |
+ 1j*numpy.random.randn(*self.output_array.shape))) |
| 958 |
|
| 959 |
returned_output_array = self.fft( |
| 960 |
byte_align(input_array.copy()), |
| 961 |
byte_align(output_array.copy())).copy() |
| 962 |
|
| 963 |
self.update_arrays(input_array, output_array) |
| 964 |
self.fft.execute() |
| 965 |
|
| 966 |
self.assertTrue(numpy.all(returned_output_array == output_array)) |
| 967 |
|
| 968 |
def test_call_with_keyword_updates(self): |
| 969 |
'''Test the class call with a positional output update. |
| 970 |
''' |
| 971 |
|
| 972 |
input_array = byte_align( |
| 973 |
(numpy.random.randn(*self.input_array.shape) |
| 974 |
+ 1j*numpy.random.randn(*self.input_array.shape))) |
| 975 |
|
| 976 |
output_array = byte_align( |
| 977 |
(numpy.random.randn(*self.output_array.shape) |
| 978 |
+ 1j*numpy.random.randn(*self.output_array.shape))) |
| 979 |
|
| 980 |
returned_output_array = self.fft( |
| 981 |
output_array=byte_align(output_array.copy()), |
| 982 |
input_array=byte_align(input_array.copy())).copy() |
| 983 |
|
| 984 |
self.update_arrays(input_array, output_array) |
| 985 |
self.fft.execute() |
| 986 |
|
| 987 |
self.assertTrue(numpy.all(returned_output_array == output_array)) |
| 988 |
|
| 989 |
def test_call_with_different_input_dtype(self): |
| 990 |
'''Test the class call with an array with a different input dtype |
| 991 |
''' |
| 992 |
input_array = byte_align(numpy.complex64( |
| 993 |
numpy.random.randn(*self.input_array.shape) |
| 994 |
+ 1j*numpy.random.randn(*self.input_array.shape))) |
| 995 |
|
| 996 |
output_array = self.fft(byte_align(input_array.copy())).copy() |
| 997 |
|
| 998 |
_input_array = numpy.asarray(input_array, |
| 999 |
dtype=self.input_array.dtype) |
| 1000 |
|
| 1001 |
self.update_arrays(_input_array, self.output_array) |
| 1002 |
self.fft.execute() |
| 1003 |
|
| 1004 |
self.assertTrue(numpy.all(output_array == self.output_array)) |
| 1005 |
|
| 1006 |
def test_call_with_list_input(self): |
| 1007 |
'''Test the class call with a list rather than an array |
| 1008 |
''' |
| 1009 |
|
| 1010 |
output_array = self.fft().copy() |
| 1011 |
|
| 1012 |
test_output_array = self.fft(self.input_array.tolist()).copy() |
| 1013 |
|
| 1014 |
self.assertTrue(numpy.all(output_array == test_output_array)) |
| 1015 |
|
| 1016 |
|
| 1017 |
def test_call_with_invalid_update(self): |
| 1018 |
'''Test the class call with an invalid update. |
| 1019 |
''' |
| 1020 |
|
| 1021 |
new_shape = self.input_array.shape + (2, ) |
| 1022 |
invalid_array = (numpy.random.randn(*new_shape) |
| 1023 |
+ 1j*numpy.random.randn(*new_shape)) |
| 1024 |
|
| 1025 |
self.assertRaises(ValueError, self.fft, |
| 1026 |
*(), |
| 1027 |
**{'output_array':invalid_array}) |
| 1028 |
|
| 1029 |
self.assertRaises(ValueError, self.fft, |
| 1030 |
*(), |
| 1031 |
**{'input_array':invalid_array}) |
| 1032 |
|
| 1033 |
|
| 1034 |
def test_call_with_invalid_output_striding(self): |
| 1035 |
'''Test the class call with an invalid strided output update. |
| 1036 |
''' |
| 1037 |
# Add an extra dimension to bugger up the striding |
| 1038 |
new_shape = self.output_array.shape + (2,) |
| 1039 |
output_array = byte_align(numpy.random.randn(*new_shape) |
| 1040 |
+ 1j*numpy.random.randn(*new_shape)) |
| 1041 |
|
| 1042 |
self.assertRaisesRegex(ValueError, 'Invalid output striding', |
| 1043 |
self.fft, **{'output_array': output_array[:,:,1]}) |
| 1044 |
|
| 1045 |
def test_call_with_different_striding(self): |
| 1046 |
'''Test the input update with different strides to internal array. |
| 1047 |
''' |
| 1048 |
input_array_shape = self.input_array.shape + (2,) |
| 1049 |
internal_array_shape = self.internal_array.shape |
| 1050 |
|
| 1051 |
internal_array = byte_align( |
| 1052 |
numpy.random.randn(*internal_array_shape) |
| 1053 |
+ 1j*numpy.random.randn(*internal_array_shape)) |
| 1054 |
|
| 1055 |
fft = utils._FFTWWrapper(internal_array, self.output_array, |
| 1056 |
input_array_slicer=self.input_array_slicer, |
| 1057 |
FFTW_array_slicer=self.FFTW_array_slicer) |
| 1058 |
|
| 1059 |
test_output_array = fft().copy() |
| 1060 |
|
| 1061 |
new_input_array = empty_aligned(input_array_shape, |
| 1062 |
dtype=internal_array.dtype) |
| 1063 |
new_input_array[:] = 0 |
| 1064 |
|
| 1065 |
new_input_array[:,:,0][self.input_array_slicer] = ( |
| 1066 |
internal_array[self.FFTW_array_slicer]) |
| 1067 |
|
| 1068 |
new_output = fft(new_input_array[:,:,0]).copy() |
| 1069 |
|
| 1070 |
# Test the test! |
| 1071 |
self.assertTrue( |
| 1072 |
new_input_array[:,:,0].strides != internal_array.strides) |
| 1073 |
|
| 1074 |
self.assertTrue(numpy.all(test_output_array == new_output)) |
| 1075 |
|
| 1076 |
def test_call_with_copy_with_missized_array_error(self): |
| 1077 |
'''Force an input copy with a missized array. |
| 1078 |
''' |
| 1079 |
shape = list(self.input_array.shape + (2,)) |
| 1080 |
shape[0] += 1 |
| 1081 |
|
| 1082 |
input_array = byte_align(numpy.random.randn(*shape) |
| 1083 |
+ 1j*numpy.random.randn(*shape)) |
| 1084 |
|
| 1085 |
self.assertRaisesRegex(ValueError, 'Invalid input shape', |
| 1086 |
self.fft, **{'input_array': input_array[:,:,0]}) |
| 1087 |
|
| 1088 |
def test_call_with_normalisation_on(self): |
| 1089 |
_input_array = empty_aligned(self.internal_array.shape, |
| 1090 |
dtype='complex128') |
| 1091 |
|
| 1092 |
ifft = utils._FFTWWrapper(self.output_array, _input_array, |
| 1093 |
direction='FFTW_BACKWARD', |
| 1094 |
input_array_slicer=slice(None), |
| 1095 |
FFTW_array_slicer=slice(None)) |
| 1096 |
|
| 1097 |
self.fft(normalise_idft=True) # Shouldn't make any difference |
| 1098 |
ifft(normalise_idft=True) |
| 1099 |
|
| 1100 |
self.assertTrue(numpy.allclose( |
| 1101 |
self.input_array[self.input_array_slicer], |
| 1102 |
_input_array[self.FFTW_array_slicer])) |
| 1103 |
|
| 1104 |
def test_call_with_normalisation_off(self): |
| 1105 |
|
| 1106 |
_input_array = empty_aligned(self.internal_array.shape, |
| 1107 |
dtype='complex128') |
| 1108 |
|
| 1109 |
ifft = utils._FFTWWrapper(self.output_array, _input_array, |
| 1110 |
direction='FFTW_BACKWARD', |
| 1111 |
input_array_slicer=slice(None), |
| 1112 |
FFTW_array_slicer=slice(None)) |
| 1113 |
|
| 1114 |
self.fft(normalise_idft=True) # Shouldn't make any difference |
| 1115 |
ifft(normalise_idft=False) |
| 1116 |
|
| 1117 |
_input_array /= ifft.N |
| 1118 |
|
| 1119 |
self.assertTrue(numpy.allclose( |
| 1120 |
self.input_array[self.input_array_slicer], |
| 1121 |
_input_array[self.FFTW_array_slicer])) |
| 1122 |
|
| 1123 |
def test_call_with_normalisation_default(self): |
| 1124 |
_input_array = empty_aligned(self.internal_array.shape, |
| 1125 |
dtype='complex128') |
| 1126 |
|
| 1127 |
ifft = utils._FFTWWrapper(self.output_array, _input_array, |
| 1128 |
direction='FFTW_BACKWARD', |
| 1129 |
input_array_slicer=slice(None), |
| 1130 |
FFTW_array_slicer=slice(None)) |
| 1131 |
|
| 1132 |
self.fft() |
| 1133 |
ifft() |
| 1134 |
|
| 1135 |
# Scaling is performed by default |
| 1136 |
self.assertTrue(numpy.allclose( |
| 1137 |
self.input_array[self.input_array_slicer], |
| 1138 |
_input_array[self.FFTW_array_slicer])) |
| 1139 |
|
| 1140 |
def test_call_norm_ortho(self): |
| 1141 |
_input_array = empty_aligned(self.internal_array.shape, |
| 1142 |
dtype='complex128') |
| 1143 |
|
| 1144 |
normdict = utils._norm_args("ortho") |
| 1145 |
|
| 1146 |
ifft = utils._FFTWWrapper(self.output_array, _input_array, |
| 1147 |
direction='FFTW_BACKWARD', |
| 1148 |
input_array_slicer=slice(None), |
| 1149 |
FFTW_array_slicer=slice(None), |
| 1150 |
normalise_idft=normdict["normalise_idft"], |
| 1151 |
ortho=normdict["ortho"]) |
| 1152 |
|
| 1153 |
self.fft(normalise_idft=normdict["normalise_idft"], |
| 1154 |
ortho=normdict["ortho"]) |
| 1155 |
ifft() |
| 1156 |
self.assertTrue(numpy.allclose( |
| 1157 |
self.input_array[self.input_array_slicer], |
| 1158 |
_input_array[self.FFTW_array_slicer])) |
| 1159 |
|
| 1160 |
def test_call_norm_backward(self): |
| 1161 |
_input_array = empty_aligned(self.internal_array.shape, |
| 1162 |
dtype='complex128') |
| 1163 |
|
| 1164 |
normdict = utils._norm_args("backward") |
| 1165 |
|
| 1166 |
ifft = utils._FFTWWrapper(self.output_array, _input_array, |
| 1167 |
direction='FFTW_BACKWARD', |
| 1168 |
input_array_slicer=slice(None), |
| 1169 |
FFTW_array_slicer=slice(None), |
| 1170 |
normalise_idft=normdict["normalise_idft"], |
| 1171 |
ortho=normdict["ortho"]) |
| 1172 |
|
| 1173 |
self.fft(normalise_idft=normdict["normalise_idft"], |
| 1174 |
ortho=normdict["ortho"]) |
| 1175 |
ifft() |
| 1176 |
self.assertTrue(numpy.allclose( |
| 1177 |
self.input_array[self.input_array_slicer], |
| 1178 |
_input_array[self.FFTW_array_slicer])) |
| 1179 |
|
| 1180 |
def test_call_norm_none(self): |
| 1181 |
_input_array = empty_aligned(self.internal_array.shape, |
| 1182 |
dtype='complex128') |
| 1183 |
|
| 1184 |
normdict = utils._norm_args(None) |
| 1185 |
|
| 1186 |
ifft = utils._FFTWWrapper(self.output_array, _input_array, |
| 1187 |
direction='FFTW_BACKWARD', |
| 1188 |
input_array_slicer=slice(None), |
| 1189 |
FFTW_array_slicer=slice(None), |
| 1190 |
normalise_idft=normdict["normalise_idft"], |
| 1191 |
ortho=normdict["ortho"]) |
| 1192 |
|
| 1193 |
self.fft(normalise_idft=normdict["normalise_idft"], |
| 1194 |
ortho=normdict["ortho"]) |
| 1195 |
ifft() |
| 1196 |
self.assertTrue(numpy.allclose( |
| 1197 |
self.input_array[self.input_array_slicer], |
| 1198 |
_input_array[self.FFTW_array_slicer])) |
| 1199 |
|
| 1200 |
def test_call_norm_forward(self): |
| 1201 |
_input_array = empty_aligned(self.internal_array.shape, |
| 1202 |
dtype='complex128') |
| 1203 |
|
| 1204 |
normdict = utils._norm_args("forward") |
| 1205 |
|
| 1206 |
ifft = utils._FFTWWrapper(self.output_array, _input_array, |
| 1207 |
direction='FFTW_BACKWARD', |
| 1208 |
input_array_slicer=slice(None), |
| 1209 |
FFTW_array_slicer=slice(None), |
| 1210 |
normalise_idft=normdict["normalise_idft"], |
| 1211 |
ortho=normdict["ortho"]) |
| 1212 |
|
| 1213 |
self.fft(normalise_idft=normdict["normalise_idft"], |
| 1214 |
ortho=normdict["ortho"]) |
| 1215 |
ifft() |
| 1216 |
self.assertTrue(numpy.allclose( |
| 1217 |
self.input_array[self.input_array_slicer], |
| 1218 |
_input_array[self.FFTW_array_slicer])) |
| 1219 |
|
| 1220 |
|
| 1221 |
class BuildersTestUtilities(unittest.TestCase): |
| 1222 |
|
| 1223 |
def __init__(self, *args, **kwargs): |
| 1224 |
|
| 1225 |
super(BuildersTestUtilities, self).__init__(*args, **kwargs) |
| 1226 |
|
| 1227 |
if not hasattr(self, 'assertRaisesRegex'): |
| 1228 |
self.assertRaisesRegex = self.assertRaisesRegexp |
| 1229 |
|
| 1230 |
def test_setup_input_slicers(self): |
| 1231 |
inputs = ( |
| 1232 |
((4, 5), (4, 5)), |
| 1233 |
((4, 4), (3, 5)), |
| 1234 |
((4, 5), (3, 5)), |
| 1235 |
) |
| 1236 |
|
| 1237 |
outputs = ( |
| 1238 |
((slice(0, 4), slice(0, 5)), (slice(None), slice(None))), |
| 1239 |
((slice(0, 3), slice(0, 4)), (slice(None), slice(0, 4))), |
| 1240 |
((slice(0, 3), slice(0, 5)), (slice(None), slice(None))), |
| 1241 |
) |
| 1242 |
|
| 1243 |
for _input, _output in zip(inputs, outputs): |
| 1244 |
self.assertEqual( |
| 1245 |
utils._setup_input_slicers(*_input), |
| 1246 |
_output) |
| 1247 |
|
| 1248 |
|
| 1249 |
|
| 1250 |
def test_compute_array_shapes(self): |
| 1251 |
# inputs are: |
| 1252 |
# (a.shape, s, axes, inverse, real) |
| 1253 |
inputs = ( |
| 1254 |
((4, 5), (4, 5), (-2, -1), False, False), |
| 1255 |
((4, 5), (4, 5), (-1, -2), False, False), |
| 1256 |
((4, 5), (4, 5), (-1, -2), True, False), |
| 1257 |
((4, 5), (4, 5), (-1, -2), True, True), |
| 1258 |
((4, 5), (4, 5), (-2, -1), True, True), |
| 1259 |
((4, 5), (4, 5), (-2, -1), False, True), |
| 1260 |
((4, 5), (4, 5), (-1, -2), False, True), |
| 1261 |
((4, 5, 6), (4, 5), (-2, -1), False, False), |
| 1262 |
((4, 5, 6), (5, 6), (-2, -1), False, False), |
| 1263 |
((4, 5, 6), (3, 5), (-3, -1), False, False), |
| 1264 |
((4, 5, 6), (4, 5), (-2, -1), True, False), |
| 1265 |
((4, 5, 6), (3, 5), (-3, -1), True, False), |
| 1266 |
((4, 5, 6), (4, 5), (-2, -1), True, True), |
| 1267 |
((4, 5, 6), (3, 5), (-3, -1), True, True), |
| 1268 |
((4, 5, 6), (4, 5), (-2, -1), False, True), |
| 1269 |
((4, 5, 6), (3, 5), (-3, -1), False, True), |
| 1270 |
) |
| 1271 |
|
| 1272 |
outputs = ( |
| 1273 |
((4, 5), (4, 5)), |
| 1274 |
((5, 4), (5, 4)), |
| 1275 |
((5, 4), (5, 4)), |
| 1276 |
((3, 4), (5, 4)), |
| 1277 |
((4, 3), (4, 5)), |
| 1278 |
((4, 5), (4, 3)), |
| 1279 |
((5, 4), (3, 4)), |
| 1280 |
((4, 4, 5), (4, 4, 5)), |
| 1281 |
((4, 5, 6), (4, 5, 6)), |
| 1282 |
((3, 5, 5), (3, 5, 5)), |
| 1283 |
((4, 4, 5), (4, 4, 5)), |
| 1284 |
((3, 5, 5), (3, 5, 5)), |
| 1285 |
((4, 4, 3), (4, 4, 5)), |
| 1286 |
((3, 5, 3), (3, 5, 5)), |
| 1287 |
((4, 4, 5), (4, 4, 3)), |
| 1288 |
((3, 5, 5), (3, 5, 3)), |
| 1289 |
) |
| 1290 |
|
| 1291 |
for _input, output in zip(inputs, outputs): |
| 1292 |
shape, s, axes, inverse, real = _input |
| 1293 |
a = numpy.empty(shape) |
| 1294 |
|
| 1295 |
self.assertEqual( |
| 1296 |
utils._compute_array_shapes(a, s, axes, inverse, real), |
| 1297 |
output) |
| 1298 |
|
| 1299 |
def test_compute_array_shapes_invalid_axes(self): |
| 1300 |
|
| 1301 |
a = numpy.zeros((3, 4)) |
| 1302 |
s = (3, 4) |
| 1303 |
test_axes = ((1, 2, 3),) |
| 1304 |
|
| 1305 |
for each_axes in test_axes: |
| 1306 |
|
| 1307 |
args = (a, s, each_axes, False, False) |
| 1308 |
self.assertRaisesRegex(IndexError, 'Invalid axes', |
| 1309 |
utils._compute_array_shapes, *args) |
| 1310 |
|
| 1311 |
def _call_cook_nd_args(self, arg_tuple): |
| 1312 |
a = numpy.zeros(arg_tuple[0]) |
| 1313 |
args = ('s', 'axes', 'invreal') |
| 1314 |
arg_dict = {'a': a} |
| 1315 |
for arg_name, arg in zip(args, arg_tuple[1:]): |
| 1316 |
if arg is not None: |
| 1317 |
arg_dict[arg_name] = arg |
| 1318 |
|
| 1319 |
return utils._cook_nd_args(**arg_dict) |
| 1320 |
|
| 1321 |
def test_cook_nd_args_normal(self): |
| 1322 |
# inputs are (a.shape, s, axes, invreal) |
| 1323 |
# None corresponds to no argument |
| 1324 |
inputs = ( |
| 1325 |
((2, 3), None, (-1,), False), |
| 1326 |
((2, 3), (5, 6), (-2, -1), False), |
| 1327 |
((2, 3), (5, 6), (-1, -2), False), |
| 1328 |
((2, 3), None, (-1, -2), False), |
| 1329 |
((2, 3, 5), (5, 6), (-1, -2), False), |
| 1330 |
((2, 3, 5), (5, 6), None, False), |
| 1331 |
((2, 3, 5), None, (-1, -2), False), |
| 1332 |
((2, 3, 5), None, (-1, -3), False)) |
| 1333 |
|
| 1334 |
outputs = ( |
| 1335 |
((3,), (-1,)), |
| 1336 |
((5, 6), (-2, -1)), |
| 1337 |
((5, 6), (-1, -2)), |
| 1338 |
((3, 2), (-1, -2)), |
| 1339 |
((5, 6), (-1, -2)), |
| 1340 |
((5, 6), (-2, -1)), |
| 1341 |
((5, 3), (-1, -2)), |
| 1342 |
((5, 2), (-1, -3)) |
| 1343 |
) |
| 1344 |
|
| 1345 |
for each_input, each_output in zip(inputs, outputs): |
| 1346 |
self.assertEqual(self._call_cook_nd_args(each_input), |
| 1347 |
each_output) |
| 1348 |
|
| 1349 |
def test_cook_nd_args_invreal(self): |
| 1350 |
|
| 1351 |
# inputs are (a.shape, s, axes, invreal) |
| 1352 |
# None corresponds to no argument |
| 1353 |
inputs = ( |
| 1354 |
((2, 3), None, (-1,), True), |
| 1355 |
((2, 3), (5, 6), (-2, -1), True), |
| 1356 |
((2, 3), (5, 6), (-1, -2), True), |
| 1357 |
((2, 3), None, (-1, -2), True), |
| 1358 |
((2, 3, 5), (5, 6), (-1, -2), True), |
| 1359 |
((2, 3, 5), (5, 6), None, True), |
| 1360 |
((2, 3, 5), None, (-1, -2), True), |
| 1361 |
((2, 3, 5), None, (-1, -3), True)) |
| 1362 |
|
| 1363 |
outputs = ( |
| 1364 |
((4,), (-1,)), |
| 1365 |
((5, 6), (-2, -1)), |
| 1366 |
((5, 6), (-1, -2)), |
| 1367 |
((3, 2), (-1, -2)), |
| 1368 |
((5, 6), (-1, -2)), |
| 1369 |
((5, 6), (-2, -1)), |
| 1370 |
((5, 4), (-1, -2)), |
| 1371 |
((5, 2), (-1, -3)) |
| 1372 |
) |
| 1373 |
|
| 1374 |
for each_input, each_output in zip(inputs, outputs): |
| 1375 |
self.assertEqual(self._call_cook_nd_args(each_input), |
| 1376 |
each_output) |
| 1377 |
|
| 1378 |
|
| 1379 |
def test_cook_nd_args_invalid_inputs(self): |
| 1380 |
# inputs are (a.shape, s, axes, invreal) |
| 1381 |
# None corresponds to no argument |
| 1382 |
inputs = ( |
| 1383 |
((2, 3), (1,), (-1, -2), None), |
| 1384 |
((2, 3), (2, 3, 4), (-3, -2, -1), None), |
| 1385 |
) |
| 1386 |
|
| 1387 |
# all the inputs should yield an error |
| 1388 |
for each_input in inputs: |
| 1389 |
self.assertRaisesRegex(ValueError, 'Shape error', |
| 1390 |
self._call_cook_nd_args, *(each_input,)) |
| 1391 |
|
| 1392 |
test_cases = ( |
| 1393 |
BuildersTestFFTWWrapper, |
| 1394 |
BuildersTestUtilities, |
| 1395 |
BuildersTestFFT, |
| 1396 |
BuildersTestIFFT, |
| 1397 |
BuildersTestRFFT, |
| 1398 |
BuildersTestIRFFT, |
| 1399 |
BuildersTestFFT2, |
| 1400 |
BuildersTestIFFT2, |
| 1401 |
BuildersTestRFFT2, |
| 1402 |
BuildersTestIRFFT2, |
| 1403 |
BuildersTestFFTN, |
| 1404 |
BuildersTestIFFTN, |
| 1405 |
BuildersTestRFFTN, |
| 1406 |
BuildersTestIRFFTN) |
| 1407 |
|
| 1408 |
#test_set = {'BuildersTestRFFTN': ['test_dtype_coercian']} |
| 1409 |
test_set = None |
| 1410 |
|
| 1411 |
|
| 1412 |
if __name__ == '__main__': |
| 1413 |
|
| 1414 |
run_test_suites(test_cases, test_set) |