test_representation.py 7.35 KB
Newer Older
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/>.
#
Martin Reinecke's avatar
Martin Reinecke committed
14
# Copyright(C) 2013-2020 Max-Planck-Society
15
16
17
18
19
20
#
# NIFTy is being developed at the Max-Planck-Institut fuer Astrophysik.

import numpy as np
import pytest

Martin Reinecke's avatar
Martin Reinecke committed
21
import nifty7 as ift
22

23
from ..common import list2fixture, setup_function, teardown_function
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40

_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
def _check_repr(op):
Philipp Arras's avatar
Philipp Arras committed
42
    op.__repr__()
Martin Reinecke's avatar
Martin Reinecke committed
43
44


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)
Martin Reinecke's avatar
Martin Reinecke committed
51
    _check_repr(ift.LOSResponse(sp, starts, ends, sigma_low, sigma_ups))
52
53
54
55


@pmp('sp', _h_spaces + _p_spaces + _pow_spaces)
def testOperatorCombinations(sp, dtype):
56
57
    a = ift.DiagonalOperator(ift.Field.from_random(sp, "normal", dtype=dtype))
    b = ift.DiagonalOperator(ift.Field.from_random(sp, "normal", dtype=dtype))
Martin Reinecke's avatar
Martin Reinecke committed
58
59
    _check_repr(ift.SandwichOperator.make(a, b))
    _check_repr(a(b))
Philipp Arras's avatar
Philipp Arras committed
60
61
    _check_repr(a+b)
    _check_repr(a-b)
Martin Reinecke's avatar
Martin Reinecke committed
62
63
    _check_repr(a*b)
    _check_repr(a**2)
64
65
66
67


def testLinearInterpolator():
    sp = ift.RGSpace((10, 8), distances=(0.1, 3.5))
Martin Reinecke's avatar
Martin Reinecke committed
68
    pos = ift.random.current_rng().random((2, 23))
69
70
    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


@pmp('sp', _h_spaces + _p_spaces + _pow_spaces)
def testOperatorAdaptor(sp, dtype):
76
    op = ift.DiagonalOperator(ift.Field.from_random(sp, "normal", dtype=dtype))
Martin Reinecke's avatar
Martin Reinecke committed
77
78
79
    _check_repr(op.adjoint)
    _check_repr(op.inverse)
    _check_repr(op.inverse.adjoint)
80
81
82
83
84
85


@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)
Philipp Arras's avatar
Philipp Arras committed
86
    ift.extra.check_linear_operator(op, dtype, dtype)
87
88
    mdom1 = ift.MultiDomain.make({'a': sp1})
    mdom2 = ift.MultiDomain.make({'b': sp2})
Martin Reinecke's avatar
Martin Reinecke committed
89
90
91
    _check_repr(ift.NullOperator(mdom1, mdom2))
    _check_repr(ift.NullOperator(sp1, mdom2))
    _check_repr(ift.NullOperator(mdom1, sp2))
92
93
94
95


@pmp('sp', _p_RG_spaces)
def testHarmonicSmoothingOperator(sp, dtype):
Martin Reinecke's avatar
Martin Reinecke committed
96
    _check_repr(ift.HarmonicSmoothingOperator(sp, 0.1))
97
98
99
100
101
102
103
104


@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
105
    dofdex = ift.Field.from_raw(sp, dofdex)
Martin Reinecke's avatar
Martin Reinecke committed
106
    _check_repr(ift.DOFDistributor(dofdex))
107
108
109
110


@pmp('sp', _h_spaces)
def testPPO(sp, dtype):
Martin Reinecke's avatar
Martin Reinecke committed
111
    _check_repr(ift.PowerDistributor(target=sp))
112
113
    ps = ift.PowerSpace(
        sp, ift.PowerSpace.useful_binbounds(sp, logarithmic=False, nbin=3))
Martin Reinecke's avatar
Martin Reinecke committed
114
    _check_repr(ift.PowerDistributor(target=sp, power_space=ps))
115
116
    ps = ift.PowerSpace(
        sp, ift.PowerSpace.useful_binbounds(sp, logarithmic=True, nbin=3))
Martin Reinecke's avatar
Martin Reinecke committed
117
    _check_repr(ift.PowerDistributor(target=sp, power_space=ps))
118
119
120
121


@pmp('sp', _h_RG_spaces + _p_RG_spaces)
def testFFT(sp, dtype):
Martin Reinecke's avatar
Martin Reinecke committed
122
123
    _check_repr(ift.FFTOperator(sp))
    _check_repr(ift.FFTOperator(sp.get_default_codomain()))
124
125
126
127


@pmp('sp', _h_RG_spaces + _p_RG_spaces)
def testHartley(sp, dtype):
Martin Reinecke's avatar
Martin Reinecke committed
128
129
    _check_repr(ift.HartleyOperator(sp))
    _check_repr(ift.HartleyOperator(sp.get_default_codomain()))
130
131
132
133


@pmp('sp', _h_spaces)
def testHarmonic(sp, dtype):
Martin Reinecke's avatar
Martin Reinecke committed
134
    _check_repr(ift.HarmonicTransformOperator(sp))
135
136
137
138
139


@pmp('sp', _p_spaces)
def testMask(sp, dtype):
    # Create mask
140
    f = ift.from_random(sp, 'normal').val
141
142
    mask = np.zeros_like(f)
    mask[f > 0] = 1
Martin Reinecke's avatar
Martin Reinecke committed
143
    mask = ift.Field.from_raw(sp, mask)
144
    # Test MaskOperator
Martin Reinecke's avatar
Martin Reinecke committed
145
    _check_repr(ift.MaskOperator(mask))
146
147
148
149


@pmp('sp', _h_spaces + _p_spaces)
def testDiagonal(sp, dtype):
150
    op = ift.DiagonalOperator(ift.Field.from_random(sp, "normal", dtype=dtype))
Martin Reinecke's avatar
Martin Reinecke committed
151
    _check_repr(op)
152
153
154
155


@pmp('sp', _h_spaces + _p_spaces + _pow_spaces)
def testGeometryRemover(sp, dtype):
Martin Reinecke's avatar
Martin Reinecke committed
156
    _check_repr(ift.GeometryRemover(sp))
157
158
159
160
161
162


@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
163
    _check_repr(ift.ContractionOperator(dom, spaces, wgt))
164
165
166


def testDomainTupleFieldInserter():
Martin Reinecke's avatar
fix    
Martin Reinecke committed
167
168
    target = ift.DomainTuple.make((ift.UnstructuredDomain([3, 2]),
                                   ift.UnstructuredDomain(7),
169
                                   ift.RGSpace([4, 22])))
Martin Reinecke's avatar
fix    
Martin Reinecke committed
170
    _check_repr(ift.DomainTupleFieldInserter(target, 1, (5,)))
171
172
173
174
175
176
177
178
179


@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
180
    _check_repr(ift.FieldZeroPadder(dom, newshape, space, central))
181
182
183
184
185
186
187
188


@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
189
    _check_repr(ift.RegriddingOperator(*args))
190
191


Philipp Arras's avatar
Philipp Arras committed
192
193
194
195
196
@pmp('fdomain', [
    ift.DomainTuple.make((ift.RGSpace(
        (3, 5, 4)), ift.RGSpace((16,), distances=(7.,))),),
    ift.DomainTuple.make(ift.HPSpace(12),)
])
197
198
199
200
201
@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):
202
203
    f = ift.from_random(fdomain, 'normal')
    _check_repr(ift.OuterProduct(domain, f))
204
205
206
207
208


@pmp('sp', _h_spaces + _p_spaces + _pow_spaces)
@pmp('seed', [12, 3])
def testValueInserter(sp, seed):
Martin Reinecke's avatar
Martin Reinecke committed
209
210
211
212
213
214
215
216
    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)))
        _check_repr(ift.ValueInserter(sp, ind))
Philipp Arras's avatar
Philipp Arras committed
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234


@pmp('dom', _h_spaces + _p_spaces)
def testGaussianEnergy(dom):
    _check_repr(ift.GaussianEnergy(domain=dom))


@pmp('dom', _h_spaces + _p_spaces)
def testHamiltonian(dom):
    lh = ift.GaussianEnergy(domain=dom)
    _check_repr(ift.StandardHamiltonian(lh))


@pmp('dom', _h_RG_spaces + _p_RG_spaces)
def testSumOperator(dom):
    op0 = ift.FFTOperator(dom)
    op1 = op0.scale(2)
    _check_repr(op0+op1)