test_map.py 4.15 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):
32
33
    @expand(product([ift.GLSpace(15),
                     ift.RGSpace(64, distances=.789),
34
35
36
37
                     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
38
        dim = len(space.shape)
39
40
        hspace = space.get_default_codomain()
        ht = ift.HarmonicTransformOperator(hspace, target=space)
Philipp Arras's avatar
Philipp Arras committed
41
42
        binbounds = ift.PowerSpace.useful_binbounds(hspace, logarithmic=False)
        pspace = ift.PowerSpace(hspace, binbounds=binbounds)
Martin Reinecke's avatar
Martin Reinecke committed
43
        Dist = ift.PowerDistributor(target=hspace, power_space=pspace)
Philipp Arras's avatar
Philipp Arras committed
44
45
46
47
        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
48
        A = Dist(ift.sqrt(pspec))
Philipp Arras's avatar
Philipp Arras committed
49
        n = ift.Field.from_random(domain=space, random_type='normal')
50
        s0 = xi0 * A
Martin Reinecke's avatar
Martin Reinecke committed
51
        Instrument = ift.ScalingOperator(10., space)
52
        R = Instrument * ht
Martin Reinecke's avatar
Martin Reinecke committed
53
        N = ift.ScalingOperator(1., space)
54
        d = R(s0) + n
Philipp Arras's avatar
Philipp Arras committed
55

56
57
58
59
        IC = ift.GradientNormController(
            iteration_limit=100,
            tol_abs_gradnorm=1e-5)
        inverter = ift.ConjugateGradient(IC)
Philipp Arras's avatar
Philipp Arras committed
60

Martin Reinecke's avatar
Martin Reinecke committed
61
        S = ift.create_power_operator(hspace, power_spectrum=_flat_PS)
62
        energy = ift.library.WienerFilterEnergy(
63
            position=s0, d=d, R=R, N=N, S=S, inverter=inverter)
Martin Reinecke's avatar
Martin Reinecke committed
64
65
        ift.extra.check_value_gradient_curvature_consistency(
            energy, tol=1e-4, ntries=10)
Philipp Arras's avatar
Philipp Arras committed
66

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

Martin Reinecke's avatar
Martin Reinecke committed
93
        S = ift.create_power_operator(hspace, power_spectrum=_flat_PS)
94
        energy = ift.library.NonlinearWienerFilterEnergy(
95
96
            position=xi0, d=d, Instrument=R, nonlinearity=f, ht=ht, power=A,
            N=N, S=S)
Martin Reinecke's avatar
Martin Reinecke committed
97
98
99
100
101
102
        if isinstance(nonlinearity, ift.library.Linear):
            ift.extra.check_value_gradient_curvature_consistency(
                energy, tol=1e-4, ntries=10)
        else:
            ift.extra.check_value_gradient_consistency(
                energy, tol=1e-4, ntries=10)