test_energy_gradients.py 4.46 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/>.
#
14
# Copyright(C) 2013-2020 Max-Planck-Society
Philipp Arras's avatar
Philipp Arras committed
15
16
17
18
19
20
#
# 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
from itertools import product
23
from .common import setup_function, teardown_function
Philipp Arras's avatar
Philipp Arras committed
24
25
26
27
28

# 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
29
30
31
SPACES = [ift.GLSpace(15),
          ift.RGSpace(64, distances=.789),
          ift.RGSpace([32, 32], distances=.789)]
Philipp Arras's avatar
Philipp Arras committed
32
33
for sp in SPACES[:3]:
    SPACES.append(ift.MultiDomain.make({'asdf': sp}))
Philipp Arras's avatar
Philipp Arras committed
34
35
SEEDS = [4, 78, 23]
PARAMS = product(SEEDS, SPACES)
36
pmp = pytest.mark.parametrize
Philipp Arras's avatar
Philipp Arras committed
37
38
39
40


@pytest.fixture(params=PARAMS)
def field(request):
Martin Reinecke's avatar
Martin Reinecke committed
41
42
    with ift.random.Context(request.param[0]):
        S = ift.ScalingOperator(request.param[1], 1.)
43
        return S.draw_sample(dtype=np.float64)
Philipp Arras's avatar
Philipp Arras committed
44

Philipp Arras's avatar
Philipp Arras committed
45

Philipp Arras's avatar
Philipp Arras committed
46
47
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

Philipp Arras's avatar
Philipp Arras committed
50

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

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

Philipp Arras's avatar
Philipp Arras committed
64

Philipp Arras's avatar
Philipp Arras committed
65
66
def test_QuadraticFormOperator(field):
    op = ift.ScalingOperator(field.domain, 1.2)
67
    endo = ift.makeOp(op.draw_sample(dtype=np.float64))
Philipp Arras's avatar
Philipp Arras committed
68
69
70
71
    energy = ift.QuadraticFormOperator(endo)
    ift.extra.check_jacobian_consistency(energy, field)


72
def test_studentt(field):
Philipp Arras's avatar
Philipp Arras committed
73
74
    if isinstance(field.domain, ift.MultiDomain):
        return
75
76
    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
77

Martin Reinecke's avatar
Martin Reinecke committed
78

Philipp Arras's avatar
Philipp Arras committed
79
def test_hamiltonian_and_KL(field):
Martin Reinecke's avatar
Martin Reinecke committed
80
    field = field.ptw("exp")
Philipp Arras's avatar
Philipp Arras committed
81
82
83
84
85
    space = field.domain
    lh = ift.GaussianEnergy(domain=space)
    hamiltonian = ift.StandardHamiltonian(lh)
    ift.extra.check_jacobian_consistency(hamiltonian, field)
    S = ift.ScalingOperator(space, 1.)
86
    samps = [S.draw_sample(dtype=np.float64) for i in range(3)]
Philipp Arras's avatar
Philipp Arras committed
87
88
89
90
91
92
93
    kl = ift.AveragedEnergy(hamiltonian, samps)
    ift.extra.check_jacobian_consistency(kl, field)


def test_variablecovariancegaussian(field):
    if isinstance(field.domain, ift.MultiDomain):
        return
Martin Reinecke's avatar
Martin Reinecke committed
94
    dc = {'a': field, 'b': field.ptw("exp")}
Philipp Arras's avatar
Philipp Arras committed
95
96
97
    mf = ift.MultiField.from_dict(dc)
    energy = ift.VariableCovarianceGaussianEnergy(field.domain, 'a', 'b')
    ift.extra.check_jacobian_consistency(energy, mf, tol=1e-6)
98
    energy(ift.Linearization.make_var(mf, want_metric=True)).metric.draw_sample(dtype=np.float64)
Philipp Arras's avatar
Philipp Arras committed
99
100


Philipp Arras's avatar
Philipp Arras committed
101
def test_inverse_gamma(field):
Philipp Arras's avatar
Philipp Arras committed
102
103
    if isinstance(field.domain, ift.MultiDomain):
        return
Martin Reinecke's avatar
Martin Reinecke committed
104
    field = field.ptw("exp")
Philipp Arras's avatar
Philipp Arras committed
105
    space = field.domain
Martin Reinecke's avatar
Martin Reinecke committed
106
    d = ift.random.current_rng().normal(10, size=space.shape)**2
Martin Reinecke's avatar
stage2    
Martin Reinecke committed
107
    d = ift.Field(space, d)
Philipp Arras's avatar
Philipp Arras committed
108
    energy = ift.InverseGammaLikelihood(d)
109
    ift.extra.check_jacobian_consistency(energy, field, tol=1e-5)
Philipp Arras's avatar
Philipp Arras committed
110
111
112


def testPoissonian(field):
Philipp Arras's avatar
Philipp Arras committed
113
114
    if isinstance(field.domain, ift.MultiDomain):
        return
Martin Reinecke's avatar
Martin Reinecke committed
115
    field = field.ptw("exp")
Philipp Arras's avatar
Philipp Arras committed
116
    space = field.domain
Martin Reinecke's avatar
Martin Reinecke committed
117
    d = ift.random.current_rng().poisson(120, size=space.shape)
Martin Reinecke's avatar
stage2    
Martin Reinecke committed
118
    d = ift.Field(space, d)
Philipp Arras's avatar
Philipp Arras committed
119
    energy = ift.PoissonianEnergy(d)
120
    ift.extra.check_jacobian_consistency(energy, field, tol=1e-6)
Philipp Arras's avatar
Philipp Arras committed
121
122
123


def test_bernoulli(field):
Philipp Arras's avatar
Philipp Arras committed
124
125
    if isinstance(field.domain, ift.MultiDomain):
        return
Martin Reinecke's avatar
Martin Reinecke committed
126
    field = field.ptw("sigmoid")
Philipp Arras's avatar
Philipp Arras committed
127
    space = field.domain
Martin Reinecke's avatar
Martin Reinecke committed
128
    d = ift.random.current_rng().binomial(1, 0.1, size=space.shape)
Martin Reinecke's avatar
stage2    
Martin Reinecke committed
129
    d = ift.Field(space, d)
Philipp Arras's avatar
Philipp Arras committed
130
    energy = ift.BernoulliEnergy(d)
131
    ift.extra.check_jacobian_consistency(energy, field, tol=1e-5)