test_convolution_operators.py 1.89 KB
Newer Older
Lukas Platz's avatar
Lukas Platz committed
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-2019 Max-Planck-Society
#
# NIFTy is being developed at the Max-Planck-Institut fuer Astrophysik.

Philipp Arras's avatar
Philipp Arras committed
18
from numpy.testing import assert_allclose
Lukas Platz's avatar
Lukas Platz committed
19
20
21
22

import nifty5 as ift
import numpy as np

Lukas Platz's avatar
Lukas Platz committed
23
24
25
26
27
28
29
30
31
32
33
from ..common import list2fixture

space = list2fixture([
    ift.RGSpace(4),
    ift.HPSpace(4),
    ift.GLSpace(4)
])


def test_const_func(space):
    sig = ift.Field.from_random('normal', domain=space)
Philipp Arras's avatar
Philipp Arras committed
34
    fco_op = ift.FuncConvolutionOperator(space, lambda x: np.ones(x.shape))
Lukas Platz's avatar
Lukas Platz committed
35
36
37
38
    vals = fco_op(sig).to_global_data()
    vals = np.round(vals, decimals=5)
    assert len(np.unique(vals)) == 1

Lukas Platz's avatar
Lukas Platz committed
39
40
41
42
43
44

def gauss(x, sigma):
    normalization = np.sqrt(2. * np.pi) * sigma
    return np.exp(-0.5 * x * x / sigma**2) / normalization


Lukas Platz's avatar
Lukas Platz committed
45
def test_gaussian_smoothing():
Lukas Platz's avatar
Lukas Platz committed
46
    N = 128
Lukas Platz's avatar
Lukas Platz committed
47
    sigma = N / 10**4
Lukas Platz's avatar
Lukas Platz committed
48
    dom = ift.RGSpace(N)
Lukas Platz's avatar
Lukas Platz committed
49
    sig = ift.exp(ift.Field.from_random('normal', dom))
Lukas Platz's avatar
Lukas Platz committed
50
51
52
53
54
55
56
57
    fco_op = ift.FuncConvolutionOperator(dom, lambda x: gauss(x, sigma))
    sm_op = ift.HarmonicSmoothingOperator(dom, sigma)
    assert_allclose(fco_op(sig).to_global_data(),
                    sm_op(sig).to_global_data(),
                    rtol=1e-05)
    assert_allclose(fco_op.adjoint_times(sig).to_global_data(),
                    sm_op.adjoint_times(sig).to_global_data(),
                    rtol=1e-05)