test_wgridder.py 5.58 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

pmp = pytest.mark.parametrize


25
26
27
28
29
30
31
32
33
34
35
# attempt to write a more accurate version of numpy.vdot()
def my_vdot(a, b):
    import math
    if np.issubdtype(a.dtype, np.complexfloating) or np.issubdtype(b.dtype, np.complexfloating):
        tmp = (np.conj(a)*b).reshape((-1,))
        return math.fsum(tmp.real)+1j*math.fsum(tmp.imag)
    else:
        tmp = (a*b).reshape((-1,))
        return math.fsum(tmp)


Martin Reinecke's avatar
Martin Reinecke committed
36
37
38
39
40
41
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,
42
                     apply_w, mask):
Martin Reinecke's avatar
Martin Reinecke committed
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
    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]):
58
59
            if mask is not None and mask[row, chan] == 0:
                continue
Martin Reinecke's avatar
Martin Reinecke committed
60
61
62
63
64
            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
65
66
                res += (ms[row, chan]*wgt[row, chan]
                        * np.exp(2j*np.pi*phase)).real
Martin Reinecke's avatar
Martin Reinecke committed
67
68
69
    return res/n


Martin Reinecke's avatar
Martin Reinecke committed
70
71
@pmp("nxdirty", (30, 128))
@pmp("nydirty", (128, 250))
Martin Reinecke's avatar
Martin Reinecke committed
72
@pmp("nrow", (1, 2, 27))
Martin Reinecke's avatar
Martin Reinecke committed
73
74
75
76
77
@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))
78
@pmp("use_mask", (False, True))
79
@pmp("nthreads", (1, 2, 7))
Martin Reinecke's avatar
Martin Reinecke committed
80
def test_adjointness_ms2dirty(nxdirty, nydirty, nrow, nchan, epsilon,
81
                              singleprec, wstacking, use_wgt, nthreads, use_mask):
Martin Reinecke's avatar
Martin Reinecke committed
82
    if singleprec and epsilon < 5e-5:
Philipp Arras's avatar
Philipp Arras committed
83
        pytest.skip()
Martin Reinecke's avatar
Martin Reinecke committed
84
85
86
87
88
89
90
    rng = np.random.default_rng(42)
    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)
    uvw = (rng.random((nrow, 3))-0.5)/(pixsizey*f0/speedoflight)
    ms = rng.random((nrow, nchan))-0.5 + 1j*(rng.random((nrow, nchan))-0.5)
Martin Reinecke's avatar
Martin Reinecke committed
91
    wgt = rng.uniform(0.9, 1.1, (nrow, nchan)) if use_wgt else None
92
    mask = (rng.uniform(0, 1, (nrow, nchan)) > 0.5).astype(np.uint8) if use_mask else None
Martin Reinecke's avatar
Martin Reinecke committed
93
    dirty = rng.random((nxdirty, nydirty))-0.5
Martin Reinecke's avatar
Martin Reinecke committed
94
    nu = nv = 0
Martin Reinecke's avatar
Martin Reinecke committed
95
96
97
98
99
100
    if singleprec:
        ms = ms.astype("c8")
        dirty = dirty.astype("f4")
        if wgt is not None:
            wgt = wgt.astype("f4")
    dirty2 = ng.ms2dirty(uvw, freq, ms, wgt, nxdirty, nydirty, pixsizex,
101
                         pixsizey, nu, nv, epsilon, wstacking, nthreads, 0, mask).astype("f8")
102
    ms2 = ng.dirty2ms(uvw, freq, dirty, wgt, pixsizex, pixsizey, nu, nv, epsilon,
103
                      wstacking, nthreads+1, 0, mask).astype("c16")
104
105
    ref = max(my_vdot(ms,ms).real, my_vdot(ms2,ms2).real,
              my_vdot(dirty, dirty).real, my_vdot(dirty2, dirty2).real)
Martin Reinecke's avatar
Martin Reinecke committed
106
    tol = 1e-5*ref if singleprec else 1e-11*ref
107
    assert_allclose(my_vdot(ms, ms2).real, my_vdot(dirty2, dirty), rtol=tol)
Martin Reinecke's avatar
Martin Reinecke committed
108
109
110


@pmp('nxdirty', [16, 64])
Martin Reinecke's avatar
Martin Reinecke committed
111
@pmp('nydirty', [64])
Martin Reinecke's avatar
Martin Reinecke committed
112
@pmp("nrow", (1, 2, 27))
Martin Reinecke's avatar
Martin Reinecke committed
113
114
@pmp("nchan", (1, 5))
@pmp("epsilon", (1e-2, 1e-3, 1e-4, 1e-7))
Martin Reinecke's avatar
Martin Reinecke committed
115
@pmp("singleprec", (False,))
Martin Reinecke's avatar
Martin Reinecke committed
116
@pmp("wstacking", (False, True))
Martin Reinecke's avatar
Martin Reinecke committed
117
@pmp("use_wgt", (True,))
118
@pmp("use_mask", (True,))
119
@pmp("nthreads", (1, 2, 7))
Martin Reinecke's avatar
tweaks  
Martin Reinecke committed
120
@pmp("fov", (0.001, 0.01, 0.1, 1., 20.))
Martin Reinecke's avatar
Martin Reinecke committed
121
def test_ms2dirty_against_wdft2(nxdirty, nydirty, nrow, nchan, epsilon, singleprec, wstacking, use_wgt, use_mask, fov, nthreads):
Martin Reinecke's avatar
Martin Reinecke committed
122
    if singleprec and epsilon < 5e-5:
123
        pytest.skip()
124
    rng = np.random.default_rng(42)
Martin Reinecke's avatar
Martin Reinecke committed
125
126
127
128
    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)
129
130
    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
131
    wgt = rng.uniform(0.9, 1.1, (nrow, 1)) if use_wgt else None
132
    mask = (rng.uniform(0, 1, (nrow, nchan)) > 0.5).astype(np.uint8) if use_mask else None
Martin Reinecke's avatar
Martin Reinecke committed
133
    wgt = np.broadcast_to(wgt, (nrow, nchan)) if use_wgt else None
Martin Reinecke's avatar
Martin Reinecke committed
134
    nu = nv = 0
Martin Reinecke's avatar
Martin Reinecke committed
135
136
137
138
    if singleprec:
        ms = ms.astype("c8")
        if wgt is not None:
            wgt = wgt.astype("f4")
139
    dirty = ng.ms2dirty(uvw, freq, ms, wgt, nxdirty, nydirty, pixsizex,
140
141
142
143
                        pixsizey, nu, nv, epsilon, wstacking, nthreads,
                        0, mask).astype("f8")
    ref = explicit_gridder(uvw, freq, ms, wgt, nxdirty, nydirty, pixsizex,
                           pixsizey, wstacking, mask)
Martin Reinecke's avatar
Martin Reinecke committed
144
    assert_allclose(_l2error(dirty, ref), 0, atol=epsilon)