test_sugar.py 2.04 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# 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.

import numpy as np
Philipp Arras's avatar
Philipp Arras committed
19
from numpy.testing import assert_equal
20

Martin Reinecke's avatar
5->6    
Martin Reinecke committed
21
import nifty6 as ift
22
from .common import setup_function, teardown_function
23
24
25
26
27


def test_get_signal_variance():
    space = ift.RGSpace(3)
    hspace = space.get_default_codomain()
Philipp Arras's avatar
Philipp Arras committed
28
29
    sv = ift.get_signal_variance(lambda x: np.ones_like(x), hspace)
    assert_equal(sv, 3.)
30
31
32

    space = ift.RGSpace(3, distances=1.)
    hspace = space.get_default_codomain()
Martin Reinecke's avatar
Martin Reinecke committed
33

34
35
    def spec2(k):
        t = np.zeros_like(k)
Martin Reinecke's avatar
Martin Reinecke committed
36
        t[k == 0] = 1.
37
        return t
38
    assert_equal(ift.get_signal_variance(spec2, hspace), 1/9.)
Philipp Arras's avatar
Philipp Arras committed
39
40
41
42
43


def test_exec_time():
    dom = ift.RGSpace(12, harmonic=True)
    op = ift.HarmonicTransformOperator(dom)
Martin Reinecke's avatar
Martin Reinecke committed
44
    op1 = op.ptw("exp")
Philipp Arras's avatar
Philipp Arras committed
45
    lh = ift.GaussianEnergy(domain=op.target, sampling_dtype=np.float64) @ op1
Philipp Arras's avatar
Philipp Arras committed
46
47
48
49
50
51
52
53
54
55
56
    ic = ift.GradientNormController(iteration_limit=2)
    ham = ift.StandardHamiltonian(lh, ic_samp=ic)
    kl = ift.MetricGaussianKL(ift.full(ham.domain, 0.), ham, 1)
    ops = [op, op1, lh, ham, kl]
    for oo in ops:
        for wm in [True, False]:
            ift.exec_time(oo, wm)


def test_calc_pos():
    dom = ift.RGSpace(12, harmonic=True)
Martin Reinecke's avatar
Martin Reinecke committed
57
    op = ift.HarmonicTransformOperator(dom).ptw("exp")
58
    fld = op(0.1 * ift.from_random(op.domain, 'normal'))
Philipp Arras's avatar
Philipp Arras committed
59
60
    pos = ift.calculate_position(op, fld)
    ift.extra.assert_allclose(op(pos), fld, 1e-1, 1e-1)