test_nft.py 3.22 KB
Newer Older
Philipp Arras's avatar
Philipp Arras committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# 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/>.
#
# Copyright(C) 2019 Max-Planck-Society
#
# NIFTy is being developed at the Max-Planck-Institut fuer Astrophysik.

import numpy as np
import pytest
Philipp Arras's avatar
Philipp Arras committed
20
from numpy.testing import assert_
Philipp Arras's avatar
Philipp Arras committed
21

Martin Reinecke's avatar
5->6    
Martin Reinecke committed
22
import nifty6 as ift
Philipp Arras's avatar
Philipp Arras committed
23
24
25
26
27

np.random.seed(40)

pmp = pytest.mark.parametrize

Martin Reinecke's avatar
Martin Reinecke committed
28

Martin Reinecke's avatar
Martin Reinecke committed
29
def _l2error(a, b):
Martin Reinecke's avatar
fixes    
Martin Reinecke committed
30
    return np.sqrt(np.sum(np.abs(a-b)**2)/np.sum(np.abs(a)**2))
Philipp Arras's avatar
Philipp Arras committed
31

Martin Reinecke's avatar
fixes    
Martin Reinecke committed
32

Martin Reinecke's avatar
Martin Reinecke committed
33
@pmp('eps', [1e-2, 1e-4, 1e-7, 1e-10, 1e-11, 1e-12, 2e-13])
Philipp Arras's avatar
Philipp Arras committed
34
35
36
@pmp('nu', [12, 128])
@pmp('nv', [4, 12, 128])
@pmp('N', [1, 10, 100])
37
38
39
def test_gridding(nu, nv, N, eps):
    uv = np.random.rand(N, 2) - 0.5
    vis = np.random.randn(N) + 1j*np.random.randn(N)
Philipp Arras's avatar
Philipp Arras committed
40
41

    # Nifty
42
43
    dom = ift.RGSpace((nu, nv), distances=(0.2, 1.12))
    dstx, dsty = dom.distances
Martin Reinecke's avatar
Martin Reinecke committed
44
45
    uv[:, 0] = uv[:, 0]/dstx
    uv[:, 1] = uv[:, 1]/dsty
46
    GM = ift.GridderMaker(dom, uv=uv, eps=eps)
Martin Reinecke's avatar
Martin Reinecke committed
47
    vis2 = ift.makeField(ift.UnstructuredDomain(vis.shape), vis)
Philipp Arras's avatar
Philipp Arras committed
48

Martin Reinecke's avatar
Martin Reinecke committed
49
    Op = GM.getFull()
Martin Reinecke's avatar
stage 3    
Martin Reinecke committed
50
    pynu = Op(vis2).val
Philipp Arras's avatar
Philipp Arras committed
51
52
53
54
55
    # DFT
    x, y = np.meshgrid(
        *[-ss/2 + np.arange(ss) for ss in [nu, nv]], indexing='ij')
    dft = pynu*0.
    for i in range(N):
Martin Reinecke's avatar
Martin Reinecke committed
56
57
        dft += (
            vis[i]*np.exp(2j*np.pi*(x*uv[i, 0]*dstx + y*uv[i, 1]*dsty))).real
Martin Reinecke's avatar
Martin Reinecke committed
58
    assert_(_l2error(dft, pynu) < eps)
Philipp Arras's avatar
Philipp Arras committed
59
60


Martin Reinecke's avatar
Martin Reinecke committed
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
def test_cartesian():
    nx, ny = 2, 6
    dstx, dsty = 0.3, 0.2
    dom = ift.RGSpace((nx, ny), (dstx, dsty))

    kx = np.fft.fftfreq(nx, dstx)
    ky = np.fft.fftfreq(ny, dsty)
    uu, vv = np.meshgrid(kx, ky)
    tmp = np.vstack([uu[None, :], vv[None, :]])
    uv = np.transpose(tmp, (2, 1, 0)).reshape(-1, 2)

    GM = ift.GridderMaker(dom, uv=uv)
    op = GM.getFull().adjoint

    fld = ift.from_random('normal', dom)
Martin Reinecke's avatar
stage 3    
Martin Reinecke committed
76
    arr = fld.val
Martin Reinecke's avatar
Martin Reinecke committed
77

Martin Reinecke's avatar
Martin Reinecke committed
78
    fld2 = ift.makeField(dom, np.roll(arr, (nx//2, ny//2), axis=(0, 1)))
Martin Reinecke's avatar
stage 3    
Martin Reinecke committed
79
    res = op(fld2).val.reshape(nx, ny)
Martin Reinecke's avatar
Martin Reinecke committed
80
81
82

    fft = ift.FFTOperator(dom.get_default_codomain(), target=dom).adjoint
    vol = ift.full(dom, 1.).integrate()
Martin Reinecke's avatar
stage 3    
Martin Reinecke committed
83
    res1 = fft(fld).val
Martin Reinecke's avatar
Martin Reinecke committed
84
85
86
87
88

    # FIXME: we don't understand the conjugate() yet
    np.testing.assert_allclose(res, res1.conjugate()*vol)


Martin Reinecke's avatar
Martin Reinecke committed
89
@pmp('eps', [1e-2, 1e-6, 2e-13])
Philipp Arras's avatar
Philipp Arras committed
90
91
92
@pmp('nu', [12, 128])
@pmp('nv', [4, 12, 128])
@pmp('N', [1, 10, 100])
93
def test_build(nu, nv, N, eps):
Philipp Arras's avatar
Philipp Arras committed
94
    dom = ift.RGSpace([nu, nv])
95
96
    uv = np.random.rand(N, 2) - 0.5
    GM = ift.GridderMaker(dom, uv=uv, eps=eps)
Martin Reinecke's avatar
Martin Reinecke committed
97
    R0 = GM.getGridder()
Philipp Arras's avatar
Philipp Arras committed
98
99
    R1 = GM.getRest()
    R = R1@R0
Martin Reinecke's avatar
Martin Reinecke committed
100
    RF = GM.getFull()
Philipp Arras's avatar
Philipp Arras committed
101
102

    # Consistency checks
103
104
105
106
107
108
    flt = np.float64
    cmplx = np.complex128
    ift.extra.consistency_check(R0, cmplx, flt, only_r_linear=True)
    ift.extra.consistency_check(R1, flt, flt)
    ift.extra.consistency_check(R, cmplx, flt, only_r_linear=True)
    ift.extra.consistency_check(RF, cmplx, flt, only_r_linear=True)