test_misc.py 5.08 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
22
23
24
# 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
from numpy.testing import assert_,\
                          assert_equal, \
                          assert_allclose
25
from nifty.config import dependency_injector as di
Martin Reinecke's avatar
Martin Reinecke committed
26
27
28
29
30
31
32
33

from nifty import Field,\
                  RGSpace,\
                  LMSpace,\
                  GLSpace,\
                  FieldArray, \
                  RGRGTransformation, \
                  LMGLTransformation, \
34
                  LMHPTransformation, \
Martin Reinecke's avatar
Martin Reinecke committed
35
36
                  FFTOperator

37
38
from nose.plugins.skip import SkipTest

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

Martin Reinecke's avatar
Martin Reinecke committed
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
def _sub_fft1D(module):
    for dim1 in [10,11]:
        for zc1 in [False,True]:
            for zc2 in [False,True]:
                for d in [0.1,1,3.7]:
                    for itp in [np.float64,np.complex128,np.float32,np.complex64]:
                        a = RGSpace(dim1, zerocenter=zc1, distances=d)
                        b = RGRGTransformation.get_codomain(a, zerocenter=zc2)
                        fft = FFTOperator(domain=a, target=b, domain_dtype=itp, target_dtype=_harmonic_type(itp),module=module)
                        inp = Field.from_random(domain=a,random_type='normal',std=7,mean=3,dtype=itp)
                        out = fft.inverse_times(fft.times(inp))
                        assert_allclose(inp.val, out.val)
def _sub_fft2D(module):
  for dim1 in [10,11]:
    for dim2 in [9,12]:
      for zc1 in [False,True]:
        for zc2 in [False,True]:
          for zc3 in [False,True]:
            for zc4 in [False,True]:
              for d in [0.1,1,3.7]:
                for itp in [np.float64,np.complex128,np.float32,np.complex64]:
                  a = RGSpace([dim1,dim2], zerocenter=[zc1,zc2], distances=d)
                  b = RGRGTransformation.get_codomain(a, zerocenter=[zc3,zc4])
                  fft = FFTOperator(domain=a, target=b, domain_dtype=itp, target_dtype=_harmonic_type(itp),module=module)
                  inp = Field.from_random(domain=a,random_type='normal',std=7,mean=3,dtype=itp)
                  out = fft.inverse_times(fft.times(inp))
                  assert_allclose(inp.val, out.val)
Martin Reinecke's avatar
Martin Reinecke committed
74

Martin Reinecke's avatar
Martin Reinecke committed
75
76
77
78
79
80
81
82
83
84
85
class Misc_Tests(unittest.TestCase):
    def test_RG_distance_1D(self):
        for dim1 in [10,11]:
            for zc1 in [False,True]:
                for d in [0.1,1,3.7]:
                    foo = RGSpace([dim1],zerocenter=zc1)
                    res = foo.get_distance_array('not')
                    assert_equal(res[zc1*(dim1//2)],0.)

    def test_RG_distance_2D(self):
      for dim1 in [10,11]:
86
        for dim2 in [9,28]:
Martin Reinecke's avatar
Martin Reinecke committed
87
88
89
90
91
92
93
          for zc1 in [False,True]:
            for zc2 in [False,True]:
              for d in [0.1,1,3.7]:
                foo = RGSpace([dim1,dim2],zerocenter=[zc1,zc2])
                res = foo.get_distance_array('not')
                assert_equal(res[zc1*(dim1//2),zc2*(dim2//2)],0.)

Martin Reinecke's avatar
Martin Reinecke committed
94
95
96
97
98
99
    def test_fft1D_numpy(self):
        _sub_fft1D("numpy")
    def test_fft1D_pyfftw(self):
        if 'pyfftw' not in di:
            raise SkipTest
        _sub_fft1D("fftw")
Martin Reinecke's avatar
Martin Reinecke committed
100

Martin Reinecke's avatar
Martin Reinecke committed
101
102
103
104
105
106
    def test_fft2D_numpy(self):
        _sub_fft2D("numpy")
    def test_fft2D_pyfftw(self):
        if 'pyfftw' not in di:
            raise SkipTest
        _sub_fft2D("fftw")
Martin Reinecke's avatar
Martin Reinecke committed
107
108

    def test_sht(self):
109
110
        if 'pyHealpix' not in di:
            raise SkipTest
Martin Reinecke's avatar
Martin Reinecke committed
111
112
113
114
115
116
117
118
        for lm in [0,3,6,11,30]:
            for tp in [np.float64,np.complex128,np.float32,np.complex64]:
                a = LMSpace(lmax=lm)
                b = LMGLTransformation.get_codomain(a)
                fft = FFTOperator(domain=a, target=b, domain_dtype=tp, target_dtype=tp)
                inp = Field.from_random(domain=a,random_type='normal',std=7,mean=3,dtype=tp)
                out = fft.inverse_times(fft.times(inp))
                assert_allclose(inp.val, out.val)
119
120
121
122
123
124
125
126
127
128
129
130

    def test_sht2(self):
        if 'pyHealpix' not in di:
            raise SkipTest
        for lm in [128,256]:
            for tp in [np.float64,np.complex128,np.float32,np.complex64]:
                a = LMSpace(lmax=lm)
                b = LMHPTransformation.get_codomain(a)
                fft = FFTOperator(domain=a, target=b, domain_dtype=tp, target_dtype=tp)
                inp = Field.from_random(domain=a,random_type='normal',std=1,mean=0,dtype=tp)
                out = fft.inverse_times(fft.times(inp))
                assert_allclose(inp.val, out.val,rtol=1e-3,atol=1e-3)