gridder.py 3.7 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
# 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.

Philipp Arras's avatar
Philipp Arras committed
18
19
20
21
22
import numpy as np

from ..domain_tuple import DomainTuple
from ..domains.rg_space import RGSpace
from ..domains.unstructured_domain import UnstructuredDomain
23
from ..fft import hartley
Philipp Arras's avatar
Philipp Arras committed
24
25
26
27
28
from ..operators.linear_operator import LinearOperator
from ..sugar import from_global_data, makeDomain


class GridderMaker(object):
29
    def __init__(self, dirty_domain, uvw, channel_fact, eps=2e-13):
Martin Reinecke's avatar
Martin Reinecke committed
30
31
32
33
34
35
        import nifty_gridder
        dirty_domain = makeDomain(dirty_domain)
        if (len(dirty_domain) != 1 or
                not isinstance(dirty_domain[0], RGSpace) or
                not len(dirty_domain.shape) == 2):
            raise ValueError("need dirty_domain with exactly one 2D RGSpace")
36
37
38
        if channel_fact.ndim != 1:
            raise ValueError("channel_fact must be a 1D array")
        bl = nifty_gridder.Baselines(uvw, channel_fact);
Martin Reinecke's avatar
Martin Reinecke committed
39
40
41
42
        nxdirty, nydirty = dirty_domain.shape
        gconf = nifty_gridder.GridderConfig(nxdirty, nydirty, eps, 1., 1.)
        nu = gconf.Nu()
        nv = gconf.Nv()
43
        idx = nifty_gridder.getIndices(bl, gconf)
Martin Reinecke's avatar
Martin Reinecke committed
44
45
46
47
48
49
50
51

        grid_domain = RGSpace([nu, nv], distances=[1, 1], harmonic=False)

        self._rest = _RestOperator(dirty_domain, grid_domain, gconf)
        self._gridder = RadioGridder(grid_domain, bl, gconf, idx)

    def getGridder(self):
        return self._gridder
Philipp Arras's avatar
Philipp Arras committed
52
53
54
55

    def getRest(self):
        return self._rest

Martin Reinecke's avatar
Martin Reinecke committed
56
57
    def getFull(self):
        return self.getRest() @ self._gridder
Philipp Arras's avatar
Philipp Arras committed
58
59
60


class _RestOperator(LinearOperator):
Martin Reinecke's avatar
Martin Reinecke committed
61
62
63
64
65
    def __init__(self, dirty_domain, grid_domain, gconf):
        import nifty_gridder
        self._domain = makeDomain(grid_domain)
        self._target = makeDomain(dirty_domain)
        self._gconf = gconf
Philipp Arras's avatar
Philipp Arras committed
66
67
68
        self._capability = self.TIMES | self.ADJOINT_TIMES

    def apply(self, x, mode):
Martin Reinecke's avatar
Martin Reinecke committed
69
        import nifty_gridder
Philipp Arras's avatar
Philipp Arras committed
70
        self._check_input(x, mode)
71
        res = x.to_global_data()
Philipp Arras's avatar
Philipp Arras committed
72
        if mode == self.TIMES:
73
            res = self._gconf.grid2dirty(res)
Philipp Arras's avatar
Philipp Arras committed
74
        else:
75
            res = self._gconf.dirty2grid(res)
Philipp Arras's avatar
Philipp Arras committed
76
77
78
79
        return from_global_data(self._tgt(mode), res)


class RadioGridder(LinearOperator):
Martin Reinecke's avatar
Martin Reinecke committed
80
    def __init__(self, grid_domain, bl, gconf, idx):
81
82
        self._domain = DomainTuple.make(UnstructuredDomain(
                                        (idx.shape[0],)))
Martin Reinecke's avatar
Martin Reinecke committed
83
84
85
86
        self._target = DomainTuple.make(grid_domain)
        self._bl = bl
        self._gconf = gconf
        self._idx = idx
Philipp Arras's avatar
Philipp Arras committed
87
88
89
        self._capability = self.TIMES | self.ADJOINT_TIMES

    def apply(self, x, mode):
Martin Reinecke's avatar
Martin Reinecke committed
90
        import nifty_gridder
Philipp Arras's avatar
Philipp Arras committed
91
92
        self._check_input(x, mode)
        if mode == self.TIMES:
Martin Reinecke's avatar
fixes    
Martin Reinecke committed
93
94
            x = x.to_global_data().reshape((-1,1))
            x = self._bl.ms2vis(x, self._idx)
95
            res = nifty_gridder.vis2grid(
Martin Reinecke's avatar
fixes    
Martin Reinecke committed
96
                self._bl, self._gconf, self._idx, x)
Philipp Arras's avatar
Philipp Arras committed
97
        else:
98
            res = nifty_gridder.grid2vis(
Martin Reinecke's avatar
Martin Reinecke committed
99
                self._bl, self._gconf, self._idx, x.to_global_data())
Martin Reinecke's avatar
fixes    
Martin Reinecke committed
100
            res = self._bl.vis2ms(res, self._idx).reshape((-1,))
Philipp Arras's avatar
Philipp Arras committed
101
        return from_global_data(self._tgt(mode), res)