Debian Codegraph @

tests/test_pyfftw_call.py

raw
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
36 from pyfftw import (
37 FFTW, empty_aligned, byte_align)
38
39 from .test_pyfftw_base import run_test_suites, miss, require
40 import numpy
41 import unittest
42
43 @unittest.skipIf(*miss('64'))
44 class FFTWCallTest(unittest.TestCase):
45
46 def __init__(self, *args, **kwargs):
47
48 super(FFTWCallTest, self).__init__(*args, **kwargs)
49
50 if not hasattr(self, 'assertRaisesRegex'):
51 self.assertRaisesRegex = self.assertRaisesRegexp
52
53 def setUp(self):
54
55 self.input_array = empty_aligned((256, 512), dtype='complex128', n=16)
56 self.output_array = empty_aligned((256, 512), dtype='complex128', n=16)
57
58 self.fft = FFTW(self.input_array, self.output_array)
59
60 self.input_array[:] = (numpy.random.randn(*self.input_array.shape)
61 + 1j*numpy.random.randn(*self.input_array.shape))
62
63
64 def test_call(self):
65 '''Test a call to an instance of the class.
66 '''
67
68 self.input_array[:] = (numpy.random.randn(*self.input_array.shape)
69 + 1j*numpy.random.randn(*self.input_array.shape))
70
71 output_array = self.fft()
72
73 self.assertTrue(numpy.all(output_array == self.output_array))
74
75
76 def test_call_with_positional_input_update(self):
77 '''Test the class call with a positional input update.
78 '''
79
80 input_array = byte_align(
81 (numpy.random.randn(*self.input_array.shape)
82 + 1j*numpy.random.randn(*self.input_array.shape)), n=16)
83
84 output_array = self.fft(byte_align(input_array.copy(), n=16)).copy()
85
86 self.fft.update_arrays(input_array, self.output_array)
87 self.fft.execute()
88
89 self.assertTrue(numpy.all(output_array == self.output_array))
90
91 def test_call_with_keyword_input_update(self):
92 '''Test the class call with a keyword input update.
93 '''
94 input_array = byte_align(
95 numpy.random.randn(*self.input_array.shape)
96 + 1j*numpy.random.randn(*self.input_array.shape), n=16)
97
98 output_array = self.fft(
99 input_array=byte_align(input_array.copy(), n=16)).copy()
100
101 self.fft.update_arrays(input_array, self.output_array)
102 self.fft.execute()
103
104 self.assertTrue(numpy.all(output_array == self.output_array))
105
106
107 def test_call_with_keyword_output_update(self):
108 '''Test the class call with a keyword output update.
109 '''
110 output_array = byte_align(
111 (numpy.random.randn(*self.output_array.shape)
112 + 1j*numpy.random.randn(*self.output_array.shape)), n=16)
113
114 returned_output_array = self.fft(
115 output_array=byte_align(output_array.copy(), n=16)).copy()
116
117 self.fft.update_arrays(self.input_array, output_array)
118 self.fft.execute()
119
120 self.assertTrue(
121 numpy.all(returned_output_array == output_array))
122
123 def test_call_with_positional_updates(self):
124 '''Test the class call with a positional array updates.
125 '''
126
127 input_array = byte_align((numpy.random.randn(*self.input_array.shape)
128 + 1j*numpy.random.randn(*self.input_array.shape)), n=16)
129
130 output_array = byte_align((numpy.random.randn(*self.output_array.shape)
131 + 1j*numpy.random.randn(*self.output_array.shape)), n=16)
132
133 returned_output_array = self.fft(
134 byte_align(input_array.copy(), n=16),
135 byte_align(output_array.copy(), n=16)).copy()
136
137 self.fft.update_arrays(input_array, output_array)
138 self.fft.execute()
139
140 self.assertTrue(numpy.all(returned_output_array == output_array))
141
142 def test_call_with_keyword_updates(self):
143 '''Test the class call with a positional output update.
144 '''
145
146 input_array = byte_align(
147 (numpy.random.randn(*self.input_array.shape)
148 + 1j*numpy.random.randn(*self.input_array.shape)), n=16)
149
150 output_array = byte_align(
151 (numpy.random.randn(*self.output_array.shape)
152 + 1j*numpy.random.randn(*self.output_array.shape)), n=16)
153
154 returned_output_array = self.fft(
155 output_array=byte_align(output_array.copy(), n=16),
156 input_array=byte_align(input_array.copy(), n=16)).copy()
157
158 self.fft.update_arrays(input_array, output_array)
159 self.fft.execute()
160
161 self.assertTrue(numpy.all(returned_output_array == output_array))
162
163 def test_call_with_different_input_dtype(self):
164 '''Test the class call with an array with a different input dtype
165 '''
166 input_array = byte_align(numpy.complex64(
167 numpy.random.randn(*self.input_array.shape)
168 + 1j*numpy.random.randn(*self.input_array.shape)), n=16)
169
170 output_array = self.fft(byte_align(input_array.copy(), n=16)).copy()
171
172 _input_array = byte_align(numpy.asarray(input_array,
173 dtype=self.input_array.dtype), n=16)
174
175 self.assertTrue(_input_array.dtype != input_array.dtype)
176
177 self.fft.update_arrays(_input_array, self.output_array)
178 self.fft.execute()
179
180 self.assertTrue(numpy.all(output_array == self.output_array))
181
182 def test_call_with_list_input(self):
183 '''Test the class call with a list rather than an array
184 '''
185
186 output_array = self.fft().copy()
187
188 test_output_array = self.fft(self.input_array.tolist()).copy()
189
190 self.assertTrue(numpy.all(output_array == test_output_array))
191
192
193 def test_call_with_invalid_update(self):
194 '''Test the class call with an invalid update.
195 '''
196
197 new_shape = self.input_array.shape + (2, )
198 invalid_array = (numpy.random.randn(*new_shape)
199 + 1j*numpy.random.randn(*new_shape))
200
201 self.assertRaises(ValueError, self.fft,
202 *(),
203 **{'output_array':invalid_array})
204
205 self.assertRaises(ValueError, self.fft,
206 *(),
207 **{'input_array':invalid_array})
208
209
210 @unittest.skipIf(*miss('32'))
211 def test_call_with_auto_input_alignment(self):
212 '''Test the class call with a keyword input update.
213 '''
214 input_array = (numpy.random.randn(*self.input_array.shape)
215 + 1j*numpy.random.randn(*self.input_array.shape))
216
217 output_array = self.fft(
218 input_array=byte_align(input_array.copy(), n=16)).copy()
219
220 # Offset by one from 16 byte aligned to guarantee it's not
221 # 16 byte aligned
222 a = input_array
223 a__ = empty_aligned(numpy.prod(a.shape)*a.itemsize+1, dtype='int8',
224 n=16)
225
226 a_ = a__[1:].view(dtype=a.dtype).reshape(*a.shape)
227 a_[:] = a
228
229 # Just confirm that a usual update will fail
230 self.assertRaisesRegex(ValueError, 'Invalid input alignment',
231 self.fft.update_arrays, *(a_, self.output_array))
232
233 self.fft(a_, self.output_array)
234
235 self.assertTrue(numpy.all(output_array == self.output_array))
236
237 # now try with a single byte offset and SIMD off
238 ar, ai = numpy.float32(numpy.random.randn(2, 257))
239 a = ar[1:] + 1j*ai[1:]
240
241 b = a.copy()
242
243 a_size = len(a.ravel())*a.itemsize
244
245 update_array = numpy.frombuffer(
246 numpy.zeros(a_size + 1, dtype='int8')[1:].data,
247 dtype=a.dtype).reshape(a.shape)
248
249 fft = FFTW(a, b, flags=('FFTW_UNALIGNED',))
250 # Confirm that a usual update will fail (it's not on the
251 # byte boundary)
252 self.assertRaisesRegex(ValueError, 'Invalid input alignment',
253 fft.update_arrays, *(update_array, b))
254
255 fft(update_array, b)
256
257 def test_call_with_invalid_output_striding(self):
258 '''Test the class call with an invalid strided output update.
259 '''
260 # Add an extra dimension to bugger up the striding
261 new_shape = self.output_array.shape + (2,)
262 output_array = byte_align(numpy.random.randn(*new_shape)
263 + 1j*numpy.random.randn(*new_shape), n=16)
264
265 self.assertRaisesRegex(ValueError, 'Invalid output striding',
266 self.fft, **{'output_array': output_array[:,:,1]})
267
268 def test_call_with_different_striding(self):
269 '''Test the input update with different strides to internal array.
270 '''
271 shape = self.input_array.shape + (2,)
272
273 input_array = byte_align(numpy.random.randn(*shape)
274 + 1j*numpy.random.randn(*shape), n=16)
275
276 fft = FFTW(input_array[:,:,0], self.output_array)
277
278 test_output_array = fft().copy()
279
280 new_input_array = byte_align(
281 input_array[:, :, 0].copy(), n=16)
282
283 new_output = fft(new_input_array).copy()
284
285 # Test the test!
286 self.assertTrue(new_input_array.strides != input_array[:,:,0].strides)
287
288 self.assertTrue(numpy.all(test_output_array == new_output))
289
290 def test_call_with_copy_with_missized_array_error(self):
291 '''Force an input copy with a missized array.
292 '''
293 shape = list(self.input_array.shape + (2,))
294 shape[0] += 1
295
296 input_array = byte_align(numpy.random.randn(*shape)
297 + 1j*numpy.random.randn(*shape), n=16)
298
299 fft = FFTW(self.input_array, self.output_array)
300
301 self.assertRaisesRegex(ValueError, 'Invalid input shape',
302 self.fft, **{'input_array': input_array[:,:,0]})
303
304 def test_call_with_unaligned(self):
305 '''Make sure the right thing happens with unaligned data.
306 '''
307 input_array = (numpy.random.randn(*self.input_array.shape)
308 + 1j*numpy.random.randn(*self.input_array.shape))
309
310 output_array = self.fft(
311 input_array=byte_align(input_array.copy(), n=16)).copy()
312
313 input_array = byte_align(input_array, n=16)
314 output_array = byte_align(output_array, n=16)
315
316 # Offset by one from 16 byte aligned to guarantee it's not
317 # 16 byte aligned
318 a = byte_align(input_array.copy(), n=16)
319 a__ = empty_aligned(numpy.prod(a.shape)*a.itemsize+1, dtype='int8',
320 n=16)
321
322 a_ = a__[1:].view(dtype=a.dtype).reshape(*a.shape)
323 a_[:] = a
324
325 # Create a different second array the same way
326 b = byte_align(output_array.copy(), n=16)
327 b__ = empty_aligned(numpy.prod(b.shape)*a.itemsize+1, dtype='int8',
328 n=16)
329
330 b_ = b__[1:].view(dtype=b.dtype).reshape(*b.shape)
331 b_[:] = a
332
333 # Set up for the first array
334 fft = FFTW(input_array, output_array)
335 a_[:] = a
336 output_array = fft().copy()
337
338 # Check a_ is not aligned...
339 self.assertRaisesRegex(ValueError, 'Invalid input alignment',
340 self.fft.update_arrays, *(a_, output_array))
341
342 # and b_ too
343 self.assertRaisesRegex(ValueError, 'Invalid output alignment',
344 self.fft.update_arrays, *(input_array, b_))
345
346 # But it should still work with the a_
347 fft(a_)
348
349 # However, trying to update the output will raise an error
350 self.assertRaisesRegex(ValueError, 'Invalid output alignment',
351 self.fft.update_arrays, *(input_array, b_))
352
353 # Same with SIMD off
354 fft = FFTW(input_array, output_array, flags=('FFTW_UNALIGNED',))
355 fft(a_)
356 self.assertRaisesRegex(ValueError, 'Invalid output alignment',
357 self.fft.update_arrays, *(input_array, b_))
358
359 def test_call_with_normalisation_on(self):
360 _input_array = empty_aligned((256, 512), dtype='complex128', n=16)
361
362 ifft = FFTW(self.output_array, _input_array,
363 direction='FFTW_BACKWARD')
364
365 self.fft(normalise_idft=True) # Shouldn't make any difference
366 ifft(normalise_idft=True)
367
368 self.assertTrue(numpy.allclose(self.input_array, _input_array))
369
370 def test_call_with_normalisation_off(self):
371 _input_array = empty_aligned((256, 512), dtype='complex128', n=16)
372
373 ifft = FFTW(self.output_array, _input_array,
374 direction='FFTW_BACKWARD')
375
376 self.fft(normalise_idft=True) # Shouldn't make any difference
377 ifft(normalise_idft=False)
378
379 _input_array /= ifft.N
380
381 self.assertTrue(numpy.allclose(self.input_array, _input_array))
382
383 def test_call_with_normalisation_default(self):
384 _input_array = empty_aligned((256, 512), dtype='complex128', n=16)
385
386 ifft = FFTW(self.output_array, _input_array,
387 direction='FFTW_BACKWARD')
388
389 self.fft()
390 ifft()
391
392 # Scaling is performed by default
393 self.assertTrue(numpy.allclose(self.input_array, _input_array))
394
395 @unittest.skipIf(*miss('32', '64'))
396 def test_call_with_normalisation_precision(self):
397 '''The normalisation should use a double precision scaling.
398 '''
399 # Should be the case for double inputs...
400 _input_array = empty_aligned((256, 512), dtype='complex128', n=16)
401
402 self.fft()
403 ifft = FFTW(self.output_array, _input_array,
404 direction='FFTW_BACKWARD')
405
406 ref_output = ifft(normalise_idft=False).copy()/numpy.float64(ifft.N)
407 test_output = ifft(normalise_idft=True).copy()
408
409 self.assertTrue(numpy.all(ref_output == test_output))
410
411 # ... and single inputs.
412 _input_array = empty_aligned((256, 512), dtype='complex64', n=16)
413
414 ifft = FFTW(numpy.array(self.output_array, _input_array.dtype),
415 _input_array,
416 direction='FFTW_BACKWARD')
417
418 ref_output = ifft(normalise_idft=False).copy()/numpy.float64(ifft.N)
419 test_output = ifft(normalise_idft=True).copy()
420
421 self.assertTrue(numpy.all(ref_output == test_output))
422
423 def test_call_with_ortho_on(self):
424 _input_array = empty_aligned((256, 512), dtype='complex128', n=16)
425
426 ifft = FFTW(self.output_array, _input_array,
427 direction='FFTW_BACKWARD')
428
429 self.fft(ortho=True, normalise_idft=False)
430
431 # ortho case preserves the norm in forward direction
432 self.assertTrue(
433 numpy.allclose(numpy.linalg.norm(self.input_array),
434 numpy.linalg.norm(self.output_array)))
435
436 ifft(ortho=True, normalise_idft=False)
437
438 # ortho case preserves the norm in backward direction
439 self.assertTrue(
440 numpy.allclose(numpy.linalg.norm(_input_array),
441 numpy.linalg.norm(self.output_array)))
442
443 self.assertTrue(numpy.allclose(self.input_array, _input_array))
444
445 # cant select both ortho and normalise_idft
446 self.assertRaisesRegex(ValueError, 'Invalid options',
447 self.fft, normalise_idft=True, ortho=True)
448 # cant specify orth=True with default normalise_idft=True
449 self.assertRaisesRegex(ValueError, 'Invalid options',
450 self.fft, ortho=True)
451
452 def test_call_with_ortho_off(self):
453 _input_array = empty_aligned((256, 512), dtype='complex128', n=16)
454
455 ifft = FFTW(self.output_array, _input_array,
456 direction='FFTW_BACKWARD')
457
458 self.fft(ortho=False)
459 ifft(ortho=False)
460
461 # Scaling by normalise_idft is performed by default
462 self.assertTrue(numpy.allclose(self.input_array, _input_array))
463
464 test_cases = (
465 FFTWCallTest,)
466
467 test_set = None
468
469 if __name__ == '__main__':
470
471 run_test_suites(test_cases, test_set)