test_nft.py 2.93 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
Martin Reinecke's avatar
Martin Reinecke committed
20
from numpy.testing import assert_allclose, assert_
Philipp Arras's avatar
Philipp Arras committed
21
22
23
24
25
26
27

import nifty5 as ift

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

32
speedOfLight = 299792458.
Martin Reinecke's avatar
fixes    
Martin Reinecke committed
33

Martin Reinecke's avatar
Martin Reinecke committed
34
@pmp('eps', [1e-2, 1e-4, 1e-7, 1e-10, 1e-11, 1e-12, 2e-13])
Philipp Arras's avatar
Philipp Arras committed
35
36
37
@pmp('nu', [12, 128])
@pmp('nv', [4, 12, 128])
@pmp('N', [1, 10, 100])
38
39
40
41
42
43
44
@pmp('freq', [1e9])
def test_gridding(nu, nv, N, eps, freq):
    fovx = 0.0001
    fovy = 0.0002
    uvw = (np.random.rand(N, 3) - 0.5)
    uvw[:,0] /= fovx*freq/speedOfLight
    uvw[:,1] /= fovy*freq/speedOfLight
Martin Reinecke's avatar
Martin Reinecke committed
45
    vis = (np.random.randn(N) + 1j*np.random.randn(N)).reshape((-1,1))
Philipp Arras's avatar
Philipp Arras committed
46
47

    # Nifty
Martin Reinecke's avatar
adjust    
Martin Reinecke committed
48
    GM = ift.GridderMaker(ift.RGSpace((nu, nv)), uvw=uvw,
49
                          freq=np.array([freq]), eps=eps, fovx=fovx, fovy=fovy,
Martin Reinecke's avatar
Martin Reinecke committed
50
                          flags=np.zeros((N, 1), dtype=np.bool))
Martin Reinecke's avatar
fix    
Martin Reinecke committed
51
    vis2 = ift.from_global_data(ift.UnstructuredDomain(vis.shape), vis)
Philipp Arras's avatar
Philipp Arras committed
52

Martin Reinecke's avatar
Martin Reinecke committed
53
    Op = GM.getFull()
Martin Reinecke's avatar
fix    
Martin Reinecke committed
54
    pynu = Op(vis2).to_global_data()
Philipp Arras's avatar
Philipp Arras committed
55
56
57
    # DFT
    x, y = np.meshgrid(
        *[-ss/2 + np.arange(ss) for ss in [nu, nv]], indexing='ij')
58
59
    x *= fovx*freq/speedOfLight
    y *= fovy*freq/speedOfLight
Philipp Arras's avatar
Philipp Arras committed
60
61
    dft = pynu*0.
    for i in range(N):
62
        dft += (vis[i]*np.exp(2j*np.pi*(x*uvw[i, 0] + y*uvw[i, 1]))).real
Martin Reinecke's avatar
Martin Reinecke committed
63
    assert_(_l2error(dft, pynu) < eps)
Philipp Arras's avatar
Philipp Arras committed
64
65


Martin Reinecke's avatar
Martin Reinecke committed
66
@pmp('eps', [1e-2, 1e-6, 2e-13])
Philipp Arras's avatar
Philipp Arras committed
67
68
69
@pmp('nu', [12, 128])
@pmp('nv', [4, 12, 128])
@pmp('N', [1, 10, 100])
70
71
@pmp('freq', [np.array([1e9]), np.array([1e9, 2e9, 2.5e9])])
def test_build(nu, nv, N, eps, freq):
Philipp Arras's avatar
Philipp Arras committed
72
    dom = ift.RGSpace([nu, nv])
73
    fov = np.pi/180/60
74
    uvw = np.random.rand(N, 3) - 0.5
75
    flags=np.zeros((N, freq.shape[0]), dtype=np.bool)
Martin Reinecke's avatar
Martin Reinecke committed
76
    flags[0,0]=True
77
78
    GM = ift.GridderMaker(dom, uvw=uvw, freq=freq, eps=eps,
                          flags=flags, fovx=fov, fovy=fov)
Martin Reinecke's avatar
Martin Reinecke committed
79
    R0 = GM.getGridder()
Philipp Arras's avatar
Philipp Arras committed
80
81
    R1 = GM.getRest()
    R = R1@R0
Martin Reinecke's avatar
Martin Reinecke committed
82
    RF = GM.getFull()
Philipp Arras's avatar
Philipp Arras committed
83
84

    # Consistency checks
85
86
87
88
89
90
    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)