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

18
19
import nifty5 as ift
import numpy as np
20
21


Philipp Arras's avatar
Philipp Arras committed
22
def make_chess_mask(position_space):
23
24
25
    mask = np.ones(position_space.shape)
    for i in range(4):
        for j in range(4):
26
            if (i+j) % 2 == 0:
Philipp Arras's avatar
Philipp Arras committed
27
                mask[i*128//4:(i+1)*128//4, j*128//4:(j+1)*128//4] = 0
28
29
    return mask

Philipp Arras's avatar
Philipp Arras committed
30

31
def make_random_mask():
32
    mask = ift.from_random('pm1', position_space)
33
    mask = (mask+1)/2
Martin Reinecke's avatar
Martin Reinecke committed
34
    return mask.to_global_data()
35

Philipp Arras's avatar
Philipp Arras committed
36

Philipp Arras's avatar
Philipp Arras committed
37
38
39
40
41
42
def mask_to_nan(mask, field):
    masked_data = field.local_data.copy()
    masked_data[mask.local_data == 0] = np.nan
    return ift.from_local_data(field.domain, masked_data)


43
if __name__ == '__main__':
Philipp Arras's avatar
Philipp Arras committed
44
    np.random.seed(42)
Philipp Arras's avatar
Philipp Arras committed
45
46
    # FIXME description of the tutorial

47
    # Choose problem geometry and masking
Martin Reinecke's avatar
Martin Reinecke committed
48
    mode = 1
Philipp Arras's avatar
Philipp Arras committed
49
50
51
52
53
54
55
56
57
58
59
60
    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()
61

62
63
    harmonic_space = position_space.get_default_codomain()
    HT = ift.HarmonicTransformOperator(harmonic_space, target=position_space)
64

Philipp Arras's avatar
Philipp Arras committed
65
    # Set correlation structure with a power spectrum and build
66
    # prior correlation covariance
67
68
69
70
71
    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))
72

73
    S = ift.DiagonalOperator(prior_correlation_structure)
74

Philipp Arras's avatar
Philipp Arras committed
75
    # Build instrument response consisting of a discretization, mask
76
    # and harmonic transformaion
77
    GR = ift.GeometryRemover(position_space)
78
    mask = ift.Field.from_global_data(position_space, mask)
79
    Mask = ift.DiagonalOperator(mask)
Martin Reinecke's avatar
Martin Reinecke committed
80
    R = GR(Mask(HT))
81
82
83

    data_space = GR.target

Philipp Arras's avatar
Philipp Arras committed
84
    # Set the noise covariance
85
86
    noise = 5.
    N = ift.ScalingOperator(noise, data_space)
87

Philipp Arras's avatar
Philipp Arras committed
88
    # Create mock data
89
90
91
    MOCK_SIGNAL = S.draw_sample()
    MOCK_NOISE = N.draw_sample()
    data = R(MOCK_SIGNAL) + MOCK_NOISE
92

Philipp Arras's avatar
Philipp Arras committed
93
    # Build propagator D and information source j
94
    j = R.adjoint_times(N.inverse_times(data))
Martin Reinecke's avatar
Martin Reinecke committed
95
    D_inv = R.adjoint(N.inverse(R)) + S.inverse
Philipp Arras's avatar
Philipp Arras committed
96
    # Make it invertible
97
    IC = ift.GradientNormController(iteration_limit=500, tol_abs_gradnorm=1e-3)
98
    D = ift.InversionEnabler(D_inv, IC, approximation=S.inverse).inverse
99
100
101
102

    # WIENER FILTER
    m = D(j)

103
    # PLOTTING
Philipp Arras's avatar
Philipp Arras committed
104
    rg = isinstance(position_space, ift.RGSpace)
105
    plot = ift.Plot()
Philipp Arras's avatar
Philipp Arras committed
106
    if rg and len(position_space.shape) == 1:
107
        plot.add([HT(MOCK_SIGNAL), GR.adjoint(data), HT(m)],
Philipp Arras's avatar
Philipp Arras committed
108
                 label=['Mock signal', 'Data', 'Reconstruction'],
Martin Reinecke's avatar
Martin Reinecke committed
109
                 alpha=[1, .3, 1])
110
111
        plot.add(mask_to_nan(mask, HT(m-MOCK_SIGNAL)), title='Residuals')
        plot.output(nx=2, ny=1, xsize=10, ysize=4, title="getting_started_1")
Philipp Arras's avatar
Philipp Arras committed
112
    else:
113
114
        plot.add(HT(MOCK_SIGNAL), title='Mock Signal')
        plot.add(mask_to_nan(mask, (GR(Mask)).adjoint(data)),
115
                 title='Data')
116
117
118
        plot.add(HT(m), title='Reconstruction')
        plot.add(mask_to_nan(mask, HT(m-MOCK_SIGNAL)), title='Residuals')
        plot.output(nx=2, ny=2, xsize=10, ysize=10, title="getting_started_1")