test_nft.py 2.3 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 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
# 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
from numpy.testing import assert_allclose

import nifty5 as ift

np.random.seed(40)

pmp = pytest.mark.parametrize


@pmp('nu', [12, 128])
@pmp('nv', [4, 12, 128])
@pmp('N', [1, 10, 100])
def test_gridding(nu, nv, N):
    uv = np.random.rand(N, 2) - 0.5
    vis = np.random.randn(N) + 1j*np.random.randn(N)

    # Nifty
    GM = ift.GridderMaker(ift.RGSpace((nu, nv)))
    # re-order for performance
    idx = GM.getReordering(uv)
    uv, vis = uv[idx], vis[idx]
Martin Reinecke's avatar
fix  
Martin Reinecke committed
41
    vis2 = ift.from_global_data(ift.UnstructuredDomain(vis.shape), vis)
Philipp Arras's avatar
Philipp Arras committed
42 43

    Op = GM.getFull(uv)
Martin Reinecke's avatar
fix  
Martin Reinecke committed
44
    pynu = Op(vis2).to_global_data()
Philipp Arras's avatar
Philipp Arras committed
45 46 47 48 49
    # 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
fix  
Martin Reinecke committed
50
        dft += (vis[i]*np.exp(2j*np.pi*(x*uv[i, 0] + y*uv[i, 1]))).real
Philipp Arras's avatar
Philipp Arras committed
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
    assert_allclose(dft, pynu)


@pmp('eps', [1e-2, 1e-6, 1e-15])
@pmp('nu', [12, 128])
@pmp('nv', [4, 12, 128])
@pmp('N', [1, 10, 100])
def test_build(nu, nv, N, eps):
    dom = ift.RGSpace([nu, nv])
    uv = np.random.rand(N, 2) - 0.5
    GM = ift.GridderMaker(dom)
    # re-order for performance
    idx = GM.getReordering(uv)
    uv = uv[idx]
    R0 = GM.getGridder(uv)
    R1 = GM.getRest()
    R = R1@R0
    RF = GM.getFull(uv)

    # Consistency checks
71 72 73 74 75 76
    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)