test_jacobian.py 5.96 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
# 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
20
from numpy.testing import assert_
Philipp Arras's avatar
Philipp Arras committed
21
22
23

import nifty5 as ift

24
from ..common import list2fixture
Philipp Arras's avatar
Philipp Arras committed
25
26
27
28
29
30
31
32
33
34
35

pmp = pytest.mark.parametrize
space = list2fixture([
    ift.GLSpace(15),
    ift.RGSpace(64, distances=.789),
    ift.RGSpace([32, 32], distances=.789)
])
space1 = space
seed = list2fixture([4, 78, 23])


Philipp Arras's avatar
Philipp Arras committed
36
def testBasics(space, seed):
Philipp Arras's avatar
Philipp Arras committed
37
38
39
    np.random.seed(seed)
    S = ift.ScalingOperator(1., space)
    s = S.draw_sample()
Philipp Arras's avatar
Philipp Arras committed
40
    var = ift.Linearization.make_var(s)
Philipp Arras's avatar
Philipp Arras committed
41
    model = ift.ScalingOperator(6., var.target)
Martin Reinecke's avatar
Martin Reinecke committed
42
    ift.extra.check_jacobian_consistency(model, var.val)
Philipp Arras's avatar
Philipp Arras committed
43
44
45
46
47
48
49


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


def testModelLibrary(space, seed):
    # Tests amplitude model and coorelated field model
    np.random.seed(seed)
91
    domain = ift.PowerSpace(space.get_default_codomain())
92
93
    model = ift.SLAmplitude(target=domain, n_pix=4, a=.5, k0=2, sm=3, sv=1.5,
                            im=1.75, iv=1.3)
94
    assert_(isinstance(model, ift.Operator))
Philipp Arras's avatar
Philipp Arras committed
95
96
    S = ift.ScalingOperator(1., model.domain)
    pos = S.draw_sample()
Martin Reinecke's avatar
Martin Reinecke committed
97
    ift.extra.check_jacobian_consistency(model, pos, ntries=20)
Philipp Arras's avatar
Philipp Arras committed
98
99
100
101

    model2 = ift.CorrelatedField(space, model)
    S = ift.ScalingOperator(1., model2.domain)
    pos = S.draw_sample()
Martin Reinecke's avatar
Martin Reinecke committed
102
    ift.extra.check_jacobian_consistency(model2, pos, ntries=20)
Philipp Arras's avatar
Philipp Arras committed
103

104
105
106
107
    domtup = ift.DomainTuple.make((space, space))
    model3 = ift.MfCorrelatedField(domtup, [model, model])
    S = ift.ScalingOperator(1., model3.domain)
    pos = S.draw_sample()
Martin Reinecke's avatar
Martin Reinecke committed
108
    ift.extra.check_jacobian_consistency(model3, pos, ntries=20)
109

Philipp Arras's avatar
Philipp Arras committed
110
111
112
113
114
115

def testPointModel(space, seed):
    S = ift.ScalingOperator(1., space)
    pos = S.draw_sample()
    alpha = 1.5
    q = 0.73
Philipp Arras's avatar
Fixups    
Philipp Arras committed
116
    model = ift.InverseGammaOperator(space, alpha, q)
Philipp Arras's avatar
Philipp Arras committed
117
    # FIXME All those cdfs and ppfs are not very accurate
Martin Reinecke's avatar
Martin Reinecke committed
118
    ift.extra.check_jacobian_consistency(model, pos, tol=1e-2, ntries=20)
Martin Reinecke's avatar
Martin Reinecke committed
119

120

Philipp Frank's avatar
Philipp Frank committed
121
@pmp('target', [
Martin Reinecke's avatar
Martin Reinecke committed
122
123
124
    ift.RGSpace(64, distances=.789, harmonic=True),
    ift.RGSpace([32, 32], distances=.789, harmonic=True),
    ift.RGSpace([32, 32, 8], distances=.789, harmonic=True)
125
])
Martin Reinecke's avatar
Martin Reinecke committed
126
127
128
@pmp('causal', [True, False])
@pmp('minimum_phase', [True, False])
@pmp('seed', [4, 78, 23])
Philipp Frank's avatar
Philipp Frank committed
129
130
131
132
133
134
135
136
137
138
139
def testDynamicModel(target, causal, minimum_phase, seed):
    dct = {
            'target': target,
            'harmonic_padding': None,
            'sm_s0': 3.,
            'sm_x0': 1.,
            'key': 'f',
            'causal': causal,
            'minimum_phase': minimum_phase
            }
    model, _ = ift.dynamic_operator(**dct)
Martin Reinecke's avatar
Martin Reinecke committed
140
141
142
    S = ift.ScalingOperator(1., model.domain)
    pos = S.draw_sample()
    # FIXME I dont know why smaller tol fails for 3D example
Martin Reinecke's avatar
Martin Reinecke committed
143
    ift.extra.check_jacobian_consistency(model, pos, tol=1e-5, ntries=20)
Philipp Frank's avatar
Philipp Frank committed
144
    if len(target.shape) > 1:
145
        dct = {
Philipp Frank's avatar
Philipp Frank committed
146
            'target': target,
147
148
149
150
151
152
153
154
155
156
            'harmonic_padding': None,
            'sm_s0': 3.,
            'sm_x0': 1.,
            'key': 'f',
            'lightcone_key': 'c',
            'sigc': 1.,
            'quant': 5,
            'causal': causal,
            'minimum_phase': minimum_phase
        }
Philipp Frank's avatar
Philipp Frank committed
157
158
159
        dct['lightcone_key'] = 'c'
        dct['sigc'] = 1.
        dct['quant'] = 5
160
        model, _ = ift.dynamic_lightcone_operator(**dct)
Martin Reinecke's avatar
Martin Reinecke committed
161
162
163
        S = ift.ScalingOperator(1., model.domain)
        pos = S.draw_sample()
        # FIXME I dont know why smaller tol fails for 3D example
Martin Reinecke's avatar
Martin Reinecke committed
164
        ift.extra.check_jacobian_consistency(
165
            model, pos, tol=1e-5, ntries=20)