getting_started_1.py 3 KB
Newer Older
1
2
import nifty5 as ift
import numpy as np
3
4


Philipp Arras's avatar
Philipp Arras committed
5
def make_chess_mask(position_space):
6
7
8
    mask = np.ones(position_space.shape)
    for i in range(4):
        for j in range(4):
9
            if (i+j) % 2 == 0:
Philipp Arras's avatar
Philipp Arras committed
10
                mask[i*128//4:(i+1)*128//4, j*128//4:(j+1)*128//4] = 0
11
12
    return mask

Philipp Arras's avatar
Philipp Arras committed
13

14
def make_random_mask():
15
    mask = ift.from_random('pm1', position_space)
16
    mask = (mask+1)/2
Martin Reinecke's avatar
Martin Reinecke committed
17
    return mask.to_global_data()
18

Philipp Arras's avatar
Philipp Arras committed
19

20
if __name__ == '__main__':
Philipp Arras's avatar
Philipp Arras committed
21
    np.random.seed(42)
Philipp Arras's avatar
Philipp Arras committed
22
23
    # FIXME description of the tutorial

24
    # Choose problem geometry and masking
Philipp Arras's avatar
Philipp Arras committed
25
26
27
28
29
30
31
32
33
34
35
36
37
    mode = 0
    if mode == 0:
        # One dimensional regular grid
        position_space = ift.RGSpace([1024])
        mask = np.ones(position_space.shape)
    elif mode == 1:
        # Two dimensional regular grid with chess mask
        position_space = ift.RGSpace([128, 128])
        mask = make_chess_mask(position_space)
    else:
        # Sphere with half of its locations randomly masked
        position_space = ift.HPSpace(128)
        mask = make_random_mask()
38

39
40
    harmonic_space = position_space.get_default_codomain()
    HT = ift.HarmonicTransformOperator(harmonic_space, target=position_space)
41

Philipp Arras's avatar
Philipp Arras committed
42
    # Set correlation structure with a power spectrum and build
43
    # prior correlation covariance
44
45
46
47
48
    def power_spectrum(k):
        return 100. / (20.+k**3)
    power_space = ift.PowerSpace(harmonic_space)
    PD = ift.PowerDistributor(harmonic_space, power_space)
    prior_correlation_structure = PD(ift.PS_field(power_space, power_spectrum))
49

50
    S = ift.DiagonalOperator(prior_correlation_structure)
51

Philipp Arras's avatar
Philipp Arras committed
52
    # Build instrument response consisting of a discretization, mask
53
    # and harmonic transformaion
54
    GR = ift.GeometryRemover(position_space)
55
    mask = ift.Field.from_global_data(position_space, mask)
56
57
58
59
60
    Mask = ift.DiagonalOperator(mask)
    R = GR * Mask * HT

    data_space = GR.target

Philipp Arras's avatar
Philipp Arras committed
61
    # Set the noise covariance
62
63
    noise = 5.
    N = ift.ScalingOperator(noise, data_space)
64

Philipp Arras's avatar
Philipp Arras committed
65
    # Create mock data
66
67
68
    MOCK_SIGNAL = S.draw_sample()
    MOCK_NOISE = N.draw_sample()
    data = R(MOCK_SIGNAL) + MOCK_NOISE
69

Philipp Arras's avatar
Philipp Arras committed
70
    # Build propagator D and information source j
71
72
    j = R.adjoint_times(N.inverse_times(data))
    D_inv = R.adjoint * N.inverse * R + S.inverse
Philipp Arras's avatar
Philipp Arras committed
73
    # Make it invertible
74
    IC = ift.GradientNormController(iteration_limit=500, tol_abs_gradnorm=1e-3)
75
    D = ift.InversionEnabler(D_inv, IC, approximation=S.inverse).inverse
76
77
78
79

    # WIENER FILTER
    m = D(j)

80
    # PLOTTING
Philipp Arras's avatar
Philipp Arras committed
81
82
83
84
85
86
87
88
89
90
91
    rg = isinstance(position_space, ift.RGSpace)
    if rg and len(position_space.shape) == 1:
        ift.plot([HT(MOCK_SIGNAL), GR.adjoint(data), HT(m)],
                 label=['Mock signal', 'Data', 'Reconstruction'],
                 alpha=[1, .3, 1],
                 name='getting_started_1.png')
    else:
        ift.plot(HT(MOCK_SIGNAL), title='Mock Signal', name='mock_signal.png')
        ift.plot((GR*Mask).adjoint(data), title='Data', name='data.png')
        ift.plot(HT(m), title='Reconstruction', name='reconstruction.png')
    ift.plot(HT(m-MOCK_SIGNAL), name='residuals.png')