| 1 |
# Copyright 2014 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 ( |
| 36 |
FFTW, empty_aligned, is_byte_aligned, simd_alignment) |
| 37 |
import pyfftw |
| 38 |
|
| 39 |
from .test_pyfftw_base import run_test_suites, miss, require |
| 40 |
|
| 41 |
import unittest |
| 42 |
import numpy |
| 43 |
import warnings |
| 44 |
|
| 45 |
# FFTW tests that don't seem to fit anywhere else |
| 46 |
|
| 47 |
class FFTWMiscTest(unittest.TestCase): |
| 48 |
|
| 49 |
def __init__(self, *args, **kwargs): |
| 50 |
|
| 51 |
super(FFTWMiscTest, self).__init__(*args, **kwargs) |
| 52 |
|
| 53 |
# Assume python 3, but keep backwards compatibility |
| 54 |
if not hasattr(self, 'assertRaisesRegex'): |
| 55 |
self.assertRaisesRegex = self.assertRaisesRegexp |
| 56 |
|
| 57 |
|
| 58 |
def setUp(self): |
| 59 |
require(self, '64') |
| 60 |
|
| 61 |
self.input_array = empty_aligned((256, 512), dtype='complex128', n=16) |
| 62 |
self.output_array = empty_aligned((256, 512), dtype='complex128', n=16) |
| 63 |
|
| 64 |
self.fft = FFTW(self.input_array, self.output_array) |
| 65 |
|
| 66 |
self.output_array[:] = (numpy.random.randn(*self.output_array.shape) |
| 67 |
+ 1j*numpy.random.randn(*self.output_array.shape)) |
| 68 |
|
| 69 |
def test_aligned_flag(self): |
| 70 |
'''Test to see if the aligned flag is correct |
| 71 |
''' |
| 72 |
fft = FFTW(self.input_array, self.output_array) |
| 73 |
self.assertTrue(fft.simd_aligned) |
| 74 |
|
| 75 |
fft = FFTW(self.input_array, self.output_array, |
| 76 |
flags=('FFTW_UNALIGNED',)) |
| 77 |
|
| 78 |
self.assertFalse(fft.simd_aligned) |
| 79 |
|
| 80 |
@unittest.skipIf(*miss('32')) |
| 81 |
def test_flags(self): |
| 82 |
'''Test to see if the flags are correct |
| 83 |
''' |
| 84 |
fft = FFTW(self.input_array, self.output_array) |
| 85 |
self.assertEqual(fft.flags, ('FFTW_MEASURE',)) |
| 86 |
|
| 87 |
fft = FFTW(self.input_array, self.output_array, |
| 88 |
flags=('FFTW_DESTROY_INPUT', 'FFTW_UNALIGNED')) |
| 89 |
self.assertEqual(fft.flags, ('FFTW_DESTROY_INPUT', 'FFTW_UNALIGNED')) |
| 90 |
|
| 91 |
# Test an implicit flag |
| 92 |
_input_array = empty_aligned(256, dtype='complex64', n=16) |
| 93 |
_output_array = empty_aligned(256, dtype='complex64', n=16) |
| 94 |
|
| 95 |
# These are guaranteed to be misaligned (due to dtype size == 8) |
| 96 |
input_array = _input_array[:-1] |
| 97 |
output_array = _output_array[:-1] |
| 98 |
u_input_array = _input_array[1:] |
| 99 |
u_output_array = _output_array[1:] |
| 100 |
|
| 101 |
fft = FFTW(input_array, u_output_array) |
| 102 |
self.assertEqual(fft.flags, ('FFTW_MEASURE', 'FFTW_UNALIGNED')) |
| 103 |
|
| 104 |
fft = FFTW(u_input_array, output_array) |
| 105 |
self.assertEqual(fft.flags, ('FFTW_MEASURE', 'FFTW_UNALIGNED')) |
| 106 |
|
| 107 |
fft = FFTW(u_input_array, u_output_array) |
| 108 |
self.assertEqual(fft.flags, ('FFTW_MEASURE', 'FFTW_UNALIGNED')) |
| 109 |
|
| 110 |
@unittest.skipIf(*miss('32')) |
| 111 |
def test_differing_aligned_arrays_update(self): |
| 112 |
'''Test to see if the alignment code is working as expected |
| 113 |
''' |
| 114 |
|
| 115 |
# Start by creating arrays that are only on various byte |
| 116 |
# alignments (4, 16 and 32) |
| 117 |
_input_array = empty_aligned(len(self.input_array.ravel())*2+5, |
| 118 |
dtype='float32', n=32) |
| 119 |
_output_array = empty_aligned(len(self.output_array.ravel())*2+5, |
| 120 |
dtype='float32', n=32) |
| 121 |
|
| 122 |
_input_array[:] = 0 |
| 123 |
_output_array[:] = 0 |
| 124 |
|
| 125 |
input_array_4 = ( |
| 126 |
numpy.frombuffer(_input_array[1:-4].data, dtype='complex64') |
| 127 |
.reshape(self.input_array.shape)) |
| 128 |
output_array_4 = ( |
| 129 |
numpy.frombuffer(_output_array[1:-4].data, dtype='complex64') |
| 130 |
.reshape(self.output_array.shape)) |
| 131 |
|
| 132 |
input_array_16 = ( |
| 133 |
numpy.frombuffer(_input_array[4:-1].data, dtype='complex64') |
| 134 |
.reshape(self.input_array.shape)) |
| 135 |
output_array_16 = ( |
| 136 |
numpy.frombuffer(_output_array[4:-1].data, dtype='complex64') |
| 137 |
.reshape(self.output_array.shape)) |
| 138 |
|
| 139 |
input_array_32 = ( |
| 140 |
numpy.frombuffer(_input_array[:-5].data, dtype='complex64') |
| 141 |
.reshape(self.input_array.shape)) |
| 142 |
output_array_32 = ( |
| 143 |
numpy.frombuffer(_output_array[:-5].data, dtype='complex64') |
| 144 |
.reshape(self.output_array.shape)) |
| 145 |
|
| 146 |
input_arrays = {4: input_array_4, |
| 147 |
16: input_array_16, |
| 148 |
32: input_array_32} |
| 149 |
|
| 150 |
output_arrays = {4: output_array_4, |
| 151 |
16: output_array_16, |
| 152 |
32: output_array_32} |
| 153 |
|
| 154 |
alignments = (4, 16, 32) |
| 155 |
|
| 156 |
# Test the arrays are aligned on 4 bytes... |
| 157 |
self.assertTrue(is_byte_aligned(input_arrays[4], n=4)) |
| 158 |
self.assertTrue(is_byte_aligned(output_arrays[4], n=4)) |
| 159 |
|
| 160 |
# ...and on 16... |
| 161 |
self.assertFalse(is_byte_aligned(input_arrays[4], n=16)) |
| 162 |
self.assertFalse(is_byte_aligned(output_arrays[4], n=16)) |
| 163 |
self.assertTrue(is_byte_aligned(input_arrays[16], n=16)) |
| 164 |
self.assertTrue(is_byte_aligned(output_arrays[16], n=16)) |
| 165 |
|
| 166 |
# ...and on 32... |
| 167 |
self.assertFalse(is_byte_aligned(input_arrays[16], n=32)) |
| 168 |
self.assertFalse(is_byte_aligned(output_arrays[16], n=32)) |
| 169 |
self.assertTrue(is_byte_aligned(input_arrays[32], n=32)) |
| 170 |
self.assertTrue(is_byte_aligned(output_arrays[32], n=32)) |
| 171 |
|
| 172 |
if len(pyfftw.pyfftw._valid_simd_alignments) > 0: |
| 173 |
max_align = pyfftw.pyfftw._valid_simd_alignments[0] |
| 174 |
else: |
| 175 |
max_align = simd_alignment |
| 176 |
|
| 177 |
for in_align in alignments: |
| 178 |
for out_align in alignments: |
| 179 |
expected_align = min(in_align, out_align, max_align) |
| 180 |
fft = FFTW(input_arrays[in_align], output_arrays[out_align]) |
| 181 |
|
| 182 |
self.assertTrue(fft.input_alignment == expected_align) |
| 183 |
self.assertTrue(fft.output_alignment == expected_align) |
| 184 |
|
| 185 |
for update_align in alignments: |
| 186 |
|
| 187 |
if update_align < expected_align: |
| 188 |
# This should fail (not aligned properly) |
| 189 |
self.assertRaisesRegex(ValueError, |
| 190 |
'Invalid input alignment', |
| 191 |
fft.update_arrays, |
| 192 |
input_arrays[update_align], |
| 193 |
output_arrays[out_align]) |
| 194 |
|
| 195 |
self.assertRaisesRegex(ValueError, |
| 196 |
'Invalid output alignment', |
| 197 |
fft.update_arrays, |
| 198 |
input_arrays[in_align], |
| 199 |
output_arrays[update_align]) |
| 200 |
|
| 201 |
else: |
| 202 |
# This should work (and not segfault!) |
| 203 |
fft.update_arrays(input_arrays[update_align], |
| 204 |
output_arrays[out_align]) |
| 205 |
fft.update_arrays(input_arrays[in_align], |
| 206 |
output_arrays[update_align]) |
| 207 |
fft.execute() |
| 208 |
|
| 209 |
|
| 210 |
def test_get_input_array(self): |
| 211 |
'''Test to see the get_input_array method returns the correct thing |
| 212 |
''' |
| 213 |
with warnings.catch_warnings(record=True) as w: |
| 214 |
# This method is deprecated, so check the deprecation warning |
| 215 |
# is raised. |
| 216 |
warnings.simplefilter("always") |
| 217 |
input_array = self.fft.get_input_array() |
| 218 |
self.assertEqual(len(w), 1) |
| 219 |
self.assertTrue(issubclass(w[-1].category, DeprecationWarning)) |
| 220 |
|
| 221 |
self.assertIs(self.input_array, input_array) |
| 222 |
|
| 223 |
def test_get_output_array(self): |
| 224 |
'''Test to see the get_output_array method returns the correct thing |
| 225 |
''' |
| 226 |
with warnings.catch_warnings(record=True) as w: |
| 227 |
# This method is deprecated, so check the deprecation warning |
| 228 |
# is raised. |
| 229 |
warnings.simplefilter("always") |
| 230 |
output_array = self.fft.get_output_array() |
| 231 |
self.assertEqual(len(w), 1) |
| 232 |
self.assertTrue(issubclass(w[-1].category, DeprecationWarning)) |
| 233 |
|
| 234 |
self.assertIs(self.output_array, output_array) |
| 235 |
|
| 236 |
def test_input_array(self): |
| 237 |
'''Test to see the input_array property returns the correct thing |
| 238 |
''' |
| 239 |
self.assertIs(self.input_array, self.fft.input_array) |
| 240 |
|
| 241 |
def test_output_array(self): |
| 242 |
'''Test to see the output_array property returns the correct thing |
| 243 |
''' |
| 244 |
self.assertIs(self.output_array, self.fft.output_array) |
| 245 |
|
| 246 |
def test_input_strides(self): |
| 247 |
'''Test to see if the input_strides property returns the correct thing |
| 248 |
''' |
| 249 |
self.assertEqual(self.fft.input_strides, self.input_array.strides) |
| 250 |
|
| 251 |
new_input_array = self.input_array[::2, ::4] |
| 252 |
new_output_array = self.output_array[::2, ::4] |
| 253 |
|
| 254 |
new_fft = FFTW(new_input_array, new_output_array) |
| 255 |
|
| 256 |
self.assertEqual(new_fft.input_strides, new_input_array.strides) |
| 257 |
|
| 258 |
def test_output_strides(self): |
| 259 |
'''Test to see if the output_strides property returns the correct thing |
| 260 |
''' |
| 261 |
self.assertEqual(self.fft.output_strides, self.output_array.strides) |
| 262 |
|
| 263 |
new_input_array = self.output_array[::2, ::4] |
| 264 |
new_output_array = self.output_array[::2, ::4] |
| 265 |
|
| 266 |
new_fft = FFTW(new_input_array, new_output_array) |
| 267 |
|
| 268 |
self.assertEqual(new_fft.output_strides, new_output_array.strides) |
| 269 |
|
| 270 |
def test_input_shape(self): |
| 271 |
'''Test to see if the input_shape property returns the correct thing |
| 272 |
''' |
| 273 |
self.assertEqual(self.fft.input_shape, self.input_array.shape) |
| 274 |
|
| 275 |
new_input_array = self.input_array[::2, ::4] |
| 276 |
new_output_array = self.output_array[::2, ::4] |
| 277 |
|
| 278 |
new_fft = FFTW(new_input_array, new_output_array) |
| 279 |
|
| 280 |
self.assertEqual(new_fft.input_shape, new_input_array.shape) |
| 281 |
|
| 282 |
def test_output_strides(self): |
| 283 |
'''Test to see if the output_shape property returns the correct thing |
| 284 |
''' |
| 285 |
self.assertEqual(self.fft.output_shape, self.output_array.shape) |
| 286 |
|
| 287 |
new_input_array = self.output_array[::2, ::4] |
| 288 |
new_output_array = self.output_array[::2, ::4] |
| 289 |
|
| 290 |
new_fft = FFTW(new_input_array, new_output_array) |
| 291 |
|
| 292 |
self.assertEqual(new_fft.output_shape, new_output_array.shape) |
| 293 |
|
| 294 |
@unittest.skipIf(*miss('32')) |
| 295 |
def test_input_dtype(self): |
| 296 |
'''Test to see if the input_dtype property returns the correct thing |
| 297 |
''' |
| 298 |
self.assertEqual(self.fft.input_dtype, self.input_array.dtype) |
| 299 |
|
| 300 |
new_input_array = numpy.complex64(self.input_array) |
| 301 |
new_output_array = numpy.complex64(self.output_array) |
| 302 |
|
| 303 |
new_fft = FFTW(new_input_array, new_output_array) |
| 304 |
|
| 305 |
self.assertEqual(new_fft.input_dtype, new_input_array.dtype) |
| 306 |
|
| 307 |
@unittest.skipIf(*miss('32')) |
| 308 |
def test_output_dtype(self): |
| 309 |
'''Test to see if the output_dtype property returns the correct thing |
| 310 |
''' |
| 311 |
self.assertEqual(self.fft.output_dtype, self.output_array.dtype) |
| 312 |
|
| 313 |
new_input_array = numpy.complex64(self.input_array) |
| 314 |
new_output_array = numpy.complex64(self.output_array) |
| 315 |
|
| 316 |
new_fft = FFTW(new_input_array, new_output_array) |
| 317 |
|
| 318 |
self.assertEqual(new_fft.output_dtype, new_output_array.dtype) |
| 319 |
|
| 320 |
def test_direction_property(self): |
| 321 |
'''Test to see if the direction property returns the correct thing |
| 322 |
''' |
| 323 |
self.assertEqual(self.fft.direction, 'FFTW_FORWARD') |
| 324 |
|
| 325 |
new_fft = FFTW(self.input_array, self.output_array, |
| 326 |
direction='FFTW_BACKWARD') |
| 327 |
|
| 328 |
self.assertEqual(new_fft.direction, 'FFTW_BACKWARD') |
| 329 |
|
| 330 |
def test_axes_property(self): |
| 331 |
'''Test to see if the axes property returns the correct thing |
| 332 |
''' |
| 333 |
self.assertEqual(self.fft.axes, (1,)) |
| 334 |
|
| 335 |
new_fft = FFTW(self.input_array, self.output_array, axes=(-1, -2)) |
| 336 |
self.assertEqual(new_fft.axes, (1, 0)) |
| 337 |
|
| 338 |
new_fft = FFTW(self.input_array, self.output_array, axes=(-2, -1)) |
| 339 |
self.assertEqual(new_fft.axes, (0, 1)) |
| 340 |
|
| 341 |
new_fft = FFTW(self.input_array, self.output_array, axes=(1, 0)) |
| 342 |
self.assertEqual(new_fft.axes, (1, 0)) |
| 343 |
|
| 344 |
new_fft = FFTW(self.input_array, self.output_array, axes=(1,)) |
| 345 |
self.assertEqual(new_fft.axes, (1,)) |
| 346 |
|
| 347 |
new_fft = FFTW(self.input_array, self.output_array, axes=(0,)) |
| 348 |
self.assertEqual(new_fft.axes, (0,)) |
| 349 |
|
| 350 |
def test_ortho_property(self): |
| 351 |
'''ortho property defaults to False |
| 352 |
''' |
| 353 |
self.assertEqual(self.fft.ortho, False) |
| 354 |
|
| 355 |
newfft = FFTW(self.input_array, self.output_array, ortho=True, |
| 356 |
normalise_idft=False) |
| 357 |
self.assertEqual(newfft.ortho, True) |
| 358 |
|
| 359 |
def test_normalise_idft_property(self): |
| 360 |
'''normalise_idft property defaults to True |
| 361 |
''' |
| 362 |
self.assertEqual(self.fft.normalise_idft, True) |
| 363 |
|
| 364 |
newfft = FFTW(self.input_array, self.output_array, |
| 365 |
normalise_idft=False) |
| 366 |
self.assertEqual(newfft.normalise_idft, False) |
| 367 |
|
| 368 |
def test_invalid_normalisation(self): |
| 369 |
# both ortho and normalise_idft cannot be True |
| 370 |
self.assertRaisesRegex( |
| 371 |
ValueError, 'Invalid options: ortho', |
| 372 |
FFTW, self.input_array, self.output_array, |
| 373 |
direction='FFTW_BACKWARD', ortho=True, normalise_idft=True) |
| 374 |
|
| 375 |
test_cases = ( |
| 376 |
FFTWMiscTest,) |
| 377 |
|
| 378 |
test_set = None |
| 379 |
|
| 380 |
if __name__ == '__main__': |
| 381 |
|
| 382 |
run_test_suites(test_cases, test_set) |