| 1 |
# |
| 2 |
# Copyright 2014 Knowledge Economy Developments Ltd |
| 3 |
# |
| 4 |
# Henry Gomersall |
| 5 |
# [email protected] |
| 6 |
# |
| 7 |
# All rights reserved. |
| 8 |
# |
| 9 |
# Redistribution and use in source and binary forms, with or without |
| 10 |
# modification, are permitted provided that the following conditions are met: |
| 11 |
# |
| 12 |
# * Redistributions of source code must retain the above copyright notice, this |
| 13 |
# list of conditions and the following disclaimer. |
| 14 |
# |
| 15 |
# * Redistributions in binary form must reproduce the above copyright notice, |
| 16 |
# this list of conditions and the following disclaimer in the documentation |
| 17 |
# and/or other materials provided with the distribution. |
| 18 |
# |
| 19 |
# * Neither the name of the copyright holder nor the names of its contributors |
| 20 |
# may be used to endorse or promote products derived from this software without |
| 21 |
# specific prior written permission. |
| 22 |
# |
| 23 |
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 24 |
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 25 |
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 26 |
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 27 |
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 28 |
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 29 |
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 30 |
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 31 |
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 32 |
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 33 |
# POSSIBILITY OF SUCH DAMAGE. |
| 34 |
# |
| 35 |
|
| 36 |
from pyfftw import FFTW, byte_align, empty_aligned, forget_wisdom |
| 37 |
import pyfftw |
| 38 |
import numpy |
| 39 |
from timeit import Timer |
| 40 |
import time |
| 41 |
|
| 42 |
import unittest |
| 43 |
|
| 44 |
from .test_pyfftw_base import FFTWBaseTest, run_test_suites, miss, np_fft |
| 45 |
|
| 46 |
|
| 47 |
# We make this 1D case not inherit from FFTWBaseTest. |
| 48 |
# It needs to be combined with FFTWBaseTest to work. |
| 49 |
# This allows us to separate out tests that are use |
| 50 |
# in multiple locations. |
| 51 |
class Complex64FFTW1DTest(object): |
| 52 |
|
| 53 |
def test_time(self): |
| 54 |
|
| 55 |
in_shape = self.input_shapes['2d'] |
| 56 |
out_shape = self.output_shapes['2d'] |
| 57 |
|
| 58 |
axes=(-1,) |
| 59 |
a, b = self.create_test_arrays(in_shape, out_shape) |
| 60 |
|
| 61 |
fft, ifft = self.run_validate_fft(a, b, axes) |
| 62 |
|
| 63 |
self.timer_routine(fft.execute, |
| 64 |
lambda: self.np_fft_comparison(a)) |
| 65 |
self.assertTrue(True) |
| 66 |
|
| 67 |
def test_invalid_args_raise(self): |
| 68 |
|
| 69 |
in_shape = self.input_shapes['1d'] |
| 70 |
out_shape = self.output_shapes['1d'] |
| 71 |
|
| 72 |
axes=(-1,) |
| 73 |
a, b = self.create_test_arrays(in_shape, out_shape) |
| 74 |
|
| 75 |
# Note "thread" is incorrect, it should be "threads" |
| 76 |
self.assertRaises(TypeError, FFTW, a, b, axes, thread=4) |
| 77 |
|
| 78 |
def test_1d(self): |
| 79 |
in_shape = self.input_shapes['1d'] |
| 80 |
out_shape = self.output_shapes['1d'] |
| 81 |
|
| 82 |
axes=(0,) |
| 83 |
a, b = self.create_test_arrays(in_shape, out_shape) |
| 84 |
|
| 85 |
self.run_validate_fft(a, b, axes) |
| 86 |
|
| 87 |
def test_multiple_1d(self): |
| 88 |
in_shape = self.input_shapes['2d'] |
| 89 |
out_shape = self.output_shapes['2d'] |
| 90 |
|
| 91 |
axes=(-1,) |
| 92 |
a, b = self.create_test_arrays(in_shape, out_shape) |
| 93 |
|
| 94 |
self.run_validate_fft(a, b, axes) |
| 95 |
|
| 96 |
def test_default_args(self): |
| 97 |
in_shape = self.input_shapes['2d'] |
| 98 |
out_shape = self.output_shapes['2d'] |
| 99 |
|
| 100 |
a, b = self.create_test_arrays(in_shape, out_shape) |
| 101 |
|
| 102 |
fft = FFTW(a,b) |
| 103 |
fft.execute() |
| 104 |
ref_b = self.reference_fftn(a, axes=(-1,)) |
| 105 |
self.assertTrue(numpy.allclose(b, ref_b, rtol=1e-2, atol=1e-3)) |
| 106 |
|
| 107 |
def test_time_with_array_update(self): |
| 108 |
in_shape = self.input_shapes['2d'] |
| 109 |
out_shape = self.output_shapes['2d'] |
| 110 |
|
| 111 |
axes=(-1,) |
| 112 |
a, b = self.create_test_arrays(in_shape, out_shape) |
| 113 |
|
| 114 |
fft, ifft = self.run_validate_fft(a, b, axes) |
| 115 |
|
| 116 |
def fftw_callable(): |
| 117 |
fft.update_arrays(a,b) |
| 118 |
fft.execute() |
| 119 |
|
| 120 |
self.timer_routine(fftw_callable, |
| 121 |
lambda: self.np_fft_comparison(a)) |
| 122 |
|
| 123 |
self.assertTrue(True) |
| 124 |
|
| 125 |
def test_planning_time_limit(self): |
| 126 |
in_shape = self.input_shapes['1d'] |
| 127 |
out_shape = self.output_shapes['1d'] |
| 128 |
|
| 129 |
axes=(0,) |
| 130 |
a, b = self.create_test_arrays(in_shape, out_shape) |
| 131 |
|
| 132 |
# run this a few times |
| 133 |
runs = 10 |
| 134 |
t1 = time.time() |
| 135 |
for n in range(runs): |
| 136 |
forget_wisdom() |
| 137 |
fft = FFTW(a, b, axes=axes) |
| 138 |
|
| 139 |
unlimited_time = (time.time() - t1)/runs |
| 140 |
|
| 141 |
time_limit = (unlimited_time)/8 |
| 142 |
|
| 143 |
# Now do it again but with an upper limit on the time |
| 144 |
t1 = time.time() |
| 145 |
for n in range(runs): |
| 146 |
forget_wisdom() |
| 147 |
fft = FFTW(a, b, axes=axes, planning_timelimit=time_limit) |
| 148 |
|
| 149 |
limited_time = (time.time() - t1)/runs |
| 150 |
|
| 151 |
import sys |
| 152 |
if sys.platform == 'win32': |
| 153 |
# Give a 6x margin on windows. The timers are low |
| 154 |
# precision and FFTW seems to take longer anyway. |
| 155 |
# Also, we need to allow for processor contention which |
| 156 |
# Appveyor seems prone to. |
| 157 |
self.assertTrue(limited_time < time_limit*6) |
| 158 |
else: |
| 159 |
# Otherwise have a 2x margin |
| 160 |
self.assertTrue(limited_time < time_limit*2) |
| 161 |
|
| 162 |
def test_invalid_planning_time_limit(self): |
| 163 |
in_shape = self.input_shapes['1d'] |
| 164 |
out_shape = self.output_shapes['1d'] |
| 165 |
|
| 166 |
axes=(0,) |
| 167 |
a, b = self.create_test_arrays(in_shape, out_shape) |
| 168 |
|
| 169 |
self.assertRaisesRegex(TypeError, 'Invalid planning timelimit', |
| 170 |
FFTW, *(a,b, axes), **{'planning_timelimit': 'foo'}) |
| 171 |
|
| 172 |
def test_planner_flags(self): |
| 173 |
'''Test all the planner flags on a small array |
| 174 |
''' |
| 175 |
in_shape = self.input_shapes['small_1d'] |
| 176 |
out_shape = self.output_shapes['small_1d'] |
| 177 |
|
| 178 |
axes=(0,) |
| 179 |
a, b = self.create_test_arrays(in_shape, out_shape) |
| 180 |
|
| 181 |
for each_flag in pyfftw.pyfftw._flag_dict: |
| 182 |
if each_flag == 'FFTW_WISDOM_ONLY': |
| 183 |
continue |
| 184 |
fft, ifft = self.run_validate_fft(a, b, axes, |
| 185 |
flags=(each_flag,)) |
| 186 |
|
| 187 |
self.assertTrue(each_flag in fft.flags) |
| 188 |
self.assertTrue(each_flag in ifft.flags) |
| 189 |
|
| 190 |
# also, test no flags (which should still work) |
| 191 |
fft, ifft = self.run_validate_fft(a, b, axes, |
| 192 |
flags=()) |
| 193 |
|
| 194 |
def test_wisdom_only(self): |
| 195 |
in_shape = self.input_shapes['small_1d'] |
| 196 |
out_shape = self.output_shapes['small_1d'] |
| 197 |
|
| 198 |
axes=(0,) |
| 199 |
a, b = self.create_test_arrays(in_shape, out_shape) |
| 200 |
forget_wisdom() |
| 201 |
# with no wisdom, an error should be raised with FFTW_WISDOM_ONLY |
| 202 |
# |
| 203 |
# NB: wisdom is specific to aligned/unaligned distinction, so we must |
| 204 |
# ensure that the arrays don't get copied (and potentially |
| 205 |
# switched between aligned and unaligned) by run_validate_fft()... |
| 206 |
self.assertRaisesRegex(RuntimeError, 'No FFTW wisdom', |
| 207 |
self.run_validate_fft, *(a, b, axes), |
| 208 |
**{'flags':('FFTW_ESTIMATE', 'FFTW_WISDOM_ONLY'), |
| 209 |
'create_array_copies': False}) |
| 210 |
# now plan the FFT |
| 211 |
self.run_validate_fft(a, b, axes, flags=('FFTW_ESTIMATE',), |
| 212 |
create_array_copies=False) |
| 213 |
# now FFTW_WISDOM_ONLY should not raise an error because the plan should |
| 214 |
# be in the wisdom |
| 215 |
self.run_validate_fft(a, b, axes, flags=('FFTW_ESTIMATE', |
| 216 |
'FFTW_WISDOM_ONLY'), create_array_copies=False) |
| 217 |
|
| 218 |
def test_destroy_input(self): |
| 219 |
'''Test the destroy input flag |
| 220 |
''' |
| 221 |
# We can't really test it actually destroys the input, as it might |
| 222 |
# not (plus it's not exactly something we want). |
| 223 |
# It's enough just to check it runs ok with that flag. |
| 224 |
in_shape = self.input_shapes['1d'] |
| 225 |
out_shape = self.output_shapes['1d'] |
| 226 |
|
| 227 |
axes=(0,) |
| 228 |
a, b = self.create_test_arrays(in_shape, out_shape) |
| 229 |
|
| 230 |
self.run_validate_fft(a, b, axes, |
| 231 |
flags=('FFTW_ESTIMATE','FFTW_DESTROY_INPUT')) |
| 232 |
|
| 233 |
def test_invalid_flag_fail(self): |
| 234 |
'''Test passing a garbage flag fails |
| 235 |
''' |
| 236 |
in_shape = self.input_shapes['1d'] |
| 237 |
out_shape = self.output_shapes['1d'] |
| 238 |
|
| 239 |
axes=(0,) |
| 240 |
a, b = self.create_test_arrays(in_shape, out_shape) |
| 241 |
|
| 242 |
self.assertRaisesRegex(ValueError, 'Invalid flag', |
| 243 |
self.run_validate_fft, *(a, b, axes), |
| 244 |
**{'flags':('garbage',)}) |
| 245 |
|
| 246 |
def test_alignment(self): |
| 247 |
'''Test to see if the alignment is returned correctly |
| 248 |
''' |
| 249 |
in_shape = self.input_shapes['2d'] |
| 250 |
out_shape = self.output_shapes['2d'] |
| 251 |
|
| 252 |
input_dtype_alignment = self.get_input_dtype_alignment() |
| 253 |
output_dtype_alignment = self.get_output_dtype_alignment() |
| 254 |
|
| 255 |
axes=(-1,) |
| 256 |
a, b = self.create_test_arrays(in_shape, out_shape) |
| 257 |
|
| 258 |
a = byte_align(a, n=16) |
| 259 |
b = byte_align(b, n=16) |
| 260 |
|
| 261 |
fft, ifft = self.run_validate_fft(a, b, axes, |
| 262 |
force_unaligned_data=True) |
| 263 |
|
| 264 |
a, b = self.create_test_arrays(in_shape, out_shape) |
| 265 |
|
| 266 |
a = byte_align(a, n=16) |
| 267 |
b = byte_align(b, n=16) |
| 268 |
|
| 269 |
a_orig = a.copy() |
| 270 |
b_orig = b.copy() |
| 271 |
|
| 272 |
# Offset from 16 byte aligned to guarantee it's not |
| 273 |
# 16 byte aligned |
| 274 |
a__ = empty_aligned( |
| 275 |
numpy.prod(in_shape)*a.itemsize + input_dtype_alignment, |
| 276 |
dtype='int8', n=16) |
| 277 |
|
| 278 |
a_ = (a__[input_dtype_alignment:] |
| 279 |
.view(dtype=self.input_dtype).reshape(*in_shape)) |
| 280 |
a_[:] = a |
| 281 |
|
| 282 |
b__ = empty_aligned( |
| 283 |
numpy.prod(out_shape)*b.itemsize + input_dtype_alignment, |
| 284 |
dtype='int8', n=16) |
| 285 |
|
| 286 |
b_ = (b__[input_dtype_alignment:] |
| 287 |
.view(dtype=self.output_dtype).reshape(*out_shape)) |
| 288 |
b_[:] = b |
| 289 |
|
| 290 |
a[:] = a_orig |
| 291 |
fft, ifft = self.run_validate_fft(a, b, axes, |
| 292 |
create_array_copies=False) |
| 293 |
|
| 294 |
self.assertTrue(fft.input_alignment == 16) |
| 295 |
self.assertTrue(fft.output_alignment == 16) |
| 296 |
|
| 297 |
a[:] = a_orig |
| 298 |
fft, ifft = self.run_validate_fft(a, b_, axes, |
| 299 |
create_array_copies=False) |
| 300 |
|
| 301 |
self.assertTrue(fft.input_alignment == input_dtype_alignment) |
| 302 |
self.assertTrue(fft.output_alignment == output_dtype_alignment) |
| 303 |
|
| 304 |
a_[:] = a_orig |
| 305 |
fft, ifft = self.run_validate_fft(a_, b, axes, |
| 306 |
create_array_copies=False) |
| 307 |
self.assertTrue(fft.input_alignment == input_dtype_alignment) |
| 308 |
self.assertTrue(fft.output_alignment == output_dtype_alignment) |
| 309 |
|
| 310 |
a_[:] = a_orig |
| 311 |
fft, ifft = self.run_validate_fft(a_, b_, axes, |
| 312 |
create_array_copies=False) |
| 313 |
self.assertTrue(fft.input_alignment == input_dtype_alignment) |
| 314 |
self.assertTrue(fft.output_alignment == output_dtype_alignment) |
| 315 |
|
| 316 |
a[:] = a_orig |
| 317 |
fft, ifft = self.run_validate_fft(a, b, axes, |
| 318 |
create_array_copies=False, force_unaligned_data=True) |
| 319 |
self.assertTrue(fft.input_alignment == input_dtype_alignment) |
| 320 |
self.assertTrue(fft.output_alignment == output_dtype_alignment) |
| 321 |
|
| 322 |
def test_incorrect_byte_alignment_fails(self): |
| 323 |
in_shape = self.input_shapes['2d'] |
| 324 |
out_shape = self.output_shapes['2d'] |
| 325 |
|
| 326 |
input_dtype_alignment = self.get_input_dtype_alignment() |
| 327 |
|
| 328 |
axes=(-1,) |
| 329 |
a, b = self.create_test_arrays(in_shape, out_shape) |
| 330 |
|
| 331 |
a = byte_align(a, n=16) |
| 332 |
b = byte_align(b, n=16) |
| 333 |
|
| 334 |
fft, ifft = self.run_validate_fft(a, b, axes, |
| 335 |
force_unaligned_data=True) |
| 336 |
|
| 337 |
a, b = self.create_test_arrays(in_shape, out_shape) |
| 338 |
|
| 339 |
# Offset from 16 byte aligned to guarantee it's not |
| 340 |
# 16 byte aligned |
| 341 |
a__ = empty_aligned( |
| 342 |
numpy.prod(in_shape)*a.itemsize + 1, |
| 343 |
dtype='int8', n=16) |
| 344 |
|
| 345 |
a_ = a__[1:].view(dtype=self.input_dtype).reshape(*in_shape) |
| 346 |
a_[:] = a |
| 347 |
|
| 348 |
b__ = empty_aligned( |
| 349 |
numpy.prod(out_shape)*b.itemsize + 1, |
| 350 |
dtype='int8', n=16) |
| 351 |
|
| 352 |
b_ = b__[1:].view(dtype=self.output_dtype).reshape(*out_shape) |
| 353 |
b_[:] = b |
| 354 |
|
| 355 |
self.assertRaisesRegex(ValueError, 'Invalid output alignment', |
| 356 |
FFTW, *(a, b_)) |
| 357 |
|
| 358 |
self.assertRaisesRegex(ValueError, 'Invalid input alignment', |
| 359 |
FFTW, *(a_, b)) |
| 360 |
|
| 361 |
self.assertRaisesRegex(ValueError, 'Invalid input alignment', |
| 362 |
FFTW, *(a_, b_)) |
| 363 |
|
| 364 |
def test_zero_length_fft_axis_fail(self): |
| 365 |
|
| 366 |
in_shape = (1024, 0) |
| 367 |
out_shape = in_shape |
| 368 |
|
| 369 |
axes = (-1,) |
| 370 |
|
| 371 |
a, b = self.create_test_arrays(in_shape, out_shape) |
| 372 |
|
| 373 |
self.assertRaisesRegex(ValueError, 'Zero length array', |
| 374 |
self.run_validate_fft, *(a,b, axes)) |
| 375 |
|
| 376 |
def test_missized_fail(self): |
| 377 |
in_shape = self.input_shapes['2d'] |
| 378 |
_out_shape = self.output_shapes['2d'] |
| 379 |
|
| 380 |
out_shape = (_out_shape[0]+1, _out_shape[1]) |
| 381 |
|
| 382 |
axes=(0,) |
| 383 |
a, b = self.create_test_arrays(in_shape, out_shape) |
| 384 |
|
| 385 |
with self.assertRaisesRegex(ValueError, 'Invalid shapes'): |
| 386 |
FFTW(a, b, axes, direction=self.direction) |
| 387 |
|
| 388 |
def test_missized_nonfft_axes_fail(self): |
| 389 |
in_shape = self.input_shapes['3d'] |
| 390 |
_out_shape = self.output_shapes['3d'] |
| 391 |
out_shape = (_out_shape[0], _out_shape[1]+1, _out_shape[2]) |
| 392 |
|
| 393 |
axes=(2,) |
| 394 |
a, b = self.create_test_arrays(in_shape, out_shape) |
| 395 |
|
| 396 |
with self.assertRaisesRegex(ValueError, 'Invalid shapes'): |
| 397 |
FFTW(a, b, direction=self.direction) |
| 398 |
|
| 399 |
def test_extra_dimension_fail(self): |
| 400 |
in_shape = self.input_shapes['2d'] |
| 401 |
_out_shape = self.output_shapes['2d'] |
| 402 |
out_shape = (2, _out_shape[0], _out_shape[1]) |
| 403 |
|
| 404 |
axes=(1,) |
| 405 |
a, b = self.create_test_arrays(in_shape, out_shape) |
| 406 |
|
| 407 |
with self.assertRaisesRegex(ValueError, 'Invalid shapes'): |
| 408 |
FFTW(a, b, direction=self.direction) |
| 409 |
|
| 410 |
def test_f_contiguous_1d(self): |
| 411 |
in_shape = self.input_shapes['2d'] |
| 412 |
out_shape = self.output_shapes['2d'] |
| 413 |
|
| 414 |
axes=(0,) |
| 415 |
a, b = self.create_test_arrays(in_shape, out_shape) |
| 416 |
|
| 417 |
# Taking the transpose just makes the array F contiguous |
| 418 |
a = a.transpose() |
| 419 |
b = b.transpose() |
| 420 |
|
| 421 |
self.run_validate_fft(a, b, axes, create_array_copies=False) |
| 422 |
|
| 423 |
def test_different_dtypes_fail(self): |
| 424 |
in_shape = self.input_shapes['2d'] |
| 425 |
out_shape = self.output_shapes['2d'] |
| 426 |
|
| 427 |
axes=(-1,) |
| 428 |
a, b = self.create_test_arrays(in_shape, out_shape) |
| 429 |
|
| 430 |
a_ = numpy.complex64(a) |
| 431 |
b_ = numpy.complex128(b) |
| 432 |
self.assertRaisesRegex(ValueError, 'Invalid scheme', |
| 433 |
FFTW, *(a_,b_)) |
| 434 |
|
| 435 |
a_ = numpy.complex128(a) |
| 436 |
b_ = numpy.complex64(b) |
| 437 |
self.assertRaisesRegex(ValueError, 'Invalid scheme', |
| 438 |
FFTW, *(a_,b_)) |
| 439 |
|
| 440 |
def test_update_data(self): |
| 441 |
in_shape = self.input_shapes['2d'] |
| 442 |
out_shape = self.output_shapes['2d'] |
| 443 |
|
| 444 |
axes=(-1,) |
| 445 |
a, b = self.create_test_arrays(in_shape, out_shape) |
| 446 |
|
| 447 |
fft, ifft = self.run_validate_fft(a, b, axes) |
| 448 |
|
| 449 |
a, b = self.create_test_arrays(in_shape, out_shape) |
| 450 |
|
| 451 |
self.run_validate_fft(a, b, axes, fft=fft, ifft=ifft) |
| 452 |
|
| 453 |
def test_with_not_ndarray_error(self): |
| 454 |
in_shape = self.input_shapes['2d'] |
| 455 |
out_shape = self.output_shapes['2d'] |
| 456 |
|
| 457 |
a, b = self.create_test_arrays(in_shape, out_shape) |
| 458 |
|
| 459 |
self.assertRaisesRegex(ValueError, 'Invalid output array', |
| 460 |
FFTW, *(a,10)) |
| 461 |
|
| 462 |
self.assertRaisesRegex(ValueError, 'Invalid input array', |
| 463 |
FFTW, *(10,b)) |
| 464 |
|
| 465 |
def test_update_data_with_not_ndarray_error(self): |
| 466 |
in_shape = self.input_shapes['2d'] |
| 467 |
out_shape = self.output_shapes['2d'] |
| 468 |
|
| 469 |
axes=(-1,) |
| 470 |
a, b = self.create_test_arrays(in_shape, out_shape) |
| 471 |
|
| 472 |
fft, ifft = self.run_validate_fft(a, b, axes, |
| 473 |
create_array_copies=False) |
| 474 |
|
| 475 |
self.assertRaisesRegex(ValueError, 'Invalid output array', |
| 476 |
fft.update_arrays, *(a,10)) |
| 477 |
|
| 478 |
self.assertRaisesRegex(ValueError, 'Invalid input array', |
| 479 |
fft.update_arrays, *(10,b)) |
| 480 |
|
| 481 |
def test_update_data_with_stride_error(self): |
| 482 |
in_shape = self.input_shapes['2d'] |
| 483 |
out_shape = self.output_shapes['2d'] |
| 484 |
|
| 485 |
axes=(-1,) |
| 486 |
a, b = self.create_test_arrays(in_shape, out_shape) |
| 487 |
|
| 488 |
fft, ifft = self.run_validate_fft(a, b, axes, |
| 489 |
create_array_copies=False) |
| 490 |
|
| 491 |
# We offset by 16 to make sure the byte alignment is still correct. |
| 492 |
in_shape = (in_shape[0]+16, in_shape[1]+16) |
| 493 |
out_shape = (out_shape[0]+16, out_shape[1]+16) |
| 494 |
|
| 495 |
a_, b_ = self.create_test_arrays(in_shape, out_shape) |
| 496 |
|
| 497 |
a_ = a_[16:,16:] |
| 498 |
b_ = b_[16:,16:] |
| 499 |
|
| 500 |
with self.assertRaisesRegex(ValueError, 'Invalid input striding'): |
| 501 |
self.run_validate_fft(a_, b, axes, |
| 502 |
fft=fft, ifft=ifft, create_array_copies=False) |
| 503 |
|
| 504 |
with self.assertRaisesRegex(ValueError, 'Invalid output striding'): |
| 505 |
self.run_validate_fft(a, b_, axes, |
| 506 |
fft=fft, ifft=ifft, create_array_copies=False) |
| 507 |
|
| 508 |
def test_update_data_with_shape_error(self): |
| 509 |
in_shape = self.input_shapes['2d'] |
| 510 |
out_shape = self.output_shapes['2d'] |
| 511 |
|
| 512 |
axes=(-1,) |
| 513 |
a, b = self.create_test_arrays(in_shape, out_shape) |
| 514 |
|
| 515 |
fft, ifft = self.run_validate_fft(a, b, axes) |
| 516 |
|
| 517 |
in_shape = (in_shape[0]-10, in_shape[1]) |
| 518 |
out_shape = (out_shape[0], out_shape[1]+5) |
| 519 |
|
| 520 |
a_, b_ = self.create_test_arrays(in_shape, out_shape) |
| 521 |
|
| 522 |
with self.assertRaisesRegex(ValueError, 'Invalid input shape'): |
| 523 |
self.run_validate_fft(a_, b, axes, |
| 524 |
fft=fft, ifft=ifft, create_array_copies=False) |
| 525 |
|
| 526 |
with self.assertRaisesRegex(ValueError, 'Invalid output shape'): |
| 527 |
self.run_validate_fft(a, b_, axes, |
| 528 |
fft=fft, ifft=ifft, create_array_copies=False) |
| 529 |
|
| 530 |
def test_update_unaligned_data_with_FFTW_UNALIGNED(self): |
| 531 |
in_shape = self.input_shapes['2d'] |
| 532 |
out_shape = self.output_shapes['2d'] |
| 533 |
|
| 534 |
input_dtype_alignment = self.get_input_dtype_alignment() |
| 535 |
|
| 536 |
axes=(-1,) |
| 537 |
a, b = self.create_test_arrays(in_shape, out_shape) |
| 538 |
|
| 539 |
a = byte_align(a, n=16) |
| 540 |
b = byte_align(b, n=16) |
| 541 |
|
| 542 |
fft, ifft = self.run_validate_fft(a, b, axes, |
| 543 |
force_unaligned_data=True) |
| 544 |
|
| 545 |
a, b = self.create_test_arrays(in_shape, out_shape) |
| 546 |
|
| 547 |
# Offset from 16 byte aligned to guarantee it's not |
| 548 |
# 16 byte aligned |
| 549 |
a__ = empty_aligned( |
| 550 |
numpy.prod(in_shape)*a.itemsize + input_dtype_alignment, |
| 551 |
dtype='int8', n=16) |
| 552 |
|
| 553 |
a_ = (a__[input_dtype_alignment:] |
| 554 |
.view(dtype=self.input_dtype).reshape(*in_shape)) |
| 555 |
a_[:] = a |
| 556 |
|
| 557 |
b__ = empty_aligned( |
| 558 |
numpy.prod(out_shape)*b.itemsize + input_dtype_alignment, |
| 559 |
dtype='int8', n=16) |
| 560 |
|
| 561 |
b_ = (b__[input_dtype_alignment:] |
| 562 |
.view(dtype=self.output_dtype).reshape(*out_shape)) |
| 563 |
b_[:] = b |
| 564 |
|
| 565 |
self.run_validate_fft(a, b_, axes, fft=fft, ifft=ifft) |
| 566 |
self.run_validate_fft(a_, b, axes, fft=fft, ifft=ifft) |
| 567 |
self.run_validate_fft(a_, b_, axes, fft=fft, ifft=ifft) |
| 568 |
|
| 569 |
def test_update_data_with_unaligned_original(self): |
| 570 |
in_shape = self.input_shapes['2d'] |
| 571 |
out_shape = self.output_shapes['2d'] |
| 572 |
|
| 573 |
input_dtype_alignment = self.get_input_dtype_alignment() |
| 574 |
|
| 575 |
axes=(-1,) |
| 576 |
a, b = self.create_test_arrays(in_shape, out_shape) |
| 577 |
|
| 578 |
# Offset from 16 byte aligned to guarantee it's not |
| 579 |
# 16 byte aligned |
| 580 |
a__ = empty_aligned( |
| 581 |
numpy.prod(in_shape)*a.itemsize + input_dtype_alignment, |
| 582 |
dtype='int8', n=16) |
| 583 |
|
| 584 |
a_ = a__[input_dtype_alignment:].view(dtype=self.input_dtype).reshape(*in_shape) |
| 585 |
a_[:] = a |
| 586 |
|
| 587 |
b__ = empty_aligned( |
| 588 |
numpy.prod(out_shape)*b.itemsize + input_dtype_alignment, |
| 589 |
dtype='int8', n=16) |
| 590 |
|
| 591 |
b_ = b__[input_dtype_alignment:].view(dtype=self.output_dtype).reshape(*out_shape) |
| 592 |
b_[:] = b |
| 593 |
|
| 594 |
fft, ifft = self.run_validate_fft(a_, b_, axes, |
| 595 |
force_unaligned_data=True) |
| 596 |
|
| 597 |
self.run_validate_fft(a, b_, axes, fft=fft, ifft=ifft) |
| 598 |
self.run_validate_fft(a_, b, axes, fft=fft, ifft=ifft) |
| 599 |
self.run_validate_fft(a_, b_, axes, fft=fft, ifft=ifft) |
| 600 |
|
| 601 |
|
| 602 |
def test_update_data_with_alignment_error(self): |
| 603 |
in_shape = self.input_shapes['2d'] |
| 604 |
out_shape = self.output_shapes['2d'] |
| 605 |
|
| 606 |
byte_error = 1 |
| 607 |
|
| 608 |
axes=(-1,) |
| 609 |
a, b = self.create_test_arrays(in_shape, out_shape) |
| 610 |
|
| 611 |
a = byte_align(a, n=16) |
| 612 |
b = byte_align(b, n=16) |
| 613 |
|
| 614 |
fft, ifft = self.run_validate_fft(a, b, axes) |
| 615 |
|
| 616 |
a, b = self.create_test_arrays(in_shape, out_shape) |
| 617 |
|
| 618 |
# Offset from 16 byte aligned to guarantee it's not |
| 619 |
# 16 byte aligned |
| 620 |
a__ = empty_aligned( |
| 621 |
numpy.prod(in_shape)*a.itemsize+byte_error, |
| 622 |
dtype='int8', n=16) |
| 623 |
|
| 624 |
a_ = (a__[byte_error:] |
| 625 |
.view(dtype=self.input_dtype).reshape(*in_shape)) |
| 626 |
a_[:] = a |
| 627 |
|
| 628 |
b__ = empty_aligned( |
| 629 |
numpy.prod(out_shape)*b.itemsize+byte_error, |
| 630 |
dtype='int8', n=16) |
| 631 |
|
| 632 |
b_ = (b__[byte_error:] |
| 633 |
.view(dtype=self.output_dtype).reshape(*out_shape)) |
| 634 |
b_[:] = b |
| 635 |
|
| 636 |
with self.assertRaisesRegex(ValueError, 'Invalid output alignment'): |
| 637 |
self.run_validate_fft(a, b_, axes, fft=fft, ifft=ifft, |
| 638 |
create_array_copies=False) |
| 639 |
|
| 640 |
with self.assertRaisesRegex(ValueError, 'Invalid input alignment'): |
| 641 |
self.run_validate_fft(a_, b, axes, fft=fft, ifft=ifft, |
| 642 |
create_array_copies=False) |
| 643 |
|
| 644 |
# Should also be true for the unaligned case |
| 645 |
fft, ifft = self.run_validate_fft(a, b, axes, |
| 646 |
force_unaligned_data=True) |
| 647 |
|
| 648 |
with self.assertRaisesRegex(ValueError, 'Invalid output alignment'): |
| 649 |
self.run_validate_fft(a, b_, axes, fft=fft, ifft=ifft, |
| 650 |
create_array_copies=False) |
| 651 |
|
| 652 |
with self.assertRaisesRegex(ValueError, 'Invalid input alignment'): |
| 653 |
self.run_validate_fft(a_, b, axes, fft=fft, ifft=ifft, |
| 654 |
create_array_copies=False) |
| 655 |
|
| 656 |
def test_invalid_axes(self): |
| 657 |
in_shape = self.input_shapes['2d'] |
| 658 |
out_shape = self.output_shapes['2d'] |
| 659 |
|
| 660 |
axes=(-3,) |
| 661 |
a, b = self.create_test_arrays(in_shape, out_shape) |
| 662 |
|
| 663 |
with self.assertRaisesRegex(IndexError, 'Invalid axes'): |
| 664 |
FFTW(a, b, axes, direction=self.direction) |
| 665 |
|
| 666 |
axes=(10,) |
| 667 |
with self.assertRaisesRegex(IndexError, 'Invalid axes'): |
| 668 |
FFTW(a, b, axes, direction=self.direction) |
| 669 |
|
| 670 |
class Complex64FFTWTest(Complex64FFTW1DTest, FFTWBaseTest): |
| 671 |
|
| 672 |
def test_2d(self): |
| 673 |
in_shape = self.input_shapes['2d'] |
| 674 |
out_shape = self.output_shapes['2d'] |
| 675 |
|
| 676 |
axes=(-2,-1) |
| 677 |
a, b = self.create_test_arrays(in_shape, out_shape) |
| 678 |
|
| 679 |
self.run_validate_fft(a, b, axes, create_array_copies=False) |
| 680 |
|
| 681 |
def test_multiple_2d(self): |
| 682 |
in_shape = self.input_shapes['3d'] |
| 683 |
out_shape = self.output_shapes['3d'] |
| 684 |
|
| 685 |
axes=(-2,-1) |
| 686 |
a, b = self.create_test_arrays(in_shape, out_shape) |
| 687 |
|
| 688 |
self.run_validate_fft(a, b, axes, create_array_copies=False) |
| 689 |
|
| 690 |
def test_3d(self): |
| 691 |
in_shape = self.input_shapes['3d'] |
| 692 |
out_shape = self.output_shapes['3d'] |
| 693 |
|
| 694 |
axes=(0, 1, 2) |
| 695 |
a, b = self.create_test_arrays(in_shape, out_shape) |
| 696 |
|
| 697 |
self.run_validate_fft(a, b, axes, create_array_copies=False) |
| 698 |
|
| 699 |
def test_non_monotonic_increasing_axes(self): |
| 700 |
'''Test the case where the axes arg does not monotonically increase. |
| 701 |
''' |
| 702 |
axes=(1, 0) |
| 703 |
|
| 704 |
# We still need the shapes to work! |
| 705 |
in_shape = numpy.asarray(self.input_shapes['2d'])[list(axes)] |
| 706 |
out_shape = numpy.asarray(self.output_shapes['2d'])[list(axes)] |
| 707 |
|
| 708 |
a, b = self.create_test_arrays(in_shape, out_shape, axes=axes) |
| 709 |
|
| 710 |
self.run_validate_fft(a, b, axes, create_array_copies=False) |
| 711 |
|
| 712 |
def test_non_contiguous_2d(self): |
| 713 |
in_shape = self.input_shapes['2d'] |
| 714 |
out_shape = self.output_shapes['2d'] |
| 715 |
|
| 716 |
axes=(-2,-1) |
| 717 |
a, b = self.create_test_arrays(in_shape, out_shape) |
| 718 |
|
| 719 |
# Some arbitrary and crazy slicing |
| 720 |
a_sliced = a[12:200:3, 300:2041:9] |
| 721 |
# b needs to be the same size |
| 722 |
b_sliced = b[20:146:2, 100:1458:7] |
| 723 |
|
| 724 |
self.run_validate_fft(a_sliced, b_sliced, axes, create_array_copies=False) |
| 725 |
|
| 726 |
def test_non_contiguous_2d_in_3d(self): |
| 727 |
in_shape = (256, 4, 2048) |
| 728 |
out_shape = in_shape |
| 729 |
axes=(0,2) |
| 730 |
a, b = self.create_test_arrays(in_shape, out_shape) |
| 731 |
|
| 732 |
# Some arbitrary and crazy slicing |
| 733 |
a_sliced = a[12:200:3, :, 300:2041:9] |
| 734 |
# b needs to be the same size |
| 735 |
b_sliced = b[20:146:2, :, 100:1458:7] |
| 736 |
|
| 737 |
self.run_validate_fft(a_sliced, b_sliced, axes, create_array_copies=False) |
| 738 |
|
| 739 |
@unittest.skipIf(*miss('64')) |
| 740 |
class Complex128FFTWTest(Complex64FFTWTest): |
| 741 |
|
| 742 |
def setUp(self): |
| 743 |
self.input_dtype = numpy.complex128 |
| 744 |
self.output_dtype = numpy.complex128 |
| 745 |
self.np_fft_comparison = np_fft.fft |
| 746 |
|
| 747 |
self.direction = 'FFTW_FORWARD' |
| 748 |
return |
| 749 |
|
| 750 |
@unittest.skipIf(*miss('ld')) |
| 751 |
class ComplexLongDoubleFFTWTest(Complex64FFTWTest): |
| 752 |
|
| 753 |
def setUp(self): |
| 754 |
|
| 755 |
self.input_dtype = numpy.clongdouble |
| 756 |
self.output_dtype = numpy.clongdouble |
| 757 |
self.np_fft_comparison = self.reference_fftn |
| 758 |
|
| 759 |
self.direction = 'FFTW_FORWARD' |
| 760 |
return |
| 761 |
|
| 762 |
def reference_fftn(self, a, axes): |
| 763 |
|
| 764 |
# numpy.fft.fftn doesn't support complex256 type, |
| 765 |
# so we need to compare to a lower precision type. |
| 766 |
a = numpy.complex128(a) |
| 767 |
return np_fft.fftn(a, axes=axes) |
| 768 |
|
| 769 |
@unittest.skip('numpy.fft has issues with this dtype.') |
| 770 |
def test_time(self): |
| 771 |
pass |
| 772 |
|
| 773 |
@unittest.skip('numpy.fft has issues with this dtype.') |
| 774 |
def test_time_with_array_update(self): |
| 775 |
pass |
| 776 |
|
| 777 |
test_cases = ( |
| 778 |
Complex64FFTWTest, |
| 779 |
Complex128FFTWTest, |
| 780 |
ComplexLongDoubleFFTWTest,) |
| 781 |
|
| 782 |
test_set = None |
| 783 |
#test_set = {'all':['test_alignment', 'test_incorrect_byte_alignment_fails']} |
| 784 |
|
| 785 |
if __name__ == '__main__': |
| 786 |
run_test_suites(test_cases, test_set) |