test_adjoint.py 11.8 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

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

Martin Reinecke's avatar
5->6    
Martin Reinecke committed
21
import nifty6 as ift
22

23
from ..common import list2fixture, setup_function, teardown_function
Martin Reinecke's avatar
Martin Reinecke committed
24

Philipp Arras's avatar
Philipp Arras committed
25
26
27
28
29
30
31
32
33
_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)]
_p_RG_spaces = [
    ift.RGSpace(19, distances=0.7),
    ift.RGSpace((1, 2, 3, 6), distances=(0.2, 0.25, 0.34, .8))
]
34
_p_spaces = _p_RG_spaces + [ift.HPSpace(17), ift.GLSpace(8, 13)]
35
36
37
38
39
_pow_spaces = [
    ift.PowerSpace(ift.RGSpace((17, 38), (0.99, 1340), harmonic=True)),
    ift.PowerSpace(ift.LMSpace(18),
                   ift.PowerSpace.useful_binbounds(ift.LMSpace(18), False))
]
40

Philipp Arras's avatar
Philipp Arras committed
41
42
43
pmp = pytest.mark.parametrize
dtype = list2fixture([np.float64, np.complex128])

Martin Reinecke's avatar
Martin Reinecke committed
44

Philipp Arras's avatar
Philipp Arras committed
45
46
@pmp('sp', _p_RG_spaces)
def testLOSResponse(sp, dtype):
Martin Reinecke's avatar
Martin Reinecke committed
47
48
49
50
    starts = ift.random.current_rng().standard_normal((len(sp.shape), 10))
    ends = ift.random.current_rng().standard_normal((len(sp.shape), 10))
    sigma_low = 1e-4*ift.random.current_rng().standard_normal(10)
    sigma_ups = 1e-5*ift.random.current_rng().standard_normal(10)
Philipp Arras's avatar
Philipp Arras committed
51
52
53
54
55
56
57
58
59
60
61
62
63
64
    op = ift.LOSResponse(sp, starts, ends, sigma_low, sigma_ups)
    ift.extra.consistency_check(op, dtype, dtype)


@pmp('sp', _h_spaces + _p_spaces + _pow_spaces)
def testOperatorCombinations(sp, dtype):
    a = ift.DiagonalOperator(ift.Field.from_random("normal", sp, dtype=dtype))
    b = ift.DiagonalOperator(ift.Field.from_random("normal", sp, dtype=dtype))
    op = ift.SandwichOperator.make(a, b)
    ift.extra.consistency_check(op, dtype, dtype)
    op = a(b)
    ift.extra.consistency_check(op, dtype, dtype)
    op = a + b
    ift.extra.consistency_check(op, dtype, dtype)
65
66
    op = a - b
    ift.extra.consistency_check(op, dtype, dtype)
Philipp Arras's avatar
Philipp Arras committed
67
68
69
70


def testLinearInterpolator():
    sp = ift.RGSpace((10, 8), distances=(0.1, 3.5))
Martin Reinecke's avatar
Martin Reinecke committed
71
    pos = ift.random.current_rng().random((2, 23))
Philipp Arras's avatar
Philipp Arras committed
72
73
74
75
76
77
    pos[0, :] *= 0.9
    pos[1, :] *= 7*3.5
    op = ift.LinearInterpolator(sp, pos)
    ift.extra.consistency_check(op)


78
79
80
81
@pmp('sp', _h_spaces + _p_spaces + _pow_spaces)
def testRealizer(sp):
    op = ift.Realizer(sp)
    ift.extra.consistency_check(op, np.complex128, np.float64,
Martin Reinecke's avatar
Martin Reinecke committed
82
                                only_r_linear=True)
83
84


Philipp Arras's avatar
Philipp Arras committed
85
86
87
88
89
90
91
@pmp('sp', _h_spaces + _p_spaces + _pow_spaces)
def testImaginizer(sp):
    op = ift.Imaginizer(sp)
    ift.extra.consistency_check(op, np.complex128, np.float64,
                                only_r_linear=True)


92
93
94
95
@pmp('sp', _h_spaces + _p_spaces + _pow_spaces)
def testConjugationOperator(sp):
    op = ift.ConjugationOperator(sp)
    ift.extra.consistency_check(op, np.complex128, np.complex128,
Martin Reinecke's avatar
Martin Reinecke committed
96
                                only_r_linear=True)
97
98


Philipp Arras's avatar
Philipp Arras committed
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
@pmp('sp', _h_spaces + _p_spaces + _pow_spaces)
def testOperatorAdaptor(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)


@pmp('sp1', _h_spaces + _p_spaces + _pow_spaces)
@pmp('sp2', _h_spaces + _p_spaces + _pow_spaces)
def testNullOperator(sp1, sp2, dtype):
    op = ift.NullOperator(sp1, sp2)
    ift.extra.consistency_check(op, dtype, dtype)
    mdom1 = ift.MultiDomain.make({'a': sp1})
    mdom2 = ift.MultiDomain.make({'b': sp2})
    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)


@pmp('sp', _p_RG_spaces)
def testHarmonicSmoothingOperator(sp, dtype):
    op = ift.HarmonicSmoothingOperator(sp, 0.1)
    ift.extra.consistency_check(op, dtype, dtype)


@pmp('sp', _h_spaces + _p_spaces + _pow_spaces)
def testDOFDistributor(sp, dtype):
    # TODO: Test for DomainTuple
    if sp.size < 4:
        return
    dofdex = np.arange(sp.size).reshape(sp.shape) % 3
Martin Reinecke's avatar
Martin Reinecke committed
135
    dofdex = ift.Field.from_raw(sp, dofdex)
Philipp Arras's avatar
Philipp Arras committed
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
    op = ift.DOFDistributor(dofdex)
    ift.extra.consistency_check(op, dtype, dtype)


@pmp('sp', _h_spaces)
def testPPO(sp, dtype):
    op = ift.PowerDistributor(target=sp)
    ift.extra.consistency_check(op, dtype, dtype)
    ps = ift.PowerSpace(
        sp, ift.PowerSpace.useful_binbounds(sp, logarithmic=False, nbin=3))
    op = ift.PowerDistributor(target=sp, power_space=ps)
    ift.extra.consistency_check(op, dtype, dtype)
    ps = ift.PowerSpace(
        sp, ift.PowerSpace.useful_binbounds(sp, logarithmic=True, nbin=3))
    op = ift.PowerDistributor(target=sp, power_space=ps)
    ift.extra.consistency_check(op, dtype, dtype)


@pmp('sp', _h_RG_spaces + _p_RG_spaces)
def testFFT(sp, dtype):
    op = ift.FFTOperator(sp)
    ift.extra.consistency_check(op, dtype, dtype)
    op = ift.FFTOperator(sp.get_default_codomain())
    ift.extra.consistency_check(op, dtype, dtype)


@pmp('sp', _h_RG_spaces + _p_RG_spaces)
def testHartley(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)


@pmp('sp', _h_spaces)
def testHarmonic(sp, dtype):
    op = ift.HarmonicTransformOperator(sp)
    ift.extra.consistency_check(op, dtype, dtype)


@pmp('sp', _p_spaces)
def testMask(sp, dtype):
    # Create mask
Martin Reinecke's avatar
stage 3    
Martin Reinecke committed
179
    f = ift.from_random('normal', sp).val
Philipp Arras's avatar
Philipp Arras committed
180
181
    mask = np.zeros_like(f)
    mask[f > 0] = 1
Martin Reinecke's avatar
Martin Reinecke committed
182
    mask = ift.Field.from_raw(sp, mask)
Philipp Arras's avatar
Philipp Arras committed
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
    # Test MaskOperator
    op = ift.MaskOperator(mask)
    ift.extra.consistency_check(op, dtype, dtype)


@pmp('sp', _h_spaces + _p_spaces)
def testDiagonal(sp, dtype):
    op = ift.DiagonalOperator(ift.Field.from_random("normal", sp, dtype=dtype))
    ift.extra.consistency_check(op, dtype, dtype)


@pmp('sp', _h_spaces + _p_spaces + _pow_spaces)
def testGeometryRemover(sp, dtype):
    op = ift.GeometryRemover(sp)
    ift.extra.consistency_check(op, dtype, dtype)


@pmp('spaces', [0, 1, 2, 3, (0, 1), (0, 2), (0, 1, 2), (0, 2, 3), (1, 3)])
@pmp('wgt', [0, 1, 2, -1])
def testContractionOperator(spaces, wgt, dtype):
    dom = (ift.RGSpace(10), ift.RGSpace(13), ift.GLSpace(5), ift.HPSpace(4))
    op = ift.ContractionOperator(dom, spaces, wgt)
    ift.extra.consistency_check(op, dtype, dtype)


def testDomainTupleFieldInserter():
209
210
    target = ift.DomainTuple.make((ift.UnstructuredDomain([3, 2]),
                                   ift.UnstructuredDomain(7),
Philipp Arras's avatar
Philipp Arras committed
211
                                   ift.RGSpace([4, 22])))
212
    op = ift.DomainTupleFieldInserter(target, 1, (5,))
Philipp Arras's avatar
Philipp Arras committed
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
    ift.extra.consistency_check(op)


@pmp('space', [0, 2])
@pmp('factor', [1, 2, 2.7])
@pmp('central', [False, True])
def testZeroPadder(space, factor, dtype, central):
    dom = (ift.RGSpace(10), ift.UnstructuredDomain(13), ift.RGSpace(7, 12),
           ift.HPSpace(4))
    newshape = [int(factor*l) for l in dom[space].shape]
    op = ift.FieldZeroPadder(dom, newshape, space, central)
    ift.extra.consistency_check(op, dtype, dtype)


@pmp('args', [[ift.RGSpace(
    (13, 52, 40)), (4, 6, 25), None], [ift.RGSpace(
        (128, 128)), (45, 48), 0], [ift.RGSpace(13), (7,), None], [
            (ift.HPSpace(3), ift.RGSpace((12, 24), distances=0.3)), (12, 12), 1
        ]])
def testRegridding(args):
    op = ift.RegriddingOperator(*args)
    ift.extra.consistency_check(op)

Martin Reinecke's avatar
Martin Reinecke committed
236

Philipp Arras's avatar
Philipp Arras committed
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
@pmp(
    'fdomain',
    [
        ift.DomainTuple.make((ift.RGSpace(
            (3, 5, 4)), ift.RGSpace((16,), distances=(7.,))),),
        ift.DomainTuple.make(ift.HPSpace(12),)
    ],
)
@pmp('domain', [
    ift.DomainTuple.make((ift.RGSpace((2,)), ift.GLSpace(10)),),
    ift.DomainTuple.make(ift.RGSpace((10, 12), distances=(0.1, 1.)),)
])
def testOuter(fdomain, domain):
    f = ift.from_random('normal', fdomain)
    op = ift.OuterProduct(f, domain)
    ift.extra.consistency_check(op)
Philipp Arras's avatar
Philipp Arras committed
253
254
255


@pmp('sp', _h_spaces + _p_spaces + _pow_spaces)
Philipp Arras's avatar
Philipp Arras committed
256
257
@pmp('seed', [12, 3])
def testValueInserter(sp, seed):
Martin Reinecke's avatar
Martin Reinecke committed
258
259
260
261
262
263
264
265
266
    with ift.random.Context(seed):
        ind = []
        for ss in sp.shape:
            if ss == 1:
                ind.append(0)
            else:
                ind.append(int(ift.random.current_rng().integers(0, ss-1)))
        op = ift.ValueInserter(sp, ind)
        ift.extra.consistency_check(op)
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284


@pmp('sp', _pow_spaces)
def testSlopeRemover(sp):
    op = ift.library.correlated_fields._SlopeRemover(sp)
    ift.extra.consistency_check(op)


@pmp('sp', _pow_spaces)
def testTwoLogIntegrations(sp):
    op = ift.library.correlated_fields._TwoLogIntegrations(sp)
    ift.extra.consistency_check(op)


@pmp('sp', _h_spaces + _p_spaces + _pow_spaces)
def testSpecialSum(sp):
    op = ift.library.correlated_fields._SpecialSum(sp)
    ift.extra.consistency_check(op)
285

286
def metatestMatrixProductOperator(sp, mat_shape, seed, **kwargs):
Martin Reinecke's avatar
Martin Reinecke committed
287
288
289
290
291
292
293
    with ift.random.Context(seed):
        mat = ift.random.current_rng().standard_normal(mat_shape)
        op = ift.MatrixProductOperator(sp, mat, **kwargs)
        ift.extra.consistency_check(op)
        mat = mat + 1j*ift.random.current_rng().standard_normal(mat_shape)
        op = ift.MatrixProductOperator(sp, mat, **kwargs)
        ift.extra.consistency_check(op)
Philipp Arras's avatar
Philipp Arras committed
294

295
296
297
298
299
300
@pmp('sp', [ift.RGSpace(10)])
@pmp('spaces', [None, (0,)])
@pmp('seed', [12, 3])
def testMatrixProductOperator_1d(sp, spaces, seed):
    mat_shape = sp.shape * 2
    metatestMatrixProductOperator(sp, mat_shape, seed, spaces=spaces)
Philipp Arras's avatar
Philipp Arras committed
301

302
303
@pmp('sp', [ift.DomainTuple.make((ift.RGSpace((2)), ift.RGSpace((10))))])
@pmp('spaces', [(0,), (1,), (0, 1)])
304
@pmp('seed', [12, 3])
305
306
307
308
309
310
311
def testMatrixProductOperator_2d_spaces(sp, spaces, seed):
    appl_shape = []
    for sp_idx in spaces:
        appl_shape += sp[sp_idx].shape
    appl_shape = tuple(appl_shape)
    mat_shape = appl_shape * 2
    metatestMatrixProductOperator(sp, mat_shape, seed, spaces=spaces)
312

313
314
315
316
317
318
@pmp('sp', [ift.RGSpace((2, 10))])
@pmp('seed', [12, 3])
def testMatrixProductOperator_2d_flatten(sp, seed):
    appl_shape = (ift.utilities.my_product(sp.shape),)
    mat_shape = appl_shape * 2
    metatestMatrixProductOperator(sp, mat_shape, seed, flatten=True)
319

Philipp Arras's avatar
Philipp Arras committed
320
321
@pmp('seed', [12, 3])
def testPartialExtractor(seed):
Martin Reinecke's avatar
Martin Reinecke committed
322
323
324
325
326
327
328
329
    with ift.random.Context(seed):
        tgt = {'a': ift.RGSpace(1), 'b': ift.RGSpace(2)}
        dom = tgt.copy()
        dom['c'] = ift.RGSpace(3)
        dom = ift.MultiDomain.make(dom)
        tgt = ift.MultiDomain.make(tgt)
        op = ift.PartialExtractor(dom, tgt)
        ift.extra.consistency_check(op)
Philipp Arras's avatar
Philipp Arras committed
330
331
332
333
334
335

@pmp('seed', [12, 3])
def testSlowFieldAdapter(seed):
    dom = {'a': ift.RGSpace(1), 'b': ift.RGSpace(2)}
    op = ift.operators.simple_linear_operators._SlowFieldAdapter(dom, 'a')
    ift.extra.consistency_check(op)
Lukas Platz's avatar
Lukas Platz committed
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352

@pmp('sp1', [0, 2])
@pmp('sp2', [1])
@pmp('seed', [12, 3])
def testSwitchSpacesOperator(sp1, sp2, seed):
    with ift.random.Context(seed):
        dom1 = ift.RGSpace(1)
        dom2 = ift.RGSpace((2, 2))
        dom3 = ift.RGSpace(3)
        dom = ift.DomainTuple.make([dom1, dom2, dom3])
        op = ift.SwitchSpacesOperator(dom, sp1, sp2)

        tgt = list(dom)
        tgt[sp1] = dom[sp2]
        tgt[sp2] = dom[sp1]
        assert op.target == ift.DomainTuple.make(tgt)
        ift.extra.consistency_check(op)