test.py 5.05 KB
Newer Older
Martin Reinecke's avatar
Martin Reinecke committed
1
2
3
4
5
6
7
import pypocketfft
import pyfftw
import numpy as np
import pytest

pmp = pytest.mark.parametrize

Martin Reinecke's avatar
more    
Martin Reinecke committed
8
9
10
11
shapes1D = ((10,), (127,))
shapes2D = ((128, 128), (128, 129), (1, 129), (129,1))
shapes3D = ((32,17,39),)
shapes = shapes1D+shapes2D+shapes3D
Martin Reinecke's avatar
Martin Reinecke committed
12
len1D=range(1,2048)
Martin Reinecke's avatar
Martin Reinecke committed
13

Martin Reinecke's avatar
Martin Reinecke committed
14
15
16
17
18
19
def _l2error(a,b):
    return np.sqrt(np.sum(np.abs(a-b)**2)/np.sum(np.abs(a)**2))

@pmp("len", len1D)
def test1D(len):
    a=np.random.rand(len)-0.5 + 1j*np.random.rand(len)-0.5j
Martin Reinecke's avatar
Martin Reinecke committed
20
    b=a.astype(np.complex64)
Martin Reinecke's avatar
Martin Reinecke committed
21
22
    c=a.astype(np.complex256)
    assert(_l2error(a, pypocketfft.ifftn(pypocketfft.fftn(c))*np.float128(1.)/len)<1e-18)
Martin Reinecke's avatar
Martin Reinecke committed
23
    assert(_l2error(a, pypocketfft.ifftn(pypocketfft.fftn(a),fct=1./len))<1.5e-15)
Martin Reinecke's avatar
Martin Reinecke committed
24
    assert(_l2error(a.real, pypocketfft.irfftn(pypocketfft.rfftn(a.real),fct=1./len,lastsize=len))<1.5e-15)
25
26
27
    tmp=a.copy()
    assert (pypocketfft.ifftn(pypocketfft.fftn(tmp, inplace=True), fct=1./len, inplace=True) is tmp)
    assert(_l2error(tmp, a)<1.5e-15)
Martin Reinecke's avatar
Martin Reinecke committed
28
29
    assert(_l2error(b, pypocketfft.ifftn(pypocketfft.fftn(b),fct=1./len))<6e-7)
    assert(_l2error(b.real, pypocketfft.irfftn(pypocketfft.rfftn(b.real),fct=1./len,lastsize=len))<6e-7)
30
31
    tmp=b.copy()
    assert (pypocketfft.ifftn(pypocketfft.fftn(tmp, inplace=True), fct=1./len, inplace=True) is tmp)
Martin Reinecke's avatar
Martin Reinecke committed
32
    assert(_l2error(tmp, b)<6e-7)
Martin Reinecke's avatar
Martin Reinecke committed
33

Martin Reinecke's avatar
Martin Reinecke committed
34
@pmp("shp", shapes)
Martin Reinecke's avatar
Martin Reinecke committed
35
36
@pmp("nthreads", (0,1,2))
def test_fftn(shp, nthreads):
Martin Reinecke's avatar
Martin Reinecke committed
37
    a=np.random.rand(*shp)-0.5 + 1j*np.random.rand(*shp)-0.5j
Martin Reinecke's avatar
Martin Reinecke committed
38
    assert(_l2error(pyfftw.interfaces.numpy_fft.fftn(a), pypocketfft.fftn(a, nthreads=nthreads))<1e-15)
Martin Reinecke's avatar
Martin Reinecke committed
39
    a=a.astype(np.complex64)
Martin Reinecke's avatar
Martin Reinecke committed
40
    assert(_l2error(pyfftw.interfaces.numpy_fft.fftn(a), pypocketfft.fftn(a, nthreads=nthreads))<5e-7)
Martin Reinecke's avatar
Martin Reinecke committed
41

Martin Reinecke's avatar
more    
Martin Reinecke committed
42
43
44
45
@pmp("shp", shapes2D)
@pmp("axes", ((0,),(1,),(0,1),(1,0)))
def test_fftn2D(shp, axes):
    a=np.random.rand(*shp)-0.5 + 1j*np.random.rand(*shp)-0.5j
46
    assert(_l2error(pyfftw.interfaces.numpy_fft.fftn(a, axes=axes), pypocketfft.fftn(a, axes=axes))<1e-15)
Martin Reinecke's avatar
more    
Martin Reinecke committed
47
    a=a.astype(np.complex64)
48
    assert(_l2error(pyfftw.interfaces.numpy_fft.fftn(a, axes=axes), pypocketfft.fftn(a, axes=axes))<5e-7)
Martin Reinecke's avatar
more    
Martin Reinecke committed
49
50
51
52

@pmp("shp", shapes)
def test_rfftn(shp):
    a=np.random.rand(*shp)-0.5
53
    assert(_l2error(pyfftw.interfaces.numpy_fft.rfftn(a), pypocketfft.rfftn(a))<1e-15)
Martin Reinecke's avatar
more    
Martin Reinecke committed
54
    a=a.astype(np.float32)
55
56
57
58
59
60
61
62
    assert(_l2error(pyfftw.interfaces.numpy_fft.rfftn(a), pypocketfft.rfftn(a))<5e-7)

@pmp("shp", shapes)
def test_rfft_scipy(shp):
    for i in range(len(shp)):
        a=np.random.rand(*shp)-0.5
        assert(_l2error(pyfftw.interfaces.scipy_fftpack.rfft(a,axis=i), pypocketfft.rfft_scipy(a,axis=i))<1e-15)
        assert(_l2error(pyfftw.interfaces.scipy_fftpack.irfft(a,axis=i), pypocketfft.irfft_scipy(a,axis=i,fct=1./a.shape[i]))<1e-15)
Martin Reinecke's avatar
more    
Martin Reinecke committed
63
64
65
66
67

@pmp("shp", shapes2D)
@pmp("axes", ((0,),(1,),(0,1),(1,0)))
def test_rfftn2D(shp, axes):
    a=np.random.rand(*shp)-0.5
68
    assert(_l2error(pyfftw.interfaces.numpy_fft.rfftn(a, axes=axes), pypocketfft.rfftn(a, axes=axes))<1e-15)
Martin Reinecke's avatar
more    
Martin Reinecke committed
69
    a=a.astype(np.float32)
70
    assert(_l2error(pyfftw.interfaces.numpy_fft.rfftn(a, axes=axes), pypocketfft.rfftn(a, axes=axes))<5e-7)
Martin Reinecke's avatar
more    
Martin Reinecke committed
71

Martin Reinecke's avatar
Martin Reinecke committed
72
73
74
@pmp("shp", shapes)
def test_identity(shp):
    a=np.random.rand(*shp)-0.5 + 1j*np.random.rand(*shp)-0.5j
75
    assert(_l2error(pypocketfft.ifftn(pypocketfft.fftn(a),fct=1./a.size), a)<1.5e-15)
76
77
    tmp=a.copy()
    assert (pypocketfft.ifftn(pypocketfft.fftn(tmp, inplace=True), fct=1./a.size, inplace=True) is tmp)
78
    assert(_l2error(tmp, a)<1.5e-15)
Martin Reinecke's avatar
Martin Reinecke committed
79
    a=a.astype(np.complex64)
80
    assert(_l2error(pypocketfft.ifftn(pypocketfft.fftn(a),fct=1./a.size), a)<6e-7)
Martin Reinecke's avatar
Martin Reinecke committed
81
82

@pmp("shp", shapes)
Martin Reinecke's avatar
Martin Reinecke committed
83
def test_identity_r(shp):
Martin Reinecke's avatar
Martin Reinecke committed
84
85
86
87
    a=np.random.rand(*shp)-0.5
    b=a.astype(np.float32)
    for ax in range(a.ndim):
        n = a.shape[ax]
88
89
        assert(_l2error(pypocketfft.irfftn(pypocketfft.rfftn(a,(ax,)),(ax,),lastsize=n,fct=1./n), a)<1e-15)
        assert(_l2error(pypocketfft.irfftn(pypocketfft.rfftn(b,(ax,)),(ax,),lastsize=n,fct=1./n), b)<5e-7)
Martin Reinecke's avatar
more    
Martin Reinecke committed
90

Martin Reinecke's avatar
Martin Reinecke committed
91
92
93
94
95
@pmp("shp", shapes)
def test_identity_r2(shp):
    a=np.random.rand(*shp)-0.5 + 1j*np.random.rand(*shp)-0.5j
    a=pypocketfft.rfftn(pypocketfft.irfftn(a))
    fct = 1./pypocketfft.irfftn(a).size
96
    assert(_l2error(pypocketfft.rfftn(pypocketfft.irfftn(a),fct=fct), a)<1e-15)
Martin Reinecke's avatar
Martin Reinecke committed
97

Martin Reinecke's avatar
fixes    
Martin Reinecke committed
98
99
@pmp("shp", shapes2D+shapes3D)
def test_hartley2(shp):
Martin Reinecke's avatar
more    
Martin Reinecke committed
100
    a=np.random.rand(*shp)-0.5
Martin Reinecke's avatar
fixes    
Martin Reinecke committed
101
    v1 = pypocketfft.hartley2(a)
Martin Reinecke's avatar
more    
Martin Reinecke committed
102
103
    v2 = pypocketfft.fftn(a.astype(np.complex128))
    v2 = v2.real+v2.imag
104
    assert(_l2error(v1, v2)<1e-15)
Martin Reinecke's avatar
more    
Martin Reinecke committed
105

Martin Reinecke's avatar
fixes    
Martin Reinecke committed
106
@pmp("shp", shapes)
Martin Reinecke's avatar
more    
Martin Reinecke committed
107
108
109
def test_hartley_identity(shp):
    a=np.random.rand(*shp)-0.5
    v1 = pypocketfft.hartley(pypocketfft.hartley(a))/a.size
110
    assert(_l2error(a, v1)<1e-15)
Martin Reinecke's avatar
fixes    
Martin Reinecke committed
111
112
113
114
115

@pmp("shp", shapes)
def test_hartley2_identity(shp):
    a=np.random.rand(*shp)-0.5
    v1 = pypocketfft.hartley2(pypocketfft.hartley2(a))/a.size
116
    assert(_l2error(a, v1)<1e-15)
Martin Reinecke's avatar
Martin Reinecke committed
117
118
    v1 = a.copy()
    assert (pypocketfft.hartley2(pypocketfft.hartley2(v1, inplace=True), fct=1./a.size, inplace=True) is v1)
119
    assert(_l2error(a, v1)<1e-15)
Martin Reinecke's avatar
fixes    
Martin Reinecke committed
120
121
122
123
124
125

@pmp("shp", shapes2D)
@pmp("axes", ((0,),(1,),(0,1),(1,0)))
def test_hartley2_2D(shp, axes):
    a=np.random.rand(*shp)-0.5
    fct = 1./np.prod(np.take(shp,axes))
126
    assert(_l2error(pypocketfft.hartley2(pypocketfft.hartley2(a, axes=axes), axes=axes, fct=fct),a)<1e-15)