test_harmonic_transform_operator.py 2.46 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
# 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/>.
#
14
# Copyright(C) 2013-2019 Max-Planck-Society
15
#
16
# NIFTy is being developed at the Max-Planck-Institut fuer Astrophysik.
17
18

import numpy as np
Philipp Arras's avatar
Philipp Arras committed
19
import pytest
20
21
from numpy.testing import assert_allclose

Martin Reinecke's avatar
5->6    
Martin Reinecke committed
22
import nifty6 as ift
Philipp Arras's avatar
Philipp Arras committed
23

24
from ..common import list2fixture, setup_function, teardown_function
Philipp Arras's avatar
Philipp Arras committed
25

26
27
28
29
30
31
32
33

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


Philipp Arras's avatar
Philipp Arras committed
34
35
36
37
38
39
40
41
42
43
44
45
46
tp = list2fixture([np.float64, np.float32, np.complex64, np.complex128])
lm = list2fixture([128, 256])
pmp = pytest.mark.parametrize


def test_dotsht(lm, tp):
    tol = 10*_get_rtol(tp)
    a = ift.LMSpace(lmax=lm)
    b = ift.GLSpace(nlat=lm + 1)
    fft = ift.HarmonicTransformOperator(domain=a, target=b)
    inp = ift.Field.from_random(
        domain=a, random_type='normal', std=1, mean=0, dtype=tp)
    out = fft.times(inp)
Martin Reinecke's avatar
Martin Reinecke committed
47
48
    v1 = np.sqrt(out.s_vdot(out))
    v2 = np.sqrt(inp.s_vdot(fft.adjoint_times(out)))
Philipp Arras's avatar
Philipp Arras committed
49
50
51
52
53
54
55
56
57
58
59
    assert_allclose(v1, v2, rtol=tol, atol=tol)


def test_dotsht2(lm, tp):
    tol = 10*_get_rtol(tp)
    a = ift.LMSpace(lmax=lm)
    b = ift.HPSpace(nside=lm//2)
    fft = ift.HarmonicTransformOperator(domain=a, target=b)
    inp = ift.Field.from_random(
        domain=a, random_type='normal', std=1, mean=0, dtype=tp)
    out = fft.times(inp)
Martin Reinecke's avatar
Martin Reinecke committed
60
61
    v1 = np.sqrt(out.s_vdot(out))
    v2 = np.sqrt(inp.s_vdot(fft.adjoint_times(out)))
Philipp Arras's avatar
Philipp Arras committed
62
    assert_allclose(v1, v2, rtol=tol, atol=tol)
63
64


Philipp Arras's avatar
Philipp Arras committed
65
66
67
68
69
70
71
72
73
74
@pmp('space', [ift.LMSpace(lmax=30, mmax=25)])
def test_normalisation(space, tp):
    tol = 10*_get_rtol(tp)
    cospace = space.get_default_codomain()
    fft = ift.HarmonicTransformOperator(space, cospace)
    inp = ift.Field.from_random(
        domain=space, random_type='normal', std=1, mean=2, dtype=tp)
    out = fft.times(inp)
    zero_idx = tuple([0]*len(space.shape))
    assert_allclose(
Martin Reinecke's avatar
Martin Reinecke committed
75
        inp.val[zero_idx], out.s_integrate(), rtol=tol, atol=tol)