test_field.py 11.2 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
#
Philipp Arras's avatar
Philipp Arras committed
14
# Copyright(C) 2013-2020 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

Martin Reinecke's avatar
Martin Reinecke committed
22
import nifty7 as ift
Philipp Arras's avatar
Cleanup    
Philipp Arras committed
23

24
from .common import setup_function, teardown_function
Philipp Arras's avatar
Philipp Arras committed
25
26

pmp = pytest.mark.parametrize
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


Philipp Arras's avatar
Philipp Arras committed
31
32
@pmp('domain', SPACE_COMBINATIONS)
@pmp('attribute_desired_type',
Martin Reinecke's avatar
stage 1    
Martin Reinecke committed
33
     [['domain', ift.DomainTuple], ['val', np.ndarray],
34
      ['shape', tuple], ['size', (int, np.int64)]])
Philipp Arras's avatar
Philipp Arras committed
35
36
37
38
39
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)
40

Martin Reinecke's avatar
Martin Reinecke committed
41

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


def _spec2(k):
Philipp Arras's avatar
Philipp Arras committed
47
48
49
    return 42/(1. + k)**3


Philipp Arras's avatar
Philipp Arras committed
50
51
@pmp('space1', [ift.RGSpace((8,), harmonic=True),
                ift.RGSpace((8, 8), harmonic=True, distances=0.123)])
Philipp Arras's avatar
Philipp Arras committed
52
53
54
55
56
57
58
59
@pmp('space2', [ift.RGSpace((8,), harmonic=True), ift.LMSpace(12)])
def test_power_synthesize_analyze(space1, space2):
    p1 = ift.PowerSpace(space1)
    fp1 = ift.PS_field(p1, _spec1)
    p2 = ift.PowerSpace(space2)
    fp2 = ift.PS_field(p2, _spec2)
    op1 = ift.create_power_operator((space1, space2), _spec1, 0)
    op2 = ift.create_power_operator((space1, space2), _spec2, 1)
Philipp Arras's avatar
Philipp Arras committed
60
    opfull = op2 @ op1
Philipp Arras's avatar
Philipp Arras committed
61

Philipp Arras's avatar
Philipp Arras committed
62
    samples = 120
Philipp Arras's avatar
Philipp Arras committed
63
64
65
    sc1 = ift.StatCalculator()
    sc2 = ift.StatCalculator()
    for ii in range(samples):
Philipp Arras's avatar
Philipp Arras committed
66
        sk = opfull.draw_sample_with_dtype(dtype=np.float64)
Philipp Arras's avatar
Philipp Arras committed
67
        sp = ift.power_analyze(sk, spaces=(0, 1), keep_phase_information=False)
Martin Reinecke's avatar
Martin Reinecke committed
68
69
        sc1.add(sp.sum(spaces=1)/fp2.s_sum())
        sc2.add(sp.sum(spaces=0)/fp1.s_sum())
Martin Reinecke's avatar
stage 1    
Martin Reinecke committed
70
71
    assert_allclose(sc1.mean.val, fp1.val, rtol=0.2)
    assert_allclose(sc2.mean.val, fp2.val, rtol=0.2)
Philipp Arras's avatar
Philipp Arras committed
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91


@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):
    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):
Philipp Arras's avatar
Philipp Arras committed
92
        sk = S_full.draw_sample_with_dtype(dtype=np.float64)
Philipp Arras's avatar
Philipp Arras committed
93
        sp = ift.power_analyze(sk, spaces=(0, 1), keep_phase_information=False)
Martin Reinecke's avatar
Martin Reinecke committed
94
95
        sc1.add(sp.sum(spaces=1)/fp2.s_sum())
        sc2.add(sp.sum(spaces=0)/fp1.s_sum())
Philipp Arras's avatar
Philipp Arras committed
96

Martin Reinecke's avatar
stage 1    
Martin Reinecke committed
97
98
    assert_allclose(sc1.mean.val, fp1.val, rtol=0.2)
    assert_allclose(sc2.mean.val, fp2.val, rtol=0.2)
Philipp Arras's avatar
Philipp Arras committed
99
100
101
102
103
104
105
106


@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):
107
    f = ift.Field.from_random(domain=space, random_type="normal", dtype=np.complex128)
Martin Reinecke's avatar
stage 1    
Martin Reinecke committed
108
    gd = f.val.reshape(-1)
Philipp Arras's avatar
Philipp Arras committed
109
110
111
112
113
114
115
    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
116
def test_vdot():
Philipp Arras's avatar
Philipp Arras committed
117
    s = ift.RGSpace((10,))
118
119
    f1 = ift.Field.from_random(domain=s, random_type="normal", dtype=np.complex128)
    f2 = ift.Field.from_random(domain=s, random_type="normal", dtype=np.complex128)
Martin Reinecke's avatar
Martin Reinecke committed
120
121
    assert_allclose(f1.s_vdot(f2), f1.vdot(f2, spaces=0).val)
    assert_allclose(f1.s_vdot(f2), np.conj(f2.s_vdot(f1)))
Philipp Arras's avatar
Philipp Arras committed
122
123


Philipp Arras's avatar
Fixups    
Philipp Arras committed
124
def test_vdot2():
Philipp Arras's avatar
Philipp Arras committed
125
126
127
128
    x1 = ift.RGSpace((200,))
    x2 = ift.RGSpace((150,))
    m = ift.Field.full((x1, x2), .5)
    res = m.vdot(m, spaces=1)
Martin Reinecke's avatar
stage 1    
Martin Reinecke committed
129
    assert_allclose(res.val, 37.5)
Philipp Arras's avatar
Philipp Arras committed
130
131


Philipp Arras's avatar
Fixups    
Philipp Arras committed
132
def test_outer():
Philipp Arras's avatar
Philipp Arras committed
133
134
135
136
137
    x1 = ift.RGSpace((9,))
    x2 = ift.RGSpace((3,))
    m1 = ift.Field.full(x1, .5)
    m2 = ift.Field.full(x2, 3.)
    res = m1.outer(m2)
Martin Reinecke's avatar
stage 1    
Martin Reinecke committed
138
    assert_allclose(res.val, np.full((9, 3), 1.5))
Philipp Arras's avatar
Philipp Arras committed
139
140


Philipp Arras's avatar
Fixups    
Philipp Arras committed
141
def test_sum():
Philipp Arras's avatar
Philipp Arras committed
142
    x1 = ift.RGSpace((9,), distances=2.)
Philipp Arras's avatar
Cleanup    
Philipp Arras committed
143
    x2 = ift.RGSpace((2, 12), distances=(0.3,))
Martin Reinecke's avatar
stage 1    
Martin Reinecke committed
144
    m1 = ift.Field(ift.makeDomain(x1), np.arange(9))
Philipp Arras's avatar
Philipp Arras committed
145
    m2 = ift.Field.full(ift.makeDomain((x1, x2)), 0.45)
Martin Reinecke's avatar
Martin Reinecke committed
146
    res1 = m1.s_sum()
Philipp Arras's avatar
Philipp Arras committed
147
148
    res2 = m2.sum(spaces=1)
    assert_allclose(res1, 36)
Martin Reinecke's avatar
stage 1    
Martin Reinecke committed
149
    assert_allclose(res2.val, np.full(9, 2*12*0.45))
Philipp Arras's avatar
Philipp Arras committed
150
151


Philipp Arras's avatar
Fixups    
Philipp Arras committed
152
def test_integrate():
Philipp Arras's avatar
Philipp Arras committed
153
154
    x1 = ift.RGSpace((9,), distances=2.)
    x2 = ift.RGSpace((2, 12), distances=(0.3,))
Martin Reinecke's avatar
stage 1    
Martin Reinecke committed
155
    m1 = ift.Field(ift.makeDomain(x1), np.arange(9))
Philipp Arras's avatar
Philipp Arras committed
156
    m2 = ift.Field.full(ift.makeDomain((x1, x2)), 0.45)
Martin Reinecke's avatar
Martin Reinecke committed
157
    res1 = m1.s_integrate()
Philipp Arras's avatar
Philipp Arras committed
158
159
    res2 = m2.integrate(spaces=1)
    assert_allclose(res1, 36*2)
Martin Reinecke's avatar
stage 1    
Martin Reinecke committed
160
    assert_allclose(res2.val, np.full(9, 2*12*0.45*0.3**2))
161
162
163
164
    for m in [m1, m2]:
        res3 = m.integrate()
        res4 = m.s_integrate()
        assert_allclose(res3.val, res4)
Vincent Eberle's avatar
Vincent Eberle committed
165
166
    dom = ift.HPSpace(3)
    assert_allclose(ift.full(dom, 1).s_integrate(), 4*np.pi)
Philipp Arras's avatar
Philipp Arras committed
167
168


Philipp Arras's avatar
Fixups    
Philipp Arras committed
169
def test_dataconv():
Philipp Arras's avatar
Philipp Arras committed
170
171
    s1 = ift.RGSpace((10,))
    gd = np.arange(s1.shape[0])
Martin Reinecke's avatar
Martin Reinecke committed
172
    assert_equal(gd, ift.makeField(s1, gd).val)
Philipp Arras's avatar
Philipp Arras committed
173
174


Philipp Arras's avatar
Fixups    
Philipp Arras committed
175
def test_cast_domain():
Philipp Arras's avatar
Philipp Arras committed
176
177
178
    s1 = ift.RGSpace((10,))
    s2 = ift.RGSpace((10,), distances=20.)
    d = np.arange(s1.shape[0])
Martin Reinecke's avatar
Martin Reinecke committed
179
    d2 = ift.makeField(s1, d).cast_domain(s2).val
Philipp Arras's avatar
Philipp Arras committed
180
181
182
    assert_equal(d, d2)


Philipp Arras's avatar
Fixups    
Philipp Arras committed
183
def test_empty_domain():
Philipp Arras's avatar
Philipp Arras committed
184
    f = ift.Field.full((), 5)
Martin Reinecke's avatar
stage 1    
Martin Reinecke committed
185
    assert_equal(f.val, 5)
Philipp Arras's avatar
Philipp Arras committed
186
    f = ift.Field.full(None, 5)
Martin Reinecke's avatar
stage 1    
Martin Reinecke committed
187
    assert_equal(f.val, 5)
Philipp Arras's avatar
Philipp Arras committed
188
189


Philipp Arras's avatar
Fixups    
Philipp Arras committed
190
def test_trivialities():
Philipp Arras's avatar
Philipp Arras committed
191
192
    s1 = ift.RGSpace((10,))
    f1 = ift.Field.full(s1, 27)
Martin Reinecke's avatar
Martin Reinecke committed
193
194
    assert_equal(f1.clip(a_min=29, a_max=50).val, 29.)
    assert_equal(f1.clip(a_min=0, a_max=25).val, 25.)
Martin Reinecke's avatar
stage 1    
Martin Reinecke committed
195
196
    assert_equal(f1.val, f1.real.val)
    assert_equal(f1.val, (+f1).val)
Philipp Arras's avatar
Philipp Arras committed
197
    f1 = ift.Field.full(s1, 27. + 3j)
Martin Reinecke's avatar
Martin Reinecke committed
198
    assert_equal(f1.ptw("reciprocal").val, (1./f1).val)
Martin Reinecke's avatar
stage 1    
Martin Reinecke committed
199
200
    assert_equal(f1.real.val, 27.)
    assert_equal(f1.imag.val, 3.)
Martin Reinecke's avatar
Martin Reinecke committed
201
    assert_equal(f1.s_sum(), f1.sum(0).val)
Martin Reinecke's avatar
stage 1    
Martin Reinecke committed
202
203
    assert_equal(f1.conjugate().val,
                 ift.Field.full(s1, 27. - 3j).val)
Martin Reinecke's avatar
Martin Reinecke committed
204
    f1 = ift.makeField(s1, np.arange(10))
Philipp Arras's avatar
Philipp Arras committed
205
206
    # assert_equal(f1.min(), 0)
    # assert_equal(f1.max(), 9)
Martin Reinecke's avatar
Martin Reinecke committed
207
    assert_equal(f1.s_prod(), 0)
Philipp Arras's avatar
Philipp Arras committed
208
209


Philipp Arras's avatar
Fixups    
Philipp Arras committed
210
def test_weight():
Philipp Arras's avatar
Philipp Arras committed
211
212
213
    s1 = ift.RGSpace((10,))
    f = ift.Field.full(s1, 10.)
    f2 = f.weight(1)
Martin Reinecke's avatar
stage 1    
Martin Reinecke committed
214
    assert_equal(f.weight(1).val, f2.val)
215
216
217
    assert_equal(f.domain.total_volume(), 1)
    assert_equal(f.domain.total_volume(0), 1)
    assert_equal(f.domain.total_volume((0,)), 1)
Philipp Arras's avatar
Philipp Arras committed
218
219
220
    assert_equal(f.total_volume(), 1)
    assert_equal(f.total_volume(0), 1)
    assert_equal(f.total_volume((0,)), 1)
221
222
223
    assert_equal(f.domain.scalar_weight(), 0.1)
    assert_equal(f.domain.scalar_weight(0), 0.1)
    assert_equal(f.domain.scalar_weight((0,)), 0.1)
Philipp Arras's avatar
Philipp Arras committed
224
225
226
227
228
    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.)
229
230
231
    assert_equal(f.domain.scalar_weight(), None)
    assert_equal(f.domain.scalar_weight(0), None)
    assert_equal(f.domain.scalar_weight((0,)), None)
Philipp Arras's avatar
Philipp Arras committed
232
233
234
235
236
237


@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.))
Martin Reinecke's avatar
Martin Reinecke committed
238
239
240
241
242
243
    assert_allclose(s1.s_mean(), 1.)
    assert_allclose(s1.mean(0).val, 1.)
    assert_allclose(s1.s_var(), 0., atol=1e-14)
    assert_allclose(s1.var(0).val, 0., atol=1e-14)
    assert_allclose(s1.s_std(), 0., atol=1e-14)
    assert_allclose(s1.std(0).val, 0., atol=1e-14)
Philipp Arras's avatar
Philipp Arras committed
244
245


Philipp Arras's avatar
Fixups    
Philipp Arras committed
246
def test_err():
Philipp Arras's avatar
Philipp Arras committed
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
    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])
271
    with assert_raises(TypeError):
Martin Reinecke's avatar
Martin Reinecke committed
272
        ift.Field(s2, [0, 1])
273
    with assert_raises(TypeError):
Martin Reinecke's avatar
Martin Reinecke committed
274
        f1.outer([0, 1])
275
276
277
278
279
280
281
    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
282
283


Philipp Arras's avatar
Fixups    
Philipp Arras committed
284
def test_stdfunc():
Philipp Arras's avatar
Philipp Arras committed
285
286
    s = ift.RGSpace((200,))
    f = ift.Field.full(s, 27)
Martin Reinecke's avatar
stage 1    
Martin Reinecke committed
287
    assert_equal(f.val, 27)
Philipp Arras's avatar
Philipp Arras committed
288
    assert_equal(f.shape, (200,))
289
    assert_equal(f.dtype, np.int64)
Philipp Arras's avatar
Philipp Arras committed
290
291
292
    fx = ift.full(f.domain, 0)
    assert_equal(f.dtype, fx.dtype)
    assert_equal(f.shape, fx.shape)
Martin Reinecke's avatar
stage 1    
Martin Reinecke committed
293
    assert_equal(fx.val, 0)
Philipp Arras's avatar
Philipp Arras committed
294
295
296
    fx = ift.full(f.domain, 1)
    assert_equal(f.dtype, fx.dtype)
    assert_equal(f.shape, fx.shape)
Martin Reinecke's avatar
stage 1    
Martin Reinecke committed
297
    assert_equal(fx.val, 1)
Philipp Arras's avatar
Philipp Arras committed
298
299
    fx = ift.full(f.domain, 67.)
    assert_equal(f.shape, fx.shape)
Martin Reinecke's avatar
stage 1    
Martin Reinecke committed
300
    assert_equal(fx.val, 67.)
301
302
    f = ift.Field.from_random(s, "normal")
    f2 = ift.Field.from_random(s, "normal")
Martin Reinecke's avatar
stage 1    
Martin Reinecke committed
303
304
305
306
307
308
309
310
311
312
313
314
    assert_equal((f > f2).val, f.val > f2.val)
    assert_equal((f >= f2).val, f.val >= f2.val)
    assert_equal((f < f2).val, f.val < f2.val)
    assert_equal((f <= f2).val, f.val <= f2.val)
    assert_equal((f != f2).val, f.val != f2.val)
    assert_equal((f == f2).val, f.val == f2.val)
    assert_equal((f + f2).val, f.val + f2.val)
    assert_equal((f - f2).val, f.val - f2.val)
    assert_equal((f*f2).val, f.val*f2.val)
    assert_equal((f/f2).val, f.val/f2.val)
    assert_equal((-f).val, -(f.val))
    assert_equal(abs(f).val, abs(f.val))
Philipp Arras's avatar
Philipp Arras committed
315
316


Philipp Arras's avatar
Fixups    
Philipp Arras committed
317
def test_emptydomain():
Philipp Arras's avatar
Philipp Arras committed
318
    f = ift.Field.full((), 3.)
Martin Reinecke's avatar
Martin Reinecke committed
319
320
    assert_equal(f.s_sum(), 3.)
    assert_equal(f.s_prod(), 3.)
Martin Reinecke's avatar
stage 1    
Martin Reinecke committed
321
322
323
    assert_equal(f.val, 3.)
    assert_equal(f.val.shape, ())
    assert_equal(f.val.size, 1)
Martin Reinecke's avatar
Martin Reinecke committed
324
    assert_equal(f.s_vdot(f), 9.)
Philipp Arras's avatar
Philipp Arras committed
325
326
327
328
329
330


@pmp('num', [float(5), 5.])
@pmp('dom', [ift.RGSpace((8,), harmonic=True), ()])
@pmp('func', [
    "exp", "log", "sin", "cos", "tan", "sinh", "cosh", "sinc", "absolute",
331
    "sign", "log10", "log1p", "expm1"
Philipp Arras's avatar
Philipp Arras committed
332
333
334
335
])
def test_funcs(num, dom, func):
    num = 5
    f = ift.Field.full(dom, num)
Martin Reinecke's avatar
Martin Reinecke committed
336
    res = f.ptw(func)
Philipp Arras's avatar
Philipp Arras committed
337
    res2 = getattr(np, func)(num)
Martin Reinecke's avatar
stage 1    
Martin Reinecke committed
338
    assert_allclose(res.val, res2)
339

Martin Reinecke's avatar
Martin Reinecke committed
340

341
342
343
344
@pmp('rtype', ['normal', 'pm1', 'uniform'])
@pmp('dtype', [np.float64, np.complex128])
def test_from_random(rtype, dtype):
    sp = ift.RGSpace(3)
345
    ift.Field.from_random(sp, rtype, dtype=dtype)
346

Martin Reinecke's avatar
Martin Reinecke committed
347

348
349
350
def test_field_of_objects():
    arr = np.array(['x', 'y', 'z'])
    sp = ift.RGSpace(3)
351
    with assert_raises(TypeError):
Martin Reinecke's avatar
stage 1    
Martin Reinecke committed
352
        ift.Field(sp, arr)