test_energy_gradients.py 4.48 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
#
# NIFTy is being developed at the Max-Planck-Institut fuer Astrophysik.

Philipp Arras's avatar
Philipp Arras committed
18
19
from itertools import product

Philipp Arras's avatar
Philipp Arras committed
20
21
22
import numpy as np
import pytest

Martin Reinecke's avatar
5->6    
Martin Reinecke committed
23
import nifty6 as ift
Philipp Arras's avatar
Philipp Arras committed
24

25
from .common import setup_function, teardown_function
Philipp Arras's avatar
Philipp Arras committed
26
27
28
29
30

# 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
31
32
33
spaces = [ift.GLSpace(15),
          ift.MultiDomain.make({'': ift.RGSpace(64, distances=.789)}),
          (ift.RGSpace(4, distances=.789), ift.UnstructuredDomain(3))]
34
pmp = pytest.mark.parametrize
Philipp Arras's avatar
Philipp Arras committed
35
36


Philipp Arras's avatar
Philipp Arras committed
37
@pytest.fixture(params=[spaces])
Philipp Arras's avatar
Philipp Arras committed
38
def field(request):
Philipp Arras's avatar
Philipp Arras committed
39
    return ift.from_random('normal', request.param[0])
Philipp Arras's avatar
Philipp Arras committed
40

Philipp Arras's avatar
Philipp Arras committed
41

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

Philipp Arras's avatar
Philipp Arras committed
46

Philipp Arras's avatar
Philipp Arras committed
47
48
def test_ScaledEnergy(field):
    icov = ift.ScalingOperator(field.domain, 1.2)
Philipp Arras's avatar
Philipp Arras committed
49
    energy = ift.GaussianEnergy(inverse_covariance=icov, sampling_dtype=np.float64)
50
51
    ift.extra.check_jacobian_consistency(energy.scale(0.3), field)

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

Philipp Arras's avatar
Philipp Arras committed
61

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


69
def test_studentt(field):
Philipp Arras's avatar
Philipp Arras committed
70
71
    if isinstance(field.domain, ift.MultiDomain):
        return
72
73
    energy = ift.StudentTEnergy(domain=field.domain, theta=.5)
    ift.extra.check_jacobian_consistency(energy, field, tol=1e-6)
Reimar Leike's avatar
Fixup    
Reimar Leike committed
74
75
76
    theta = ift.from_random('normal',field.domain).exp()
    energy = ift.StudentTEnergy(domain=field.domain, theta=theta)
    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
    space = field.domain
    lh = ift.GaussianEnergy(domain=space)
    hamiltonian = ift.StandardHamiltonian(lh)
    ift.extra.check_jacobian_consistency(hamiltonian, field)
Philipp Arras's avatar
Philipp Arras committed
85
    samps = [ift.from_random('normal', space) for i in range(3)]
Philipp Arras's avatar
Philipp Arras committed
86
87
88
89
90
91
92
    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
93
    dc = {'a': field, 'b': field.ptw("exp")}
Philipp Arras's avatar
Philipp Arras committed
94
    mf = ift.MultiField.from_dict(dc)
Philipp Arras's avatar
Philipp Arras committed
95
    energy = ift.VariableCovarianceGaussianEnergy(field.domain, 'a', 'b', np.float64)
Philipp Arras's avatar
Philipp Arras committed
96
    ift.extra.check_jacobian_consistency(energy, mf, tol=1e-6)
Philipp Arras's avatar
Philipp Arras committed
97
    energy(ift.Linearization.make_var(mf, want_metric=True)).metric.draw_sample()
Philipp Arras's avatar
Philipp Arras committed
98
99


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


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


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