test_smoothing_operator.py 3.15 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
# 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/>.
Theo Steininger's avatar
Theo Steininger committed
13
#
Martin Reinecke's avatar
Martin Reinecke committed
14
# Copyright(C) 2013-2018 Max-Planck-Society
Theo Steininger's avatar
Theo Steininger committed
15
16
17
#
# NIFTy is being developed at the Max-Planck-Institut fuer Astrophysik
# and financially supported by the Studienstiftung des deutschen Volkes.
18
19
20

import unittest
from itertools import product
21
from test.common import expand
22

23
24
25
26
import nifty5 as ift
import numpy as np
from numpy.testing import assert_allclose

27
28
29
30
31
32
33

def _get_rtol(tp):
    if (tp == np.float64) or (tp == np.complex128):
        return 1e-10
    else:
        return 1e-5

Martin Reinecke's avatar
Martin Reinecke committed
34

35
class SmoothingOperator_Tests(unittest.TestCase):
Martin Reinecke's avatar
Martin Reinecke committed
36
    spaces = [ift.RGSpace(128)]
37

38
39
    @expand(product(spaces, [0., .5, 5.]))
    def test_property(self, space, sigma):
Martin Reinecke's avatar
Martin Reinecke committed
40
        op = ift.HarmonicSmoothingOperator(space, sigma=sigma)
41
42
43
        if op.domain[0] != space:
            raise TypeError

44
45
    @expand(product(spaces, [0., .5, 5.]))
    def test_adjoint_times(self, space, sigma):
Martin Reinecke's avatar
Martin Reinecke committed
46
        op = ift.HarmonicSmoothingOperator(space, sigma=sigma)
Martin Reinecke's avatar
Martin Reinecke committed
47
48
        rand1 = ift.Field.from_random('normal', domain=space)
        rand2 = ift.Field.from_random('normal', domain=space)
Martin Reinecke's avatar
Martin Reinecke committed
49
50
        tt1 = rand1.vdot(op.times(rand2))
        tt2 = rand2.vdot(op.adjoint_times(rand1))
51
        assert_allclose(tt1, tt2)
52

53
54
    @expand(product(spaces, [0., .5, 5.]))
    def test_times(self, space, sigma):
Martin Reinecke's avatar
Martin Reinecke committed
55
        op = ift.HarmonicSmoothingOperator(space, sigma=sigma)
Martin Reinecke's avatar
tweaks    
Martin Reinecke committed
56
57
        fld = np.zeros(space.shape, dtype=np.float64)
        fld[0] = 1.
58
        rand1 = ift.Field.from_global_data(space, fld)
59
        tt1 = op.times(rand1)
60
        assert_allclose(1, tt1.sum())
61

62
    @expand(product([128, 256], [1, 0.4], [0., 1.,  3.7],
63
64
65
                    [np.float64, np.complex128]))
    def test_smooth_regular1(self, sz, d, sigma, tp):
        tol = _get_rtol(tp)
Martin Reinecke's avatar
Martin Reinecke committed
66
        sp = ift.RGSpace(sz, distances=d)
Martin Reinecke's avatar
Martin Reinecke committed
67
        smo = ift.HarmonicSmoothingOperator(sp, sigma=sigma)
Martin Reinecke's avatar
Martin Reinecke committed
68
69
        inp = ift.Field.from_random(domain=sp, random_type='normal', std=1,
                                    mean=4, dtype=tp)
70
71
72
73
74
75
76
        out = smo(inp)
        assert_allclose(inp.sum(), out.sum(), rtol=tol, atol=tol)

    @expand(product([10, 15], [7, 10], [1, 0.4], [2, 0.3], [0., 1.,  3.7],
                    [np.float64, np.complex128]))
    def test_smooth_regular2(self, sz1, sz2, d1, d2, sigma, tp):
        tol = _get_rtol(tp)
Martin Reinecke's avatar
Martin Reinecke committed
77
        sp = ift.RGSpace([sz1, sz2], distances=[d1, d2])
Martin Reinecke's avatar
Martin Reinecke committed
78
        smo = ift.HarmonicSmoothingOperator(sp, sigma=sigma)
Martin Reinecke's avatar
Martin Reinecke committed
79
80
        inp = ift.Field.from_random(domain=sp, random_type='normal', std=1,
                                    mean=4, dtype=tp)
81
82
        out = smo(inp)
        assert_allclose(inp.sum(), out.sum(), rtol=tol, atol=tol)