multi_amplitudes_consistency.py 3.8 KB
Newer Older
1
2
import nifty5 as ift
import numpy as np
Philipp Arras's avatar
Philipp Arras committed
3

4

Philipp Arras's avatar
Philipp Arras committed
5
6
7


def testAmplitudesConsistency(seed, sspace):
8
    np.random.seed(seed)
9
10
11
12
    offset_std = 30
    intergated_fluct_std0 = .003
    intergated_fluct_std1 = 0.1
    
13
    nsam = 1000
Philipp Arras's avatar
Philipp Arras committed
14
15
16
17
18


    fsspace = ift.RGSpace((12,), (0.4,))

    fa = ift.CorrelatedFieldMaker()
19
    fa.add_fluctuations(sspace, intergated_fluct_std0, 1E-8, 1.1, 2., 2.1, .5,
Philipp Arras's avatar
Philipp Arras committed
20
                        -2, 1., 'spatial')
21
    fa.add_fluctuations(fsspace, intergated_fluct_std1, 1E-8, 3.1, 1., .5, .1,
Philipp Arras's avatar
Philipp Arras committed
22
23
24
                        -4, 1., 'freq')
    op = fa.finalize(offset_std, 1E-8, '')

25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
    samples = [ift.from_random('normal',op.domain) for _ in range(nsam)]
    tot_flm, _ = fa.stats(fa.total_fluctuation,samples)
    offset_std,_ = fa.stats(fa.amplitude_total_offset,samples)
    intergated_fluct_std0,_ = fa.stats(fa.average_fluctuation(0),samples)
    intergated_fluct_std1,_ = fa.stats(fa.average_fluctuation(1),samples)
    
    slice_fluct_std0,_ = fa.stats(fa.slice_fluctuation(0),samples)
    slice_fluct_std1,_ = fa.stats(fa.slice_fluctuation(1),samples)

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

Philipp Arras's avatar
Minor    
Philipp Arras committed
42
43
    np.testing.assert_allclose(offset_std, zm_std_mean, rtol=0.5)
    np.testing.assert_allclose(intergated_fluct_std0, fluct_space, rtol=0.5)
44
    np.testing.assert_allclose(intergated_fluct_std1, fluct_freq, rtol=0.5)
Philipp Arras's avatar
Minor    
Philipp Arras committed
45
    np.testing.assert_allclose(tot_flm, fluct_total, rtol=0.5)
46
47
    np.testing.assert_allclose(slice_fluct_std0, sl_fluct_space, rtol=0.5)
    np.testing.assert_allclose(slice_fluct_std1, sl_fluct_freq, rtol=0.5)
Philipp Arras's avatar
Minor    
Philipp Arras committed
48

Philipp Arras's avatar
Philipp Arras committed
49
50
51
52
53
54
55
56
57
58
    print("Expected  offset Std: " + str(offset_std))
    print("Estimated offset Std: " + str(zm_std_mean))

    print("Expected  integrated fluct. space Std: " +
          str(intergated_fluct_std0))
    print("Estimated integrated fluct. space Std: " + str(fluct_space))

    print("Expected  integrated fluct. frequency Std: " +
          str(intergated_fluct_std1))
    print("Estimated integrated fluct. frequency Std: " + str(fluct_freq))
59
60
61
62
63
64
65
66
    
    print("Expected  slice fluct. space Std: " +
          str(slice_fluct_std0))
    print("Estimated slice fluct. space Std: " + str(sl_fluct_space))

    print("Expected  slice fluct. frequency Std: " +
          str(slice_fluct_std1))
    print("Estimated slice fluct. frequency Std: " + str(sl_fluct_freq))
Philipp Arras's avatar
Philipp Arras committed
67
68
69

    print("Expected  total fluct. Std: " + str(tot_flm))
    print("Estimated total fluct. Std: " + str(fluct_total))
70
71
    
    fa = ift.CorrelatedFieldMaker()
72
    fa.add_fluctuations(fsspace, intergated_fluct_std1, 1., 3.1, 1., .5, .1,
73
74
75
                        -4, 1., 'freq')
    m = 3.
    x = fa.moment_slice_to_average(m)
76
    fa.add_fluctuations(sspace, x, 1.5, 1.1, 2., 2.1, .5,
77
78
79
80
81
82
                        -2, 1., 'spatial', 0)
    op = fa.finalize(offset_std, .1, '')
    em, estd = fa.stats(fa.slice_fluctuation(0),samples)
    print("Forced   slice fluct. space Std: "+str(m))
    print("Expected slice fluct. Std: " + str(em))
    np.testing.assert_allclose(m, em, rtol=0.5)
83
84
85
    
    assert op.target[0] == sspace
    assert op.target[1] == fsspace
Philipp Arras's avatar
Philipp Arras committed
86
87
88
89
90
91
92
93
94
95
96
97
98
99


# Move to tests
# FIXME This test fails but it is not relevant for the final result
# assert_allclose(ampl(from_random('normal', ampl.domain)).val[0], vol) or 1??
# End move to tests

# move to tests
# assert_allclose(
#     smooth(from_random('normal', smooth.domain)).val[0:2], 0)
# end move to tests
for seed in [1, 42]:
    for sp in [
            ift.RGSpace((32, 64), (1.1, 0.3)),
Philipp Frank's avatar
fixup    
Philipp Frank committed
100
101
            ift.HPSpace(32),
            ift.GLSpace(32)
Philipp Arras's avatar
Philipp Arras committed
102
103
    ]:
        testAmplitudesConsistency(seed, sp)