test_field.py 10.1 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
#
Martin Reinecke's avatar
Martin Reinecke committed
14
# Copyright(C) 2013-2018 Max-Planck-Society
Theo Steininger's avatar
Theo Steininger committed
15
16
17
#
# NIFTy is being developed at the Max-Planck-Institut fuer Astrophysik
# and financially supported by the Studienstiftung des deutschen Volkes.
18
19

import unittest
20
from itertools import product
Jait Dixit's avatar
Jait Dixit committed
21
from test.common import expand
22

23
24
25
import nifty5 as ift
import numpy as np
from numpy.testing import assert_allclose, assert_equal, assert_raises
26

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


class Test_Interface(unittest.TestCase):
32
    @expand(product(SPACE_COMBINATIONS,
Martin Reinecke's avatar
Martin Reinecke committed
33
34
                    [['domain', ift.DomainTuple],
                     ['val', ift.dobj.data_object],
35
                     ['shape', tuple],
Martin Reinecke's avatar
Martin Reinecke committed
36
                     ['size', (np.int, np.int64)]]))
37
38
39
    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
40
        f = ift.Field.full(domain, 1.)
Martin Reinecke's avatar
Martin Reinecke committed
41
        assert_equal(isinstance(getattr(f, attribute), desired_type), True)
42

Martin Reinecke's avatar
Martin Reinecke committed
43

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


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


52
class Test_Functionality(unittest.TestCase):
Martin Reinecke's avatar
Martin Reinecke committed
53
54
55
56
    @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
57
    def test_power_synthesize_analyze(self, space1, space2):
Martin Reinecke's avatar
stage1    
Martin Reinecke committed
58
        np.random.seed(11)
59

Martin Reinecke's avatar
Martin Reinecke committed
60
        p1 = ift.PowerSpace(space1)
61
        fp1 = ift.PS_field(p1, _spec1)
Martin Reinecke's avatar
Martin Reinecke committed
62
        p2 = ift.PowerSpace(space2)
63
64
        fp2 = ift.PS_field(p2, _spec2)
        outer = np.outer(fp1.to_global_data(), fp2.to_global_data())
65
        fp = ift.Field.from_global_data((p1, p2), outer)
66

67
68
        op1 = ift.create_power_operator((space1, space2), _spec1, 0)
        op2 = ift.create_power_operator((space1, space2), _spec2, 1)
Martin Reinecke's avatar
Martin Reinecke committed
69
        opfull = op2(op1)
70

Martin Reinecke's avatar
Martin Reinecke committed
71
        samples = 500
72
73
        sc1 = ift.StatCalculator()
        sc2 = ift.StatCalculator()
Martin Reinecke's avatar
Martin Reinecke committed
74
        for ii in range(samples):
75
            sk = opfull.draw_sample()
76

Martin Reinecke's avatar
Martin Reinecke committed
77
78
            sp = ift.power_analyze(sk, spaces=(0, 1),
                                   keep_phase_information=False)
79
80
            sc1.add(sp.sum(spaces=1)/fp2.sum())
            sc2.add(sp.sum(spaces=0)/fp1.sum())
81

82
83
        assert_allclose(sc1.mean.local_data, fp1.local_data, rtol=0.2)
        assert_allclose(sc2.mean.local_data, fp2.local_data, rtol=0.2)
Martin Reinecke's avatar
Martin Reinecke committed
84

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

92
93
        fp1 = ift.PS_field(ift.PowerSpace(space1), _spec1)
        fp2 = ift.PS_field(ift.PowerSpace(space2), _spec2)
Martin Reinecke's avatar
Martin Reinecke committed
94

95
96
        S_1 = ift.create_power_operator((space1, space2), _spec1, 0)
        S_2 = ift.create_power_operator((space1, space2), _spec2, 1)
Martin Reinecke's avatar
Martin Reinecke committed
97
        S_full = S_2(S_1)
Martin Reinecke's avatar
Martin Reinecke committed
98
99

        samples = 500
100
101
        sc1 = ift.StatCalculator()
        sc2 = ift.StatCalculator()
Martin Reinecke's avatar
Martin Reinecke committed
102
103

        for ii in range(samples):
104
            sk = S_full.draw_sample()
Martin Reinecke's avatar
Martin Reinecke committed
105
106
            sp = ift.power_analyze(sk, spaces=(0, 1),
                                   keep_phase_information=False)
107
108
            sc1.add(sp.sum(spaces=1)/fp2.sum())
            sc2.add(sp.sum(spaces=0)/fp1.sum())
Martin Reinecke's avatar
Martin Reinecke committed
109

110
111
        assert_allclose(sc1.mean.local_data, fp1.local_data, rtol=0.2)
        assert_allclose(sc2.mean.local_data, fp2.local_data, rtol=0.2)
Martin Reinecke's avatar
Martin Reinecke committed
112

Martin Reinecke's avatar
Martin Reinecke committed
113
114
115
116
117
118
119
120
121
122
    def test_norm(self):
        s = ift.RGSpace((10,))
        f = ift.Field.from_random("normal", domain=s, dtype=np.complex128)
        gd = f.to_global_data()
        assert_allclose(f.norm(), np.linalg.norm(gd))
        assert_allclose(f.norm(1), np.linalg.norm(gd, ord=1))
        assert_allclose(f.norm(2), np.linalg.norm(gd, ord=2))
        assert_allclose(f.norm(3), np.linalg.norm(gd, ord=3))
        assert_allclose(f.norm(np.inf), np.linalg.norm(gd, ord=np.inf))

Martin Reinecke's avatar
Martin Reinecke committed
123
    def test_vdot(self):
Martin Reinecke's avatar
Martin Reinecke committed
124
125
126
        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)
Martin Reinecke's avatar
Martin Reinecke committed
127
        assert_allclose(f1.vdot(f2), f1.vdot(f2, spaces=0))
128
        assert_allclose(f1.vdot(f2), np.conj(f2.vdot(f1)))
129
130
131
132

    def test_vdot2(self):
        x1 = ift.RGSpace((200,))
        x2 = ift.RGSpace((150,))
Martin Reinecke's avatar
Martin Reinecke committed
133
        m = ift.Field.full((x1, x2), .5)
134
        res = m.vdot(m, spaces=1)
Martin Reinecke's avatar
Martin Reinecke committed
135
        assert_allclose(res.local_data, 37.5)
Martin Reinecke's avatar
Martin Reinecke committed
136

137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
    def test_dataconv(self):
        s1 = ift.RGSpace((10,))
        ld = np.arange(ift.dobj.local_shape(s1.shape)[0])
        gd = np.arange(s1.shape[0])
        assert_equal(ld, ift.from_local_data(s1, ld).local_data)
        assert_equal(gd, ift.from_global_data(s1, gd).to_global_data())

    def test_cast_domain(self):
        s1 = ift.RGSpace((10,))
        s2 = ift.RGSpace((10,), distances=20.)
        d = np.arange(s1.shape[0])
        d2 = ift.from_global_data(s1, d).cast_domain(s2).to_global_data()
        assert_equal(d, d2)

    def test_empty_domain(self):
152
        f = ift.Field.full((), 5)
153
        assert_equal(f.local_data, 5)
154
        f = ift.Field.full(None, 5)
155
        assert_equal(f.local_data, 5)
156
157
158

    def test_trivialities(self):
        s1 = ift.RGSpace((10,))
159
        f1 = ift.Field.full(s1, 27)
160
        assert_equal(f1.local_data, f1.real.local_data)
161
        f1 = ift.Field.full(s1, 27.+3j)
162
163
164
165
166
        assert_equal(f1.real.local_data, 27.)
        assert_equal(f1.imag.local_data, 3.)
        assert_equal(f1.local_data, +f1.local_data)
        assert_equal(f1.sum(), f1.sum(0))
        f1 = ift.from_global_data(s1, np.arange(10))
167
168
#        assert_equal(f1.min(), 0)
#        assert_equal(f1.max(), 9)
169
170
171
172
        assert_equal(f1.prod(), 0)

    def test_weight(self):
        s1 = ift.RGSpace((10,))
173
        f = ift.Field.full(s1, 10.)
174
        f2 = f.weight(1)
175
176
177
178
179
180
181
182
        assert_equal(f.weight(1).local_data, f2.local_data)
        assert_equal(f.total_volume(), 1)
        assert_equal(f.total_volume(0), 1)
        assert_equal(f.total_volume((0,)), 1)
        assert_equal(f.scalar_weight(), 0.1)
        assert_equal(f.scalar_weight(0), 0.1)
        assert_equal(f.scalar_weight((0,)), 0.1)
        s1 = ift.GLSpace(10)
183
        f = ift.Field.full(s1, 10.)
184
185
186
187
188
189
190
        assert_equal(f.scalar_weight(), None)
        assert_equal(f.scalar_weight(0), None)
        assert_equal(f.scalar_weight((0,)), None)

    @expand(product([ift.RGSpace(10), ift.GLSpace(10)],
                    [np.float64, np.complex128]))
    def test_reduction(self, dom, dt):
191
        s1 = ift.Field.full(dom, dt(1.))
192
193
194
195
196
197
198
199
200
201
        assert_allclose(s1.mean(), 1.)
        assert_allclose(s1.mean(0), 1.)
        assert_allclose(s1.var(), 0., atol=1e-14)
        assert_allclose(s1.var(0), 0., atol=1e-14)
        assert_allclose(s1.std(), 0., atol=1e-14)
        assert_allclose(s1.std(0), 0., atol=1e-14)

    def test_err(self):
        s1 = ift.RGSpace((10,))
        s2 = ift.RGSpace((11,))
202
        f1 = ift.Field.full(s1, 27)
203
        with assert_raises(ValueError):
204
            f2 = ift.Field(ift.DomainTuple.make(s2), f1.val)
205
        with assert_raises(TypeError):
206
            f2 = ift.Field.full(s2, "xyz")
207
208
209
210
211
212
213
        with assert_raises(TypeError):
            if f1:
                pass
        with assert_raises(TypeError):
            f1.full((2, 4, 6))
        with assert_raises(TypeError):
            f2 = ift.Field(None, None)
214
        with assert_raises(TypeError):
215
216
217
218
219
220
            f2 = ift.Field(s1, None)
        with assert_raises(ValueError):
            f1.imag
        with assert_raises(TypeError):
            f1.vdot(42)
        with assert_raises(ValueError):
221
            f1.vdot(ift.Field.full(s2, 1.))
222
223
224
        with assert_raises(TypeError):
            ift.full(s1, [2, 3])

Martin Reinecke's avatar
Martin Reinecke committed
225
226
    def test_stdfunc(self):
        s = ift.RGSpace((200,))
227
        f = ift.Field.full(s, 27)
Martin Reinecke's avatar
Martin Reinecke committed
228
229
230
        assert_equal(f.local_data, 27)
        assert_equal(f.shape, (200,))
        assert_equal(f.dtype, np.int)
Martin Reinecke's avatar
step 1    
Martin Reinecke committed
231
        fx = ift.full(f.domain, 0)
Martin Reinecke's avatar
Martin Reinecke committed
232
233
234
        assert_equal(f.dtype, fx.dtype)
        assert_equal(f.shape, fx.shape)
        assert_equal(fx.local_data, 0)
Martin Reinecke's avatar
step 1    
Martin Reinecke committed
235
        fx = ift.full(f.domain, 1)
Martin Reinecke's avatar
Martin Reinecke committed
236
237
238
        assert_equal(f.dtype, fx.dtype)
        assert_equal(f.shape, fx.shape)
        assert_equal(fx.local_data, 1)
Martin Reinecke's avatar
step 1    
Martin Reinecke committed
239
        fx = ift.full(f.domain, 67.)
Martin Reinecke's avatar
Martin Reinecke committed
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
        assert_equal(f.shape, fx.shape)
        assert_equal(fx.local_data, 67.)
        f = ift.Field.from_random("normal", s)
        f2 = ift.Field.from_random("normal", s)
        assert_equal((f > f2).local_data, f.local_data > f2.local_data)
        assert_equal((f >= f2).local_data, f.local_data >= f2.local_data)
        assert_equal((f < f2).local_data, f.local_data < f2.local_data)
        assert_equal((f <= f2).local_data, f.local_data <= f2.local_data)
        assert_equal((f != f2).local_data, f.local_data != f2.local_data)
        assert_equal((f == f2).local_data, f.local_data == f2.local_data)
        assert_equal((f+f2).local_data, f.local_data+f2.local_data)
        assert_equal((f-f2).local_data, f.local_data-f2.local_data)
        assert_equal((f*f2).local_data, f.local_data*f2.local_data)
        assert_equal((f/f2).local_data, f.local_data/f2.local_data)
        assert_equal((-f).local_data, -(f.local_data))
        assert_equal(abs(f).local_data, abs(f.local_data))
Martin Reinecke's avatar
Martin Reinecke committed
256
257
258
259
260
261
262
263
264

    def test_emptydomain(self):
        f = ift.Field.full((), 3.)
        assert_equal(f.sum(), 3.)
        assert_equal(f.prod(), 3.)
        assert_equal(f.local_data, 3.)
        assert_equal(f.local_data.shape, ())
        assert_equal(f.local_data.size, 1)
        assert_equal(f.vdot(f), 9.)