test_gaussian_energy.py 2.35 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
# 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-2019 Max-Planck-Society
#
# NIFTy is being developed at the Max-Planck-Institut fuer Astrophysik.

import numpy as np
import pytest

Martin Reinecke's avatar
5->6  
Martin Reinecke committed
21
import nifty6 as ift
22
from .common import setup_function, teardown_function
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40


def _flat_PS(k):
    return np.ones_like(k)


pmp = pytest.mark.parametrize


@pmp('space', [
    ift.GLSpace(15),
    ift.RGSpace(64, distances=.789),
    ift.RGSpace([32, 32], distances=.789)
])
@pmp('nonlinearity', ["tanh", "exp", ""])
@pmp('noise', [1, 1e-2, 1e2])
@pmp('seed', [4, 78, 23])
def test_gaussian_energy(space, nonlinearity, noise, seed):
Martin Reinecke's avatar
Martin Reinecke committed
41 42 43 44 45 46 47 48
    with ift.random.Context(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)
        Dist = ift.PowerDistributor(target=hspace, power_space=pspace)
        xi0 = ift.Field.from_random(domain=hspace, random_type='normal')
49

Martin Reinecke's avatar
Martin Reinecke committed
50 51
        def pspec(k):
            return 1/(1 + k**2)**dim
52

Martin Reinecke's avatar
Martin Reinecke committed
53
        pspec = ift.PS_field(pspace, pspec)
Martin Reinecke's avatar
Martin Reinecke committed
54
        A = Dist(pspec.ptw("sqrt"))
Martin Reinecke's avatar
Martin Reinecke committed
55 56 57
        N = ift.ScalingOperator(space, noise)
        n = N.draw_sample()
        R = ift.ScalingOperator(space, 10.)
58

Martin Reinecke's avatar
Martin Reinecke committed
59 60 61 62 63
        def d_model():
            if nonlinearity == "":
                return R @ ht @ ift.makeOp(A)
            else:
                tmp = ht @ ift.makeOp(A)
Martin Reinecke's avatar
Martin Reinecke committed
64
                nonlin = tmp.ptw(nonlinearity)
Martin Reinecke's avatar
Martin Reinecke committed
65
                return R @ nonlin
66

Martin Reinecke's avatar
Martin Reinecke committed
67
        d = d_model()(xi0) + n
68

Martin Reinecke's avatar
Martin Reinecke committed
69 70
        if noise == 1:
            N = None
71

Martin Reinecke's avatar
Martin Reinecke committed
72 73 74
        energy = ift.GaussianEnergy(d, N) @ d_model()
        ift.extra.check_jacobian_consistency(
            energy, xi0, ntries=10, tol=1e-6)