test_map.py 6.52 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 to reasonable values
Philipp Arras's avatar
Philipp Arras committed
29
30
31
32


class Map_Energy_Tests(unittest.TestCase):
    @expand(product([ift.RGSpace(64, distances=.789),
33
34
                     ift.RGSpace([32, 32], distances=.789)]))
    def testLinearMap(self, space):
Philipp Arras's avatar
Philipp Arras committed
35
        dim = len(space.shape)
36
37
        hspace = space.get_default_codomain()
        ht = ift.HarmonicTransformOperator(hspace, target=space)
Philipp Arras's avatar
Philipp Arras committed
38
39
40
41
42
43
44
45
46
        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')
47
        s0 = xi0 * A
Philipp Arras's avatar
Philipp Arras committed
48
        diag = ift.Field.ones(space) * 10
49
50
        Instrument = ift.DiagonalOperator(diag)
        R = Instrument * ht
Philipp Arras's avatar
Philipp Arras committed
51
52
        diag = ift.Field.ones(space)
        N = ift.DiagonalOperator(diag)
53
        d = R(s0) + n
Philipp Arras's avatar
Philipp Arras committed
54
55
56
57

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

        IC = ift.GradientNormController(
            name='IC',
            verbose=False,
            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.)
68
69
70
71
        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
72
73
74

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

    @expand(product([ift.RGSpace(64, distances=.789),
                     ift.RGSpace([32, 32], distances=.789)]))
80
    def testLognormalMap(self, space):
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)
Philipp Arras's avatar
Philipp Arras committed
95
96
97
98
99
        diag = ift.Field.ones(space) * 10
        Instrument = ift.DiagonalOperator(diag)
        R = Instrument * ht
        diag = ift.Field.ones(space)
        N = ift.DiagonalOperator(diag)
100
        d = Instrument(ift.exp(s)) + n
Philipp Arras's avatar
Philipp Arras committed
101
102
103
104

        direction = ift.Field.from_random('normal', hspace)
        direction /= np.sqrt(direction.var())
        eps = 1e-10
105
        sh1 = sh0 + eps * direction
Philipp Arras's avatar
Philipp Arras committed
106
107
108
109
110
111
112
113
114

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

        S = ift.create_power_operator(hspace, power_spectrum=lambda k: 1.)
115
116
117
118
        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
119
120
121

        a = (energy1.value - energy0.value) / eps
        b = energy0.gradient.vdot(direction)
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
        tol = 1e-2
        assert_allclose(a, b, rtol=tol, atol=tol)

    @expand(product([ift.RGSpace(64, distances=.789),
                     ift.RGSpace([32, 32], distances=.789)],
                    [ift.library.Exponential, ift.library.Linear]))
    def testNonlinearMap(self, space, nonlinearity):
        f = nonlinearity()
        dim = len(space.shape)
        fft = ift.FFTOperator(space)
        hspace = fft.target[0]
        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 = fft.inverse_times(xi0 * A)
        diag = ift.Field.ones(space) * 10
        R = ift.DiagonalOperator(diag)
        diag = ift.Field.ones(space)
        N = ift.DiagonalOperator(diag)
        d = R(f(s)) + n

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

        S = ift.create_power_operator(hspace, power_spectrum=lambda k: 1.)
        energy0 = ift.library.NonlinearWienerFilterEnergy(
            position=xi0, d=d, Instrument=R, nonlinearity=f, FFT=fft, power=A, N=N, S=S)
        energy1 = ift.library.NonlinearWienerFilterEnergy(
            position=xi1, d=d, Instrument=R, nonlinearity=f, FFT=fft, power=A, N=N, S=S)

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