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
|
# Copyright 2012 Knowledge Economy Developments Ltd
#
# Henry Gomersall
# heng@kedevelopments.co.uk
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from pyfftw import (
FFTW, n_byte_align_empty, n_byte_align)
from .test_pyfftw_base import run_test_suites
import numpy
import unittest
class FFTWCallTest(unittest.TestCase):
def __init__(self, *args, **kwargs):
super(FFTWCallTest, self).__init__(*args, **kwargs)
if not hasattr(self, 'assertRaisesRegex'):
self.assertRaisesRegex = self.assertRaisesRegexp
def setUp(self):
self.input_array = n_byte_align_empty((256, 512), 16,
dtype='complex128')
self.output_array = n_byte_align_empty((256, 512), 16,
dtype='complex128')
self.fft = FFTW(self.input_array, self.output_array)
self.input_array[:] = (numpy.random.randn(*self.input_array.shape)
+ 1j*numpy.random.randn(*self.input_array.shape))
def test_call(self):
'''Test a call to an instance of the class.
'''
self.input_array[:] = (numpy.random.randn(*self.input_array.shape)
+ 1j*numpy.random.randn(*self.input_array.shape))
output_array = self.fft()
self.assertTrue(numpy.alltrue(output_array == self.output_array))
def test_call_with_positional_input_update(self):
'''Test the class call with a positional input update.
'''
input_array = n_byte_align(
(numpy.random.randn(*self.input_array.shape)
+ 1j*numpy.random.randn(*self.input_array.shape)), 16)
output_array = self.fft(n_byte_align(input_array.copy(), 16)).copy()
self.fft.update_arrays(input_array, self.output_array)
self.fft.execute()
self.assertTrue(numpy.alltrue(output_array == self.output_array))
def test_call_with_keyword_input_update(self):
'''Test the class call with a keyword input update.
'''
input_array = n_byte_align(
numpy.random.randn(*self.input_array.shape)
+ 1j*numpy.random.randn(*self.input_array.shape), 16)
output_array = self.fft(
input_array=n_byte_align(input_array.copy(), 16)).copy()
self.fft.update_arrays(input_array, self.output_array)
self.fft.execute()
self.assertTrue(numpy.alltrue(output_array == self.output_array))
def test_call_with_keyword_output_update(self):
'''Test the class call with a keyword output update.
'''
output_array = n_byte_align(
(numpy.random.randn(*self.output_array.shape)
+ 1j*numpy.random.randn(*self.output_array.shape)), 16)
returned_output_array = self.fft(
output_array=n_byte_align(output_array.copy(), 16)).copy()
self.fft.update_arrays(self.input_array, output_array)
self.fft.execute()
self.assertTrue(
numpy.alltrue(returned_output_array == output_array))
def test_call_with_positional_updates(self):
'''Test the class call with a positional array updates.
'''
input_array = n_byte_align((numpy.random.randn(*self.input_array.shape)
+ 1j*numpy.random.randn(*self.input_array.shape)), 16)
output_array = n_byte_align((numpy.random.randn(*self.output_array.shape)
+ 1j*numpy.random.randn(*self.output_array.shape)), 16)
returned_output_array = self.fft(
n_byte_align(input_array.copy(), 16),
n_byte_align(output_array.copy(), 16)).copy()
self.fft.update_arrays(input_array, output_array)
self.fft.execute()
self.assertTrue(numpy.alltrue(returned_output_array == output_array))
def test_call_with_keyword_updates(self):
'''Test the class call with a positional output update.
'''
input_array = n_byte_align(
(numpy.random.randn(*self.input_array.shape)
+ 1j*numpy.random.randn(*self.input_array.shape)), 16)
output_array = n_byte_align(
(numpy.random.randn(*self.output_array.shape)
+ 1j*numpy.random.randn(*self.output_array.shape)), 16)
returned_output_array = self.fft(
output_array=n_byte_align(output_array.copy(), 16),
input_array=n_byte_align(input_array.copy(), 16)).copy()
self.fft.update_arrays(input_array, output_array)
self.fft.execute()
self.assertTrue(numpy.alltrue(returned_output_array == output_array))
def test_call_with_different_input_dtype(self):
'''Test the class call with an array with a different input dtype
'''
input_array = n_byte_align(numpy.complex64(
numpy.random.randn(*self.input_array.shape)
+ 1j*numpy.random.randn(*self.input_array.shape)), 16)
output_array = self.fft(n_byte_align(input_array.copy(), 16)).copy()
_input_array = n_byte_align(numpy.asarray(input_array,
dtype=self.input_array.dtype), 16)
self.assertTrue(_input_array.dtype != input_array.dtype)
self.fft.update_arrays(_input_array, self.output_array)
self.fft.execute()
self.assertTrue(numpy.alltrue(output_array == self.output_array))
def test_call_with_list_input(self):
'''Test the class call with a list rather than an array
'''
output_array = self.fft().copy()
test_output_array = self.fft(self.input_array.tolist()).copy()
self.assertTrue(numpy.alltrue(output_array == test_output_array))
def test_call_with_invalid_update(self):
'''Test the class call with an invalid update.
'''
new_shape = self.input_array.shape + (2, )
invalid_array = (numpy.random.randn(*new_shape)
+ 1j*numpy.random.randn(*new_shape))
self.assertRaises(ValueError, self.fft,
*(),
**{'output_array':invalid_array})
self.assertRaises(ValueError, self.fft,
*(),
**{'input_array':invalid_array})
def test_call_with_auto_input_alignment(self):
'''Test the class call with a keyword input update.
'''
input_array = (numpy.random.randn(*self.input_array.shape)
+ 1j*numpy.random.randn(*self.input_array.shape))
output_array = self.fft(
input_array=n_byte_align(input_array.copy(), 16)).copy()
# Offset by one from 16 byte aligned to guarantee it's not
# 16 byte aligned
a = input_array
a__ = n_byte_align_empty(
numpy.prod(a.shape)*a.itemsize+1, 16, dtype='int8')
a_ = a__[1:].view(dtype=a.dtype).reshape(*a.shape)
a_[:] = a
# Just confirm that a usual update will fail
self.assertRaisesRegex(ValueError, 'Invalid input alignment',
self.fft.update_arrays, *(a_, self.output_array))
self.fft(a_, self.output_array)
self.assertTrue(numpy.alltrue(output_array == self.output_array))
# now try with a single byte offset and SIMD off
ar, ai = numpy.float32(numpy.random.randn(2, 257))
a = ar[1:] + 1j*ai[1:]
b = a.copy()
a_size = len(a.ravel())*a.itemsize
update_array = numpy.frombuffer(
numpy.zeros(a_size + 1, dtype='int8')[1:].data,
dtype=a.dtype).reshape(a.shape)
fft = FFTW(a, b, flags=('FFTW_UNALIGNED',))
# Confirm that a usual update will fail (it's not on the
# byte boundary)
self.assertRaisesRegex(ValueError, 'Invalid input alignment',
fft.update_arrays, *(update_array, b))
fft(update_array, b)
def test_call_with_invalid_output_striding(self):
'''Test the class call with an invalid strided output update.
'''
# Add an extra dimension to bugger up the striding
new_shape = self.output_array.shape + (2,)
output_array = n_byte_align(numpy.random.randn(*new_shape)
+ 1j*numpy.random.randn(*new_shape), 16)
self.assertRaisesRegex(ValueError, 'Invalid output striding',
self.fft, **{'output_array': output_array[:,:,1]})
def test_call_with_different_striding(self):
'''Test the input update with different strides to internal array.
'''
shape = self.input_array.shape + (2,)
input_array = n_byte_align(numpy.random.randn(*shape)
+ 1j*numpy.random.randn(*shape), 16)
fft = FFTW(input_array[:,:,0], self.output_array)
test_output_array = fft().copy()
new_input_array = n_byte_align(
input_array[:, :, 0].copy(), 16)
new_output = fft(new_input_array).copy()
# Test the test!
self.assertTrue(new_input_array.strides != input_array[:,:,0].strides)
self.assertTrue(numpy.alltrue(test_output_array == new_output))
def test_call_with_copy_with_missized_array_error(self):
'''Force an input copy with a missized array.
'''
shape = list(self.input_array.shape + (2,))
shape[0] += 1
input_array = n_byte_align(numpy.random.randn(*shape)
+ 1j*numpy.random.randn(*shape), 16)
fft = FFTW(self.input_array, self.output_array)
self.assertRaisesRegex(ValueError, 'Invalid input shape',
self.fft, **{'input_array': input_array[:,:,0]})
def test_call_with_unaligned(self):
'''Make sure the right thing happens with unaligned data.
'''
input_array = (numpy.random.randn(*self.input_array.shape)
+ 1j*numpy.random.randn(*self.input_array.shape))
output_array = self.fft(
input_array=n_byte_align(input_array.copy(), 16)).copy()
input_array = n_byte_align(input_array, 16)
output_array = n_byte_align(output_array, 16)
# Offset by one from 16 byte aligned to guarantee it's not
# 16 byte aligned
a = n_byte_align(input_array.copy(), 16)
a__ = n_byte_align_empty(
numpy.prod(a.shape)*a.itemsize+1, 16, dtype='int8')
a_ = a__[1:].view(dtype=a.dtype).reshape(*a.shape)
a_[:] = a
# Create a different second array the same way
b = n_byte_align(output_array.copy(), 16)
b__ = n_byte_align_empty(
numpy.prod(b.shape)*a.itemsize+1, 16, dtype='int8')
b_ = b__[1:].view(dtype=b.dtype).reshape(*b.shape)
b_[:] = a
# Set up for the first array
fft = FFTW(input_array, output_array)
a_[:] = a
output_array = fft().copy()
# Check a_ is not aligned...
self.assertRaisesRegex(ValueError, 'Invalid input alignment',
self.fft.update_arrays, *(a_, output_array))
# and b_ too
self.assertRaisesRegex(ValueError, 'Invalid output alignment',
self.fft.update_arrays, *(input_array, b_))
# But it should still work with the a_
fft(a_)
# However, trying to update the output will raise an error
self.assertRaisesRegex(ValueError, 'Invalid output alignment',
self.fft.update_arrays, *(input_array, b_))
# Same with SIMD off
fft = FFTW(input_array, output_array, flags=('FFTW_UNALIGNED',))
fft(a_)
self.assertRaisesRegex(ValueError, 'Invalid output alignment',
self.fft.update_arrays, *(input_array, b_))
def test_call_with_normalisation_on(self):
_input_array = n_byte_align_empty((256, 512), 16,
dtype='complex128')
ifft = FFTW(self.output_array, _input_array,
direction='FFTW_BACKWARD')
self.fft(normalise_idft=True) # Shouldn't make any difference
ifft(normalise_idft=True)
self.assertTrue(numpy.allclose(self.input_array, _input_array))
def test_call_with_normalisation_off(self):
_input_array = n_byte_align_empty((256, 512), 16,
dtype='complex128')
ifft = FFTW(self.output_array, _input_array,
direction='FFTW_BACKWARD')
self.fft(normalise_idft=True) # Shouldn't make any difference
ifft(normalise_idft=False)
_input_array /= ifft.N
self.assertTrue(numpy.allclose(self.input_array, _input_array))
def test_call_with_normalisation_default(self):
_input_array = n_byte_align_empty((256, 512), 16,
dtype='complex128')
ifft = FFTW(self.output_array, _input_array,
direction='FFTW_BACKWARD')
self.fft()
ifft()
# Scaling is performed by default
self.assertTrue(numpy.allclose(self.input_array, _input_array))
test_cases = (
FFTWCallTest,)
test_set = None
if __name__ == '__main__':
run_test_suites(test_cases, test_set)
|