test_map.py 3.97 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
#
# NIFTy is being developed at the Max-Planck-Institut fuer Astrophysik
# and financially supported by the Studienstiftung des deutschen Volkes.

import unittest
Philipp Arras's avatar
Philipp Arras committed
20
import nifty5 as ift
Philipp Arras's avatar
Philipp Arras committed
21
22
23
24
25
import numpy as np
from itertools import product
from test.common import expand


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


30
class Energy_Tests(unittest.TestCase):
31
32
    @expand(product([ift.GLSpace(15),
                     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
57
        IC = ift.GradientNormController(
            iteration_limit=100,
            tol_abs_gradnorm=1e-5)
Philipp Arras's avatar
Philipp Arras committed
58

Martin Reinecke's avatar
Martin Reinecke committed
59
        S = ift.create_power_operator(hspace, power_spectrum=_flat_PS)
Philipp Arras's avatar
Philipp Arras committed
60
        energy = ift.WienerFilterEnergy(
61
            position=s0, d=d, R=R, N=N, S=S, iteration_controller=IC)
Martin Reinecke's avatar
Martin Reinecke committed
62
        ift.extra.check_value_gradient_curvature_consistency(
Martin Reinecke's avatar
Martin Reinecke committed
63
            energy, ntries=10)
Philipp Arras's avatar
Philipp Arras committed
64

65
66
    @expand(product([ift.GLSpace(15),
                     ift.RGSpace(64, distances=.789),
67
                     ift.RGSpace([32, 32], distances=.789)],
Philipp Arras's avatar
Fixups    
Philipp Arras committed
68
                    [ift.Tanh, ift.Exponential, ift.Linear],
69
                    [1, 1e-2, 1e2],
Philipp Arras's avatar
Philipp Arras committed
70
                    [4, 78, 23]))
71
    def testGaussianEnergy(self, space, nonlinearity, noise, seed):
72
        np.random.seed(seed)
73
        dim = len(space.shape)
74
75
        hspace = space.get_default_codomain()
        ht = ift.HarmonicTransformOperator(hspace, target=space)
76
77
        binbounds = ift.PowerSpace.useful_binbounds(hspace, logarithmic=False)
        pspace = ift.PowerSpace(hspace, binbounds=binbounds)
Martin Reinecke's avatar
Martin Reinecke committed
78
        Dist = ift.PowerDistributor(target=hspace, power_space=pspace)
79
        xi0 = ift.Field.from_random(domain=hspace, random_type='normal')
Philipp Arras's avatar
Philipp Arras committed
80
        xi0_var = ift.Variable(ift.MultiField({'xi': xi0}))['xi']
81
82
83

        def pspec(k): return 1 / (1 + k**2)**dim
        pspec = ift.PS_field(pspace, pspec)
Martin Reinecke's avatar
Martin Reinecke committed
84
        A = Dist(ift.sqrt(pspec))
85
86
        N = ift.ScalingOperator(noise, space)
        n = N.draw_sample()
87
        s = ht(ift.makeOp(A)(xi0_var))
Martin Reinecke's avatar
Martin Reinecke committed
88
        R = ift.ScalingOperator(10., space)
89
90
        d_model = R(ift.LocalModel(s, nonlinearity()))
        d = d_model.value + n
91

92
93
94
        if noise == 1:
            N = None

Philipp Arras's avatar
Philipp Arras committed
95
        energy = ift.GaussianEnergy(d_model, d, N)
96
        if isinstance(nonlinearity(), ift.Linear):
Martin Reinecke's avatar
Martin Reinecke committed
97
            ift.extra.check_value_gradient_curvature_consistency(
Martin Reinecke's avatar
Martin Reinecke committed
98
                energy, ntries=10)
Martin Reinecke's avatar
Martin Reinecke committed
99
100
        else:
            ift.extra.check_value_gradient_consistency(
Martin Reinecke's avatar
Martin Reinecke committed
101
                energy, ntries=10)