extra.py 7.53 KB
Newer Older
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-2019 Max-Planck-Society
15
#
16
# NIFTy is being developed at the Max-Planck-Institut fuer Astrophysik.
17
18

import numpy as np
Philipp Arras's avatar
Philipp Arras committed
19

20
from .domain_tuple import DomainTuple
Martin Reinecke's avatar
fix    
Martin Reinecke committed
21
22
from .field import Field
from .linearization import Linearization
23
from .multi_domain import MultiDomain
24
from .operators.linear_operator import LinearOperator
Martin Reinecke's avatar
fix    
Martin Reinecke committed
25
from .sugar import from_random
26

Martin Reinecke's avatar
Martin Reinecke committed
27
__all__ = ["consistency_check", "check_jacobian_consistency"]
28

Philipp Arras's avatar
Philipp Arras committed
29

Martin Reinecke's avatar
Martin Reinecke committed
30
31
32
33
34
35
36
37
def _assert_allclose(f1, f2, atol, rtol):
    if isinstance(f1, Field):
        return np.testing.assert_allclose(f1.local_data, f2.local_data,
                                          atol=atol, rtol=rtol)
    for key, val in f1.items():
        _assert_allclose(val, f2[key], atol=atol, rtol=rtol)


38
39
def _adjoint_implementation(op, domain_dtype, target_dtype, atol, rtol,
                            only_r_linear):
Martin Reinecke's avatar
Martin Reinecke committed
40
41
42
43
44
45
46
    needed_cap = op.TIMES | op.ADJOINT_TIMES
    if (op.capability & needed_cap) != needed_cap:
        return
    f1 = from_random("normal", op.domain, dtype=domain_dtype)
    f2 = from_random("normal", op.target, dtype=target_dtype)
    res1 = f1.vdot(op.adjoint_times(f2))
    res2 = op.times(f1).vdot(f2)
47
48
    if only_r_linear:
        res1, res2 = res1.real, res2.real
Martin Reinecke's avatar
Martin Reinecke committed
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
    np.testing.assert_allclose(res1, res2, atol=atol, rtol=rtol)


def _inverse_implementation(op, domain_dtype, target_dtype, atol, rtol):
    needed_cap = op.TIMES | op.INVERSE_TIMES
    if (op.capability & needed_cap) != needed_cap:
        return
    foo = from_random("normal", op.target, dtype=target_dtype)
    res = op(op.inverse_times(foo))
    _assert_allclose(res, foo, atol=atol, rtol=rtol)

    foo = from_random("normal", op.domain, dtype=domain_dtype)
    res = op.inverse_times(op(foo))
    _assert_allclose(res, foo, atol=atol, rtol=rtol)


65
66
67
68
def _full_implementation(op, domain_dtype, target_dtype, atol, rtol,
                         only_r_linear):
    _adjoint_implementation(op, domain_dtype, target_dtype, atol, rtol,
                            only_r_linear)
Martin Reinecke's avatar
Martin Reinecke committed
69
70
71
    _inverse_implementation(op, domain_dtype, target_dtype, atol, rtol)


72
def _check_linearity(op, domain_dtype, atol, rtol):
Martin Reinecke's avatar
Martin Reinecke committed
73
74
75
    needed_cap = op.TIMES
    if (op.capability & needed_cap) != needed_cap:
        return
76
77
    fld1 = from_random("normal", op.domain, dtype=domain_dtype)
    fld2 = from_random("normal", op.domain, dtype=domain_dtype)
Martin Reinecke's avatar
Martin Reinecke committed
78
    alpha = np.random.random()  # FIXME: this can break badly with MPI!
79
80
81
82
83
    val1 = op(alpha*fld1+fld2)
    val2 = alpha*op(fld1)+op(fld2)
    _assert_allclose(val1, val2, atol=atol, rtol=rtol)


84
85
86
def _domain_check(op):
    for dd in [op.domain, op.target]:
        if not isinstance(dd, (DomainTuple, MultiDomain)):
Martin Reinecke's avatar
Martin Reinecke committed
87
88
89
            raise TypeError(
                'The domain and the target of an operator need to',
                'be instances of either DomainTuple or MultiDomain.')
90
91


Martin Reinecke's avatar
Martin Reinecke committed
92
def consistency_check(op, domain_dtype=np.float64, target_dtype=np.float64,
93
                      atol=0, rtol=1e-7, only_r_linear=False):
Reimar H Leike's avatar
Reimar H Leike committed
94
95
96
97
    """
    Checks an operator for algebraic consistency of its capabilities.

    Checks whether times(), adjoint_times(), inverse_times() and
Philipp Arras's avatar
Philipp Arras committed
98
    adjoint_inverse_times() (if in capability list) is implemented
Reimar H Leike's avatar
Reimar H Leike committed
99
    consistently. Additionally, it checks whether the operator is linear.
Philipp Arras's avatar
Philipp Arras committed
100
101
102
103
104

    Parameters
    ----------
    op : LinearOperator
        Operator which shall be checked.
Reimar H Leike's avatar
Reimar H Leike committed
105
    domain_dtype : dtype
Philipp Arras's avatar
Philipp Arras committed
106
107
        The data type of the random vectors in the operator's domain. Default
        is `np.float64`.
Reimar H Leike's avatar
Reimar H Leike committed
108
    target_dtype : dtype
Philipp Arras's avatar
Philipp Arras committed
109
110
111
        The data type of the random vectors in the operator's target. Default
        is `np.float64`.
    atol : float
Martin Reinecke's avatar
Martin Reinecke committed
112
113
        Absolute tolerance for the check. If rtol is specified,
        then satisfying any tolerance will let the check pass.
Reimar H Leike's avatar
Reimar H Leike committed
114
        Default: 0.
Philipp Arras's avatar
Philipp Arras committed
115
    rtol : float
Martin Reinecke's avatar
Martin Reinecke committed
116
117
        Relative tolerance for the check. If atol is specified,
        then satisfying any tolerance will let the check pass.
Reimar H Leike's avatar
Reimar H Leike committed
118
        Default: 0.
119
120
121
    only_r_linear: bool
        set to True if the operator is only R-linear, not C-linear.
        This will relax the adjointness test accordingly.
Philipp Arras's avatar
Philipp Arras committed
122
    """
123
124
    if not isinstance(op, LinearOperator):
        raise TypeError('This test tests only linear operators.')
125
    _domain_check(op)
126
    _check_linearity(op, domain_dtype, atol, rtol)
Martin Reinecke's avatar
Martin Reinecke committed
127
128
129
    _check_linearity(op.adjoint, target_dtype, atol, rtol)
    _check_linearity(op.inverse, target_dtype, atol, rtol)
    _check_linearity(op.adjoint.inverse, domain_dtype, atol, rtol)
130
131
132
133
134
135
    _full_implementation(op, domain_dtype, target_dtype, atol, rtol,
                         only_r_linear)
    _full_implementation(op.adjoint, target_dtype, domain_dtype, atol, rtol,
                         only_r_linear)
    _full_implementation(op.inverse, target_dtype, domain_dtype, atol, rtol,
                         only_r_linear)
Martin Reinecke's avatar
Martin Reinecke committed
136
    _full_implementation(op.adjoint.inverse, domain_dtype, target_dtype, atol,
137
                         rtol, only_r_linear)
Martin Reinecke's avatar
Martin Reinecke committed
138
139


Martin Reinecke's avatar
Martin Reinecke committed
140
def _get_acceptable_location(op, loc, lin):
Martin Reinecke's avatar
Martin Reinecke committed
141
    if not np.isfinite(lin.val.sum()):
Martin Reinecke's avatar
Martin Reinecke committed
142
143
144
145
        raise ValueError('Initial value must be finite')
    dir = from_random("normal", loc.domain)
    dirder = lin.jac(dir)
    if dirder.norm() == 0:
Martin Reinecke's avatar
Martin Reinecke committed
146
        dir = dir * (lin.val.norm()*1e-5)
Martin Reinecke's avatar
Martin Reinecke committed
147
    else:
Martin Reinecke's avatar
Martin Reinecke committed
148
        dir = dir * (lin.val.norm()*1e-5/dirder.norm())
Martin Reinecke's avatar
Martin Reinecke committed
149
150
151
152
    # Find a step length that leads to a "reasonable" location
    for i in range(50):
        try:
            loc2 = loc+dir
153
            lin2 = op(Linearization.make_var(loc2, lin.want_metric))
Martin Reinecke's avatar
Martin Reinecke committed
154
155
156
157
158
159
160
161
162
            if np.isfinite(lin2.val.sum()) and abs(lin2.val.sum()) < 1e20:
                break
        except FloatingPointError:
            pass
        dir = dir*0.5
    else:
        raise ValueError("could not find a reasonable initial step")
    return loc2, lin2

Martin Reinecke's avatar
Martin Reinecke committed
163

Martin Reinecke's avatar
Martin Reinecke committed
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
def check_jacobian_consistency(op, loc, tol=1e-8, ntries=100):
    """
    Checks the Jacobian of an operator against its finite difference
    approximation.

    Computes the Jacobian with finite differences and compares it to the
    implemented Jacobian.

    Parameters
    ----------
    op : Operator
        Operator which shall be checked.
    loc : Field or MultiField
        An Field or MultiField instance which has the same domain
        as op. The location at which the gradient is checked
    tol : float
        Tolerance for the check.
    """
182
    _domain_check(op)
Martin Reinecke's avatar
Martin Reinecke committed
183
    for _ in range(ntries):
184
        lin = op(Linearization.make_var(loc))
Martin Reinecke's avatar
Martin Reinecke committed
185
        loc2, lin2 = _get_acceptable_location(op, loc, lin)
Martin Reinecke's avatar
Martin Reinecke committed
186
        dir = loc2-loc
Martin Reinecke's avatar
Martin Reinecke committed
187
188
189
190
        locnext = loc2
        dirnorm = dir.norm()
        for i in range(50):
            locmid = loc + 0.5*dir
191
            linmid = op(Linearization.make_var(locmid))
Martin Reinecke's avatar
Martin Reinecke committed
192
193
            dirder = linmid.jac(dir)
            numgrad = (lin2.val-lin.val)
Martin Reinecke's avatar
Martin Reinecke committed
194
            xtol = tol * dirder.norm() / np.sqrt(dirder.size)
Martin Reinecke's avatar
Martin Reinecke committed
195
            if (abs(numgrad-dirder) <= xtol).all():
Martin Reinecke's avatar
Martin Reinecke committed
196
197
198
                break
            dir = dir*0.5
            dirnorm *= 0.5
Martin Reinecke's avatar
Martin Reinecke committed
199
            loc2, lin2 = locmid, linmid
Martin Reinecke's avatar
Martin Reinecke committed
200
201
202
        else:
            raise ValueError("gradient and value seem inconsistent")
        loc = locnext