test_energy_gradients.py 3.45 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
# 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
Philipp Arras's avatar
Philipp Arras committed
22
23
24
25
26
27
28
29
30
31
32
33
34
from itertools import product

# Currently it is not possible to parametrize fixtures. But this will
# hopefully be fixed in the future.
# https://docs.pytest.org/en/latest/proposals/parametrize_with_fixtures.html

SPACES = [
    ift.GLSpace(15),
    ift.RGSpace(64, distances=.789),
    ift.RGSpace([32, 32], distances=.789)
]
SEEDS = [4, 78, 23]
PARAMS = product(SEEDS, SPACES)
35
pmp = pytest.mark.parametrize
Philipp Arras's avatar
Philipp Arras committed
36
37
38
39
40


@pytest.fixture(params=PARAMS)
def field(request):
    np.random.seed(request.param[0])
41
    S = ift.ScalingOperator(request.param[1], 1.)
Philipp Arras's avatar
Philipp Arras committed
42
43
44
45
46
47
    s = S.draw_sample()
    return ift.MultiField.from_dict({'s1': s})['s1']


def test_gaussian(field):
    energy = ift.GaussianEnergy(domain=field.domain)
Martin Reinecke's avatar
Martin Reinecke committed
48
    ift.extra.check_jacobian_consistency(energy, field)
Philipp Arras's avatar
Philipp Arras committed
49

50
51
52
53
54
@pmp('icov', [lambda dom:ift.ScalingOperator(dom, 1.),
    lambda dom:ift.SandwichOperator.make(ift.GeometryRemover(dom))])
def test_ScaledEnergy(field, icov):
    icov = icov(field.domain)
    energy = ift.GaussianEnergy(inverse_covariance=icov)
55
56
    ift.extra.check_jacobian_consistency(energy.scale(0.3), field)

Vincent Eberle's avatar
Vincent Eberle committed
57
58
59
60
61
    lin =  ift.Linearization.make_var(field, want_metric=True)
    met1 = energy(lin).metric
    sE = energy.scale(0.3)
    linn = sE(lin)
    met2 = linn.metric
62
63
    np.testing.assert_allclose(met1(field).val, met2(field).val / 0.3, rtol=1e-12)
    met2.draw_sample()
Martin Reinecke's avatar
Martin Reinecke committed
64

65
66
67
def test_studentt(field):
    energy = ift.StudentTEnergy(domain=field.domain, theta=.5)
    ift.extra.check_jacobian_consistency(energy, field, tol=1e-6)
Philipp Arras's avatar
Philipp Arras committed
68

Martin Reinecke's avatar
Martin Reinecke committed
69

Philipp Arras's avatar
Philipp Arras committed
70
71
72
73
def test_inverse_gamma(field):
    field = field.exp()
    space = field.domain
    d = np.random.normal(10, size=space.shape)**2
Martin Reinecke's avatar
stage2    
Martin Reinecke committed
74
    d = ift.Field(space, d)
Philipp Arras's avatar
Philipp Arras committed
75
    energy = ift.InverseGammaLikelihood(d)
76
    ift.extra.check_jacobian_consistency(energy, field, tol=1e-5)
Philipp Arras's avatar
Philipp Arras committed
77
78
79
80
81
82


def testPoissonian(field):
    field = field.exp()
    space = field.domain
    d = np.random.poisson(120, size=space.shape)
Martin Reinecke's avatar
stage2    
Martin Reinecke committed
83
    d = ift.Field(space, d)
Philipp Arras's avatar
Philipp Arras committed
84
    energy = ift.PoissonianEnergy(d)
Martin Reinecke's avatar
Martin Reinecke committed
85
    ift.extra.check_jacobian_consistency(energy, field, tol=1e-7)
Philipp Arras's avatar
Philipp Arras committed
86
87
88
89
90
91


def test_hamiltonian_and_KL(field):
    field = field.exp()
    space = field.domain
    lh = ift.GaussianEnergy(domain=space)
92
    hamiltonian = ift.StandardHamiltonian(lh)
Martin Reinecke's avatar
Martin Reinecke committed
93
    ift.extra.check_jacobian_consistency(hamiltonian, field)
94
    S = ift.ScalingOperator(space, 1.)
Philipp Arras's avatar
Philipp Arras committed
95
    samps = [S.draw_sample() for i in range(3)]
Martin Reinecke's avatar
Martin Reinecke committed
96
    kl = ift.AveragedEnergy(hamiltonian, samps)
Martin Reinecke's avatar
Martin Reinecke committed
97
    ift.extra.check_jacobian_consistency(kl, field)
Philipp Arras's avatar
Philipp Arras committed
98
99
100


def test_bernoulli(field):
Philipp Arras's avatar
Fixups    
Philipp Arras committed
101
    field = field.sigmoid()
Philipp Arras's avatar
Philipp Arras committed
102
103
    space = field.domain
    d = np.random.binomial(1, 0.1, size=space.shape)
Martin Reinecke's avatar
stage2    
Martin Reinecke committed
104
    d = ift.Field(space, d)
Philipp Arras's avatar
Philipp Arras committed
105
    energy = ift.BernoulliEnergy(d)
106
    ift.extra.check_jacobian_consistency(energy, field, tol=1e-5)