test_wgridder.py 4.89 KB
Newer Older
Martin Reinecke's avatar
Martin Reinecke committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 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) 2020 Max-Planck-Society


Martin Reinecke's avatar
Martin Reinecke committed
17
import ducc0.wgridder as ng
Martin Reinecke's avatar
Martin Reinecke committed
18
19
import numpy as np
import pytest
Martin Reinecke's avatar
Martin Reinecke committed
20
from numpy.testing import assert_allclose
Martin Reinecke's avatar
Martin Reinecke committed
21
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
48
49
50
51

pmp = pytest.mark.parametrize


def _l2error(a, b):
    return np.sqrt(np.sum(np.abs(a-b)**2)/np.maximum(np.sum(np.abs(a)**2),
                                                     np.sum(np.abs(b)**2)))


def explicit_gridder(uvw, freq, ms, wgt, nxdirty, nydirty, xpixsize, ypixsize,
                     apply_w):
    speedoflight = 299792458.
    x, y = np.meshgrid(*[-ss/2 + np.arange(ss) for ss in [nxdirty, nydirty]],
                       indexing='ij')
    x *= xpixsize
    y *= ypixsize
    res = np.zeros((nxdirty, nydirty))
    eps = x**2+y**2
    if apply_w:
        nm1 = -eps/(np.sqrt(1.-eps)+1.)
        n = nm1+1
    else:
        nm1 = 0.
        n = 1.
    for row in range(ms.shape[0]):
        for chan in range(ms.shape[1]):
            phase = (freq[chan]/speedoflight *
                     (x*uvw[row, 0] + y*uvw[row, 1] - uvw[row, 2]*nm1))
            if wgt is None:
                res += (ms[row, chan]*np.exp(2j*np.pi*phase)).real
            else:
Martin Reinecke's avatar
Martin Reinecke committed
52
53
                res += (ms[row, chan]*wgt[row, chan]
                        * np.exp(2j*np.pi*phase)).real
Martin Reinecke's avatar
Martin Reinecke committed
54
55
56
57
58
59
60
61
62
63
64
65
66
    return res/n


@pmp("nxdirty", (30, 128))
@pmp("nydirty", (128, 250))
@pmp("ofactor", (1.2, 1.5, 1.7, 2.0))
@pmp("nrow", (2, 27))
@pmp("nchan", (1, 5))
@pmp("epsilon", (1e-1, 1e-3, 1e-5))
@pmp("singleprec", (True, False))
@pmp("wstacking", (True, False))
@pmp("use_wgt", (True, False))
@pmp("nthreads", (1, 2))
Martin Reinecke's avatar
Martin Reinecke committed
67
68
def test_adjointness_ms2dirty(nxdirty, nydirty, ofactor, nrow, nchan, epsilon,
                              singleprec, wstacking, use_wgt, nthreads):
Martin Reinecke's avatar
Martin Reinecke committed
69
70
    if singleprec and epsilon < 5e-5:
        return
71
    rng = np.random.default_rng(42)
Martin Reinecke's avatar
Martin Reinecke committed
72
73
74
75
    pixsizex = np.pi/180/60/nxdirty*0.2398
    pixsizey = np.pi/180/60/nxdirty
    speedoflight, f0 = 299792458., 1e9
    freq = f0 + np.arange(nchan)*(f0/nchan)
76
77
78
79
    uvw = (rng.random((nrow, 3))-0.5)/(pixsizey*f0/speedoflight)
    ms = rng.random((nrow, nchan))-0.5 + 1j*(rng.random((nrow, nchan))-0.5)
    wgt = rng.random((nrow, nchan)) if use_wgt else None
    dirty = rng.random((nxdirty, nydirty))-0.5
Martin Reinecke's avatar
Martin Reinecke committed
80
    nu, nv = int(nxdirty*ofactor)+1, int(nydirty*ofactor)+1
Martin Reinecke's avatar
Martin Reinecke committed
81
82
83
84
    if nu & 1:
        nu += 1
    if nv & 1:
        nv += 1
Martin Reinecke's avatar
Martin Reinecke committed
85
86
87
88
89
    if singleprec:
        ms = ms.astype("c8")
        dirty = dirty.astype("f4")
        if wgt is not None:
            wgt = wgt.astype("f4")
90
    dirty2 = ng.ms2dirty(uvw, freq, ms, wgt, nxdirty, nydirty, pixsizex,
Martin Reinecke's avatar
Martin Reinecke committed
91
                         pixsizey, nu, nv, epsilon, wstacking, nthreads, 0).astype("f8")
92
    ms2 = ng.dirty2ms(uvw, freq, dirty, wgt, pixsizex, pixsizey, nu, nv, epsilon,
Martin Reinecke's avatar
Martin Reinecke committed
93
94
95
96
97
98
99
100
101
102
103
                      wstacking, nthreads, 0).astype("c16")
    tol = 5e-4 if singleprec else 5e-11
    assert_allclose(np.vdot(ms, ms2).real, np.vdot(dirty2, dirty), rtol=tol)


@pmp('nxdirty', [16, 64])
@pmp('nydirty', [64])
@pmp('ofactor', [1.2, 1.4, 1.7, 2])
@pmp("nrow", (2, 27))
@pmp("nchan", (1, 5))
@pmp("epsilon", (1e-2, 1e-3, 1e-4, 1e-7))
Martin Reinecke's avatar
Martin Reinecke committed
104
@pmp("singleprec", (False,))
Martin Reinecke's avatar
Martin Reinecke committed
105
@pmp("wstacking", (True,))
Martin Reinecke's avatar
Martin Reinecke committed
106
@pmp("use_wgt", (True,))
Martin Reinecke's avatar
Martin Reinecke committed
107
108
109
110
111
@pmp("nthreads", (1, 2))
@pmp("fov", (1., 20.))
def test_ms2dirty_against_wdft2(nxdirty, nydirty, ofactor, nrow, nchan, epsilon, singleprec, wstacking, use_wgt, fov, nthreads):
    if singleprec and epsilon < 5e-5:
        return
112
    rng = np.random.default_rng(42)
Martin Reinecke's avatar
Martin Reinecke committed
113
114
115
116
    pixsizex = fov*np.pi/180/nxdirty
    pixsizey = fov*np.pi/180/nydirty*1.1
    speedoflight, f0 = 299792458., 1e9
    freq = f0 + np.arange(nchan)*(f0/nchan)
117
118
    uvw = (rng.random((nrow, 3))-0.5)/(pixsizex*f0/speedoflight)
    ms = rng.random((nrow, nchan))-0.5 + 1j*(rng.random((nrow, nchan))-0.5)
Martin Reinecke's avatar
Martin Reinecke committed
119
120
    wgt = rng.random((nrow, 1)) if use_wgt else None
    wgt = np.broadcast_to(wgt, (nrow, nchan)) if use_wgt else None
Martin Reinecke's avatar
Martin Reinecke committed
121
    nu, nv = int(nxdirty*ofactor)+1, int(nydirty*ofactor)+1
Martin Reinecke's avatar
Martin Reinecke committed
122
123
124
125
    if nu & 1:
        nu += 1
    if nv & 1:
        nv += 1
Martin Reinecke's avatar
Martin Reinecke committed
126
127
128
129
    if singleprec:
        ms = ms.astype("c8")
        if wgt is not None:
            wgt = wgt.astype("f4")
130
    dirty = ng.ms2dirty(uvw, freq, ms, wgt, nxdirty, nydirty, pixsizex,
Martin Reinecke's avatar
Martin Reinecke committed
131
132
133
                        pixsizey, nu, nv, epsilon, wstacking, nthreads, 0).astype("f8")
    ref = explicit_gridder(uvw, freq, ms, wgt, nxdirty, nydirty, pixsizex, pixsizey, wstacking)
    assert_allclose(_l2error(dirty, ref), 0, atol=epsilon)