File: test_pyfftw_scipy_interface.py

package info (click to toggle)
pyfftw 0.9.2%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 1,312 kB
  • ctags: 1,802
  • sloc: python: 4,418; ansic: 525; makefile: 7
file content (122 lines) | stat: -rw-r--r-- 3,914 bytes parent folder | download
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
# 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.interfaces import scipy_fftpack
import pyfftw
import numpy
import scipy
import scipy.fftpack
import scipy.signal

import unittest
from .test_pyfftw_base import run_test_suites


'''pyfftw.interfaces.scipy_fftpack just wraps pyfftw.interfaces.numpy_fft.

All the tests here just check that the call is made correctly.
'''

funcs = ('fft','ifft', 'fft2', 'ifft2', 'fftn', 'ifftn', 
           'rfft', 'irfft')

acquired_names = ('dct', 'idct', 'diff', 'tilbert', 'itilbert', 'hilbert', 
        'ihilbert', 'cs_diff', 'sc_diff', 'ss_diff', 'cc_diff', 'shift', 
        'fftshift', 'ifftshift', 'fftfreq', 'rfftfreq', 'convolve', 
        '_fftpack')

def numpy_fft_replacement(a, s, axes, overwrite_input, planner_effort, 
        threads, auto_align_input, auto_contiguous):

    return (a, s, axes, overwrite_input, planner_effort, 
        threads, auto_align_input, auto_contiguous)

class InterfacesScipyFFTPackTestFFT(unittest.TestCase):
    ''' A really simple test suite to check simple implementation.
    '''

    def test_scipy_overwrite(self):
        scipy_fftn = scipy.signal.signaltools.fftn
        scipy_ifftn = scipy.signal.signaltools.ifftn

        a = pyfftw.n_byte_align_empty((128, 64), 16, dtype='complex128')
        b = pyfftw.n_byte_align_empty((128, 64), 16, dtype='complex128')

        a[:] = (numpy.random.randn(*a.shape) + 
                1j*numpy.random.randn(*a.shape))
        b[:] = (numpy.random.randn(*b.shape) + 
                1j*numpy.random.randn(*b.shape))


        scipy_c = scipy.signal.fftconvolve(a, b)

        scipy.signal.signaltools.fftn = scipy_fftpack.fftn
        scipy.signal.signaltools.ifftn = scipy_fftpack.ifftn

        scipy_replaced_c = scipy.signal.fftconvolve(a, b)

        self.assertTrue(numpy.allclose(scipy_c, scipy_replaced_c))

        scipy.signal.signaltools.fftn = scipy_fftn
        scipy.signal.signaltools.ifftn = scipy_ifftn

    def test_funcs(self):

        for each_func in funcs:
            func_being_replaced = getattr(scipy_fftpack, each_func)

            #create args (8 of them)
            args = []
            for n in range(8):
                args.append(object())

            args = tuple(args)

            try:
                setattr(scipy_fftpack, each_func, 
                        numpy_fft_replacement)

                return_args = getattr(scipy_fftpack, each_func)(*args)
                for n, each_arg in enumerate(args):
                    # Check that what comes back is what is sent
                    # (which it should be)
                    self.assertIs(each_arg, return_args[n])
            except:
                raise

            finally:
                setattr(scipy_fftpack, each_func, 
                        func_being_replaced)

    def test_acquired_names(self):
        for each_name in acquired_names: 

            fftpack_attr = getattr(scipy.fftpack, each_name)
            acquired_attr = getattr(scipy_fftpack, each_name)

            self.assertIs(fftpack_attr, acquired_attr)


test_cases = (
        InterfacesScipyFFTPackTestFFT,)

test_set = None

if __name__ == '__main__':

    run_test_suites(test_cases, test_set)