test_jacobian.py 6.91 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/>.
#
Philipp Arras's avatar
Philipp Arras committed
14
# Copyright(C) 2013-2021 Max-Planck-Society
Philipp Arras's avatar
Philipp Arras committed
15
16
17
18
19
#
# NIFTy is being developed at the Max-Planck-Institut fuer Astrophysik.

import pytest

Philipp Arras's avatar
Philipp Arras committed
20
import nifty8 as ift
Philipp Arras's avatar
Philipp Arras committed
21

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

pmp = pytest.mark.parametrize
space = list2fixture([
Philipp Arras's avatar
Philipp Arras committed
26
27
28
    ift.GLSpace(5),
    ift.RGSpace(3, distances=.789),
    ift.RGSpace([4, 4], distances=.789)
Philipp Arras's avatar
Philipp Arras committed
29
])
Philipp Arras's avatar
Philipp Arras committed
30
31
_h_RG_spaces = [
    ift.RGSpace(7, distances=0.2, harmonic=True),
Philipp Arras's avatar
Philipp Arras committed
32
    ift.RGSpace((3, 4), distances=(.2, .3), harmonic=True)
Philipp Arras's avatar
Philipp Arras committed
33
34
]
_h_spaces = _h_RG_spaces + [ift.LMSpace(17)]
Philipp Arras's avatar
Philipp Arras committed
35
space1 = space
Philipp Arras's avatar
Philipp Arras committed
36
37
seed = list2fixture([4, 78])
ntries = 10
Philipp Arras's avatar
Philipp Arras committed
38
39


Philipp Arras's avatar
Philipp Arras committed
40
def testBasics(space, seed):
Martin Reinecke's avatar
Martin Reinecke committed
41
    with ift.random.Context(seed):
42
        s = ift.from_random(space, 'normal')
Martin Reinecke's avatar
Martin Reinecke committed
43
44
        var = ift.Linearization.make_var(s)
        model = ift.ScalingOperator(var.target, 6.)
Philipp Arras's avatar
Philipp Arras committed
45
        ift.extra.check_operator(model, var.val, ntries=ntries)
Philipp Arras's avatar
Philipp Arras committed
46
47
48
49
50


@pmp('type1', ['Variable', 'Constant'])
@pmp('type2', ['Variable'])
def testBinary(type1, type2, space, seed):
Martin Reinecke's avatar
Martin Reinecke committed
51
52
53
54
55
56
57
    with ift.random.Context(seed):
        dom1 = ift.MultiDomain.make({'s1': space})
        dom2 = ift.MultiDomain.make({'s2': space})
        dom = ift.MultiDomain.union((dom1, dom2))
        select_s1 = ift.ducktape(None, dom1, "s1")
        select_s2 = ift.ducktape(None, dom2, "s2")
        model = select_s1*select_s2
58
        pos = ift.from_random(dom, "normal")
Philipp Arras's avatar
Philipp Arras committed
59
        ift.extra.check_operator(model, pos, ntries=ntries)
Martin Reinecke's avatar
Martin Reinecke committed
60
        model = select_s1 + select_s2
61
        pos = ift.from_random(dom, "normal")
Philipp Arras's avatar
Philipp Arras committed
62
        ift.extra.check_operator(model, pos, ntries=ntries)
Martin Reinecke's avatar
Martin Reinecke committed
63
        model = select_s1.scale(3.)
64
        pos = ift.from_random(dom1, "normal")
Philipp Arras's avatar
Philipp Arras committed
65
        ift.extra.check_operator(model, pos, ntries=ntries)
Martin Reinecke's avatar
Martin Reinecke committed
66
        model = ift.ScalingOperator(space, 2.456)(select_s1*select_s2)
67
        pos = ift.from_random(dom, "normal")
Philipp Arras's avatar
Philipp Arras committed
68
        ift.extra.check_operator(model, pos, ntries=ntries)
Martin Reinecke's avatar
Martin Reinecke committed
69
        model = (2.456*(select_s1*select_s2)).ptw("sigmoid")
70
        pos = ift.from_random(dom, "normal")
Philipp Arras's avatar
Philipp Arras committed
71
        ift.extra.check_operator(model, pos, ntries=ntries)
72
73
        pos = ift.from_random(dom, "normal")
        model = ift.OuterProduct(ift.makeDomain(space), pos['s1'])
Philipp Arras's avatar
Philipp Arras committed
74
        ift.extra.check_operator(model, pos['s2'], ntries=ntries)
Martin Reinecke's avatar
Martin Reinecke committed
75
        model = select_s1**2
76
        pos = ift.from_random(dom1, "normal")
Philipp Arras's avatar
Philipp Arras committed
77
        ift.extra.check_operator(model, pos, ntries=ntries)
Martin Reinecke's avatar
Martin Reinecke committed
78
        model = select_s1.clip(-1, 1)
79
        pos = ift.from_random(dom1, "normal")
Philipp Arras's avatar
Philipp Arras committed
80
        ift.extra.check_operator(model, pos, ntries=ntries)
81
        f = ift.from_random(space, "normal")
Martin Reinecke's avatar
Martin Reinecke committed
82
        model = select_s1.clip(f-0.1, f+1.)
83
        pos = ift.from_random(dom1, "normal")
Philipp Arras's avatar
Philipp Arras committed
84
        ift.extra.check_operator(model, pos, ntries=ntries)
Martin Reinecke's avatar
Martin Reinecke committed
85
86
        if isinstance(space, ift.RGSpace):
            model = ift.FFTOperator(space)(select_s1*select_s2)
87
            pos = ift.from_random(dom, "normal")
Philipp Arras's avatar
Philipp Arras committed
88
            ift.extra.check_operator(model, pos, ntries=ntries)
Philipp Arras's avatar
Philipp Arras committed
89
90


Philipp Arras's avatar
Philipp Arras committed
91
def testInverseGamma(space, seed):
Martin Reinecke's avatar
Martin Reinecke committed
92
    with ift.random.Context(seed):
93
        pos = ift.from_random(space, 'normal')
Martin Reinecke's avatar
Martin Reinecke committed
94
95
96
        alpha = 1.5
        q = 0.73
        model = ift.InverseGammaOperator(space, alpha, q)
Philipp Arras's avatar
Philipp Arras committed
97
        ift.extra.check_operator(model, pos, ntries=20)
Philipp Arras's avatar
Philipp Arras committed
98
99
        model = ift.LogInverseGammaOperator(space, alpha, q)
        ift.extra.check_operator(model, pos, ntries=20)
Philipp Arras's avatar
Philipp Arras committed
100
101
102
103
104
105
106
107
108


@pmp("loc", [0, 13.2])
@pmp("scale", [1, 551.09])
@pmp("op", [ift.UniformOperator, ift.LaplaceOperator])
def testSpecialDistributionOps(space, seed, loc, scale, op):
    with ift.random.Context(seed):
        pos = ift.from_random(space, 'normal')
        model = op(space, loc, scale)
Vincent Eberle's avatar
Vincent Eberle committed
109
        ift.extra.check_operator(model, pos, ntries=20)
Martin Reinecke's avatar
Martin Reinecke committed
110

111

Philipp Arras's avatar
Philipp Arras committed
112
113
@pmp('neg', [True, False])
def testAdder(space, seed, neg):
Martin Reinecke's avatar
Martin Reinecke committed
114
    with ift.random.Context(seed):
115
116
        f = ift.from_random(space, 'normal')
        f1 = ift.from_random(space, 'normal')
Martin Reinecke's avatar
Martin Reinecke committed
117
        op = ift.Adder(f1, neg)
Philipp Arras's avatar
Philipp Arras committed
118
        ift.extra.check_operator(op, f, ntries=ntries)
Martin Reinecke's avatar
Martin Reinecke committed
119
        op = ift.Adder(f1.val.ravel()[0], neg=neg, domain=space)
Philipp Arras's avatar
Philipp Arras committed
120
        ift.extra.check_operator(op, f, ntries=ntries)
Philipp Arras's avatar
Philipp Arras committed
121
122


Philipp Arras's avatar
Philipp Arras committed
123
124
125
126
127
128
129
def testAbs(space, seed):
    with ift.random.Context(seed):
        f = ift.from_random(space, 'normal')
        op = abs(ift.ScalingOperator(space, 1.))
        ift.extra.check_operator(op, f, ntries=ntries)


Philipp Arras's avatar
Philipp Arras committed
130
@pmp('target', [ift.RGSpace(64, distances=.789, harmonic=True),
Philipp Arras's avatar
Philipp Arras committed
131
                ift.RGSpace([10, 10], distances=.789, harmonic=True)])
Martin Reinecke's avatar
Martin Reinecke committed
132
133
@pmp('causal', [True, False])
@pmp('minimum_phase', [True, False])
Philipp Frank's avatar
Philipp Frank committed
134
def testDynamicModel(target, causal, minimum_phase, seed):
Martin Reinecke's avatar
Martin Reinecke committed
135
    with ift.random.Context(seed):
Philipp Arras's avatar
Philipp Arras committed
136
137
138
139
140
141
142
        dct = {'target': target,
               'harmonic_padding': None,
               'sm_s0': 3.,
               'sm_x0': 1.,
               'key': 'f',
               'causal': causal,
               'minimum_phase': minimum_phase}
Martin Reinecke's avatar
Martin Reinecke committed
143
        model, _ = ift.dynamic_operator(**dct)
144
        pos = ift.from_random(model.domain, 'normal')
145
        ift.extra.check_operator(model, pos, ntries=ntries)
Martin Reinecke's avatar
Martin Reinecke committed
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
        if len(target.shape) > 1:
            dct = {
                'target': target,
                'harmonic_padding': None,
                'sm_s0': 3.,
                'sm_x0': 1.,
                'key': 'f',
                'lightcone_key': 'c',
                'sigc': 1.,
                'quant': 5,
                'causal': causal,
                'minimum_phase': minimum_phase
            }
            dct['lightcone_key'] = 'c'
            dct['sigc'] = 1.
            dct['quant'] = 5
            model, _ = ift.dynamic_lightcone_operator(**dct)
163
            pos = ift.from_random(model.domain, 'normal')
164
            ift.extra.check_operator(model, pos, ntries=ntries)
Philipp Arras's avatar
Philipp Arras committed
165
166
167
168
169
170
171
172
173


@pmp('h_space', _h_spaces)
@pmp('specialbinbounds', [True, False])
@pmp('logarithmic', [True, False])
@pmp('nbin', [3, None])
def testNormalization(h_space, specialbinbounds, logarithmic, nbin):
    if not specialbinbounds and (not logarithmic or nbin is not None):
        return
Philipp Arras's avatar
Philipp Arras committed
174
    binbounds = None
Philipp Arras's avatar
Philipp Arras committed
175
176
177
178
    if specialbinbounds:
        binbounds = ift.PowerSpace.useful_binbounds(h_space, logarithmic, nbin)
    dom = ift.PowerSpace(h_space, binbounds)
    op = ift.library.correlated_fields._Normalization(dom)
179
    pos = 0.1 * ift.from_random(op.domain, 'normal')
Philipp Arras's avatar
Philipp Arras committed
180
    ift.extra.check_operator(op, pos, ntries=10)
Philipp Arras's avatar
Philipp Arras committed
181
182


Lukas Platz's avatar
Lukas Platz committed
183
184
185
186
@pmp('N', [1, 20])
def testLognormalTransform(N):
    op = ift.LognormalTransform(1, 0.2, '', N)
    loc = ift.from_random(op.domain)
Philipp Arras's avatar
Philipp Arras committed
187
    ift.extra.check_operator(op, loc, ntries=10)