test_field.py 4.62 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

import unittest

import numpy as np
Martin Reinecke's avatar
Martin Reinecke committed
22
from numpy.testing import assert_equal,\
23
                          assert_allclose
Theo Steininger's avatar
Theo Steininger committed
24
from nose.plugins.skip import SkipTest
25

26
from itertools import product
27

Martin Reinecke's avatar
Martin Reinecke committed
28
from nifty2go import Field,\
29
                  RGSpace,\
30
                  LMSpace,\
Martin Reinecke's avatar
Martin Reinecke committed
31
                  PowerSpace,\
Martin Reinecke's avatar
Martin Reinecke committed
32
33
                  DomainTuple,\
                  DiagonalOperator
34

35
from nifty2go.sugar import create_power_field
36

Jait Dixit's avatar
Jait Dixit committed
37
from test.common import expand
38
39


Martin Reinecke's avatar
Martin Reinecke committed
40
SPACES = [RGSpace((4,)), RGSpace((5))]
Theo Steininger's avatar
Theo Steininger committed
41
SPACE_COMBINATIONS = [(), SPACES[0], SPACES[1], SPACES]
42
43
44


class Test_Interface(unittest.TestCase):
45
    @expand(product(SPACE_COMBINATIONS,
Martin Reinecke's avatar
Martin Reinecke committed
46
                    [['domain', DomainTuple],
Martin Reinecke's avatar
stage1    
Martin Reinecke committed
47
                     ['val', np.ndarray],
48
                     ['shape', tuple],
Martin Reinecke's avatar
Martin Reinecke committed
49
                     ['dim', (np.int, np.int64)]]))
50
51
52
    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)
Martin Reinecke's avatar
Martin Reinecke committed
54
        assert_equal(isinstance(getattr(f, attribute), desired_type), True)
55

Martin Reinecke's avatar
Martin Reinecke committed
56

57
class Test_Functionality(unittest.TestCase):
58
59
60
    @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
61
62
                     LMSpace(12)]))
    def test_power_synthesize_analyze(self, space1, space2):
Martin Reinecke's avatar
stage1    
Martin Reinecke committed
63
        np.random.seed(11)
64

65
66
        p1 = PowerSpace(space1)
        spec1 = lambda k: 42/(1+k)**2
Martin Reinecke's avatar
Martin Reinecke committed
67
        fp1 = Field(p1, val=spec1(p1.k_lengths))
68
69
70

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

Martin Reinecke's avatar
stage1    
Martin Reinecke committed
73
        outer = np.outer(fp1.val, fp2.val)
74
75
        fp = Field((p1, p2), val=outer)

Martin Reinecke's avatar
Martin Reinecke committed
76
        samples = 500
77
78
        ps1 = 0.
        ps2 = 0.
Martin Reinecke's avatar
Martin Reinecke committed
79
        for ii in range(samples):
80
81
82
            sk = fp.power_synthesize(spaces=(0, 1), real_signal=True)

            sp = sk.power_analyze(spaces=(0, 1), keep_phase_information=False)
Martin Reinecke's avatar
Martin Reinecke committed
83
84
            ps1 += sp.sum(spaces=1)/fp2.sum()
            ps2 += sp.sum(spaces=0)/fp1.sum()
85

Martin Reinecke's avatar
Martin Reinecke committed
86
87
        assert_allclose(ps1.val/samples, fp1.val, rtol=0.2)
        assert_allclose(ps2.val/samples, fp2.val, rtol=0.2)
Martin Reinecke's avatar
Martin Reinecke committed
88

Martin Reinecke's avatar
Martin Reinecke committed
89
90
91
92
93
94
95
96

    @expand(product([RGSpace((8,), harmonic=True),
                     RGSpace((8, 8), harmonic=True, distances=0.123)],
                    [RGSpace((8,), harmonic=True),
                     LMSpace(12)]))
    def test_DiagonalOperator_power_analyze(self, space1, space2):
        np.random.seed(11)

Martin Reinecke's avatar
Martin Reinecke committed
97
98
        fulldomain = DomainTuple.make((space1, space2))

Martin Reinecke's avatar
Martin Reinecke committed
99
100
        p1 = PowerSpace(space1)
        spec1 = lambda k: 42/(1+k)**2
Martin Reinecke's avatar
Martin Reinecke committed
101
        fp1 = Field(p1, val=spec1(p1.k_lengths))
Martin Reinecke's avatar
Martin Reinecke committed
102
103
104

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

107
108
109
110
        S_1 = create_power_field(space1, lambda x: np.sqrt(spec1(x)))
        S_1 = DiagonalOperator(S_1.weight(-1), domain=fulldomain, spaces=0)
        S_2 = create_power_field(space2, lambda x: np.sqrt(spec2(x)))
        S_2 = DiagonalOperator(S_2.weight(-1), domain=fulldomain, spaces=1)
Martin Reinecke's avatar
Martin Reinecke committed
111
112
113
114
115
116

        samples = 500
        ps1 = 0.
        ps2 = 0.

        for ii in range(samples):
Martin Reinecke's avatar
Martin Reinecke committed
117
118
            rand_k = Field.from_random('normal', domain=fulldomain)
            sk = S_1.times(S_2.times(rand_k))
Martin Reinecke's avatar
Martin Reinecke committed
119
            sp = sk.power_analyze(spaces=(0, 1), keep_phase_information=False)
Martin Reinecke's avatar
Martin Reinecke committed
120
121
            ps1 += sp.sum(spaces=1)/fp2.sum()
            ps2 += sp.sum(spaces=0)/fp1.sum()
Martin Reinecke's avatar
Martin Reinecke committed
122
123
124
125

        assert_allclose(ps1.val/samples, fp1.val, rtol=0.2)
        assert_allclose(ps2.val/samples, fp2.val, rtol=0.2)

Martin Reinecke's avatar
Martin Reinecke committed
126
127
128
129
130
131
132
133
    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)))