test_power.py 7.5 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
# 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


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)],
                    [132, 42, 3]))
    def testLinearPower(self, space, seed):
        np.random.seed(seed)
Philipp Arras's avatar
Philipp Arras committed
37
38
39
40
41
42
43
44
        dim = len(space.shape)
        hspace = space.get_default_codomain()
        ht = ift.HarmonicTransformOperator(hspace, space)
        binbounds = ift.PowerSpace.useful_binbounds(hspace, logarithmic=True)
        pspace = ift.PowerSpace(hspace, binbounds=binbounds)
        P = ift.PowerProjectionOperator(domain=hspace, power_space=pspace)
        xi = ift.Field.from_random(domain=hspace, random_type='normal')

45
46
47
48
49
50
        def pspec(k): return 64 / (1 + k**2)**dim
        pspec = ift.PS_field(pspace, pspec)
        tau0 = ift.log(pspec)
        A = P.adjoint_times(ift.sqrt(pspec))
        n = ift.Field.from_random(domain=space, random_type='normal', std=.01)
        N = ift.DiagonalOperator(n**2)
Philipp Arras's avatar
Philipp Arras committed
51
        s = xi * A
Martin Reinecke's avatar
Martin Reinecke committed
52
        Instrument = ift.ScalingOperator(1., space)
Philipp Arras's avatar
Philipp Arras committed
53
54
55
56
57
        R = Instrument * ht
        d = R(s) + n

        direction = ift.Field.from_random('normal', pspace)
        direction /= np.sqrt(direction.var())
58
        eps = 1e-7
Philipp Arras's avatar
Philipp Arras committed
59
60
61
62
63
64
65
        tau1 = tau0 + eps * direction

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

Philipp Arras's avatar
Philipp Arras committed
66
67
        S = ift.create_power_operator(
            hspace, power_spectrum=lambda k: 1. / (1 + k**2))
Philipp Arras's avatar
Philipp Arras committed
68
69
70
71
72

        D = ift.library.WienerFilterEnergy(position=s, d=d, R=R, N=N, S=S,
                                           inverter=inverter).curvature

        energy0 = ift.library.CriticalPowerEnergy(
73
74
            position=tau0, m=s, inverter=inverter, D=D, samples=10,
            smoothness_prior=1.)
Philipp Arras's avatar
Cleanup    
Philipp Arras committed
75
        energy1 = energy0.at(tau1)
Philipp Arras's avatar
Philipp Arras committed
76
77
78

        a = (energy1.value - energy0.value) / eps
        b = energy0.gradient.vdot(direction)
79
        tol = 1e-4
Philipp Arras's avatar
Philipp Arras committed
80
81
82
83
        assert_allclose(a, b, rtol=tol, atol=tol)

    @expand(product([ift.RGSpace(64, distances=.789),
                     ift.RGSpace([32, 32], distances=.789)],
84
85
86
87
                    [ift.library.Exponential, ift.library.Linear],
                    [132, 42, 3]))
    def testNonlinearPower(self, space, nonlinearity, seed):
        np.random.seed(seed)
Philipp Arras's avatar
Philipp Arras committed
88
89
        f = nonlinearity()
        dim = len(space.shape)
90
91
        hspace = space.get_default_codomain()
        ht = ift.HarmonicTransformOperator(hspace, space)
Philipp Arras's avatar
Philipp Arras committed
92
93
94
95
96
97
98
99
100
        binbounds = ift.PowerSpace.useful_binbounds(hspace, logarithmic=True)
        pspace = ift.PowerSpace(hspace, binbounds=binbounds)
        P = ift.PowerProjectionOperator(domain=hspace, power_space=pspace)
        xi = ift.Field.from_random(domain=hspace, random_type='normal')

        def pspec(k): return 1 / (1 + k**2)**dim
        tau0 = ift.PS_field(pspace, pspec)
        A = P.adjoint_times(ift.sqrt(tau0))
        n = ift.Field.from_random(domain=space, random_type='normal')
101
        s = ht(xi * A)
Martin Reinecke's avatar
Martin Reinecke committed
102
        R = ift.ScalingOperator(10., space)
Philipp Arras's avatar
Philipp Arras committed
103
104
105
106
107
108
        diag = ift.Field.ones(space)
        N = ift.DiagonalOperator(diag)
        d = R(f(s)) + n

        direction = ift.Field.from_random('normal', pspace)
        direction /= np.sqrt(direction.var())
109
        eps = 1e-7
Philipp Arras's avatar
Philipp Arras committed
110
111
112
113
114
115
116
        tau1 = tau0 + eps * direction

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

Martin Reinecke's avatar
Martin Reinecke committed
117
        S = ift.create_power_operator(hspace, power_spectrum=_flat_PS)
Philipp Arras's avatar
Philipp Arras committed
118
119
120
121
122
123
124
125
        D = ift.library.NonlinearWienerFilterEnergy(
            position=xi,
            d=d,
            Instrument=R,
            nonlinearity=f,
            power=A,
            N=N,
            S=S,
126
            ht=ht,
Philipp Arras's avatar
Philipp Arras committed
127
128
129
130
131
            inverter=inverter).curvature

        energy0 = ift.library.NonlinearPowerEnergy(
            position=tau0,
            d=d,
Philipp Arras's avatar
Philipp Arras committed
132
            xi=xi,
Philipp Arras's avatar
Philipp Arras committed
133
134
135
136
            D=D,
            Instrument=R,
            Projection=P,
            nonlinearity=f,
137
            ht=ht,
Philipp Arras's avatar
Philipp Arras committed
138
            N=N,
139
            samples=10)
Philipp Arras's avatar
Cleanup    
Philipp Arras committed
140
        energy1 = energy0.at(tau1)
Philipp Arras's avatar
Philipp Arras committed
141
142
143

        a = (energy1.value - energy0.value) / eps
        b = energy0.gradient.vdot(direction)
144
        tol = 1e-4
Philipp Arras's avatar
Philipp Arras committed
145
        assert_allclose(a, b, rtol=tol, atol=tol)
146
147
148


class Curvature_Tests(unittest.TestCase):
149
150
151
152
    # Note: It is only possible to test the linear power 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.
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
179
180
181
182
183
184
185
186
187
    @expand(product([ift.RGSpace(64, distances=.789),
                     ift.RGSpace([32, 32], distances=.789)],
                    [132, 42, 3]))
    def testLinearPowerCurvature(self, space, seed):
        np.random.seed(seed)
        dim = len(space.shape)
        hspace = space.get_default_codomain()
        ht = ift.HarmonicTransformOperator(hspace, space)
        binbounds = ift.PowerSpace.useful_binbounds(hspace, logarithmic=True)
        pspace = ift.PowerSpace(hspace, binbounds=binbounds)
        P = ift.PowerProjectionOperator(domain=hspace, power_space=pspace)
        xi = ift.Field.from_random(domain=hspace, random_type='normal')

        def pspec(k): return 64 / (1 + k**2)**dim
        pspec = ift.PS_field(pspace, pspec)
        tau0 = ift.log(pspec)
        A = P.adjoint_times(ift.sqrt(pspec))
        n = ift.Field.from_random(domain=space, random_type='normal', std=.01)
        N = ift.DiagonalOperator(n**2)
        s = xi * A
        diag = ift.Field.ones(space)
        Instrument = ift.DiagonalOperator(diag)
        R = Instrument * ht
        d = R(s) + n

        direction = ift.Field.from_random('normal', pspace)
        direction /= np.sqrt(direction.var())
        eps = 1e-7
        tau1 = tau0 + eps * direction

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

Martin Reinecke's avatar
Martin Reinecke committed
188
        S = ift.create_power_operator(hspace, power_spectrum=_flat_PS)
189
190
191
192
193

        D = ift.library.WienerFilterEnergy(position=s, d=d, R=R, N=N, S=S,
                                           inverter=inverter).curvature

        energy0 = ift.library.CriticalPowerEnergy(
194
            position=tau0, m=s, inverter=inverter, samples=10)
195
196
197
198
199
200
201

        gradient0 = energy0.gradient
        gradient1 = energy0.at(tau1).gradient

        a = (gradient1 - gradient0) / eps
        b = energy0.curvature(direction)
        tol = 1e-5
Martin Reinecke's avatar
Martin Reinecke committed
202
203
        assert_allclose(ift.dobj.to_global_data(a.val),
                        ift.dobj.to_global_data(b.val), rtol=tol, atol=tol)