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

pmp = pytest.mark.parametrize

Martin Reinecke's avatar
more    
Martin Reinecke committed
9
10
11
12
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
13

Martin Reinecke's avatar
Martin Reinecke committed
14
15
16
17
18
19
20
21
22
23
def test1D():
    maxlen=8192
    a=np.random.rand(maxlen)-0.5 + 1j*np.random.rand(maxlen)-0.5j
    b=a.astype(np.complex64)
    for i in range(1,maxlen+1):
        assert_allclose(pypocketfft.ifftn(pypocketfft.fftn(a[:i]),fct=1./i),a[:i], atol=2e-15, rtol=0)
        assert_allclose(pypocketfft.irfftn(pypocketfft.rfftn(a[:i].real),fct=1./i,lastsize=i),a[:i].real, atol=2e-15, rtol=0)
        assert_allclose(pypocketfft.ifftn(pypocketfft.fftn(b[:i]),fct=1./i),b[:i], atol=8e-7, rtol=0)
        assert_allclose(pypocketfft.irfftn(pypocketfft.rfftn(b[:i].real),fct=1./i,lastsize=i),b[:i].real, atol=8e-7, rtol=0)

Martin Reinecke's avatar
Martin Reinecke committed
24
25
26
27
28
29
30
31
@pmp("shp", shapes)
def test_fftn(shp):
    a=np.random.rand(*shp)-0.5 + 1j*np.random.rand(*shp)-0.5j
    vmax = np.max(np.abs(pyfftw.interfaces.numpy_fft.fftn(a)))
    assert_allclose(pyfftw.interfaces.numpy_fft.fftn(a), pypocketfft.fftn(a), atol=2e-15*vmax, rtol=0)
    a=a.astype(np.complex64)
    assert_allclose(pyfftw.interfaces.numpy_fft.fftn(a), pypocketfft.fftn(a), atol=6e-7*vmax, rtol=0)

Martin Reinecke's avatar
more    
Martin Reinecke committed
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
@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
    vmax = np.max(np.abs(pyfftw.interfaces.numpy_fft.fftn(a, axes=axes)))
    assert_allclose(pyfftw.interfaces.numpy_fft.fftn(a, axes=axes), pypocketfft.fftn(a, axes=axes), atol=2e-15*vmax, rtol=0)
    a=a.astype(np.complex64)
    assert_allclose(pyfftw.interfaces.numpy_fft.fftn(a, axes=axes), pypocketfft.fftn(a, axes=axes), atol=6e-7*vmax, rtol=0)

@pmp("shp", shapes)
def test_rfftn(shp):
    a=np.random.rand(*shp)-0.5
    vmax = np.max(np.abs(pyfftw.interfaces.numpy_fft.fftn(a)))
    assert_allclose(pyfftw.interfaces.numpy_fft.rfftn(a), pypocketfft.rfftn(a), atol=2e-15*vmax, rtol=0)
    a=a.astype(np.float32)
    assert_allclose(pyfftw.interfaces.numpy_fft.rfftn(a), pypocketfft.rfftn(a), atol=6e-7*vmax, rtol=0)

@pmp("shp", shapes2D)
@pmp("axes", ((0,),(1,),(0,1),(1,0)))
def test_rfftn2D(shp, axes):
    a=np.random.rand(*shp)-0.5
    vmax = np.max(np.abs(pyfftw.interfaces.numpy_fft.rfftn(a, axes=axes)))
    assert_allclose(pyfftw.interfaces.numpy_fft.rfftn(a, axes=axes), pypocketfft.rfftn(a, axes=axes), atol=2e-15*vmax, rtol=0)
    a=a.astype(np.float32)
    assert_allclose(pyfftw.interfaces.numpy_fft.rfftn(a, axes=axes), pypocketfft.rfftn(a, axes=axes), atol=6e-7*vmax, rtol=0)

Martin Reinecke's avatar
Martin Reinecke committed
58
59
60
61
@pmp("shp", shapes)
def test_identity(shp):
    a=np.random.rand(*shp)-0.5 + 1j*np.random.rand(*shp)-0.5j
    vmax = np.max(np.abs(a))
62
    assert_allclose(pypocketfft.ifftn(pypocketfft.fftn(a),fct=1./a.size), a, atol=2e-15*vmax, rtol=0)
Martin Reinecke's avatar
Martin Reinecke committed
63
    a=a.astype(np.complex64)
64
    assert_allclose(pypocketfft.ifftn(pypocketfft.fftn(a),fct=1./a.size), a, atol=6e-7*vmax, rtol=0)
Martin Reinecke's avatar
Martin Reinecke committed
65
66

@pmp("shp", shapes)
Martin Reinecke's avatar
Martin Reinecke committed
67
def test_identity_r(shp):
Martin Reinecke's avatar
Martin Reinecke committed
68
69
70
71
72
    a=np.random.rand(*shp)-0.5
    b=a.astype(np.float32)
    vmax = np.max(np.abs(a))
    for ax in range(a.ndim):
        n = a.shape[ax]
73
74
        assert_allclose(pypocketfft.irfftn(pypocketfft.rfftn(a,(ax,)),(ax,),lastsize=n,fct=1./n), a, atol=2e-15*vmax, rtol=0)
        assert_allclose(pypocketfft.irfftn(pypocketfft.rfftn(b,(ax,)),(ax,),lastsize=n,fct=1./n), b, atol=6e-7*vmax, rtol=0)
Martin Reinecke's avatar
more    
Martin Reinecke committed
75

Martin Reinecke's avatar
Martin Reinecke committed
76
77
78
79
80
81
82
83
@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
    vmax = np.max(np.abs(a))
    assert_allclose(pypocketfft.rfftn(pypocketfft.irfftn(a),fct=fct), a, atol=2e-15*vmax, rtol=0)

Martin Reinecke's avatar
fixes    
Martin Reinecke committed
84
85
@pmp("shp", shapes2D+shapes3D)
def test_hartley2(shp):
Martin Reinecke's avatar
more    
Martin Reinecke committed
86
    a=np.random.rand(*shp)-0.5
Martin Reinecke's avatar
fixes    
Martin Reinecke committed
87
    v1 = pypocketfft.hartley2(a)
Martin Reinecke's avatar
more    
Martin Reinecke committed
88
89
90
91
92
    v2 = pypocketfft.fftn(a.astype(np.complex128))
    vmax = np.max(np.abs(v1))
    v2 = v2.real+v2.imag
    assert_allclose(v1, v2, atol=2e-15*vmax, rtol=0)

Martin Reinecke's avatar
fixes    
Martin Reinecke committed
93
@pmp("shp", shapes)
Martin Reinecke's avatar
more    
Martin Reinecke committed
94
95
96
97
98
def test_hartley_identity(shp):
    a=np.random.rand(*shp)-0.5
    v1 = pypocketfft.hartley(pypocketfft.hartley(a))/a.size
    vmax = np.max(np.abs(a))
    assert_allclose(a, v1, atol=2e-15*vmax, rtol=0)
Martin Reinecke's avatar
fixes    
Martin Reinecke committed
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113

@pmp("shp", shapes)
def test_hartley2_identity(shp):
    a=np.random.rand(*shp)-0.5
    v1 = pypocketfft.hartley2(pypocketfft.hartley2(a))/a.size
    vmax = np.max(np.abs(a))
    assert_allclose(a, v1, atol=2e-15*vmax, rtol=0)

@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))
    vmax = np.max(np.abs(a))
    assert_allclose(pypocketfft.hartley2(pypocketfft.hartley2(a, axes=axes), axes=axes, fct=fct),a , atol=2e-15*vmax, rtol=0)