operator_tests.py 2.86 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
# 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-2018 Max-Planck-Society
#
# NIFTy is being developed at the Max-Planck-Institut fuer Astrophysik
# and financially supported by the Studienstiftung des deutschen Volkes.
Philipp Arras's avatar
Move it  
Philipp Arras committed
18

19
from __future__ import absolute_import, division, print_function
Philipp Arras's avatar
Philipp Arras committed
20

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

from ..compat import *
Martin Reinecke's avatar
Martin Reinecke committed
24
from ..field import Field
Philipp Arras's avatar
Philipp Arras committed
25
from ..sugar import from_random
Philipp Arras's avatar
Move it  
Philipp Arras committed
26

27
__all__ = ["consistency_check"]
Philipp Arras's avatar
Move it  
Philipp Arras committed
28 29


Martin Reinecke's avatar
Martin Reinecke committed
30 31 32 33 34 35 36
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)

Martin Reinecke's avatar
Martin Reinecke committed
37

38 39 40 41
def adjoint_implementation(op, domain_dtype, target_dtype, atol, rtol):
    needed_cap = op.TIMES | op.ADJOINT_TIMES
    if (op.capability & needed_cap) != needed_cap:
        return
42 43 44
    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))
Philipp Arras's avatar
Fixups  
Philipp Arras committed
45
    res2 = op.times(f1).vdot(f2)
Philipp Arras's avatar
Move it  
Philipp Arras committed
46 47 48
    np.testing.assert_allclose(res1, res2, atol=atol, rtol=rtol)


49 50 51 52
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
53 54
    foo = from_random("normal", op.target, dtype=target_dtype)
    res = op(op.inverse_times(foo))
Martin Reinecke's avatar
Martin Reinecke committed
55
    _assert_allclose(res, foo, atol=atol, rtol=rtol)
Philipp Arras's avatar
Move it  
Philipp Arras committed
56

57 58
    foo = from_random("normal", op.domain, dtype=domain_dtype)
    res = op.inverse_times(op(foo))
Martin Reinecke's avatar
Martin Reinecke committed
59
    _assert_allclose(res, foo, atol=atol, rtol=rtol)
Philipp Arras's avatar
Move it  
Philipp Arras committed
60

61

62 63 64
def full_implementation(op, domain_dtype, target_dtype, atol, rtol):
    adjoint_implementation(op, domain_dtype, target_dtype, atol, rtol)
    inverse_implementation(op, domain_dtype, target_dtype, atol, rtol)
65 66


67 68 69 70 71 72 73
def consistency_check(op, domain_dtype=np.float64, target_dtype=np.float64,
                      atol=0, rtol=1e-7):
    full_implementation(op, domain_dtype, target_dtype, atol, rtol)
    full_implementation(op.adjoint, target_dtype, domain_dtype, atol, rtol)
    full_implementation(op.inverse, target_dtype, domain_dtype, atol, rtol)
    full_implementation(op.adjoint.inverse, domain_dtype, target_dtype, atol,
                        rtol)