test_misc.py 4.92 KB
Newer Older
Martin Reinecke's avatar
Martin Reinecke committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# 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
Theo Steininger's avatar
Theo Steininger committed
22
23
from numpy.testing import assert_equal,\
    assert_allclose
24
from nifty.config import dependency_injector as di
Martin Reinecke's avatar
Martin Reinecke committed
25
26

from nifty import Field,\
Theo Steininger's avatar
Theo Steininger committed
27
28
29
30
31
32
    RGSpace,\
    LMSpace,\
    RGRGTransformation, \
    LMGLTransformation, \
    LMHPTransformation, \
    FFTOperator
Martin Reinecke's avatar
Martin Reinecke committed
33

Martin Reinecke's avatar
Martin Reinecke committed
34
35
36
from itertools import product
from test.common import expand

37
38
from nose.plugins.skip import SkipTest

Theo Steininger's avatar
Theo Steininger committed
39

Martin Reinecke's avatar
Martin Reinecke committed
40
def _harmonic_type(itp):
Theo Steininger's avatar
Theo Steininger committed
41
42
43
44
45
    otp = itp
    if otp == np.float64:
        otp = np.complex128
    elif otp == np.float32:
        otp = np.complex64
Martin Reinecke's avatar
Martin Reinecke committed
46
47
    return otp

Theo Steininger's avatar
Theo Steininger committed
48

Martin Reinecke's avatar
Martin Reinecke committed
49
def _get_rtol(tp):
Theo Steininger's avatar
Theo Steininger committed
50
    if (tp == np.float64) or (tp == np.complex128):
Martin Reinecke's avatar
Martin Reinecke committed
51
52
53
        return 1e-10
    else:
        return 1e-5
Martin Reinecke's avatar
Martin Reinecke committed
54

Theo Steininger's avatar
Theo Steininger committed
55

Martin Reinecke's avatar
Martin Reinecke committed
56
class Misc_Tests(unittest.TestCase):
Theo Steininger's avatar
Theo Steininger committed
57
58
59
    @expand(product([10, 11], [False, True], [0.1, 1, 3.7]))
    def test_RG_distance_1D(self, dim1, zc1, d):
        foo = RGSpace([dim1], zerocenter=zc1, distances=d)
Martin Reinecke's avatar
Martin Reinecke committed
60
        res = foo.get_distance_array('not')
Theo Steininger's avatar
Theo Steininger committed
61
        assert_equal(res[zc1 * (dim1 // 2)], 0.)
Martin Reinecke's avatar
Martin Reinecke committed
62

Theo Steininger's avatar
Theo Steininger committed
63
64
    @expand(product([10, 11], [9, 28], [False, True], [False, True],
                    [0.1, 1, 3.7]))
Martin Reinecke's avatar
Martin Reinecke committed
65
    def test_RG_distance_2D(self, dim1, dim2, zc1, zc2, d):
Theo Steininger's avatar
Theo Steininger committed
66
        foo = RGSpace([dim1, dim2], zerocenter=[zc1, zc2], distances=d)
Martin Reinecke's avatar
Martin Reinecke committed
67
        res = foo.get_distance_array('not')
Theo Steininger's avatar
Theo Steininger committed
68
        assert_equal(res[zc1 * (dim1 // 2), zc2 * (dim2 // 2)], 0.)
Martin Reinecke's avatar
Martin Reinecke committed
69

Theo Steininger's avatar
Theo Steininger committed
70
71
72
73
74
    @expand(product(["numpy", "fftw"], [10, 11], [False, True], [False, True],
                    [0.1, 1, 3.7],
                    [np.float64, np.complex128, np.float32, np.complex64]))
    def test_fft1D(self, module, dim1, zc1, zc2, d, itp):
        if module == "fftw" and "pyfftw" not in di:
Martin Reinecke's avatar
Martin Reinecke committed
75
            raise SkipTest
Theo Steininger's avatar
Theo Steininger committed
76
        tol = _get_rtol(itp)
Martin Reinecke's avatar
Martin Reinecke committed
77
78
79
        a = RGSpace(dim1, zerocenter=zc1, distances=d)
        b = RGRGTransformation.get_codomain(a, zerocenter=zc2)
        fft = FFTOperator(domain=a, target=b, domain_dtype=itp,
Theo Steininger's avatar
Theo Steininger committed
80
81
                          target_dtype=_harmonic_type(itp), module=module)
        inp = Field.from_random(domain=a, random_type='normal', std=7, mean=3,
Martin Reinecke's avatar
Martin Reinecke committed
82
                                dtype=itp)
83
        out = fft.adjoint_times(fft.times(inp))
Theo Steininger's avatar
Theo Steininger committed
84
        assert_allclose(inp.val, out.val, rtol=tol, atol=tol)
Martin Reinecke's avatar
Martin Reinecke committed
85

Theo Steininger's avatar
Theo Steininger committed
86
87
88
89
90
    @expand(product(["numpy", "fftw"], [10, 11], [9, 12], [False, True],
                    [False, True], [False, True], [False, True], [0.1, 1, 3.7],
                    [np.float64, np.complex128, np.float32, np.complex64]))
    def test_fft2D(self, module, dim1, dim2, zc1, zc2, zc3, zc4, d, itp):
        if module == "fftw" and "pyfftw" not in di:
Martin Reinecke's avatar
Martin Reinecke committed
91
            raise SkipTest
Theo Steininger's avatar
Theo Steininger committed
92
93
94
        tol = _get_rtol(itp)
        a = RGSpace([dim1, dim2], zerocenter=[zc1, zc2], distances=d)
        b = RGRGTransformation.get_codomain(a, zerocenter=[zc3, zc4])
Martin Reinecke's avatar
Martin Reinecke committed
95
        fft = FFTOperator(domain=a, target=b, domain_dtype=itp,
Theo Steininger's avatar
Theo Steininger committed
96
97
                          target_dtype=_harmonic_type(itp), module=module)
        inp = Field.from_random(domain=a, random_type='normal', std=7, mean=3,
Martin Reinecke's avatar
Martin Reinecke committed
98
                                dtype=itp)
99
        out = fft.adjoint_times(fft.times(inp))
Theo Steininger's avatar
Theo Steininger committed
100
        assert_allclose(inp.val, out.val, rtol=tol, atol=tol)
Martin Reinecke's avatar
Martin Reinecke committed
101

Theo Steininger's avatar
Theo Steininger committed
102
103
104
    @expand(product([0, 3, 6, 11, 30],
                    [np.float64, np.complex128, np.float32, np.complex64]))
    def test_sht(self, lm, tp):
105
106
        if 'pyHealpix' not in di:
            raise SkipTest
Theo Steininger's avatar
Theo Steininger committed
107
        tol = _get_rtol(tp)
Martin Reinecke's avatar
Martin Reinecke committed
108
109
110
        a = LMSpace(lmax=lm)
        b = LMGLTransformation.get_codomain(a)
        fft = FFTOperator(domain=a, target=b, domain_dtype=tp, target_dtype=tp)
Theo Steininger's avatar
Theo Steininger committed
111
        inp = Field.from_random(domain=a, random_type='normal', std=7, mean=3,
Martin Reinecke's avatar
Martin Reinecke committed
112
                                dtype=tp)
113
        out = fft.adjoint_times(fft.times(inp))
Theo Steininger's avatar
Theo Steininger committed
114
        assert_allclose(inp.val, out.val, rtol=tol, atol=tol)
115

Theo Steininger's avatar
Theo Steininger committed
116
117
118
    @expand(product([128, 256],
                    [np.float64, np.complex128, np.float32, np.complex64]))
    def test_sht2(self, lm, tp):
119
120
        if 'pyHealpix' not in di:
            raise SkipTest
Martin Reinecke's avatar
Martin Reinecke committed
121
122
123
        a = LMSpace(lmax=lm)
        b = LMHPTransformation.get_codomain(a)
        fft = FFTOperator(domain=a, target=b, domain_dtype=tp, target_dtype=tp)
Theo Steininger's avatar
Theo Steininger committed
124
        inp = Field.from_random(domain=a, random_type='normal', std=1, mean=0,
Martin Reinecke's avatar
Martin Reinecke committed
125
                                dtype=tp)
126
127
        out = fft.adjoint_times(fft.times(inp))
        assert_allclose(inp.val, out.val, rtol=1e-3, atol=1e-1)