| 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 FFTW |
| 36 |
import numpy |
| 37 |
from timeit import Timer |
| 38 |
|
| 39 |
from .test_pyfftw_base import run_test_suites, miss, require, np_fft |
| 40 |
|
| 41 |
import unittest |
| 42 |
|
| 43 |
from .test_pyfftw_complex import Complex64FFTWTest |
| 44 |
|
| 45 |
class RealForwardDoubleFFTWTest(Complex64FFTWTest): |
| 46 |
|
| 47 |
def setUp(self): |
| 48 |
require(self, '64') |
| 49 |
|
| 50 |
self.input_dtype = numpy.float64 |
| 51 |
self.output_dtype = numpy.complex128 |
| 52 |
self.np_fft_comparison = np_fft.rfft |
| 53 |
|
| 54 |
self.direction = 'FFTW_FORWARD' |
| 55 |
|
| 56 |
def make_shapes(self): |
| 57 |
self.input_shapes = { |
| 58 |
'small_1d': (16,), |
| 59 |
'1d': (2048,), |
| 60 |
'2d': (256, 2048), |
| 61 |
'3d': (5, 256, 2048)} |
| 62 |
|
| 63 |
self.output_shapes = { |
| 64 |
'small_1d': (9,), |
| 65 |
'1d': (1025,), |
| 66 |
'2d': (256, 1025), |
| 67 |
'3d': (5, 256, 1025)} |
| 68 |
|
| 69 |
def create_test_arrays(self, input_shape, output_shape, axes=None): |
| 70 |
a = self.input_dtype(numpy.random.randn(*input_shape)) |
| 71 |
|
| 72 |
b = self.output_dtype(numpy.random.randn(*output_shape) |
| 73 |
+1j*numpy.random.randn(*output_shape)) |
| 74 |
|
| 75 |
return a, b |
| 76 |
|
| 77 |
def reference_fftn(self, a, axes): |
| 78 |
|
| 79 |
return np_fft.rfftn(a, axes=axes) |
| 80 |
|
| 81 |
def test_wrong_direction_fail(self): |
| 82 |
in_shape = self.input_shapes['2d'] |
| 83 |
out_shape = self.output_shapes['2d'] |
| 84 |
|
| 85 |
axes=(-1,) |
| 86 |
a, b = self.create_test_arrays(in_shape, out_shape) |
| 87 |
|
| 88 |
with self.assertRaisesRegex(ValueError, 'Invalid direction'): |
| 89 |
FFTW(a, b, direction='FFTW_BACKWARD') |
| 90 |
|
| 91 |
def test_non_contiguous_2d(self): |
| 92 |
in_shape = self.input_shapes['2d'] |
| 93 |
out_shape = self.output_shapes['2d'] |
| 94 |
|
| 95 |
axes=(-2,-1) |
| 96 |
a, b = self.create_test_arrays(in_shape, out_shape) |
| 97 |
|
| 98 |
# Some arbitrary and crazy slicing |
| 99 |
a_sliced = a[12:200:3, 300:2041:9] |
| 100 |
# b needs to be compatible |
| 101 |
b_sliced = b[20:146:2, 100:786:7] |
| 102 |
|
| 103 |
self.run_validate_fft(a_sliced, b_sliced, axes, create_array_copies=False) |
| 104 |
|
| 105 |
def test_non_contiguous_2d_in_3d(self): |
| 106 |
in_shape = (256, 4, 2048) |
| 107 |
out_shape = in_shape |
| 108 |
axes=(0,2) |
| 109 |
a, b = self.create_test_arrays(in_shape, out_shape) |
| 110 |
|
| 111 |
# Some arbitrary and crazy slicing |
| 112 |
a_sliced = a[12:200:3, :, 300:2041:9] |
| 113 |
# b needs to be compatible |
| 114 |
b_sliced = b[20:146:2, :, 100:786:7] |
| 115 |
|
| 116 |
self.run_validate_fft(a_sliced, b_sliced, axes, create_array_copies=False) |
| 117 |
|
| 118 |
@unittest.skipIf(*miss('32')) |
| 119 |
class RealForwardSingleFFTWTest(RealForwardDoubleFFTWTest): |
| 120 |
|
| 121 |
def setUp(self): |
| 122 |
|
| 123 |
self.input_dtype = numpy.float32 |
| 124 |
self.output_dtype = numpy.complex64 |
| 125 |
self.np_fft_comparison = np_fft.rfft |
| 126 |
|
| 127 |
self.direction = 'FFTW_FORWARD' |
| 128 |
|
| 129 |
@unittest.skipIf(*miss('ld')) |
| 130 |
class RealForwardLongDoubleFFTWTest(RealForwardDoubleFFTWTest): |
| 131 |
|
| 132 |
def setUp(self): |
| 133 |
|
| 134 |
self.input_dtype = numpy.longdouble |
| 135 |
self.output_dtype = numpy.clongdouble |
| 136 |
self.np_fft_comparison = np_fft.rfft |
| 137 |
|
| 138 |
self.direction = 'FFTW_FORWARD' |
| 139 |
|
| 140 |
@unittest.skip('numpy.fft has issues with this dtype.') |
| 141 |
def test_time(self): |
| 142 |
pass |
| 143 |
|
| 144 |
@unittest.skip('numpy.fft has issues with this dtype.') |
| 145 |
def test_time_with_array_update(self): |
| 146 |
pass |
| 147 |
|
| 148 |
def reference_fftn(self, a, axes): |
| 149 |
|
| 150 |
a = numpy.float64(a) |
| 151 |
return np_fft.rfftn(a, axes=axes) |
| 152 |
|
| 153 |
test_cases = ( |
| 154 |
RealForwardDoubleFFTWTest, |
| 155 |
RealForwardSingleFFTWTest, |
| 156 |
RealForwardLongDoubleFFTWTest,) |
| 157 |
|
| 158 |
test_set = None |
| 159 |
|
| 160 |
if __name__ == '__main__': |
| 161 |
|
| 162 |
run_test_suites(test_cases, test_set) |
| 163 |
|
| 164 |
del Complex64FFTWTest |