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

import unittest
import numpy as np
Martin Reinecke's avatar
Martin Reinecke committed
21
from numpy.testing import assert_equal, assert_allclose
22
from itertools import product
Martin Reinecke's avatar
Martin Reinecke committed
23
24
import nifty2go as ift
from nifty2go.dobj import to_ndarray as to_np, from_ndarray as from_np
Jait Dixit's avatar
Jait Dixit committed
25
from test.common import expand
26
27


Martin Reinecke's avatar
Martin Reinecke committed
28
SPACES = [ift.RGSpace((4,)), ift.RGSpace((5))]
Theo Steininger's avatar
Theo Steininger committed
29
SPACE_COMBINATIONS = [(), SPACES[0], SPACES[1], SPACES]
30
31
32


class Test_Interface(unittest.TestCase):
33
    @expand(product(SPACE_COMBINATIONS,
Martin Reinecke's avatar
Martin Reinecke committed
34
35
                    [['domain', ift.DomainTuple],
                     ['val', ift.dobj.data_object],
36
                     ['shape', tuple],
Martin Reinecke's avatar
Martin Reinecke committed
37
                     ['dim', (np.int, np.int64)]]))
38
39
40
    def test_return_types(self, domain, attribute_desired_type):
        attribute = attribute_desired_type[0]
        desired_type = attribute_desired_type[1]
Martin Reinecke's avatar
Martin Reinecke committed
41
        f = ift.Field(domain=domain)
Martin Reinecke's avatar
Martin Reinecke committed
42
        assert_equal(isinstance(getattr(f, attribute), desired_type), True)
43

Martin Reinecke's avatar
Martin Reinecke committed
44

Martin Reinecke's avatar
Martin Reinecke committed
45
46
47
48
49
50
51
52
def _spec1(k):
    return 42/(1.+k)**2


def _spec2(k):
    return 42/(1.+k)**3


53
class Test_Functionality(unittest.TestCase):
Martin Reinecke's avatar
Martin Reinecke committed
54
55
56
57
    @expand(product([ift.RGSpace((8,), harmonic=True),
                     ift.RGSpace((8, 8), harmonic=True, distances=0.123)],
                    [ift.RGSpace((8,), harmonic=True),
                     ift.LMSpace(12)]))
Martin Reinecke's avatar
more    
Martin Reinecke committed
58
    def test_power_synthesize_analyze(self, space1, space2):
Martin Reinecke's avatar
stage1    
Martin Reinecke committed
59
        np.random.seed(11)
60

Martin Reinecke's avatar
Martin Reinecke committed
61
        p1 = ift.PowerSpace(space1)
Martin Reinecke's avatar
Martin Reinecke committed
62
        fp1 = ift.Field(p1, val=from_np(_spec1(p1.k_lengths)))
63

Martin Reinecke's avatar
Martin Reinecke committed
64
        p2 = ift.PowerSpace(space2)
Martin Reinecke's avatar
Martin Reinecke committed
65
        fp2 = ift.Field(p2, val=from_np(_spec2(p2.k_lengths)))
66

Martin Reinecke's avatar
Martin Reinecke committed
67
68
        outer = from_np(np.outer(to_np(fp1.val), to_np(fp2.val)))
        fp = ift.Field((p1, p2), val=outer)
69

Martin Reinecke's avatar
Martin Reinecke committed
70
        samples = 500
71
72
        ps1 = 0.
        ps2 = 0.
Martin Reinecke's avatar
Martin Reinecke committed
73
        for ii in range(samples):
Martin Reinecke's avatar
Martin Reinecke committed
74
            sk = ift.power_synthesize(fp, spaces=(0, 1), real_signal=True)
75

Martin Reinecke's avatar
Martin Reinecke committed
76
77
            sp = ift.power_analyze(sk, spaces=(0, 1),
                                   keep_phase_information=False)
Martin Reinecke's avatar
Martin Reinecke committed
78
79
            ps1 += sp.sum(spaces=1)/fp2.sum()
            ps2 += sp.sum(spaces=0)/fp1.sum()
80

Martin Reinecke's avatar
Martin Reinecke committed
81
82
        assert_allclose(to_np(ps1.val/samples), to_np(fp1.val), rtol=0.2)
        assert_allclose(to_np(ps2.val/samples), to_np(fp2.val), rtol=0.2)
Martin Reinecke's avatar
Martin Reinecke committed
83

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

Martin Reinecke's avatar
Martin Reinecke committed
91
        fulldomain = ift.DomainTuple.make((space1, space2))
Martin Reinecke's avatar
Martin Reinecke committed
92

Martin Reinecke's avatar
Martin Reinecke committed
93
        p1 = ift.PowerSpace(space1)
Martin Reinecke's avatar
Martin Reinecke committed
94
        fp1 = ift.Field(p1, val=from_np(_spec1(p1.k_lengths)))
Martin Reinecke's avatar
Martin Reinecke committed
95

Martin Reinecke's avatar
Martin Reinecke committed
96
        p2 = ift.PowerSpace(space2)
Martin Reinecke's avatar
Martin Reinecke committed
97
        fp2 = ift.Field(p2, val=from_np(_spec2(p2.k_lengths)))
Martin Reinecke's avatar
Martin Reinecke committed
98

Martin Reinecke's avatar
Martin Reinecke committed
99
100
101
102
        S_1 = ift.create_power_field(space1, lambda x: np.sqrt(_spec1(x)))
        S_1 = ift.DiagonalOperator(S_1, domain=fulldomain, spaces=0)
        S_2 = ift.create_power_field(space2, lambda x: np.sqrt(_spec2(x)))
        S_2 = ift.DiagonalOperator(S_2, domain=fulldomain, spaces=1)
Martin Reinecke's avatar
Martin Reinecke committed
103
104
105
106
107
108

        samples = 500
        ps1 = 0.
        ps2 = 0.

        for ii in range(samples):
Martin Reinecke's avatar
Martin Reinecke committed
109
            rand_k = ift.Field.from_random('normal', domain=fulldomain)
Martin Reinecke's avatar
Martin Reinecke committed
110
            sk = S_1.times(S_2.times(rand_k))
Martin Reinecke's avatar
Martin Reinecke committed
111
112
            sp = ift.power_analyze(sk, spaces=(0, 1),
                                   keep_phase_information=False)
Martin Reinecke's avatar
Martin Reinecke committed
113
114
            ps1 += sp.sum(spaces=1)/fp2.sum()
            ps2 += sp.sum(spaces=0)/fp1.sum()
Martin Reinecke's avatar
Martin Reinecke committed
115

Martin Reinecke's avatar
Martin Reinecke committed
116
117
        assert_allclose(to_np(ps1.val/samples), to_np(fp1.val), rtol=0.2)
        assert_allclose(to_np(ps2.val/samples), to_np(fp2.val), rtol=0.2)
Martin Reinecke's avatar
Martin Reinecke committed
118

Martin Reinecke's avatar
Martin Reinecke committed
119
    def test_vdot(self):
Martin Reinecke's avatar
Martin Reinecke committed
120
121
122
        s = ift.RGSpace((10,))
        f1 = ift.Field.from_random("normal", domain=s, dtype=np.complex128)
        f2 = ift.Field.from_random("normal", domain=s, dtype=np.complex128)
123
124
        assert_allclose(f1.vdot(f2), f1.vdot(f2, spaces=0))
        assert_allclose(f1.vdot(f2), np.conj(f2.vdot(f1)))