getting_started_1.py 2.9 KB
Newer Older
1
2
import nifty5 as ift
import numpy as np
3
4
5
6
7
8


def make_chess_mask():
    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__':
21
    # # description of the tutorial ###
22

Philipp Arras's avatar
Philipp Arras committed
23
    np.random.seed(42)
24
    # Choose problem geometry and masking
25

26
27
28
    # One dimensional regular grid
    position_space = ift.RGSpace([1024])
    mask = np.ones(position_space.shape)
29

30
31
32
    # # Two dimensional regular grid with chess mask
    # position_space = ift.RGSpace([128,128])
    # mask = make_chess_mask()
33

34
    # # Sphere with half of its locations randomly masked
Jakob Knollmueller's avatar
Jakob Knollmueller committed
35
36
    # position_space = ift.HPSpace(128)
    # mask = make_random_mask()
37

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

41
42
    # set correlation structure with a power spectrum and build
    # prior correlation covariance
43
44
45
46
47
    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))
48

49
    S = ift.DiagonalOperator(prior_correlation_structure)
50

51
52
    # build instrument response consisting of a discretization, mask
    # and harmonic transformaion
53
    GR = ift.GeometryRemover(position_space)
54
    mask = ift.Field.from_global_data(position_space, mask)
55
56
57
58
59
60
61
62
    Mask = ift.DiagonalOperator(mask)
    R = GR * Mask * HT

    data_space = GR.target

    # setting the noise covariance
    noise = 5.
    N = ift.ScalingOperator(noise, data_space)
63

64
65
66
67
    # creating mock data
    MOCK_SIGNAL = S.draw_sample()
    MOCK_NOISE = N.draw_sample()
    data = R(MOCK_SIGNAL) + MOCK_NOISE
68

69
70
71
72
    # building propagator D and information source j
    j = R.adjoint_times(N.inverse_times(data))
    D_inv = R.adjoint * N.inverse * R + S.inverse
    # make it invertible
73
    IC = ift.GradientNormController(iteration_limit=500, tol_abs_gradnorm=1e-3)
74
    D = ift.InversionEnabler(D_inv, IC, approximation=S.inverse).inverse
75
76
77
78

    # WIENER FILTER
    m = D(j)

79
    # PLOTTING
Philipp Arras's avatar
Philipp Arras committed
80
81
82
83
84
85
86
87
88
89
90
    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')