forked from fredRos/pyFFTW
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_pyfftw_class_misc.py
377 lines (286 loc) · 14.2 KB
/
test_pyfftw_class_misc.py
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
# Copyright 2014 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 (
FFTW, empty_aligned, is_byte_aligned, simd_alignment)
import pyfftw
from .test_pyfftw_base import run_test_suites
import unittest
import numpy
import warnings
# FFTW tests that don't seem to fit anywhere else
class FFTWMiscTest(unittest.TestCase):
def __init__(self, *args, **kwargs):
super(FFTWMiscTest, self).__init__(*args, **kwargs)
# Assume python 3, but keep backwards compatibility
if not hasattr(self, 'assertRaisesRegex'):
self.assertRaisesRegex = self.assertRaisesRegexp
def setUp(self):
self.input_array = empty_aligned((256, 512), dtype='complex128', n=16)
self.output_array = empty_aligned((256, 512), dtype='complex128', n=16)
self.fft = FFTW(self.input_array, self.output_array)
self.output_array[:] = (numpy.random.randn(*self.output_array.shape)
+ 1j*numpy.random.randn(*self.output_array.shape))
def test_aligned_flag(self):
'''Test to see if the aligned flag is correct
'''
fft = FFTW(self.input_array, self.output_array)
self.assertTrue(fft.simd_aligned)
fft = FFTW(self.input_array, self.output_array,
flags=('FFTW_UNALIGNED',))
self.assertFalse(fft.simd_aligned)
def test_flags(self):
'''Test to see if the flags are correct
'''
fft = FFTW(self.input_array, self.output_array)
self.assertEqual(fft.flags, ('FFTW_MEASURE',))
fft = FFTW(self.input_array, self.output_array,
flags=('FFTW_DESTROY_INPUT', 'FFTW_UNALIGNED'))
self.assertEqual(fft.flags, ('FFTW_DESTROY_INPUT', 'FFTW_UNALIGNED'))
# Test an implicit flag
_input_array = empty_aligned(256, dtype='complex64', n=16)
_output_array = empty_aligned(256, dtype='complex64', n=16)
# These are guaranteed to be misaligned (due to dtype size == 8)
input_array = _input_array[:-1]
output_array = _output_array[:-1]
u_input_array = _input_array[1:]
u_output_array = _output_array[1:]
fft = FFTW(input_array, u_output_array)
self.assertEqual(fft.flags, ('FFTW_MEASURE', 'FFTW_UNALIGNED'))
fft = FFTW(u_input_array, output_array)
self.assertEqual(fft.flags, ('FFTW_MEASURE', 'FFTW_UNALIGNED'))
fft = FFTW(u_input_array, u_output_array)
self.assertEqual(fft.flags, ('FFTW_MEASURE', 'FFTW_UNALIGNED'))
def test_differing_aligned_arrays_update(self):
'''Test to see if the alignment code is working as expected
'''
# Start by creating arrays that are only on various byte
# alignments (4, 16 and 32)
_input_array = empty_aligned(len(self.input_array.ravel())*2+5,
dtype='float32', n=32)
_output_array = empty_aligned(len(self.output_array.ravel())*2+5,
dtype='float32', n=32)
_input_array[:] = 0
_output_array[:] = 0
input_array_4 = (
numpy.frombuffer(_input_array[1:-4].data, dtype='complex64')
.reshape(self.input_array.shape))
output_array_4 = (
numpy.frombuffer(_output_array[1:-4].data, dtype='complex64')
.reshape(self.output_array.shape))
input_array_16 = (
numpy.frombuffer(_input_array[4:-1].data, dtype='complex64')
.reshape(self.input_array.shape))
output_array_16 = (
numpy.frombuffer(_output_array[4:-1].data, dtype='complex64')
.reshape(self.output_array.shape))
input_array_32 = (
numpy.frombuffer(_input_array[:-5].data, dtype='complex64')
.reshape(self.input_array.shape))
output_array_32 = (
numpy.frombuffer(_output_array[:-5].data, dtype='complex64')
.reshape(self.output_array.shape))
input_arrays = {4: input_array_4,
16: input_array_16,
32: input_array_32}
output_arrays = {4: output_array_4,
16: output_array_16,
32: output_array_32}
alignments = (4, 16, 32)
# Test the arrays are aligned on 4 bytes...
self.assertTrue(is_byte_aligned(input_arrays[4], n=4))
self.assertTrue(is_byte_aligned(output_arrays[4], n=4))
# ...and on 16...
self.assertFalse(is_byte_aligned(input_arrays[4], n=16))
self.assertFalse(is_byte_aligned(output_arrays[4], n=16))
self.assertTrue(is_byte_aligned(input_arrays[16], n=16))
self.assertTrue(is_byte_aligned(output_arrays[16], n=16))
# ...and on 32...
self.assertFalse(is_byte_aligned(input_arrays[16], n=32))
self.assertFalse(is_byte_aligned(output_arrays[16], n=32))
self.assertTrue(is_byte_aligned(input_arrays[32], n=32))
self.assertTrue(is_byte_aligned(output_arrays[32], n=32))
if len(pyfftw.pyfftw._valid_simd_alignments) > 0:
max_align = pyfftw.pyfftw._valid_simd_alignments[0]
else:
max_align = simd_alignment
for in_align in alignments:
for out_align in alignments:
expected_align = min(in_align, out_align, max_align)
fft = FFTW(input_arrays[in_align], output_arrays[out_align])
self.assertTrue(fft.input_alignment == expected_align)
self.assertTrue(fft.output_alignment == expected_align)
for update_align in alignments:
if update_align < expected_align:
# This should fail (not aligned properly)
self.assertRaisesRegex(ValueError,
'Invalid input alignment',
fft.update_arrays,
input_arrays[update_align],
output_arrays[out_align])
self.assertRaisesRegex(ValueError,
'Invalid output alignment',
fft.update_arrays,
input_arrays[in_align],
output_arrays[update_align])
else:
# This should work (and not segfault!)
fft.update_arrays(input_arrays[update_align],
output_arrays[out_align])
fft.update_arrays(input_arrays[in_align],
output_arrays[update_align])
fft.execute()
def test_get_input_array(self):
'''Test to see the get_input_array method returns the correct thing
'''
with warnings.catch_warnings(record=True) as w:
# This method is deprecated, so check the deprecation warning
# is raised.
warnings.simplefilter("always")
input_array = self.fft.get_input_array()
self.assertEqual(len(w), 1)
self.assertTrue(issubclass(w[-1].category, DeprecationWarning))
self.assertIs(self.input_array, input_array)
def test_get_output_array(self):
'''Test to see the get_output_array method returns the correct thing
'''
with warnings.catch_warnings(record=True) as w:
# This method is deprecated, so check the deprecation warning
# is raised.
warnings.simplefilter("always")
output_array = self.fft.get_output_array()
self.assertEqual(len(w), 1)
self.assertTrue(issubclass(w[-1].category, DeprecationWarning))
self.assertIs(self.output_array, output_array)
def test_input_array(self):
'''Test to see the input_array property returns the correct thing
'''
self.assertIs(self.input_array, self.fft.input_array)
def test_output_array(self):
'''Test to see the output_array property returns the correct thing
'''
self.assertIs(self.output_array, self.fft.output_array)
def test_input_strides(self):
'''Test to see if the input_strides property returns the correct thing
'''
self.assertEqual(self.fft.input_strides, self.input_array.strides)
new_input_array = self.input_array[::2, ::4]
new_output_array = self.output_array[::2, ::4]
new_fft = FFTW(new_input_array, new_output_array)
self.assertEqual(new_fft.input_strides, new_input_array.strides)
def test_output_strides(self):
'''Test to see if the output_strides property returns the correct thing
'''
self.assertEqual(self.fft.output_strides, self.output_array.strides)
new_input_array = self.output_array[::2, ::4]
new_output_array = self.output_array[::2, ::4]
new_fft = FFTW(new_input_array, new_output_array)
self.assertEqual(new_fft.output_strides, new_output_array.strides)
def test_input_shape(self):
'''Test to see if the input_shape property returns the correct thing
'''
self.assertEqual(self.fft.input_shape, self.input_array.shape)
new_input_array = self.input_array[::2, ::4]
new_output_array = self.output_array[::2, ::4]
new_fft = FFTW(new_input_array, new_output_array)
self.assertEqual(new_fft.input_shape, new_input_array.shape)
def test_output_strides(self):
'''Test to see if the output_shape property returns the correct thing
'''
self.assertEqual(self.fft.output_shape, self.output_array.shape)
new_input_array = self.output_array[::2, ::4]
new_output_array = self.output_array[::2, ::4]
new_fft = FFTW(new_input_array, new_output_array)
self.assertEqual(new_fft.output_shape, new_output_array.shape)
def test_input_dtype(self):
'''Test to see if the input_dtype property returns the correct thing
'''
self.assertEqual(self.fft.input_dtype, self.input_array.dtype)
new_input_array = numpy.complex64(self.input_array)
new_output_array = numpy.complex64(self.output_array)
new_fft = FFTW(new_input_array, new_output_array)
self.assertEqual(new_fft.input_dtype, new_input_array.dtype)
def test_output_dtype(self):
'''Test to see if the output_dtype property returns the correct thing
'''
self.assertEqual(self.fft.output_dtype, self.output_array.dtype)
new_input_array = numpy.complex64(self.input_array)
new_output_array = numpy.complex64(self.output_array)
new_fft = FFTW(new_input_array, new_output_array)
self.assertEqual(new_fft.output_dtype, new_output_array.dtype)
def test_direction_property(self):
'''Test to see if the direction property returns the correct thing
'''
self.assertEqual(self.fft.direction, 'FFTW_FORWARD')
new_fft = FFTW(self.input_array, self.output_array,
direction='FFTW_BACKWARD')
self.assertEqual(new_fft.direction, 'FFTW_BACKWARD')
def test_axes_property(self):
'''Test to see if the axes property returns the correct thing
'''
self.assertEqual(self.fft.axes, (1,))
new_fft = FFTW(self.input_array, self.output_array, axes=(-1, -2))
self.assertEqual(new_fft.axes, (1, 0))
new_fft = FFTW(self.input_array, self.output_array, axes=(-2, -1))
self.assertEqual(new_fft.axes, (0, 1))
new_fft = FFTW(self.input_array, self.output_array, axes=(1, 0))
self.assertEqual(new_fft.axes, (1, 0))
new_fft = FFTW(self.input_array, self.output_array, axes=(1,))
self.assertEqual(new_fft.axes, (1,))
new_fft = FFTW(self.input_array, self.output_array, axes=(0,))
self.assertEqual(new_fft.axes, (0,))
def test_ortho_property(self):
'''ortho property defaults to False
'''
self.assertEqual(self.fft.ortho, False)
newfft = FFTW(self.input_array, self.output_array, ortho=True,
normalise_idft=False)
self.assertEqual(newfft.ortho, True)
def test_normalise_idft_property(self):
'''normalise_idft property defaults to True
'''
self.assertEqual(self.fft.normalise_idft, True)
newfft = FFTW(self.input_array, self.output_array,
normalise_idft=False)
self.assertEqual(newfft.normalise_idft, False)
def test_invalid_normalisation(self):
# both ortho and normalise_idft cannot be True
self.assertRaisesRegex(
ValueError, 'Invalid options: ortho',
FFTW, self.input_array, self.output_array,
direction='FFTW_BACKWARD', ortho=True, normalise_idft=True)
test_cases = (
FFTWMiscTest,)
test_set = None
if __name__ == '__main__':
run_test_suites(test_cases, test_set)