test_map.py 12 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
18
19
20
21
22
23
24
25
26
27
# 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-2017 Max-Planck-Society
#
# NIFTy is being developed at the Max-Planck-Institut fuer Astrophysik
# and financially supported by the Studienstiftung des deutschen Volkes.

import unittest
import nifty4 as ift
import numpy as np
from itertools import product
from test.common import expand
from numpy.testing import assert_allclose


# TODO Add also other space types
28
# TODO Set tolerances and eps to reasonable values
Philipp Arras's avatar
Philipp Arras committed
29
30


31
class Energy_Tests(unittest.TestCase):
Philipp Arras's avatar
Philipp Arras committed
32
    @expand(product([ift.RGSpace(64, distances=.789),
33
34
35
36
                     ift.RGSpace([32, 32], distances=.789)],
                    [4, 78, 23]))
    def testLinearMap(self, space, seed):
        np.random.seed(seed)
Philipp Arras's avatar
Philipp Arras committed
37
        dim = len(space.shape)
38
39
        hspace = space.get_default_codomain()
        ht = ift.HarmonicTransformOperator(hspace, target=space)
Philipp Arras's avatar
Philipp Arras committed
40
41
42
43
44
45
46
47
48
        binbounds = ift.PowerSpace.useful_binbounds(hspace, logarithmic=False)
        pspace = ift.PowerSpace(hspace, binbounds=binbounds)
        P = ift.PowerProjectionOperator(domain=hspace, power_space=pspace)
        xi0 = ift.Field.from_random(domain=hspace, random_type='normal')

        def pspec(k): return 1 / (1 + k**2)**dim
        pspec = ift.PS_field(pspace, pspec)
        A = P.adjoint_times(ift.sqrt(pspec))
        n = ift.Field.from_random(domain=space, random_type='normal')
49
        s0 = xi0 * A
Martin Reinecke's avatar
Martin Reinecke committed
50
        Instrument = ift.ScalingOperator(10., space)
51
        R = Instrument * ht
Martin Reinecke's avatar
Martin Reinecke committed
52
        N = ift.ScalingOperator(1., space)
53
        d = R(s0) + n
Philipp Arras's avatar
Philipp Arras committed
54
55
56

        direction = ift.Field.from_random('normal', hspace)
        direction /= np.sqrt(direction.var())
57
        eps = 1e-7
58
59
60
61
62
63
        s1 = s0 + eps * direction

        IC = ift.GradientNormController(
            iteration_limit=100,
            tol_abs_gradnorm=1e-5)
        inverter = ift.ConjugateGradient(IC)
Philipp Arras's avatar
Philipp Arras committed
64
65

        S = ift.create_power_operator(hspace, power_spectrum=lambda k: 1.)
66
67
68
69
        energy0 = ift.library.WienerFilterEnergy(
            position=s0, d=d, R=R, N=N, S=S, inverter=inverter)
        energy1 = ift.library.WienerFilterEnergy(
            position=s1, d=d, R=R, N=N, S=S, inverter=inverter)
Philipp Arras's avatar
Philipp Arras committed
70
71
72

        a = (energy1.value - energy0.value) / eps
        b = energy0.gradient.vdot(direction)
73
        tol = 1e-5
Philipp Arras's avatar
Philipp Arras committed
74
75
76
        assert_allclose(a, b, rtol=tol, atol=tol)

    @expand(product([ift.RGSpace(64, distances=.789),
77
                     ift.RGSpace([32, 32], distances=.789)],
Philipp Arras's avatar
Philipp Arras committed
78
                    [4, 78, 23]))
79
80
    def testLognormalMap(self, space, seed):
        np.random.seed(seed)
Philipp Arras's avatar
Philipp Arras committed
81
82
83
84
85
86
87
88
89
90
91
92
        dim = len(space.shape)
        hspace = space.get_default_codomain()
        ht = ift.HarmonicTransformOperator(hspace, target=space)
        binbounds = ift.PowerSpace.useful_binbounds(hspace, logarithmic=False)
        pspace = ift.PowerSpace(hspace, binbounds=binbounds)
        P = ift.PowerProjectionOperator(domain=hspace, power_space=pspace)
        xi0 = ift.Field.from_random(domain=hspace, random_type='normal')

        def pspec(k): return 1 / (1 + k**2)**dim
        pspec = ift.PS_field(pspace, pspec)
        A = P.adjoint_times(ift.sqrt(pspec))
        n = ift.Field.from_random(domain=space, random_type='normal')
93
94
        sh0 = xi0 * A
        s = ht(sh0)
Martin Reinecke's avatar
Martin Reinecke committed
95
        Instrument = ift.ScalingOperator(10., space)
Philipp Arras's avatar
Philipp Arras committed
96
        R = Instrument * ht
Martin Reinecke's avatar
Martin Reinecke committed
97
        N = ift.ScalingOperator(1., space)
98
        d = Instrument(ift.exp(s)) + n
Philipp Arras's avatar
Philipp Arras committed
99
100
101

        direction = ift.Field.from_random('normal', hspace)
        direction /= np.sqrt(direction.var())
102
        eps = 1e-6
103
        sh1 = sh0 + eps * direction
Philipp Arras's avatar
Philipp Arras committed
104
105
106
107
108
109
110

        IC = ift.GradientNormController(
            iteration_limit=100,
            tol_abs_gradnorm=1e-5)
        inverter = ift.ConjugateGradient(IC)

        S = ift.create_power_operator(hspace, power_spectrum=lambda k: 1.)
111
112
113
114
        energy0 = ift.library.LogNormalWienerFilterEnergy(
            position=sh0, d=d, R=R, N=N, S=S, inverter=inverter)
        energy1 = ift.library.LogNormalWienerFilterEnergy(
            position=sh1, d=d, R=R, N=N, S=S, inverter=inverter)
Philipp Arras's avatar
Philipp Arras committed
115
116
117

        a = (energy1.value - energy0.value) / eps
        b = energy0.gradient.vdot(direction)
118
        tol = 1e-3
119
120
121
122
        assert_allclose(a, b, rtol=tol, atol=tol)

    @expand(product([ift.RGSpace(64, distances=.789),
                     ift.RGSpace([32, 32], distances=.789)],
123
                    [ift.library.Exponential, ift.library.Linear],
Philipp Arras's avatar
Philipp Arras committed
124
                    [4, 78, 23]))
125
126
    def testNonlinearMap(self, space, nonlinearity, seed):
        np.random.seed(seed)
127
128
        f = nonlinearity()
        dim = len(space.shape)
129
130
        hspace = space.get_default_codomain()
        ht = ift.HarmonicTransformOperator(hspace, target=space)
131
132
133
134
135
136
137
138
139
        binbounds = ift.PowerSpace.useful_binbounds(hspace, logarithmic=False)
        pspace = ift.PowerSpace(hspace, binbounds=binbounds)
        P = ift.PowerProjectionOperator(domain=hspace, power_space=pspace)
        xi0 = ift.Field.from_random(domain=hspace, random_type='normal')

        def pspec(k): return 1 / (1 + k**2)**dim
        pspec = ift.PS_field(pspace, pspec)
        A = P.adjoint_times(ift.sqrt(pspec))
        n = ift.Field.from_random(domain=space, random_type='normal')
140
        s = ht(xi0 * A)
Martin Reinecke's avatar
Martin Reinecke committed
141
142
        R = ift.ScalingOperator(10., space)
        N = ift.ScalingOperator(1., space)
143
144
145
146
        d = R(f(s)) + n

        direction = ift.Field.from_random('normal', hspace)
        direction /= np.sqrt(direction.var())
147
        eps = 1e-7
148
149
150
151
        xi1 = xi0 + eps * direction

        S = ift.create_power_operator(hspace, power_spectrum=lambda k: 1.)
        energy0 = ift.library.NonlinearWienerFilterEnergy(
152
            position=xi0, d=d, Instrument=R, nonlinearity=f, ht=ht, power=A, N=N, S=S)
153
        energy1 = ift.library.NonlinearWienerFilterEnergy(
154
            position=xi1, d=d, Instrument=R, nonlinearity=f, ht=ht, power=A, N=N, S=S)
155
156
157

        a = (energy1.value - energy0.value) / eps
        b = energy0.gradient.vdot(direction)
158
        tol = 1e-4
Philipp Arras's avatar
Philipp Arras committed
159
        assert_allclose(a, b, rtol=tol, atol=tol)
Philipp Arras's avatar
Philipp Arras committed
160

161
162

class Curvature_Tests(unittest.TestCase):
Philipp Arras's avatar
Philipp Arras committed
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
    @expand(product([ift.RGSpace(64, distances=.789),
                     ift.RGSpace([32, 32], distances=.789)],
                    [4, 78, 23]))
    def testLinearMapCurvature(self, space, seed):
        np.random.seed(seed)
        dim = len(space.shape)
        hspace = space.get_default_codomain()
        ht = ift.HarmonicTransformOperator(hspace, target=space)
        binbounds = ift.PowerSpace.useful_binbounds(hspace, logarithmic=False)
        pspace = ift.PowerSpace(hspace, binbounds=binbounds)
        P = ift.PowerProjectionOperator(domain=hspace, power_space=pspace)
        xi0 = ift.Field.from_random(domain=hspace, random_type='normal')

        def pspec(k): return 1 / (1 + k**2)**dim
        pspec = ift.PS_field(pspace, pspec)
        A = P.adjoint_times(ift.sqrt(pspec))
        n = ift.Field.from_random(domain=space, random_type='normal')
        s0 = xi0 * A
Martin Reinecke's avatar
Martin Reinecke committed
181
        Instrument = ift.ScalingOperator(10., space)
Philipp Arras's avatar
Philipp Arras committed
182
        R = Instrument * ht
Martin Reinecke's avatar
Martin Reinecke committed
183
        N = ift.ScalingOperator(1., space)
Philipp Arras's avatar
Philipp Arras committed
184
185
186
187
188
189
190
191
192
193
194
195
196
        d = R(s0) + n

        direction = ift.Field.from_random('normal', hspace)
        direction /= np.sqrt(direction.var())
        eps = 1e-7
        s1 = s0 + eps * direction

        IC = ift.GradientNormController(
            iteration_limit=100,
            tol_abs_gradnorm=1e-5)
        inverter = ift.ConjugateGradient(IC)

        S = ift.create_power_operator(hspace, power_spectrum=lambda k: 1.)
Philipp Arras's avatar
Philipp Arras committed
197
198
        energy0 = ift.library.WienerFilterEnergy(
            position=s0, d=d, R=R, N=N, S=S, inverter=inverter)
Philipp Arras's avatar
Philipp Arras committed
199
200
201
202
203
204
205
206
207
208
        gradient0 = energy0.gradient
        gradient1 = energy0.at(s1).gradient

        a = (gradient1 - gradient0) / eps
        b = energy0.curvature(direction)
        tol = 1e-7
        assert_allclose(a.val, b.val, rtol=tol, atol=tol)

    @expand(product([ift.RGSpace(64, distances=.789),
                     ift.RGSpace([32, 32], distances=.789)],
Philipp Arras's avatar
Philipp Arras committed
209
                    [4, 78, 23]))
Philipp Arras's avatar
Philipp Arras committed
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
    def testLognormalMapCurvature(self, space, seed):
        np.random.seed(seed)
        dim = len(space.shape)
        hspace = space.get_default_codomain()
        ht = ift.HarmonicTransformOperator(hspace, target=space)
        binbounds = ift.PowerSpace.useful_binbounds(hspace, logarithmic=False)
        pspace = ift.PowerSpace(hspace, binbounds=binbounds)
        P = ift.PowerProjectionOperator(domain=hspace, power_space=pspace)
        xi0 = ift.Field.from_random(domain=hspace, random_type='normal')

        def pspec(k): return 1 / (1 + k**2)**dim
        pspec = ift.PS_field(pspace, pspec)
        A = P.adjoint_times(ift.sqrt(pspec))
        n = ift.Field.from_random(domain=space, random_type='normal')
        sh0 = xi0 * A
        s = ht(sh0)
Martin Reinecke's avatar
Martin Reinecke committed
226
        Instrument = ift.ScalingOperator(10., space)
Philipp Arras's avatar
Philipp Arras committed
227
        R = Instrument * ht
Martin Reinecke's avatar
Martin Reinecke committed
228
        N = ift.ScalingOperator(1., space)
Philipp Arras's avatar
Philipp Arras committed
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
        d = Instrument(ift.exp(s)) + n

        direction = ift.Field.from_random('normal', hspace)
        direction /= np.sqrt(direction.var())
        eps = 1e-7
        sh1 = sh0 + eps * direction

        IC = ift.GradientNormController(
            iteration_limit=100,
            tol_abs_gradnorm=1e-5)
        inverter = ift.ConjugateGradient(IC)

        S = ift.create_power_operator(hspace, power_spectrum=lambda k: 1.)

        energy0 = ift.library.LogNormalWienerFilterEnergy(
            position=sh0, d=d, R=R, N=N, S=S, inverter=inverter)
        gradient0 = energy0.gradient
        gradient1 = energy0.at(sh1).gradient

        a = (gradient1 - gradient0) / eps
        b = energy0.curvature(direction)
        tol = 1e-3
        assert_allclose(a.val, b.val, rtol=tol, atol=tol)

    @expand(product([ift.RGSpace(64, distances=.789),
                     ift.RGSpace([32, 32], distances=.789)],
                    [ift.library.Exponential, ift.library.Linear],
Philipp Arras's avatar
Philipp Arras committed
256
                    [4, 78, 23]))
Philipp Arras's avatar
Philipp Arras committed
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
    def testNonlinearMapCurvature(self, space, nonlinearity, seed):
        np.random.seed(seed)
        f = nonlinearity()
        dim = len(space.shape)
        hspace = space.get_default_codomain()
        ht = ift.HarmonicTransformOperator(hspace, target=space)
        binbounds = ift.PowerSpace.useful_binbounds(hspace, logarithmic=False)
        pspace = ift.PowerSpace(hspace, binbounds=binbounds)
        P = ift.PowerProjectionOperator(domain=hspace, power_space=pspace)
        xi0 = ift.Field.from_random(domain=hspace, random_type='normal')

        def pspec(k): return 1 / (1 + k**2)**dim
        pspec = ift.PS_field(pspace, pspec)
        A = P.adjoint_times(ift.sqrt(pspec))
        n = ift.Field.from_random(domain=space, random_type='normal')
        s = ht(xi0 * A)
Martin Reinecke's avatar
Martin Reinecke committed
273
274
        R = ift.ScalingOperator(10., space)
        N = ift.ScalingOperator(1., space)
Philipp Arras's avatar
Philipp Arras committed
275
276
277
278
279
280
281
282
283
284
285
286
287
288
        d = R(f(s)) + n

        direction = ift.Field.from_random('normal', hspace)
        direction /= np.sqrt(direction.var())
        eps = 1e-7
        xi1 = xi0 + eps * direction

        S = ift.create_power_operator(hspace, power_spectrum=lambda k: 1.)

        IC = ift.GradientNormController(
            iteration_limit=500,
            tol_abs_gradnorm=1e-7)
        inverter = ift.ConjugateGradient(IC)
        energy0 = ift.library.NonlinearWienerFilterEnergy(
Philipp Arras's avatar
Philipp Arras committed
289
290
291
292
293
294
295
296
297
            position=xi0,
            d=d,
            Instrument=R,
            nonlinearity=f,
            ht=ht,
            power=A,
            N=N,
            S=S,
            inverter=inverter)
Philipp Arras's avatar
Philipp Arras committed
298
299
300
301
302
        gradient0 = energy0.gradient
        gradient1 = energy0.at(xi1).gradient

        a = (gradient1 - gradient0) / eps
        b = energy0.curvature(direction)
Philipp Arras's avatar
Philipp Arras committed
303
        tol = 1e-3
Philipp Arras's avatar
Philipp Arras committed
304
        assert_allclose(a.val, b.val, rtol=tol, atol=tol)