| 1 |
# Copyright 2017 Knowledge Economy Developments Ltd |
| 2 |
# |
| 3 |
# Henry Gomersall |
| 4 |
# [email protected] |
| 5 |
# Frederik Beaujean |
| 6 |
# [email protected] |
| 7 |
# |
| 8 |
# All rights reserved. |
| 9 |
# |
| 10 |
# Redistribution and use in source and binary forms, with or without |
| 11 |
# modification, are permitted provided that the following conditions are met: |
| 12 |
# |
| 13 |
# * Redistributions of source code must retain the above copyright notice, this |
| 14 |
# list of conditions and the following disclaimer. |
| 15 |
# |
| 16 |
# * Redistributions in binary form must reproduce the above copyright notice, |
| 17 |
# this list of conditions and the following disclaimer in the documentation |
| 18 |
# and/or other materials provided with the distribution. |
| 19 |
# |
| 20 |
# * Neither the name of the copyright holder nor the names of its contributors |
| 21 |
# may be used to endorse or promote products derived from this software without |
| 22 |
# specific prior written permission. |
| 23 |
# |
| 24 |
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 25 |
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 26 |
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 27 |
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 28 |
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 29 |
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 30 |
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 31 |
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 32 |
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 33 |
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 34 |
# POSSIBILITY OF SUCH DAMAGE. |
| 35 |
# |
| 36 |
|
| 37 |
from pyfftw import ( |
| 38 |
FFTW, empty_aligned, |
| 39 |
interfaces, |
| 40 |
_all_types_np, _all_types_human_readable, _supported_types |
| 41 |
) |
| 42 |
from pyfftw.builders._utils import _rc_dtype_pairs |
| 43 |
import numpy as np |
| 44 |
import unittest |
| 45 |
import warnings |
| 46 |
|
| 47 |
@unittest.skipIf(len(_all_types_human_readable) == len(_supported_types), "All data types available") |
| 48 |
class FFTWPartialTest(unittest.TestCase): |
| 49 |
|
| 50 |
def __init__(self, *args, **kwargs): |
| 51 |
|
| 52 |
super(FFTWPartialTest, self).__init__(*args, **kwargs) |
| 53 |
|
| 54 |
if not hasattr(self, 'assertRaisesRegex'): |
| 55 |
self.assertRaisesRegex = self.assertRaisesRegexp |
| 56 |
|
| 57 |
def test_failure(self): |
| 58 |
for dtype, npdtype in zip(['32', '64', 'ld'], [np.complex64, np.complex128, np.clongdouble]): |
| 59 |
if dtype == 'ld' and np.dtype(np.clongdouble) == np.dtype(np.complex128): |
| 60 |
# skip this test on systems where clongdouble is complex128 |
| 61 |
continue |
| 62 |
if dtype not in _supported_types: |
| 63 |
a = empty_aligned((1,1024), npdtype, n=16) |
| 64 |
b = empty_aligned(a.shape, dtype=a.dtype, n=16) |
| 65 |
msg = "Rebuild pyFFTW with support for %s precision!" % _all_types_human_readable[dtype] |
| 66 |
with self.assertRaisesRegex(NotImplementedError, msg): |
| 67 |
FFTW(a,b) |
| 68 |
|
| 69 |
|
| 70 |
def conversion(self, missing, alt1, alt2): |
| 71 |
'''If the ``missing`` precision is not available, the builder should convert to |
| 72 |
``alt1`` precision. If that isn't available either, it should fall back to |
| 73 |
``alt2``. If input precision is lost, a warning should be emitted. |
| 74 |
|
| 75 |
''' |
| 76 |
|
| 77 |
missing, alt1, alt2 = [np.dtype(x) for x in (missing, alt1, alt2)] |
| 78 |
if _all_types_np[missing] in _supported_types: |
| 79 |
return |
| 80 |
|
| 81 |
with warnings.catch_warnings(record=True) as w: |
| 82 |
warnings.simplefilter("always") |
| 83 |
|
| 84 |
itemsize = alt1.itemsize |
| 85 |
a = empty_aligned((1, 512), dtype=missing) |
| 86 |
b = interfaces.numpy_fft.fft(a) |
| 87 |
res = _rc_dtype_pairs.get(alt1.char, None) |
| 88 |
if res is not None: |
| 89 |
self.assertEqual(b.dtype, res) |
| 90 |
else: |
| 91 |
itemsize = alt2.itemsize |
| 92 |
self.assertEqual(b.dtype, _rc_dtype_pairs[alt2.char]) |
| 93 |
|
| 94 |
if itemsize < missing.itemsize: |
| 95 |
print(itemsize, missing.itemsize) |
| 96 |
assert len(w) == 1 |
| 97 |
assert "Narrowing conversion" in str(w[-1].message) |
| 98 |
print("Found narrowing conversion from %d to %d bytes" % (missing.itemsize, itemsize)) |
| 99 |
else: |
| 100 |
assert len(w) == 0 |
| 101 |
|
| 102 |
|
| 103 |
def test_conversion(self): |
| 104 |
self.conversion('float32', 'float64', 'longdouble') |
| 105 |
self.conversion('float64', 'longdouble', 'single') |
| 106 |
self.conversion('longdouble', 'float64', 'float32') |
| 107 |
|
| 108 |
test_cases = ( |
| 109 |
FFTWPartialTest,) |
| 110 |
|
| 111 |
test_set = None |
| 112 |
|
| 113 |
if __name__ == '__main__': |
| 114 |
|
| 115 |
run_test_suites(test_cases, test_set) |