test_smoothing_operator.py 4.42 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
14
15
16
17
#
# Copyright(C) 2013-2017 Max-Planck-Society
#
# 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
import numpy as np
Martin Reinecke's avatar
Martin Reinecke committed
21
from numpy.testing import assert_allclose
22

Martin Reinecke's avatar
Martin Reinecke committed
23
from nifty2go import Field,\
24
25
    RGSpace,\
    PowerSpace,\
26
27
    FFTSmoothingOperator,\
    DirectSmoothingOperator
28

29
from itertools import product
30
from test.common import expand
31
32
33
34
35
36
37
38


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
39

40
class SmoothingOperator_Tests(unittest.TestCase):
41
    spaces = [RGSpace(128)]
42

43
44
    @expand(product(spaces, [0., .5, 5.]))
    def test_property(self, space, sigma):
45
        op = FFTSmoothingOperator(space, sigma=sigma)
46
47
        if op.domain[0] != space:
            raise TypeError
Martin Reinecke's avatar
Martin Reinecke committed
48
        if op.unitary:
49
            raise ValueError
Martin Reinecke's avatar
Martin Reinecke committed
50
        if not op.self_adjoint:
51
52
            raise ValueError

53
54
    @expand(product(spaces, [0., .5, 5.]))
    def test_adjoint_times(self, space, sigma):
55
        op = FFTSmoothingOperator(space, sigma=sigma)
56
57
        rand1 = Field.from_random('normal', domain=space)
        rand2 = Field.from_random('normal', domain=space)
Martin Reinecke's avatar
Martin Reinecke committed
58
59
        tt1 = rand1.vdot(op.times(rand2))
        tt2 = rand2.vdot(op.adjoint_times(rand1))
60
        assert_allclose(tt1, tt2)
61

62
63
    @expand(product(spaces, [0., .5, 5.]))
    def test_times(self, space, sigma):
64
        op = FFTSmoothingOperator(space, sigma=sigma)
65
        rand1 = Field.zeros(space)
66
67
        rand1.val[0] = 1.
        tt1 = op.times(rand1)
68
        assert_allclose(1, tt1.sum())
69

70
    @expand(product([128, 256], [1, 0.4], [0., 1.,  3.7],
71
72
73
                    [np.float64, np.complex128]))
    def test_smooth_regular1(self, sz, d, sigma, tp):
        tol = _get_rtol(tp)
Martin Reinecke's avatar
Martin Reinecke committed
74
        sp = RGSpace(sz, distances=d)
75
        smo = FFTSmoothingOperator(sp, sigma=sigma)
76
77
78
79
80
81
82
83
84
        inp = Field.from_random(domain=sp, random_type='normal', std=1, mean=4,
                                dtype=tp)
        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
85
        sp = RGSpace([sz1, sz2], distances=[d1, d2])
86
        smo = FFTSmoothingOperator(sp, sigma=sigma)
87
88
89
90
91
92
93
94
95
96
        inp = Field.from_random(domain=sp, random_type='normal', std=1, mean=4,
                                dtype=tp)
        out = smo(inp)
        assert_allclose(inp.sum(), out.sum(), rtol=tol, atol=tol)

    @expand(product([100, 200], [False, True], [0., 1.,  3.7],
                    [np.float64, np.complex128]))
    def test_smooth_irregular1(self, sz, log, sigma, tp):
        tol = _get_rtol(tp)
        sp = RGSpace(sz, harmonic=True)
97
98
        bb = PowerSpace.useful_binbounds(sp, logarithmic=log)
        ps = PowerSpace(sp, binbounds=bb)
99
        smo = DirectSmoothingOperator(ps, sigma=sigma)
100
101
102
103
104
105
106
107
108
109
        inp = Field.from_random(domain=ps, random_type='normal', std=1, mean=4,
                                dtype=tp)
        out = smo(inp)
        assert_allclose(inp.sum(), out.sum(), rtol=tol, atol=tol)

    @expand(product([10, 15], [7, 10], [False, True], [0., 1.,  3.7],
                    [np.float64, np.complex128]))
    def test_smooth_irregular2(self, sz1, sz2, log, sigma, tp):
        tol = _get_rtol(tp)
        sp = RGSpace([sz1, sz2], harmonic=True)
110
111
        bb = PowerSpace.useful_binbounds(sp, logarithmic=log)
        ps = PowerSpace(sp, binbounds=bb)
112
        smo = DirectSmoothingOperator(ps, sigma=sigma)
113
114
115
116
        inp = Field.from_random(domain=ps, random_type='normal', std=1, mean=4,
                                dtype=tp)
        out = smo(inp)
        assert_allclose(inp.sum(), out.sum(), rtol=tol, atol=tol)