1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605
|
# Copyright 2015 Knowledge Economy Developments Ltd
#
# Henry Gomersall
# heng@kedevelopments.co.uk
#
# All rights reserved.
#
# 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 the copyright holder 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 HOLDER 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.
#
from pyfftw import interfaces
try:
interfaces.dask_fft
except AttributeError:
interfaces.dask_fft = None
try:
# Kludge to skip dask tests when mkl_fft is present
import mkl_fft
interfaces.dask_fft = None
except ImportError:
pass
from .test_pyfftw_base import run_test_suites
from .test_pyfftw_numpy_interface import complex_dtypes, real_dtypes
from ._get_default_args import get_default_args
from distutils.version import LooseVersion
import unittest
import numpy
if interfaces.dask_fft:
import dask.array as da
from dask.array import fft as da_fft
from dask.array.fft import fft_wrap
else:
da = None
da_fft = None
fft_wrap = None
import warnings
import copy
warnings.filterwarnings('always')
def make_complex_data(shape, dtype):
ar, ai = dtype(numpy.random.randn(2, *shape))
ac = ar + 1j*ai
return da.from_array(ac, chunks=shape)
def make_real_data(shape, dtype):
ar = dtype(numpy.random.randn(*shape))
return da.from_array(ar, chunks=shape)
def _dask_array_fft_has_norm_kwarg():
"""returns True if dask.array's fft supports the norm keyword argument
"""
return False
functions = {
'fft': 'complex',
'fft2': 'complex',
'fftn': 'complex',
'ifft': 'complex',
'ifft2': 'complex',
'ifftn': 'complex',
'rfft': 'r2c',
'rfft2': 'r2c',
'rfftn': 'r2c',
'irfft': 'c2r',
'irfft2': 'c2r',
'irfftn': 'c2r',
'hfft': 'c2r',
'ihfft': 'r2c'}
acquired_names = ('fft_wrap', 'fftfreq', 'rfftfreq', 'fftshift', 'ifftshift')
@unittest.skipIf(
not interfaces.dask_fft,
"dask interface is not available, so skipping tests."
)
class InterfacesDaskFFTTestModule(unittest.TestCase):
''' A really simple test suite to check the module works as expected.
'''
def test_acquired_names(self):
for each_name in acquired_names:
da_fft_attr = getattr(da_fft, each_name)
acquired_attr = getattr(interfaces.dask_fft, each_name)
self.assertIs(da_fft_attr, acquired_attr)
@unittest.skipIf(
not interfaces.dask_fft,
"dask interface is not available, so skipping tests."
)
class InterfacesDaskFFTTestFFT(unittest.TestCase):
io_dtypes = {
'complex': (complex_dtypes, make_complex_data),
'r2c': (real_dtypes, make_real_data),
'c2r': (complex_dtypes, make_complex_data)}
validator_module = da_fft
test_wrapped_interface = interfaces.numpy_fft
test_interface = interfaces.dask_fft
func = 'fft'
axes_kw = 'axis'
default_s_from_shape_slicer = slice(-1, None)
test_shapes = (
((100,), {}),
((128, 64), {'axis': 0}),
((128, 32), {'axis': -1}),
((59, 100), {}),
((59, 99), {'axis': -1}),
((59, 99), {'axis': 0}),
((32, 32, 4), {'axis': 1}),
((32, 32, 2), {'axis': 1, 'norm': 'ortho'}),
((64, 128, 16), {}),
)
realinv = False
has_norm_kwarg = _dask_array_fft_has_norm_kwarg()
@property
def test_data(self):
for test_shape, kwargs in self.test_shapes:
axes = self.axes_from_kwargs(kwargs)
s = self.s_from_kwargs(test_shape, kwargs)
if not self.has_norm_kwarg and 'norm' in kwargs:
kwargs.pop('norm')
if self.realinv:
test_shape = list(test_shape)
test_shape[axes[-1]] = test_shape[axes[-1]]//2 + 1
test_shape = tuple(test_shape)
yield test_shape, s, kwargs
def __init__(self, *args, **kwargs):
super(InterfacesDaskFFTTestFFT, self).__init__(*args, **kwargs)
# Assume python 3, but keep backwards compatibility
if not hasattr(self, 'assertRaisesRegex'):
self.assertRaisesRegex = self.assertRaisesRegexp
def validate(self, array_type, test_shape, dtype,
s, kwargs):
# Do it without the cache
# without:
interfaces.cache.disable()
self._validate(array_type, test_shape, dtype, s, kwargs)
def munge_input_array(self, array, kwargs):
return array
def _validate(self, array_type, test_shape, dtype,
s, kwargs):
input_array = self.munge_input_array(
array_type(test_shape, dtype), kwargs)
orig_input_array = input_array
np_input_array = numpy.asarray(input_array)
if np_input_array.dtype == 'clongdouble':
np_input_array = numpy.complex128(input_array)
elif np_input_array.dtype == 'longdouble':
np_input_array = numpy.float64(input_array)
da_input_array = da.from_array(np_input_array,
chunks=np_input_array.shape)
with warnings.catch_warnings(record=True) as w:
# We catch the warnings so as to pick up on when
# a complex array is turned into a real array
if 'axes' in kwargs:
validator_kwargs = {'axes': kwargs['axes']}
elif 'axis' in kwargs:
validator_kwargs = {'axis': kwargs['axis']}
else:
validator_kwargs = {}
if self.has_norm_kwarg and 'norm' in kwargs:
validator_kwargs['norm'] = kwargs['norm']
try:
test_out_array = getattr(self.validator_module, self.func)(
da_input_array, s, **validator_kwargs)
except Exception as e:
interface_exception = None
try:
getattr(self.test_interface, self.func)(
input_array, s, **kwargs)
except Exception as _interface_exception:
# It's necessary to assign the exception to the
# already defined variable in Python 3.
# See http://www.python.org/dev/peps/pep-3110/#semantic-changes
interface_exception = _interface_exception
# If the test interface raised, so must this.
self.assertEqual(type(interface_exception), type(e),
msg='Interface exception raised. ' +
'Testing for: ' + repr(e))
return
output_array = getattr(self.test_interface, self.func)(
input_array, s, **kwargs)
if (functions[self.func] == 'r2c'):
if numpy.iscomplexobj(input_array):
if len(w) > 0:
# Make sure a warning is raised
self.assertIs(
w[-1].category, numpy.ComplexWarning)
self.assertTrue(
numpy.allclose(output_array, test_out_array,
rtol=1e-2, atol=1e-4))
if numpy.asanyarray(input_array).real.dtype == numpy.float16:
# FFTW output will never be single precision for half precision
# inputs as there is no half-precision FFTW routine
input_precision_dtype = numpy.float32
else:
input_precision_dtype = numpy.asanyarray(input_array).real.dtype
self.assertEqual(input_precision_dtype,
output_array.real.dtype)
self.assertTrue(numpy.allclose(input_array,
orig_input_array))
return output_array
def axes_from_kwargs(self, kwargs):
default_args = get_default_args(
getattr(self.test_wrapped_interface, self.func))
if 'axis' in kwargs:
axes = (kwargs['axis'],)
elif 'axes' in kwargs:
axes = kwargs['axes']
if axes is None:
axes = default_args['axes']
else:
if 'axis' in default_args:
# default 1D
axes = (default_args['axis'],)
else:
# default nD
axes = default_args['axes']
if axes is None:
axes = (-1,)
return axes
def s_from_kwargs(self, test_shape, kwargs):
''' Return either a scalar s or a tuple depending on
whether axis or axes is specified
'''
default_args = get_default_args(
getattr(self.test_wrapped_interface, self.func))
if 'axis' in kwargs:
s = test_shape[kwargs['axis']]
elif 'axes' in kwargs:
axes = kwargs['axes']
if axes is not None:
s = []
for each_axis in axes:
s.append(test_shape[each_axis])
else:
# default nD
s = []
try:
for each_axis in default_args['axes']:
s.append(test_shape[each_axis])
except TypeError:
try:
s = list(test_shape[
self.default_s_from_shape_slicer])
except TypeError:
# We had an integer as the default, so force
# it to be a list
s = [test_shape[self.default_s_from_shape_slicer]]
else:
if 'axis' in default_args:
# default 1D
s = test_shape[default_args['axis']]
else:
# default nD
s = []
try:
for each_axis in default_args['axes']:
s.append(test_shape[each_axis])
except TypeError:
s = None
return s
def test_valid(self):
dtype_tuple = self.io_dtypes[functions[self.func]]
for dtype in dtype_tuple[0]:
for test_shape, s, kwargs in self.test_data:
s = None
self.validate(dtype_tuple[1],
test_shape, dtype, s, kwargs)
def test_same_sized_s(self):
dtype_tuple = self.io_dtypes[functions[self.func]]
for dtype in dtype_tuple[0]:
for test_shape, s, kwargs in self.test_data:
self.validate(dtype_tuple[1],
test_shape, dtype, s, kwargs)
def test_bigger_s(self):
dtype_tuple = self.io_dtypes[functions[self.func]]
for dtype in dtype_tuple[0]:
for test_shape, s, kwargs in self.test_data:
try:
for each_axis, length in enumerate(s):
s[each_axis] += 2
except TypeError:
s += 2
self.validate(dtype_tuple[1],
test_shape, dtype, s, kwargs)
def test_smaller_s(self):
dtype_tuple = self.io_dtypes[functions[self.func]]
for dtype in dtype_tuple[0]:
for test_shape, s, kwargs in self.test_data:
try:
for each_axis, length in enumerate(s):
s[each_axis] -= 2
except TypeError:
s -= 2
self.validate(dtype_tuple[1],
test_shape, dtype, s, kwargs)
def check_arg(self, arg, arg_test_values, array_type, test_shape,
dtype, s, kwargs):
'''Check that the correct arg is passed to the builder'''
# We trust the builders to work as expected when passed
# the correct arg (the builders have their own unittests).
return_values = []
input_array = array_type(test_shape, dtype)
def fake_fft(*args, **kwargs):
return_values.append((args, kwargs))
return (args, kwargs)
try:
# Replace the function that is to be used
real_fft = getattr(self.test_interface, self.func)
setattr(self.test_interface, self.func, fake_fft)
_kwargs = kwargs.copy()
for each_value in arg_test_values:
_kwargs[arg] = each_value
builder_args = getattr(self.test_interface, self.func)(
input_array.copy(), s, **_kwargs)
self.assertTrue(builder_args[1][arg] == each_value)
# make sure it was called
self.assertTrue(len(return_values) > 0)
except:
raise
finally:
# Make sure we set it back
setattr(self.test_interface, self.func, real_fft)
# Validate it aswell
for each_value in arg_test_values:
_kwargs[arg] = each_value
builder_args = getattr(self.test_interface, self.func)(
input_array.copy(), s, **_kwargs)
self.validate(array_type, test_shape, dtype, s, _kwargs)
def test_bigger_and_smaller_s(self):
dtype_tuple = self.io_dtypes[functions[self.func]]
for dtype in dtype_tuple[0]:
i = -1
for test_shape, s, kwargs in self.test_data:
try:
for each_axis, length in enumerate(s):
s[each_axis] += i * 2
i *= i
except TypeError:
s += i * 2
i *= i
self.validate(dtype_tuple[1],
test_shape, dtype, s, kwargs)
def test_dtype_coercian(self):
# Make sure we input a dtype that needs to be coerced
if functions[self.func] == 'r2c':
dtype_tuple = self.io_dtypes['complex']
else:
dtype_tuple = self.io_dtypes['r2c']
for dtype in dtype_tuple[0]:
for test_shape, s, kwargs in self.test_data:
s = None
self.validate(dtype_tuple[1],
test_shape, dtype, s, kwargs)
def test_input_maintained(self):
'''Test to make sure the input is maintained by default.
'''
dtype_tuple = self.io_dtypes[functions[self.func]]
for dtype in dtype_tuple[0]:
for test_shape, s, kwargs in self.test_data:
input_array = dtype_tuple[1](test_shape, dtype)
orig_input_array = input_array.copy()
getattr(self.test_interface, self.func)(
input_array, s, **kwargs)
self.assertTrue(
numpy.alltrue(input_array == orig_input_array))
class InterfacesDaskFFTTestFFT2(InterfacesDaskFFTTestFFT):
axes_kw = 'axes'
func = 'ifft2'
has_norm_kwarg = False
test_shapes = (
((128, 64), {'axes': None}),
((128, 32), {'axes': None}),
((128, 32, 4), {'axes': (0, 2)}),
((59, 100), {'axes': (-2, -1)}),
((32, 32), {'axes': (-2, -1), 'norm': 'ortho'}),
((64, 128, 16), {'axes': (0, 2)}),
((4, 6, 8, 4), {'axes': (0, 3)}),
)
invalid_args = ()
def test_shape_and_s_different_lengths(self):
dtype_tuple = self.io_dtypes[functions[self.func]]
for dtype in dtype_tuple[0]:
for test_shape, s, _kwargs in self.test_data:
kwargs = copy.copy(_kwargs)
try:
s = s[1:]
except TypeError:
self.skipTest('Not meaningful test on 1d arrays.')
# Convert empty tuples to None
s = s if s else None
del kwargs['axes']
self.validate(dtype_tuple[1],
test_shape, dtype, s, kwargs)
class InterfacesDaskFFTTestFFTN(InterfacesDaskFFTTestFFT2):
func = 'ifftn'
has_norm_kwarg = False
test_shapes = (
((128, 32, 4), {'axes': None}),
((64, 128, 16), {'axes': (0, 1, 2)}),
((4, 6, 8, 4), {'axes': (0, 3, 1)}),
((4, 6, 4, 4), {'axes': (0, 3, 1), 'norm': 'ortho'}),
((4, 6, 8, 4), {'axes': (0, 3, 1, 2)}),
)
class InterfacesDaskFFTTestIFFT(InterfacesDaskFFTTestFFT):
func = 'ifft'
has_norm_kwarg = False
class InterfacesDaskFFTTestIFFT2(InterfacesDaskFFTTestFFT2):
func = 'ifft2'
has_norm_kwarg = False
class InterfacesDaskFFTTestIFFTN(InterfacesDaskFFTTestFFTN):
func = 'ifftn'
has_norm_kwarg = False
class InterfacesDaskFFTTestRFFT(InterfacesDaskFFTTestFFT):
func = 'rfft'
has_norm_kwarg = False
class InterfacesDaskFFTTestRFFT2(InterfacesDaskFFTTestFFT2):
func = 'rfft2'
has_norm_kwarg = False
class InterfacesDaskFFTTestRFFTN(InterfacesDaskFFTTestFFTN):
func = 'rfftn'
has_norm_kwarg = False
class InterfacesDaskFFTTestIRFFT(InterfacesDaskFFTTestFFT):
func = 'irfft'
realinv = True
has_norm_kwarg = False
class InterfacesDaskFFTTestIRFFT2(InterfacesDaskFFTTestFFT2):
func = 'irfft2'
has_norm_kwarg = False
realinv = True
class InterfacesDaskFFTTestIRFFTN(InterfacesDaskFFTTestFFTN):
func = 'irfftn'
has_norm_kwarg = False
realinv = True
class InterfacesDaskFFTTestHFFT(InterfacesDaskFFTTestFFT):
func = 'hfft'
realinv = True
has_norm_kwarg = False
class InterfacesDaskFFTTestIHFFT(InterfacesDaskFFTTestFFT):
func = 'ihfft'
has_norm_kwarg = False
test_cases = (
InterfacesDaskFFTTestModule,
InterfacesDaskFFTTestFFT,
InterfacesDaskFFTTestFFT2,
InterfacesDaskFFTTestFFTN,
InterfacesDaskFFTTestIFFT,
InterfacesDaskFFTTestIFFT2,
InterfacesDaskFFTTestIFFTN,
InterfacesDaskFFTTestRFFT,
InterfacesDaskFFTTestRFFT2,
InterfacesDaskFFTTestRFFTN,
InterfacesDaskFFTTestIRFFT,
InterfacesDaskFFTTestIRFFT2,
InterfacesDaskFFTTestIRFFTN,
InterfacesDaskFFTTestHFFT,
InterfacesDaskFFTTestIHFFT)
test_set = None
if __name__ == '__main__':
run_test_suites(test_cases, test_set)
|