test_misc.py 4.96 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
    RGSpace,\
    LMSpace,\
29
30
    HPSpace,\
    GLSpace,\
Theo Steininger's avatar
Theo Steininger committed
31
    FFTOperator
Martin Reinecke's avatar
Martin Reinecke committed
32

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

36
37
from nose.plugins.skip import SkipTest

Theo Steininger's avatar
Theo Steininger committed
38

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

Theo Steininger's avatar
Theo Steininger committed
47

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

Theo Steininger's avatar
Theo Steininger committed
54

Martin Reinecke's avatar
Martin Reinecke committed
55
class Misc_Tests(unittest.TestCase):
Theo Steininger's avatar
Theo Steininger committed
56
57
58
    @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
59
        res = foo.get_distance_array('not')
Theo Steininger's avatar
Theo Steininger committed
60
        assert_equal(res[zc1 * (dim1 // 2)], 0.)
Martin Reinecke's avatar
Martin Reinecke committed
61

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

Theo Steininger's avatar
Theo Steininger committed
69
70
71
72
73
    @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
74
            raise SkipTest
Theo Steininger's avatar
Theo Steininger committed
75
        tol = _get_rtol(itp)
Martin Reinecke's avatar
Martin Reinecke committed
76
        a = RGSpace(dim1, zerocenter=zc1, distances=d)
77
        b = RGSpace(dim1, zerocenter=zc2,distances=1./(dim1*d),harmonic=True)
Martin Reinecke's avatar
Martin Reinecke committed
78
        fft = FFTOperator(domain=a, target=b, domain_dtype=itp,
Theo Steininger's avatar
Theo Steininger committed
79
80
                          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
81
                                dtype=itp)
82
        out = fft.adjoint_times(fft.times(inp))
Theo Steininger's avatar
Theo Steininger committed
83
        assert_allclose(inp.val, out.val, rtol=tol, atol=tol)
Martin Reinecke's avatar
Martin Reinecke committed
84

Theo Steininger's avatar
Theo Steininger committed
85
86
    @expand(product(["numpy", "fftw"], [10, 11], [9, 12], [False, True],
                    [False, True], [False, True], [False, True], [0.1, 1, 3.7],
87
                    [0.4, 1, 2.7],
Theo Steininger's avatar
Theo Steininger committed
88
                    [np.float64, np.complex128, np.float32, np.complex64]))
89
    def test_fft2D(self, module, dim1, dim2, zc1, zc2, zc3, zc4, d1, d2, itp):
Theo Steininger's avatar
Theo Steininger committed
90
        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
        tol = _get_rtol(itp)
93
94
95
        a = RGSpace([dim1, dim2], zerocenter=[zc1, zc2], distances=[d1,d2])
        b = RGSpace([dim1, dim2], zerocenter=[zc3, zc4],
                    distances=[1./(dim1*d1),1./(dim2*d2)],harmonic=True)
Martin Reinecke's avatar
Martin Reinecke committed
96
        fft = FFTOperator(domain=a, target=b, domain_dtype=itp,
Theo Steininger's avatar
Theo Steininger committed
97
98
                          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
99
                                dtype=itp)
100
        out = fft.adjoint_times(fft.times(inp))
Theo Steininger's avatar
Theo Steininger committed
101
        assert_allclose(inp.val, out.val, rtol=tol, atol=tol)
Martin Reinecke's avatar
Martin Reinecke committed
102

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

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