test_field.py 11.4 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
# Copyright(C) 2013-2019 Max-Planck-Society
Theo Steininger's avatar
Theo Steininger committed
15
#
16
# NIFTy is being developed at the Max-Planck-Institut fuer Astrophysik.
17

18
import numpy as np
Philipp Arras's avatar
Philipp Arras committed
19
import pytest
20
from numpy.testing import assert_allclose, assert_equal, assert_raises
21

Philipp Arras's avatar
Philipp Arras committed
22
23
24
import nifty5 as ift

pmp = pytest.mark.parametrize
Martin Reinecke's avatar
Martin Reinecke committed
25
SPACES = [ift.RGSpace((4,)), ift.RGSpace((5))]
Theo Steininger's avatar
Theo Steininger committed
26
SPACE_COMBINATIONS = [(), SPACES[0], SPACES[1], SPACES]
27
28


Philipp Arras's avatar
Philipp Arras committed
29
30
31
32
33
34
35
36
37
@pmp('domain', SPACE_COMBINATIONS)
@pmp('attribute_desired_type',
     [['domain', ift.DomainTuple], ['val', ift.dobj.data_object],
      ['shape', tuple], ['size', (np.int, np.int64)]])
def test_return_types(domain, attribute_desired_type):
    attribute = attribute_desired_type[0]
    desired_type = attribute_desired_type[1]
    f = ift.Field.full(domain, 1.)
    assert_equal(isinstance(getattr(f, attribute), desired_type), True)
38

Martin Reinecke's avatar
Martin Reinecke committed
39

Martin Reinecke's avatar
Martin Reinecke committed
40
def _spec1(k):
Philipp Arras's avatar
Philipp Arras committed
41
    return 42/(1. + k)**2
Martin Reinecke's avatar
Martin Reinecke committed
42
43
44


def _spec2(k):
Philipp Arras's avatar
Philipp Arras committed
45
46
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
    return 42/(1. + k)**3


@pmp('space1', [
    ift.RGSpace((8,), harmonic=True),
    ift.RGSpace((8, 8), harmonic=True, distances=0.123)
])
@pmp('space2', [ift.RGSpace((8,), harmonic=True), ift.LMSpace(12)])
def test_power_synthesize_analyze(space1, space2):
    np.random.seed(11)

    p1 = ift.PowerSpace(space1)
    fp1 = ift.PS_field(p1, _spec1)
    p2 = ift.PowerSpace(space2)
    fp2 = ift.PS_field(p2, _spec2)
    outer = np.outer(fp1.to_global_data(), fp2.to_global_data())
    fp = ift.Field.from_global_data((p1, p2), outer)

    op1 = ift.create_power_operator((space1, space2), _spec1, 0)
    op2 = ift.create_power_operator((space1, space2), _spec2, 1)
    opfull = op2(op1)

    samples = 500
    sc1 = ift.StatCalculator()
    sc2 = ift.StatCalculator()
    for ii in range(samples):
        sk = opfull.draw_sample()

        sp = ift.power_analyze(sk, spaces=(0, 1), keep_phase_information=False)
        sc1.add(sp.sum(spaces=1)/fp2.sum())
        sc2.add(sp.sum(spaces=0)/fp1.sum())

    assert_allclose(sc1.mean.local_data, fp1.local_data, rtol=0.2)
    assert_allclose(sc2.mean.local_data, fp2.local_data, rtol=0.2)


@pmp('space1', [
    ift.RGSpace((8,), harmonic=True),
    ift.RGSpace((8, 8), harmonic=True, distances=0.123)
])
@pmp('space2', [ift.RGSpace((8,), harmonic=True), ift.LMSpace(12)])
def test_DiagonalOperator_power_analyze2(space1, space2):
    np.random.seed(11)

    fp1 = ift.PS_field(ift.PowerSpace(space1), _spec1)
    fp2 = ift.PS_field(ift.PowerSpace(space2), _spec2)

    S_1 = ift.create_power_operator((space1, space2), _spec1, 0)
    S_2 = ift.create_power_operator((space1, space2), _spec2, 1)
    S_full = S_2(S_1)

    samples = 500
    sc1 = ift.StatCalculator()
    sc2 = ift.StatCalculator()

    for ii in range(samples):
        sk = S_full.draw_sample()
        sp = ift.power_analyze(sk, spaces=(0, 1), keep_phase_information=False)
        sc1.add(sp.sum(spaces=1)/fp2.sum())
        sc2.add(sp.sum(spaces=0)/fp1.sum())

    assert_allclose(sc1.mean.local_data, fp1.local_data, rtol=0.2)
    assert_allclose(sc2.mean.local_data, fp2.local_data, rtol=0.2)


@pmp('space', [
    ift.RGSpace((8,), harmonic=True), (),
    ift.RGSpace((8, 8), harmonic=True, distances=0.123),
    ift.RGSpace((2, 3, 7))
])
def test_norm(space):
    f = ift.Field.from_random("normal", domain=space, dtype=np.complex128)
    gd = f.to_global_data().reshape(-1)
    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))


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


Philipp Arras's avatar
Fixups    
Philipp Arras committed
133
def test_vdot2():
Philipp Arras's avatar
Philipp Arras committed
134
135
136
137
138
139
140
    x1 = ift.RGSpace((200,))
    x2 = ift.RGSpace((150,))
    m = ift.Field.full((x1, x2), .5)
    res = m.vdot(m, spaces=1)
    assert_allclose(res.local_data, 37.5)


Philipp Arras's avatar
Fixups    
Philipp Arras committed
141
def test_outer():
Philipp Arras's avatar
Philipp Arras committed
142
143
144
145
146
147
148
149
    x1 = ift.RGSpace((9,))
    x2 = ift.RGSpace((3,))
    m1 = ift.Field.full(x1, .5)
    m2 = ift.Field.full(x2, 3.)
    res = m1.outer(m2)
    assert_allclose(res.to_global_data(), np.full((9, 3), 1.5))


Philipp Arras's avatar
Fixups    
Philipp Arras committed
150
def test_sum():
Philipp Arras's avatar
Philipp Arras committed
151
152
153
154
155
156
157
158
159
160
161
162
163
164
    x1 = ift.RGSpace((9,), distances=2.)
    x2 = ift.RGSpace(
        (
            2,
            12,
        ), distances=(0.3,))
    m1 = ift.Field.from_global_data(ift.makeDomain(x1), np.arange(9))
    m2 = ift.Field.full(ift.makeDomain((x1, x2)), 0.45)
    res1 = m1.sum()
    res2 = m2.sum(spaces=1)
    assert_allclose(res1, 36)
    assert_allclose(res2.to_global_data(), np.full(9, 2*12*0.45))


Philipp Arras's avatar
Fixups    
Philipp Arras committed
165
def test_integrate():
Philipp Arras's avatar
Philipp Arras committed
166
167
168
169
170
171
172
173
174
175
    x1 = ift.RGSpace((9,), distances=2.)
    x2 = ift.RGSpace((2, 12), distances=(0.3,))
    m1 = ift.Field.from_global_data(ift.makeDomain(x1), np.arange(9))
    m2 = ift.Field.full(ift.makeDomain((x1, x2)), 0.45)
    res1 = m1.integrate()
    res2 = m2.integrate(spaces=1)
    assert_allclose(res1, 36*2)
    assert_allclose(res2.to_global_data(), np.full(9, 2*12*0.45*0.3**2))


Philipp Arras's avatar
Fixups    
Philipp Arras committed
176
def test_dataconv():
Philipp Arras's avatar
Philipp Arras committed
177
178
179
180
181
182
183
    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())


Philipp Arras's avatar
Fixups    
Philipp Arras committed
184
def test_cast_domain():
Philipp Arras's avatar
Philipp Arras committed
185
186
187
188
189
190
191
    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)


Philipp Arras's avatar
Fixups    
Philipp Arras committed
192
def test_empty_domain():
Philipp Arras's avatar
Philipp Arras committed
193
194
195
196
197
198
    f = ift.Field.full((), 5)
    assert_equal(f.local_data, 5)
    f = ift.Field.full(None, 5)
    assert_equal(f.local_data, 5)


Philipp Arras's avatar
Fixups    
Philipp Arras committed
199
def test_trivialities():
Philipp Arras's avatar
Philipp Arras committed
200
201
    s1 = ift.RGSpace((10,))
    f1 = ift.Field.full(s1, 27)
202
203
    assert_equal(f1.clip(min=29).local_data, 29.)
    assert_equal(f1.clip(max=25).local_data, 25.)
Philipp Arras's avatar
Philipp Arras committed
204
    assert_equal(f1.local_data, f1.real.local_data)
205
206
207
    assert_equal(f1.local_data, (+f1).local_data)
    print(f1)
    print(str(f1))
Philipp Arras's avatar
Philipp Arras committed
208
    f1 = ift.Field.full(s1, 27. + 3j)
209
    assert_equal(f1.one_over().local_data, (1./f1).local_data)
Philipp Arras's avatar
Philipp Arras committed
210
211
212
    assert_equal(f1.real.local_data, 27.)
    assert_equal(f1.imag.local_data, 3.)
    assert_equal(f1.sum(), f1.sum(0))
213
    assert_equal(f1.conjugate().local_data, ift.Field.full(s1, 27. - 3j).local_data)
Philipp Arras's avatar
Philipp Arras committed
214
215
216
217
218
219
    f1 = ift.from_global_data(s1, np.arange(10))
    # assert_equal(f1.min(), 0)
    # assert_equal(f1.max(), 9)
    assert_equal(f1.prod(), 0)


Philipp Arras's avatar
Fixups    
Philipp Arras committed
220
def test_weight():
Philipp Arras's avatar
Philipp Arras committed
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
    s1 = ift.RGSpace((10,))
    f = ift.Field.full(s1, 10.)
    f2 = f.weight(1)
    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)
    f = ift.Field.full(s1, 10.)
    assert_equal(f.scalar_weight(), None)
    assert_equal(f.scalar_weight(0), None)
    assert_equal(f.scalar_weight((0,)), None)


@pmp('dom', [ift.RGSpace(10), ift.GLSpace(10)])
@pmp('dt', [np.float64, np.complex128])
def test_reduction(dom, dt):
    s1 = ift.Field.full(dom, dt(1.))
    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)


Philipp Arras's avatar
Fixups    
Philipp Arras committed
250
def test_err():
Philipp Arras's avatar
Philipp Arras committed
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
    s1 = ift.RGSpace((10,))
    s2 = ift.RGSpace((11,))
    f1 = ift.Field.full(s1, 27)
    with assert_raises(ValueError):
        f2 = ift.Field(ift.DomainTuple.make(s2), f1.val)
    with assert_raises(TypeError):
        f2 = ift.Field.full(s2, "xyz")
    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)
    with assert_raises(TypeError):
        f2 = ift.Field(s1, None)
    with assert_raises(ValueError):
        f1.imag
    with assert_raises(TypeError):
        f1.vdot(42)
    with assert_raises(ValueError):
        f1.vdot(ift.Field.full(s2, 1.))
    with assert_raises(TypeError):
        ift.full(s1, [2, 3])
275
    with assert_raises(TypeError):
276
        ift.Field(s2, [0,1])
277
278
279
280
281
282
283
284
285
    with assert_raises(TypeError):
        f1.outer([0,1])
    with assert_raises(ValueError):
        f1.extract(s2)
    with assert_raises(TypeError):
        f1 += f1
    f2 = ift.Field.full(s2, 27)
    with assert_raises(ValueError):
        f1 + f2
Philipp Arras's avatar
Philipp Arras committed
286
287


Philipp Arras's avatar
Fixups    
Philipp Arras committed
288
def test_stdfunc():
Philipp Arras's avatar
Philipp Arras committed
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
    s = ift.RGSpace((200,))
    f = ift.Field.full(s, 27)
    assert_equal(f.local_data, 27)
    assert_equal(f.shape, (200,))
    assert_equal(f.dtype, np.int)
    fx = ift.full(f.domain, 0)
    assert_equal(f.dtype, fx.dtype)
    assert_equal(f.shape, fx.shape)
    assert_equal(fx.local_data, 0)
    fx = ift.full(f.domain, 1)
    assert_equal(f.dtype, fx.dtype)
    assert_equal(f.shape, fx.shape)
    assert_equal(fx.local_data, 1)
    fx = ift.full(f.domain, 67.)
    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))


Philipp Arras's avatar
Fixups    
Philipp Arras committed
321
def test_emptydomain():
Philipp Arras's avatar
Philipp Arras committed
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
    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.)


@pmp('num', [float(5), 5.])
@pmp('dom', [ift.RGSpace((8,), harmonic=True), ()])
@pmp('func', [
    "exp", "log", "sin", "cos", "tan", "sinh", "cosh", "sinc", "absolute",
    "sign"
])
def test_funcs(num, dom, func):
    num = 5
    f = ift.Field.full(dom, num)
    res = getattr(f, func)()
    res2 = getattr(np, func)(num)
    assert_allclose(res.local_data, res2)
343

344
345
346
347
348
@pmp('rtype', ['normal', 'pm1', 'uniform'])
@pmp('dtype', [np.float64, np.complex128])
def test_from_random(rtype, dtype):
    sp = ift.RGSpace(3)
    f = ift.Field.from_random(rtype, sp, dtype=dtype)
349
350
351
352
353

def test_field_of_objects():
    arr = np.array(['x', 'y', 'z'])
    sp = ift.RGSpace(3)
    f = ift.Field.from_global_data(sp, arr)