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

Philipp Arras's avatar
Fixups    
Philipp Arras committed
23
from .common import list2fixture, setup_function, teardown_function
Philipp Arras's avatar
Philipp Arras committed
24

Philipp Arras's avatar
Fixups    
Philipp Arras committed
25
26
27
spaces = [ift.GLSpace(5),
          ift.MultiDomain.make({'': ift.RGSpace(5, distances=.789)}),
          (ift.RGSpace(3, distances=.789), ift.UnstructuredDomain(2))]
28
pmp = pytest.mark.parametrize
Philipp Arras's avatar
Fixups    
Philipp Arras committed
29
30
field = list2fixture([ift.from_random('normal', sp) for sp in spaces])
ntries = 10
Philipp Arras's avatar
Philipp Arras committed
31

Philipp Arras's avatar
Philipp Arras committed
32

Philipp Arras's avatar
Philipp Arras committed
33
34
def test_gaussian(field):
    energy = ift.GaussianEnergy(domain=field.domain)
Martin Reinecke's avatar
Martin Reinecke committed
35
    ift.extra.check_jacobian_consistency(energy, field)
Philipp Arras's avatar
Philipp Arras committed
36

Philipp Arras's avatar
Philipp Arras committed
37

Philipp Arras's avatar
Philipp Arras committed
38
39
def test_ScaledEnergy(field):
    icov = ift.ScalingOperator(field.domain, 1.2)
Philipp Arras's avatar
Philipp Arras committed
40
    energy = ift.GaussianEnergy(inverse_covariance=icov, sampling_dtype=np.float64)
41
42
    ift.extra.check_jacobian_consistency(energy.scale(0.3), field)

Philipp Arras's avatar
Philipp Arras committed
43
    lin = ift.Linearization.make_var(field, want_metric=True)
Vincent Eberle's avatar
Vincent Eberle committed
44
    met1 = energy(lin).metric
Vincent Eberle's avatar
Vincent Eberle committed
45
    met2 = energy.scale(0.3)(lin).metric
Philipp Arras's avatar
Philipp Arras committed
46
47
48
    res1 = met1(field)
    res2 = met2(field)/0.3
    ift.extra.assert_allclose(res1, res2, 0, 1e-12)
Philipp Arras's avatar
Philipp Arras committed
49
50
    met1.draw_sample()
    met2.draw_sample()
Martin Reinecke's avatar
Martin Reinecke committed
51

Philipp Arras's avatar
Philipp Arras committed
52

Philipp Arras's avatar
Philipp Arras committed
53
54
def test_QuadraticFormOperator(field):
    op = ift.ScalingOperator(field.domain, 1.2)
Philipp Arras's avatar
Philipp Arras committed
55
    endo = ift.makeOp(op.draw_sample_with_dtype(dtype=np.float64))
Philipp Arras's avatar
Philipp Arras committed
56
57
58
59
    energy = ift.QuadraticFormOperator(endo)
    ift.extra.check_jacobian_consistency(energy, field)


60
def test_studentt(field):
Philipp Arras's avatar
Philipp Arras committed
61
62
    if isinstance(field.domain, ift.MultiDomain):
        return
63
64
    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
65
66
    theta = ift.from_random('normal',field.domain).exp()
    energy = ift.StudentTEnergy(domain=field.domain, theta=theta)
Philipp Arras's avatar
Fixups    
Philipp Arras committed
67
    ift.extra.check_jacobian_consistency(energy, field, tol=1e-6, ntries=ntries)
Philipp Arras's avatar
Philipp Arras committed
68

Martin Reinecke's avatar
Martin Reinecke committed
69

Philipp Arras's avatar
Philipp Arras committed
70
def test_hamiltonian_and_KL(field):
Martin Reinecke's avatar
Martin Reinecke committed
71
    field = field.ptw("exp")
Philipp Arras's avatar
Philipp Arras committed
72
73
74
    space = field.domain
    lh = ift.GaussianEnergy(domain=space)
    hamiltonian = ift.StandardHamiltonian(lh)
Philipp Arras's avatar
Fixups    
Philipp Arras committed
75
76
    ift.extra.check_jacobian_consistency(hamiltonian, field, ntries=ntries)
    samps = [ift.from_random('normal', space) for i in range(2)]
Philipp Arras's avatar
Philipp Arras committed
77
    kl = ift.AveragedEnergy(hamiltonian, samps)
Philipp Arras's avatar
Fixups    
Philipp Arras committed
78
    ift.extra.check_jacobian_consistency(kl, field, ntries=ntries)
Philipp Arras's avatar
Philipp Arras committed
79
80
81
82
83


def test_variablecovariancegaussian(field):
    if isinstance(field.domain, ift.MultiDomain):
        return
Martin Reinecke's avatar
Martin Reinecke committed
84
    dc = {'a': field, 'b': field.ptw("exp")}
Philipp Arras's avatar
Philipp Arras committed
85
    mf = ift.MultiField.from_dict(dc)
Philipp Arras's avatar
Philipp Arras committed
86
    energy = ift.VariableCovarianceGaussianEnergy(field.domain, 'a', 'b', np.float64)
Philipp Arras's avatar
Fixups    
Philipp Arras committed
87
    ift.extra.check_jacobian_consistency(energy, mf, tol=1e-6, ntries=ntries)
Philipp Arras's avatar
Philipp Arras committed
88
    energy(ift.Linearization.make_var(mf, want_metric=True)).metric.draw_sample()
Philipp Arras's avatar
Philipp Arras committed
89
90


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


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


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