test.py 4.12 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
14
15
16
17
18
19
20
21

@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
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
@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
48
49
50
51
@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))
52
    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
53
    a=a.astype(np.complex64)
54
    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
55
56

@pmp("shp", shapes)
Martin Reinecke's avatar
Martin Reinecke committed
57
def test_identity_r(shp):
Martin Reinecke's avatar
Martin Reinecke committed
58
59
60
61
62
    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]
63
64
        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
65

Martin Reinecke's avatar
Martin Reinecke committed
66
67
68
69
70
71
72
73
@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
74
75
@pmp("shp", shapes2D+shapes3D)
def test_hartley2(shp):
Martin Reinecke's avatar
more    
Martin Reinecke committed
76
    a=np.random.rand(*shp)-0.5
Martin Reinecke's avatar
fixes    
Martin Reinecke committed
77
    v1 = pypocketfft.hartley2(a)
Martin Reinecke's avatar
more    
Martin Reinecke committed
78
79
80
81
82
    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
83
@pmp("shp", shapes)
Martin Reinecke's avatar
more    
Martin Reinecke committed
84
85
86
87
88
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103

@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)