test_representation.py 8.13 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# 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) 2013-2019 Max-Planck-Society
#
# NIFTy is being developed at the Max-Planck-Institut fuer Astrophysik.

import numpy as np
import pytest

import nifty5 as ift

from ..common import list2fixture

_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))
]
_p_spaces = _p_RG_spaces + [ift.HPSpace(17), ift.GLSpace(8, 13)]
_pow_spaces = [ift.PowerSpace(ift.RGSpace((17, 38), harmonic=True))]

pmp = pytest.mark.parametrize
dtype = list2fixture([np.float64, np.complex128])


Martin Reinecke's avatar
Martin Reinecke committed
41
42
43
44
def _check_repr(op):
    rep = op.__repr__()


45
46
47
48
49
50
@pmp('sp', _p_RG_spaces)
def testLOSResponse(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)
Martin Reinecke's avatar
Martin Reinecke committed
51
    _check_repr(ift.LOSResponse(sp, starts, ends, sigma_low, sigma_ups))
52
53
54
55
56
57


@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))
Martin Reinecke's avatar
Martin Reinecke committed
58
59
60
61
62
63
    _check_repr(ift.SandwichOperator.make(a, b))
    _check_repr(a(b))
    _check_repr(a+b)
    _check_repr(a-b)
    _check_repr(a*b)
    _check_repr(a**2)
64
65
66
67
68
69
70


def testLinearInterpolator():
    sp = ift.RGSpace((10, 8), distances=(0.1, 3.5))
    pos = np.random.rand(2, 23)
    pos[0, :] *= 0.9
    pos[1, :] *= 7*3.5
Martin Reinecke's avatar
Martin Reinecke committed
71
    _check_repr(ift.LinearInterpolator(sp, pos))
72
73
74
75
76
77
78
79


@pmp('args', [(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)])
def testSlopeOperator(args, dtype):
    tmp = ift.ExpTransform(ift.PowerSpace(args[0]), args[1], args[2])
    tgt = tmp.domain[0]
Martin Reinecke's avatar
Martin Reinecke committed
80
    _check_repr(ift.SlopeOperator(tgt))
81
82
83
84
85


@pmp('sp', _h_spaces + _p_spaces + _pow_spaces)
def testOperatorAdaptor(sp, dtype):
    op = ift.DiagonalOperator(ift.Field.from_random("normal", sp, dtype=dtype))
Martin Reinecke's avatar
Martin Reinecke committed
86
87
88
    _check_repr(op.adjoint)
    _check_repr(op.inverse)
    _check_repr(op.inverse.adjoint)
89
90
91
92
93
94
95
96
97


@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})
Martin Reinecke's avatar
Martin Reinecke committed
98
99
100
    _check_repr(ift.NullOperator(mdom1, mdom2))
    _check_repr(ift.NullOperator(sp1, mdom2))
    _check_repr(ift.NullOperator(mdom1, sp2))
101
102
103
104


@pmp('sp', _p_RG_spaces)
def testHarmonicSmoothingOperator(sp, dtype):
Martin Reinecke's avatar
Martin Reinecke committed
105
    _check_repr(ift.HarmonicSmoothingOperator(sp, 0.1))
106
107
108
109
110
111
112
113
114


@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
    dofdex = ift.Field.from_global_data(sp, dofdex)
Martin Reinecke's avatar
Martin Reinecke committed
115
    _check_repr(ift.DOFDistributor(dofdex))
116
117
118
119


@pmp('sp', _h_spaces)
def testPPO(sp, dtype):
Martin Reinecke's avatar
Martin Reinecke committed
120
    _check_repr(ift.PowerDistributor(target=sp))
121
122
    ps = ift.PowerSpace(
        sp, ift.PowerSpace.useful_binbounds(sp, logarithmic=False, nbin=3))
Martin Reinecke's avatar
Martin Reinecke committed
123
    _check_repr(ift.PowerDistributor(target=sp, power_space=ps))
124
125
    ps = ift.PowerSpace(
        sp, ift.PowerSpace.useful_binbounds(sp, logarithmic=True, nbin=3))
Martin Reinecke's avatar
Martin Reinecke committed
126
    _check_repr(ift.PowerDistributor(target=sp, power_space=ps))
127
128
129
130


@pmp('sp', _h_RG_spaces + _p_RG_spaces)
def testFFT(sp, dtype):
Martin Reinecke's avatar
Martin Reinecke committed
131
132
    _check_repr(ift.FFTOperator(sp))
    _check_repr(ift.FFTOperator(sp.get_default_codomain()))
133
134
135
136


@pmp('sp', _h_RG_spaces + _p_RG_spaces)
def testHartley(sp, dtype):
Martin Reinecke's avatar
Martin Reinecke committed
137
138
    _check_repr(ift.HartleyOperator(sp))
    _check_repr(ift.HartleyOperator(sp.get_default_codomain()))
139
140
141
142


@pmp('sp', _h_spaces)
def testHarmonic(sp, dtype):
Martin Reinecke's avatar
Martin Reinecke committed
143
    _check_repr(ift.HarmonicTransformOperator(sp))
144
145
146
147
148
149
150
151
152
153


@pmp('sp', _p_spaces)
def testMask(sp, dtype):
    # Create mask
    f = ift.from_random('normal', sp).to_global_data()
    mask = np.zeros_like(f)
    mask[f > 0] = 1
    mask = ift.Field.from_global_data(sp, mask)
    # Test MaskOperator
Martin Reinecke's avatar
Martin Reinecke committed
154
    _check_repr(ift.MaskOperator(mask))
155
156
157
158
159


@pmp('sp', _h_spaces + _p_spaces)
def testDiagonal(sp, dtype):
    op = ift.DiagonalOperator(ift.Field.from_random("normal", sp, dtype=dtype))
Martin Reinecke's avatar
Martin Reinecke committed
160
    _check_repr(op)
161
162
163
164


@pmp('sp', _h_spaces + _p_spaces + _pow_spaces)
def testGeometryRemover(sp, dtype):
Martin Reinecke's avatar
Martin Reinecke committed
165
    _check_repr(ift.GeometryRemover(sp))
166
167
168
169
170
171


@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))
Martin Reinecke's avatar
Martin Reinecke committed
172
    _check_repr(ift.ContractionOperator(dom, spaces, wgt))
173
174
175
176
177
178
179


def testDomainTupleFieldInserter():
    domain = ift.DomainTuple.make((ift.UnstructuredDomain(12),
                                   ift.RGSpace([4, 22])))
    new_space = ift.UnstructuredDomain(7)
    pos = (5,)
Martin Reinecke's avatar
Martin Reinecke committed
180
    _check_repr(ift.DomainTupleFieldInserter(domain, new_space, 0, pos))
181
182
183
184
185
186


@pmp('space', [0, 2])
def testSymmetrizingOperator(space, dtype):
    dom = (ift.LogRGSpace(10, [2.], [1.]), ift.UnstructuredDomain(13),
           ift.LogRGSpace((5, 27), [1., 2.7], [0., 4.]), ift.HPSpace(4))
Martin Reinecke's avatar
Martin Reinecke committed
187
    _check_repr(ift.SymmetrizingOperator(dom, space))
188
189
190
191
192
193
194
195
196


@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]
Martin Reinecke's avatar
Martin Reinecke committed
197
    _check_repr(ift.FieldZeroPadder(dom, newshape, space, central))
198
199
200
201
202
203
204
205
206


@pmp('args',
     [(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)])
def testExpTransform(args, dtype):
Martin Reinecke's avatar
Martin Reinecke committed
207
    _check_repr(ift.ExpTransform(args[0], args[1], args[2]))
208
209
210
211
212
213
214
215
216


@pmp('args',
     [(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)])
def testQHTOperator(args):
    dtype = np.float64
    tgt = ift.DomainTuple.make(args[0])
Martin Reinecke's avatar
Martin Reinecke committed
217
    _check_repr(ift.QHTOperator(tgt, args[1]))
218
219
220
221
222
223
224
225


@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):
Martin Reinecke's avatar
Martin Reinecke committed
226
    _check_repr(ift.RegriddingOperator(*args))
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242


@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)
Martin Reinecke's avatar
Martin Reinecke committed
243
    _check_repr(ift.OuterProduct(f, domain))
244
245
246
247
248
249
250
251
252
253
254
255


@pmp('sp', _h_spaces + _p_spaces + _pow_spaces)
@pmp('seed', [12, 3])
def testValueInserter(sp, seed):
    np.random.seed(seed)
    ind = []
    for ss in sp.shape:
        if ss == 1:
            ind.append(0)
        else:
            ind.append(np.random.randint(0, ss-1))
Martin Reinecke's avatar
Martin Reinecke committed
256
    _check_repr(ift.ValueInserter(sp, ind))