test_field.py 4.5 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
# 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/>.
Theo Steininger's avatar
Theo Steininger committed
13
14
15
16
17
#
# Copyright(C) 2013-2017 Max-Planck-Society
#
# NIFTy is being developed at the Max-Planck-Institut fuer Astrophysik
# and financially supported by the Studienstiftung des deutschen Volkes.
18
19
20
21
22

import unittest

import numpy as np
from numpy.testing import assert_,\
23
24
                          assert_almost_equal,\
                          assert_allclose
Theo Steininger's avatar
Theo Steininger committed
25
from nose.plugins.skip import SkipTest
26

27
from itertools import product
28
29
30

from nifty import Field,\
                  RGSpace,\
31
                  LMSpace,\
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
32
                  PowerSpace
33

Jait Dixit's avatar
Jait Dixit committed
34
from test.common import expand
35
36


Martin Reinecke's avatar
Martin Reinecke committed
37
SPACES = [RGSpace((4,)), RGSpace((5))]
Theo Steininger's avatar
Theo Steininger committed
38
SPACE_COMBINATIONS = [(), SPACES[0], SPACES[1], SPACES]
39
40
41


class Test_Interface(unittest.TestCase):
42
    @expand(product(SPACE_COMBINATIONS,
Martin Reinecke's avatar
stage1    
Martin Reinecke committed
43
                    [['domain', tuple],
44
                     ['domain_axes', tuple],
Martin Reinecke's avatar
stage1    
Martin Reinecke committed
45
                     ['val', np.ndarray],
46
47
48
49
50
51
52
                     ['shape', tuple],
                     ['dim', np.int],
                     ['dof', np.int],
                     ['total_volume', np.float]]))
    def test_return_types(self, domain, attribute_desired_type):
        attribute = attribute_desired_type[0]
        desired_type = attribute_desired_type[1]
53
        f = Field(domain=domain)
54
55
        assert_(isinstance(getattr(f, attribute), desired_type))

Martin Reinecke's avatar
Martin Reinecke committed
56

57
58
59
class Test_Functionality(unittest.TestCase):
    @expand(product([True, False], [True, False],
                    [(1,), (4,), (5,)], [(1,), (6,), (7,)]))
60
    def test_hermitian_decomposition(self, preserve, complexdata, s1, s2):
61
        np.random.seed(123)
62
63
64
        r1 = RGSpace(s1, harmonic=True)
        r2 = RGSpace(s2, harmonic=True)
        ra = RGSpace(s1+s2, harmonic=True)
Martin Reinecke's avatar
Martin Reinecke committed
65

Martin Reinecke's avatar
Martin Reinecke committed
66
67
        if preserve:
            complexdata=True
68
69
70
71
72
73
74
75
76
77
78
79
        v = np.random.random(s1+s2)
        if complexdata:
            v = v + 1j*np.random.random(s1+s2)
        f1 = Field(ra, val=v, copy=True)
        f2 = Field((r1, r2), val=v, copy=True)
        h1, a1 = Field._hermitian_decomposition((ra,), f1.val, (0,),
                                                ((0, 1,),), preserve)
        h2, a2 = Field._hermitian_decomposition((r1, r2), f2.val, (0, 1),
                                                ((0,), (1,)), preserve)
        h3, a3 = Field._hermitian_decomposition((r1, r2), f2.val, (1, 0),
                                                ((0,), (1,)), preserve)

Martin Reinecke's avatar
stage1    
Martin Reinecke committed
80
81
82
83
        assert_almost_equal(h1, h2)
        assert_almost_equal(a1, a2)
        assert_almost_equal(h1, h3)
        assert_almost_equal(a1, a3)
84

85
86
87
    @expand(product([RGSpace((8,), harmonic=True),
                     RGSpace((8, 8), harmonic=True, distances=0.123)],
                    [RGSpace((8,), harmonic=True),
Martin Reinecke's avatar
more    
Martin Reinecke committed
88
89
                     LMSpace(12)]))
    def test_power_synthesize_analyze(self, space1, space2):
Martin Reinecke's avatar
stage1    
Martin Reinecke committed
90
        np.random.seed(11)
91

92
93
        p1 = PowerSpace(space1)
        spec1 = lambda k: 42/(1+k)**2
Martin Reinecke's avatar
Martin Reinecke committed
94
        fp1 = Field(p1, val=spec1(p1.kindex))
95
96
97

        p2 = PowerSpace(space2)
        spec2 = lambda k: 42/(1+k)**3
Martin Reinecke's avatar
Martin Reinecke committed
98
        fp2 = Field(p2, val=spec2(p2.kindex))
99

Martin Reinecke's avatar
stage1    
Martin Reinecke committed
100
        outer = np.outer(fp1.val, fp2.val)
101
102
        fp = Field((p1, p2), val=outer)

103
        samples = 2000
104
105
        ps1 = 0.
        ps2 = 0.
Martin Reinecke's avatar
Martin Reinecke committed
106
        for ii in range(samples):
107
108
109
110
111
112
            sk = fp.power_synthesize(spaces=(0, 1), real_signal=True)

            sp = sk.power_analyze(spaces=(0, 1), keep_phase_information=False)
            ps1 += sp.sum(spaces=1)/fp2.sum()
            ps2 += sp.sum(spaces=0)/fp1.sum()

Martin Reinecke's avatar
stage1    
Martin Reinecke committed
113
114
        assert_allclose(ps1.val/samples,
                        fp1.val,
115
                        rtol=0.2)
Martin Reinecke's avatar
stage1    
Martin Reinecke committed
116
117
        assert_allclose(ps2.val/samples,
                        fp2.val,
118
                        rtol=0.2)
Martin Reinecke's avatar
Martin Reinecke committed
119
120
121
122
123
124
125
126
127

    def test_vdot(self):
        s=RGSpace((10,))
        f1=Field.from_random("normal",domain=s,dtype=np.complex128)
        f2=Field.from_random("normal",domain=s,dtype=np.complex128)
        assert_allclose(f1.vdot(f2),f1.vdot(f2,spaces=0))
        assert_allclose(f1.vdot(f2),np.conj(f2.vdot(f1)))