test_map.py 8.61 KB
Newer Older
Philipp Arras's avatar
Philipp Arras 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/>.
#
Martin Reinecke's avatar
Martin Reinecke committed
14
# Copyright(C) 2013-2018 Max-Planck-Society
Philipp Arras's avatar
Philipp Arras committed
15
16
17
18
19
20
21
22
23
24
25
26
#
# 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


Martin Reinecke's avatar
Martin Reinecke committed
27
28
29
30
def _flat_PS(k):
    return np.ones_like(k)


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
        binbounds = ift.PowerSpace.useful_binbounds(hspace, logarithmic=False)
        pspace = ift.PowerSpace(hspace, binbounds=binbounds)
Martin Reinecke's avatar
Martin Reinecke committed
42
        Dist = ift.PowerDistributor(target=hspace, power_space=pspace)
Philipp Arras's avatar
Philipp Arras committed
43
44
45
46
        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)
Martin Reinecke's avatar
Martin Reinecke committed
47
        A = Dist(ift.sqrt(pspec))
Philipp Arras's avatar
Philipp Arras committed
48
        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

Martin Reinecke's avatar
Martin Reinecke committed
65
        S = ift.create_power_operator(hspace, power_spectrum=_flat_PS)
66
67
        energy0 = ift.library.WienerFilterEnergy(
            position=s0, d=d, R=R, N=N, S=S, inverter=inverter)
Philipp Arras's avatar
Cleanup  
Philipp Arras committed
68
        energy1 = energy0.at(s1)
Philipp Arras's avatar
Philipp Arras committed
69
70
71

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

75
76
    @expand(product([ift.RGSpace(64, distances=.789),
                     ift.RGSpace([32, 32], distances=.789)],
Martin Reinecke's avatar
Martin Reinecke committed
77
78
                    [ift.library.Tanh, ift.library.Exponential,
                     ift.library.Linear],
Philipp Arras's avatar
Philipp Arras committed
79
                    [4, 78, 23]))
80
81
    def testNonlinearMap(self, space, nonlinearity, seed):
        np.random.seed(seed)
82
83
        f = nonlinearity()
        dim = len(space.shape)
84
85
        hspace = space.get_default_codomain()
        ht = ift.HarmonicTransformOperator(hspace, target=space)
86
87
        binbounds = ift.PowerSpace.useful_binbounds(hspace, logarithmic=False)
        pspace = ift.PowerSpace(hspace, binbounds=binbounds)
Martin Reinecke's avatar
Martin Reinecke committed
88
        Dist = ift.PowerDistributor(target=hspace, power_space=pspace)
89
90
91
92
        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)
Martin Reinecke's avatar
Martin Reinecke committed
93
        A = Dist(ift.sqrt(pspec))
94
        n = ift.Field.from_random(domain=space, random_type='normal')
95
        s = ht(xi0 * A)
Martin Reinecke's avatar
Martin Reinecke committed
96
97
        R = ift.ScalingOperator(10., space)
        N = ift.ScalingOperator(1., space)
98
99
100
101
        d = R(f(s)) + n

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

Martin Reinecke's avatar
Martin Reinecke committed
105
        S = ift.create_power_operator(hspace, power_spectrum=_flat_PS)
106
        energy0 = ift.library.NonlinearWienerFilterEnergy(
107
108
            position=xi0, d=d, Instrument=R, nonlinearity=f, ht=ht, power=A,
            N=N, S=S)
Philipp Arras's avatar
Philipp Arras committed
109
        energy1 = energy0.at(xi1)
110
111
112

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

116
117

class Curvature_Tests(unittest.TestCase):
118
119
120
121
    # Note: It is only possible to test linear curvatures since the non-linear
    # curvatures are not the exact second derivative but only a part of it. One
    # term is neglected which would render the second derivative non-positive
    # definite.
Philipp Arras's avatar
Philipp Arras committed
122
123
124
125
126
127
128
129
130
131
    @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)
Martin Reinecke's avatar
Martin Reinecke committed
132
        Dist = ift.PowerDistributor(target=hspace, power_space=pspace)
Philipp Arras's avatar
Philipp Arras committed
133
134
135
136
        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)
Martin Reinecke's avatar
Martin Reinecke committed
137
        A = Dist(ift.sqrt(pspec))
Philipp Arras's avatar
Philipp Arras committed
138
139
        n = ift.Field.from_random(domain=space, random_type='normal')
        s0 = xi0 * A
Martin Reinecke's avatar
Martin Reinecke committed
140
        Instrument = ift.ScalingOperator(10., space)
Philipp Arras's avatar
Philipp Arras committed
141
        R = Instrument * ht
Martin Reinecke's avatar
Martin Reinecke committed
142
        N = ift.ScalingOperator(1., space)
Philipp Arras's avatar
Philipp Arras committed
143
144
145
146
147
148
149
150
151
152
153
154
        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)

Martin Reinecke's avatar
Martin Reinecke committed
155
        S = ift.create_power_operator(hspace, power_spectrum=_flat_PS)
Philipp Arras's avatar
Philipp Arras committed
156
157
        energy0 = ift.library.WienerFilterEnergy(
            position=s0, d=d, R=R, N=N, S=S, inverter=inverter)
Philipp Arras's avatar
Philipp Arras committed
158
159
160
161
162
163
        gradient0 = energy0.gradient
        gradient1 = energy0.at(s1).gradient

        a = (gradient1 - gradient0) / eps
        b = energy0.curvature(direction)
        tol = 1e-7
Martin Reinecke's avatar
Martin Reinecke committed
164
165
        assert_allclose(ift.dobj.to_global_data(a.val),
                        ift.dobj.to_global_data(b.val), rtol=tol, atol=tol)
Philipp Arras's avatar
Philipp Arras committed
166
167
168

    @expand(product([ift.RGSpace(64, distances=.789),
                     ift.RGSpace([32, 32], distances=.789)],
169
170
                    # Only linear case due to approximation of Hessian in the
                    # case of nontrivial nonlinearities.
Philipp Arras's avatar
Philipp Arras committed
171
                    [ift.library.Linear],
Philipp Arras's avatar
Philipp Arras committed
172
                    [4, 78, 23]))
Philipp Arras's avatar
Philipp Arras committed
173
174
175
176
177
178
179
180
    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)
Martin Reinecke's avatar
Martin Reinecke committed
181
        Dist = ift.PowerDistributor(target=hspace, power_space=pspace)
Philipp Arras's avatar
Philipp Arras committed
182
183
184
185
        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)
Martin Reinecke's avatar
Martin Reinecke committed
186
        A = Dist(ift.sqrt(pspec))
Philipp Arras's avatar
Philipp Arras committed
187
188
        n = ift.Field.from_random(domain=space, random_type='normal')
        s = ht(xi0 * A)
Martin Reinecke's avatar
Martin Reinecke committed
189
190
        R = ift.ScalingOperator(10., space)
        N = ift.ScalingOperator(1., space)
Philipp Arras's avatar
Philipp Arras committed
191
192
193
194
195
196
197
        d = R(f(s)) + n

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

Martin Reinecke's avatar
Martin Reinecke committed
198
        S = ift.create_power_operator(hspace, power_spectrum=_flat_PS)
Philipp Arras's avatar
Philipp Arras committed
199
200
201
202
203
204

        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
205
206
207
208
209
210
211
212
213
            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
214
215
216
217
218
        gradient0 = energy0.gradient
        gradient1 = energy0.at(xi1).gradient

        a = (gradient1 - gradient0) / eps
        b = energy0.curvature(direction)
Philipp Arras's avatar
Philipp Arras committed
219
        tol = 1e-7
Martin Reinecke's avatar
Martin Reinecke committed
220
221
        assert_allclose(ift.dobj.to_global_data(a.val),
                        ift.dobj.to_global_data(b.val), rtol=tol, atol=tol)