test_adjoint.py 11.7 KB
Newer Older
Martin Reinecke's avatar
Martin Reinecke committed
1
2
3
4
5
6
7
8
9
10
11
12
13
# 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/>.
#
14
# Copyright(C) 2013-2019 Max-Planck-Society
Martin Reinecke's avatar
Martin Reinecke committed
15
#
16
# NIFTy is being developed at the Max-Planck-Institut fuer Astrophysik.
Martin Reinecke's avatar
Martin Reinecke committed
17

18
19
20
21
import unittest
from itertools import product
from test.common import expand

22
23
import nifty5 as ift
import numpy as np
24

25
26
27
_h_RG_spaces = [ift.RGSpace(7, distances=0.2, harmonic=True),
                ift.RGSpace((12, 46), distances=(.2, .3), harmonic=True)]
_h_spaces = _h_RG_spaces + [ift.LMSpace(17)]
Martin Reinecke's avatar
Martin Reinecke committed
28

29
30
31
_p_RG_spaces = [ift.RGSpace(19, distances=0.7),
                ift.RGSpace((1, 2, 3, 6), distances=(0.2, 0.25, 0.34, .8))]
_p_spaces = _p_RG_spaces + [ift.HPSpace(17), ift.GLSpace(8, 13)]
32

33
34
_pow_spaces = [ift.PowerSpace(ift.RGSpace((17, 38), harmonic=True))]

Martin Reinecke's avatar
Martin Reinecke committed
35

36
class Consistency_Tests(unittest.TestCase):
37
38
39
40
41
42
43
44
45
    @expand(product(_p_RG_spaces, [np.float64, np.complex128]))
    def testLOSResponse(self, sp, dtype):
        starts = np.random.randn(len(sp.shape), 10)
        ends = np.random.randn(len(sp.shape), 10)
        sigma_low = 1e-4*np.random.randn(10)
        sigma_ups = 1e-5*np.random.randn(10)
        op = ift.LOSResponse(sp, starts, ends, sigma_low, sigma_ups)
        ift.extra.consistency_check(op, dtype, dtype)

46
47
48
49
    @expand(product(_h_spaces + _p_spaces + _pow_spaces,
                    [np.float64, np.complex128]))
    def testOperatorCombinations(self, sp, dtype):
        a = ift.DiagonalOperator(ift.Field.from_random("normal", sp,
Martin Reinecke's avatar
Martin Reinecke committed
50
                                                       dtype=dtype))
51
        b = ift.DiagonalOperator(ift.Field.from_random("normal", sp,
Martin Reinecke's avatar
Martin Reinecke committed
52
                                                       dtype=dtype))
53
54
        op = ift.SandwichOperator.make(a, b)
        ift.extra.consistency_check(op, dtype, dtype)
Martin Reinecke's avatar
Martin Reinecke committed
55
        op = a(b)
56
57
58
59
        ift.extra.consistency_check(op, dtype, dtype)
        op = a+b
        ift.extra.consistency_check(op, dtype, dtype)

60
    def testLinearInterpolator(self):
Martin Reinecke's avatar
Martin Reinecke committed
61
        sp = ift.RGSpace((10, 8), distances=(0.1, 3.5))
62
        pos = np.random.rand(2, 23)
Martin Reinecke's avatar
Martin Reinecke committed
63
64
        pos[0, :] *= 0.9
        pos[1, :] *= 7*3.5
65
66
        op = ift.LinearInterpolator(sp, pos)
        ift.extra.consistency_check(op)
Martin Reinecke's avatar
Martin Reinecke committed
67

68
69
70
71
72
73
74
75
    @expand(product([(ift.RGSpace(10, harmonic=True), 4, 0),
                     (ift.RGSpace((24, 31), distances=(0.4, 2.34),
                                  harmonic=True), 3, 0),
                     (ift.LMSpace(4), 10, 0)],
                    [np.float64, np.complex128]))
    def testSlopeOperator(self, args, dtype):
        tmp = ift.ExpTransform(ift.PowerSpace(args[0]), args[1], args[2])
        tgt = tmp.domain[0]
Philipp Frank's avatar
Philipp Frank committed
76
        op = ift.SlopeOperator(tgt)
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
        ift.extra.consistency_check(op, dtype, dtype)

    @expand(product(_h_spaces + _p_spaces + _pow_spaces,
                    [np.float64, np.complex128]))
    def testOperatorAdaptor(self, sp, dtype):
        op = ift.DiagonalOperator(ift.Field.from_random("normal", sp,
                                                        dtype=dtype))
        ift.extra.consistency_check(op.adjoint, dtype, dtype)
        ift.extra.consistency_check(op.inverse, dtype, dtype)
        ift.extra.consistency_check(op.inverse.adjoint, dtype, dtype)
        ift.extra.consistency_check(op.adjoint.inverse, dtype, dtype)

    @expand(product(_h_spaces + _p_spaces + _pow_spaces,
                    _h_spaces + _p_spaces + _pow_spaces,
                    [np.float64, np.complex128]))
    def testNullOperator(self, sp1, sp2, dtype):
        op = ift.NullOperator(sp1, sp2)
        ift.extra.consistency_check(op, dtype, dtype)
Martin Reinecke's avatar
Martin Reinecke committed
95
96
        mdom1 = ift.MultiDomain.make({'a': sp1})
        mdom2 = ift.MultiDomain.make({'b': sp2})
97
98
99
100
101
102
        op = ift.NullOperator(mdom1, mdom2)
        ift.extra.consistency_check(op, dtype, dtype)
        op = ift.NullOperator(sp1, mdom2)
        ift.extra.consistency_check(op, dtype, dtype)
        op = ift.NullOperator(mdom1, sp2)
        ift.extra.consistency_check(op, dtype, dtype)
Martin Reinecke's avatar
Martin Reinecke committed
103

104
105
106
107
108
109
    @expand(product(_p_RG_spaces,
                    [np.float64, np.complex128]))
    def testHarmonicSmoothingOperator(self, sp, dtype):
        op = ift.HarmonicSmoothingOperator(sp, 0.1)
        ift.extra.consistency_check(op, dtype, dtype)

Martin Reinecke's avatar
Martin Reinecke committed
110
    @expand(product(_h_spaces + _p_spaces + _pow_spaces,
Reimar H Leike's avatar
Reimar H Leike committed
111
112
                    [np.float64, np.complex128]))
    def testDOFDistributor(self, sp, dtype):
Martin Reinecke's avatar
Martin Reinecke committed
113
        # TODO: Test for DomainTuple
Reimar H Leike's avatar
Reimar H Leike committed
114
115
        if sp.size < 4:
            return
Martin Reinecke's avatar
Martin Reinecke committed
116
        dofdex = np.arange(sp.size).reshape(sp.shape) % 3
117
118
119
        dofdex = ift.Field.from_global_data(sp, dofdex)
        op = ift.DOFDistributor(dofdex)
        ift.extra.consistency_check(op, dtype, dtype)
Reimar H Leike's avatar
bugfic    
Reimar H Leike committed
120

121
    @expand(product(_h_spaces, [np.float64, np.complex128]))
122
    def testPPO(self, sp, dtype):
Martin Reinecke's avatar
Martin Reinecke committed
123
        op = ift.PowerDistributor(target=sp)
124
        ift.extra.consistency_check(op, dtype, dtype)
Martin Reinecke's avatar
Martin Reinecke committed
125
126
        ps = ift.PowerSpace(
            sp, ift.PowerSpace.useful_binbounds(sp, logarithmic=False, nbin=3))
Martin Reinecke's avatar
Martin Reinecke committed
127
        op = ift.PowerDistributor(target=sp, power_space=ps)
128
        ift.extra.consistency_check(op, dtype, dtype)
Martin Reinecke's avatar
Martin Reinecke committed
129
130
        ps = ift.PowerSpace(
            sp, ift.PowerSpace.useful_binbounds(sp, logarithmic=True, nbin=3))
Martin Reinecke's avatar
Martin Reinecke committed
131
        op = ift.PowerDistributor(target=sp, power_space=ps)
132
        ift.extra.consistency_check(op, dtype, dtype)
Martin Reinecke's avatar
Martin Reinecke committed
133

134
    @expand(product(_h_RG_spaces+_p_RG_spaces,
Martin Reinecke's avatar
Martin Reinecke committed
135
                    [np.float64, np.complex128]))
Martin Reinecke's avatar
Martin Reinecke committed
136
137
    def testFFT(self, sp, dtype):
        op = ift.FFTOperator(sp)
138
        ift.extra.consistency_check(op, dtype, dtype)
Martin Reinecke's avatar
Martin Reinecke committed
139
        op = ift.FFTOperator(sp.get_default_codomain())
140
        ift.extra.consistency_check(op, dtype, dtype)
141

Martin Reinecke's avatar
Martin Reinecke committed
142
143
144
145
146
147
148
149
    @expand(product(_h_RG_spaces+_p_RG_spaces,
                    [np.float64, np.complex128]))
    def testHartley(self, sp, dtype):
        op = ift.HartleyOperator(sp)
        ift.extra.consistency_check(op, dtype, dtype)
        op = ift.HartleyOperator(sp.get_default_codomain())
        ift.extra.consistency_check(op, dtype, dtype)

150
151
152
    @expand(product(_h_spaces, [np.float64, np.complex128]))
    def testHarmonic(self, sp, dtype):
        op = ift.HarmonicTransformOperator(sp)
153
154
        ift.extra.consistency_check(op, dtype, dtype)

Philipp Arras's avatar
Philipp Arras committed
155
156
157
    @expand(product(_p_spaces, [np.float64, np.complex128]))
    def testMask(self, sp, dtype):
        # Create mask
Philipp Arras's avatar
Philipp Arras committed
158
        f = ift.from_random('normal', sp).to_global_data()
Philipp Arras's avatar
Philipp Arras committed
159
160
        mask = np.zeros_like(f)
        mask[f > 0] = 1
Philipp Arras's avatar
Philipp Arras committed
161
        mask = ift.Field.from_global_data(sp, mask)
Philipp Arras's avatar
Philipp Arras committed
162
163
164
165
        # Test MaskOperator
        op = ift.MaskOperator(mask)
        ift.extra.consistency_check(op, dtype, dtype)

166
167
168
169
170
    @expand(product(_h_spaces+_p_spaces, [np.float64, np.complex128]))
    def testDiagonal(self, sp, dtype):
        op = ift.DiagonalOperator(ift.Field.from_random("normal", sp,
                                                        dtype=dtype))
        ift.extra.consistency_check(op, dtype, dtype)
Martin Reinecke's avatar
Martin Reinecke committed
171

Martin Reinecke's avatar
Martin Reinecke committed
172
173
174
175
176
    @expand(product(_h_spaces+_p_spaces+_pow_spaces,
                    [np.float64, np.complex128]))
    def testGeometryRemover(self, sp, dtype):
        op = ift.GeometryRemover(sp)
        ift.extra.consistency_check(op, dtype, dtype)
177
178

    @expand(product([0, 1, 2, 3, (0, 1), (0, 2), (0, 1, 2), (0, 2, 3), (1, 3)],
179
180
181
                    [0, 1, 2, -1], [np.float64, np.complex128]))
    def testContractionOperator(self, spaces, wgt, dtype):
        dom = (ift.RGSpace(10), ift.RGSpace(13), ift.GLSpace(5),
182
               ift.HPSpace(4))
183
        op = ift.ContractionOperator(dom, spaces, wgt)
184
185
        ift.extra.consistency_check(op, dtype, dtype)

186
    def testDomainTupleFieldInserter(self):
Philipp Arras's avatar
Philipp Arras committed
187
188
189
        domain = ift.DomainTuple.make((ift.UnstructuredDomain(12),
                                       ift.RGSpace([4, 22])))
        new_space = ift.UnstructuredDomain(7)
190
191
        pos = (5,)
        op = ift.DomainTupleFieldInserter(domain, new_space, 0, pos)
Philipp Arras's avatar
Philipp Arras committed
192
193
        ift.extra.consistency_check(op)

194
195
196
197
198
199
200
    @expand(product([0, 2], [np.float64, np.complex128]))
    def testSymmetrizingOperator(self, space, dtype):
        dom = (ift.LogRGSpace(10, [2.], [1.]), ift.UnstructuredDomain(13),
               ift.LogRGSpace((5, 27), [1., 2.7], [0., 4.]), ift.HPSpace(4))
        op = ift.SymmetrizingOperator(dom, space)
        ift.extra.consistency_check(op, dtype, dtype)

201
    @expand(product([0, 2], [1, 2, 2.7], [np.float64, np.complex128],
202
203
                    [False, True]))
    def testZeroPadder(self, space, factor, dtype, central):
Martin Reinecke's avatar
Martin Reinecke committed
204
        dom = (ift.RGSpace(10), ift.UnstructuredDomain(13), ift.RGSpace(7, 12),
205
               ift.HPSpace(4))
206
207
        newshape = [int(factor*l) for l in dom[space].shape]
        op = ift.FieldZeroPadder(dom, newshape, space, central)
208
        ift.extra.consistency_check(op, dtype, dtype)
Martin Reinecke's avatar
Martin Reinecke committed
209

Martin Reinecke's avatar
Martin Reinecke committed
210
211
212
213
    @expand(product([0, 2], [2, 2.7], [np.float64, np.complex128]))
    def testZeroPadder2(self, space, factor, dtype):
        dom = (ift.RGSpace(10), ift.UnstructuredDomain(13), ift.RGSpace(7, 12),
               ift.HPSpace(4))
214
        newshape = [int(factor*l) for l in dom[space].shape]
Martin Reinecke's avatar
Martin Reinecke committed
215
216
217
        op = ift.CentralZeroPadder(dom, newshape, space)
        ift.extra.consistency_check(op, dtype, dtype)

Martin Reinecke's avatar
Martin Reinecke committed
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
    @expand(product([(ift.RGSpace(10, harmonic=True), 4, 0),
                     (ift.RGSpace((24, 31), distances=(0.4, 2.34),
                                  harmonic=True), (4, 3), 0),
                     ((ift.HPSpace(4), ift.RGSpace(27, distances=0.3,
                                                   harmonic=True)), (10,), 1),
                     (ift.PowerSpace(ift.RGSpace(10, distances=0.3,
                                     harmonic=True)), 6, 0)],
                    [np.float64, np.complex128]))
    def testExpTransform(self, args, dtype):
        op = ift.ExpTransform(args[0], args[1], args[2])
        ift.extra.consistency_check(op, dtype, dtype)

    @expand(product([(ift.LogRGSpace([10, 17], [2., 3.], [1., 0.]), 0),
                     ((ift.LogRGSpace(10, [2.], [1.]),
                       ift.UnstructuredDomain(13)), 0),
                     ((ift.UnstructuredDomain(13),
                       ift.LogRGSpace(17, [3.], [.7])), 1)],
                    [np.float64]))
    def testQHTOperator(self, args, dtype):
Martin Reinecke's avatar
Martin Reinecke committed
237
238
        tgt = ift.DomainTuple.make(args[0])
        op = ift.QHTOperator(tgt, args[1])
Martin Reinecke's avatar
Martin Reinecke committed
239
        ift.extra.consistency_check(op, dtype, dtype)
Philipp Arras's avatar
Philipp Arras committed
240

Martin Reinecke's avatar
Martin Reinecke committed
241
242
243
    @expand([[ift.RGSpace((13, 52, 40)), (4, 6, 25), None],
             [ift.RGSpace((128, 128)), (45, 48), 0],
             [ift.RGSpace(13), (7,), None],
244
             [(ift.HPSpace(3), ift.RGSpace((12, 24), distances=0.3)),
Martin Reinecke's avatar
Martin Reinecke committed
245
246
247
              (12, 12), 1]])
    def testRegridding(self, domain, shape, space):
        op = ift.RegriddingOperator(domain, shape, space)
Philipp Arras's avatar
Philipp Arras committed
248
        ift.extra.consistency_check(op)
Sebastian Hutschenreuter's avatar
Sebastian Hutschenreuter committed
249
250

    @expand(product([ift.DomainTuple.make((ift.RGSpace((3, 5, 4)),
Martin Reinecke's avatar
Martin Reinecke committed
251
252
                                           ift.RGSpace((16,),
                                           distances=(7.,))),),
Sebastian Hutschenreuter's avatar
Sebastian Hutschenreuter committed
253
254
255
                     ift.DomainTuple.make(ift.HPSpace(12),)],
                    [ift.DomainTuple.make((ift.RGSpace((2,)),
                                           ift.GLSpace(10)),),
Martin Reinecke's avatar
Martin Reinecke committed
256
257
                     ift.DomainTuple.make(ift.RGSpace((10, 12),
                                          distances=(0.1, 1.)),)]
Sebastian Hutschenreuter's avatar
Sebastian Hutschenreuter committed
258
259
260
261
262
                    ))
    def testOuter(self, fdomain, domain):
        f = ift.from_random('normal', fdomain)
        op = ift.OuterProduct(f, domain)
        ift.extra.consistency_check(op)