test_energy_gradients.py 4.01 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
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

Philipp Arras's avatar
Philipp Arras committed
28
29
30
SPACES = [ift.GLSpace(15),
          ift.RGSpace(64, distances=.789),
          ift.RGSpace([32, 32], distances=.789)]
Philipp Arras's avatar
Philipp Arras committed
31
32
for sp in SPACES[:3]:
    SPACES.append(ift.MultiDomain.make({'asdf': sp}))
Philipp Arras's avatar
Philipp Arras committed
33
34
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
    return S.draw_sample()
Philipp Arras's avatar
Philipp Arras committed
43

Philipp Arras's avatar
Philipp Arras committed
44

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

Philipp Arras's avatar
Philipp Arras committed
49

Philipp Arras's avatar
Philipp Arras committed
50
51
def test_ScaledEnergy(field):
    icov = ift.ScalingOperator(field.domain, 1.2)
52
    energy = ift.GaussianEnergy(inverse_covariance=icov)
53
54
    ift.extra.check_jacobian_consistency(energy.scale(0.3), field)

Philipp Arras's avatar
Philipp Arras committed
55
    lin = ift.Linearization.make_var(field, want_metric=True)
Vincent Eberle's avatar
Vincent Eberle committed
56
    met1 = energy(lin).metric
Vincent Eberle's avatar
Vincent Eberle committed
57
    met2 = energy.scale(0.3)(lin).metric
Philipp Arras's avatar
Philipp Arras committed
58
59
60
    res1 = met1(field)
    res2 = met2(field)/0.3
    ift.extra.assert_allclose(res1, res2, 0, 1e-12)
61
    met2.draw_sample()
Martin Reinecke's avatar
Martin Reinecke committed
62

Philipp Arras's avatar
Philipp Arras committed
63

64
def test_studentt(field):
Philipp Arras's avatar
Philipp Arras committed
65
66
    if isinstance(field.domain, ift.MultiDomain):
        return
67
68
    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
69

Martin Reinecke's avatar
Martin Reinecke committed
70

Philipp Arras's avatar
Philipp Arras committed
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
def test_hamiltonian_and_KL(field):
    field = field.exp()
    space = field.domain
    lh = ift.GaussianEnergy(domain=space)
    hamiltonian = ift.StandardHamiltonian(lh)
    ift.extra.check_jacobian_consistency(hamiltonian, field)
    S = ift.ScalingOperator(space, 1.)
    samps = [S.draw_sample() for i in range(3)]
    kl = ift.AveragedEnergy(hamiltonian, samps)
    ift.extra.check_jacobian_consistency(kl, field)


def test_variablecovariancegaussian(field):
    if isinstance(field.domain, ift.MultiDomain):
        return
    dc = {'a': field, 'b': field.exp()}
    mf = ift.MultiField.from_dict(dc)
    energy = ift.VariableCovarianceGaussianEnergy(field.domain, 'a', 'b')
    ift.extra.check_jacobian_consistency(energy, mf, tol=1e-6)
    energy(ift.Linearization.make_var(mf, want_metric=True)).metric.draw_sample()


Philipp Arras's avatar
Philipp Arras committed
93
def test_inverse_gamma(field):
Philipp Arras's avatar
Philipp Arras committed
94
95
    if isinstance(field.domain, ift.MultiDomain):
        return
Philipp Arras's avatar
Philipp Arras committed
96
97
98
    field = field.exp()
    space = field.domain
    d = np.random.normal(10, size=space.shape)**2
Martin Reinecke's avatar
stage2    
Martin Reinecke committed
99
    d = ift.Field(space, d)
Philipp Arras's avatar
Philipp Arras committed
100
    energy = ift.InverseGammaLikelihood(d)
101
    ift.extra.check_jacobian_consistency(energy, field, tol=1e-5)
Philipp Arras's avatar
Philipp Arras committed
102
103
104


def testPoissonian(field):
Philipp Arras's avatar
Philipp Arras committed
105
106
    if isinstance(field.domain, ift.MultiDomain):
        return
Philipp Arras's avatar
Philipp Arras committed
107
108
109
    field = field.exp()
    space = field.domain
    d = np.random.poisson(120, size=space.shape)
Martin Reinecke's avatar
stage2    
Martin Reinecke committed
110
    d = ift.Field(space, d)
Philipp Arras's avatar
Philipp Arras committed
111
    energy = ift.PoissonianEnergy(d)
Martin Reinecke's avatar
Martin Reinecke committed
112
    ift.extra.check_jacobian_consistency(energy, field, tol=1e-7)
Philipp Arras's avatar
Philipp Arras committed
113
114
115


def test_bernoulli(field):
Philipp Arras's avatar
Philipp Arras committed
116
117
    if isinstance(field.domain, ift.MultiDomain):
        return
Philipp Arras's avatar
Fixups    
Philipp Arras committed
118
    field = field.sigmoid()
Philipp Arras's avatar
Philipp Arras committed
119
120
    space = field.domain
    d = np.random.binomial(1, 0.1, size=space.shape)
Martin Reinecke's avatar
stage2    
Martin Reinecke committed
121
    d = ift.Field(space, d)
Philipp Arras's avatar
Philipp Arras committed
122
    energy = ift.BernoulliEnergy(d)
123
    ift.extra.check_jacobian_consistency(energy, field, tol=1e-5)