test_smoothing_operator.py 3.4 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# NIFTy
# Copyright (C) 2017  Theo Steininger
#
# Author: Theo Steininger
#
# 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/>.

import unittest
import numpy as np
from numpy.testing import assert_equal,\
    assert_allclose
from nifty.config import dependency_injector as di
from nifty import Field,\
    RGSpace,\
    PowerSpace,\
    SmoothingOperator
from itertools import product
from test.common import expand


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


class Misc_Tests(unittest.TestCase):

    @expand(product([100, 200], [1, 0.4], [0., 1.,  3.7],
                    [np.float64, np.complex128]))
    def test_smooth_regular1(self, sz, d, sigma, tp):
        tol = _get_rtol(tp)
        sp = RGSpace(sz, harmonic=True, distances=d)
        smo = SmoothingOperator(sp, sigma=sigma)
        inp = Field.from_random(domain=sp, random_type='normal', std=1, mean=4,
                                dtype=tp)
        out = smo(inp)
        inp = inp.val.get_full_data()
        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)
        sp = RGSpace([sz1, sz2], distances=[d1, d2], harmonic=True)
        smo = SmoothingOperator(sp, sigma=sigma)
        inp = Field.from_random(domain=sp, random_type='normal', std=1, mean=4,
                                dtype=tp)
        out = smo(inp)
        inp = inp.val.get_full_data()
        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)
        ps = PowerSpace(sp, nbin=sz, logarithmic=log)
        smo = SmoothingOperator(ps, sigma=sigma)
        inp = Field.from_random(domain=ps, random_type='normal', std=1, mean=4,
                                dtype=tp)
        out = smo(inp)
        inp = inp.val.get_full_data()
        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)
        ps = PowerSpace(sp, logarithmic=log)
        smo = SmoothingOperator(ps, sigma=sigma)
        inp = Field.from_random(domain=ps, random_type='normal', std=1, mean=4,
                                dtype=tp)
        out = smo(inp)
        inp = inp.val.get_full_data()
        assert_allclose(inp.sum(), out.sum(), rtol=tol, atol=tol)