test_correlated_fields.py 3.68 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# 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/>.
#
# Copyright(C) 2013-2019 Max-Planck-Society
#
# NIFTy is being developed at the Max-Planck-Institut fuer Astrophysik.

import pytest
Philipp Arras's avatar
Philipp Arras committed
19
from numpy.testing import assert_allclose
20

Martin Reinecke's avatar
5->6    
Martin Reinecke committed
21
import nifty6 as ift
22
23
24
25


@pytest.mark.parametrize('sspace', [
    ift.RGSpace(4),
Philipp Arras's avatar
Philipp Arras committed
26
    ift.RGSpace((4, 4), (0.123, 0.4)),
27
28
29
30
    ift.HPSpace(8),
    ift.GLSpace(4)
])
@pytest.mark.parametrize('rseed', [13, 2])
Philipp Arras's avatar
Philipp Arras committed
31
32
@pytest.mark.parametrize('Astds', [[1., 3.], [0.2, 1.4]])
@pytest.mark.parametrize('offset_std', [1., 10.])
Philipp Haim's avatar
Philipp Haim committed
33
34
@pytest.mark.parametrize('N', [0,2])
def testAmplitudesConsistency(rseed, sspace, Astds, offset_std, N):
Philipp Arras's avatar
Philipp Arras committed
35
    def stats(op, samples):
Philipp Frank's avatar
fixes    
Philipp Frank committed
36
37
38
        sc = ift.StatCalculator()
        for s in samples:
            sc.add(op(s.extract(op.domain)))
Martin Reinecke's avatar
stage 3    
Martin Reinecke committed
39
        return sc.mean.val, sc.var.sqrt().val
Philipp Arras's avatar
Philipp Arras committed
40

Martin Reinecke's avatar
Martin Reinecke committed
41
    ift.random.push_sseq_from_seed(rseed)
42
43
44
    nsam = 100

    fsspace = ift.RGSpace((12,), (0.4,))
Philipp Haim's avatar
Philipp Haim committed
45
46
47
48
49
50
    if N==2:
        dofdex1 = [0,0]
        dofdex2 = [1,0]
        dofdex3 = [1,1]
    else:
        dofdex1, dofdex2, dofdex3 = None, None, None
51

Philipp Haim's avatar
Philipp Haim committed
52
    fa = ift.CorrelatedFieldMaker.make(offset_std, 1E-8, '', N, dofdex1)
Philipp Arras's avatar
Philipp Arras committed
53
    fa.add_fluctuations(sspace, Astds[0], 1E-8, 1.1, 2., 2.1, .5, -2, 1.,
Philipp Haim's avatar
Philipp Haim committed
54
                        'spatial', dofdex = dofdex2)
Philipp Arras's avatar
Philipp Arras committed
55
    fa.add_fluctuations(fsspace, Astds[1], 1E-8, 3.1, 1., .5, .1, -4, 1.,
Philipp Haim's avatar
Philipp Haim committed
56
                        'freq', dofdex = dofdex3)
57
    op = fa.finalize()
58

Philipp Arras's avatar
Philipp Arras committed
59
60
    samples = [ift.from_random('normal', op.domain) for _ in range(nsam)]
    tot_flm, _ = stats(fa.total_fluctuation, samples)
Philipp Haim's avatar
Philipp Haim committed
61
    offset_amp_std, _ = stats(fa.amplitude_total_offset, samples)
Philipp Arras's avatar
Philipp Arras committed
62
63
    intergated_fluct_std0, _ = stats(fa.average_fluctuation(0), samples)
    intergated_fluct_std1, _ = stats(fa.average_fluctuation(1), samples)
Martin Reinecke's avatar
stage 3    
Martin Reinecke committed
64

Philipp Arras's avatar
Philipp Arras committed
65
66
    slice_fluct_std0, _ = stats(fa.slice_fluctuation(0), samples)
    slice_fluct_std1, _ = stats(fa.slice_fluctuation(1), samples)
67
68
69

    sams = [op(s) for s in samples]
    fluct_total = fa.total_fluctuation_realized(sams)
Philipp Arras's avatar
Philipp Arras committed
70
71
    fluct_space = fa.average_fluctuation_realized(sams, 0)
    fluct_freq = fa.average_fluctuation_realized(sams, 1)
72
    zm_std_mean = fa.offset_amplitude_realized(sams)
Philipp Arras's avatar
Philipp Arras committed
73
74
    sl_fluct_space = fa.slice_fluctuation_realized(sams, 0)
    sl_fluct_freq = fa.slice_fluctuation_realized(sams, 1)
75

Philipp Haim's avatar
Philipp Haim committed
76
    assert_allclose(offset_amp_std, zm_std_mean, rtol=0.5)
77
78
79
80
81
82
    assert_allclose(intergated_fluct_std0, fluct_space, rtol=0.5)
    assert_allclose(intergated_fluct_std1, fluct_freq, rtol=0.5)
    assert_allclose(tot_flm, fluct_total, rtol=0.5)
    assert_allclose(slice_fluct_std0, sl_fluct_space, rtol=0.5)
    assert_allclose(slice_fluct_std1, sl_fluct_freq, rtol=0.5)

Philipp Haim's avatar
Philipp Haim committed
83
84
85
    
    fa = ift.CorrelatedFieldMaker.make(offset_std, .1, '', N, dofdex1)
    fa.add_fluctuations(fsspace, Astds[1], 1., 3.1, 1., .5, .1, -4, 1., 'freq', dofdex = dofdex3)
86
87
    m = 3.
    x = fa.moment_slice_to_average(m)
Philipp Haim's avatar
Philipp Haim committed
88
    fa.add_fluctuations(sspace, x, 1.5, 1.1, 2., 2.1, .5, -2, 1., 'spatial', 0, dofdex = dofdex2)
89
    op = fa.finalize()
Philipp Arras's avatar
Philipp Arras committed
90
    em, estd = stats(fa.slice_fluctuation(0), samples)
91
92

    assert_allclose(m, em, rtol=0.5)
Martin Reinecke's avatar
Martin Reinecke committed
93
    ift.random.pop_sseq()
Martin Reinecke's avatar
stage 3    
Martin Reinecke committed
94

Philipp Haim's avatar
Philipp Haim committed
95
96
    assert op.target[-2] == sspace
    assert op.target[-1] == fsspace