test_convolution_operators.py 1.87 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
import numpy as np
Philipp Arras's avatar
Philipp Arras committed
19
from numpy.testing import assert_allclose
Lukas Platz's avatar
Lukas Platz committed
20

Philipp Arras's avatar
Philipp Arras committed
21
import nifty8 as ift
Lukas Platz's avatar
Lukas Platz committed
22

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

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


def test_const_func(space):
33
    sig = ift.Field.from_random(domain=space, random_type='normal')
Philipp Arras's avatar
Philipp Arras committed
34
    fco_op = ift.FuncConvolutionOperator(space, lambda x: np.ones(x.shape))
Martin Reinecke's avatar
stage 3    
Martin Reinecke committed
35
    vals = fco_op(sig).val
Lukas Platz's avatar
Lukas Platz committed
36
37
38
    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)
49
    sig = ift.Field.from_random(dom, 'normal').ptw("exp")
Lukas Platz's avatar
Lukas Platz committed
50
51
    fco_op = ift.FuncConvolutionOperator(dom, lambda x: gauss(x, sigma))
    sm_op = ift.HarmonicSmoothingOperator(dom, sigma)
Martin Reinecke's avatar
stage 3    
Martin Reinecke committed
52
53
    assert_allclose(fco_op(sig).val,
                    sm_op(sig).val,
Lukas Platz's avatar
Lukas Platz committed
54
                    rtol=1e-05)
Martin Reinecke's avatar
stage 3    
Martin Reinecke committed
55
56
    assert_allclose(fco_op.adjoint_times(sig).val,
                    sm_op.adjoint_times(sig).val,
Lukas Platz's avatar
Lukas Platz committed
57
                    rtol=1e-05)