getting_started_3.py 4.53 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-2018 Max-Planck-Society
#
# NIFTy is being developed at the Max-Planck-Institut fuer Astrophysik
# and financially supported by the Studienstiftung des deutschen Volkes.

Jakob Knollmueller's avatar
Jakob Knollmueller committed
19 20 21 22 23
import nifty5 as ift
import numpy as np


def get_random_LOS(n_los):
24 25
    starts = list(np.random.uniform(0, 1, (n_los, 2)).T)
    ends = list(np.random.uniform(0, 1, (n_los, 2)).T)
Jakob Knollmueller's avatar
Jakob Knollmueller committed
26 27
    return starts, ends

Martin Reinecke's avatar
cleanup  
Martin Reinecke committed
28

Jakob Knollmueller's avatar
Jakob Knollmueller committed
29
if __name__ == '__main__':
Philipp Arras's avatar
Philipp Arras committed
30
    # FIXME description of the tutorial
31
    np.random.seed(42)
32
    position_space = ift.RGSpace([128, 128])
Jakob Knollmueller's avatar
Jakob Knollmueller committed
33

Jakob Knollmueller's avatar
Jakob Knollmueller committed
34
    # Setting up an amplitude model
35
    A = ift.AmplitudeModel(position_space, 64, 3, 0.4, -4., 1, 1., 1.)
Martin Reinecke's avatar
cleanup  
Martin Reinecke committed
36
    dummy = ift.from_random('normal', A.domain)
Jakob Knollmueller's avatar
Jakob Knollmueller committed
37 38 39 40

    # Building the model for a correlated signal
    harmonic_space = position_space.get_default_codomain()
    ht = ift.HarmonicTransformOperator(harmonic_space, position_space)
Martin Reinecke's avatar
cleanup  
Martin Reinecke committed
41
    power_space = A.target[0]
Jakob Knollmueller's avatar
Jakob Knollmueller committed
42
    power_distributor = ift.PowerDistributor(harmonic_space, power_space)
Martin Reinecke's avatar
cleanup  
Martin Reinecke committed
43
    dummy = ift.Field.from_random('normal', harmonic_space)
Philipp Arras's avatar
Philipp Arras committed
44 45 46 47
    domain = ift.MultiDomain.union((A.domain,
                                    ift.MultiDomain.make({
                                        'xi': harmonic_space
                                    })))
Jakob Knollmueller's avatar
Jakob Knollmueller committed
48

49 50 51
    vol = harmonic_space.scalar_dvol
    vol = ift.ScalingOperator(vol ** (-0.5),harmonic_space)
    correlated_field = ht(vol(power_distributor(A))*ift.FieldAdapter(domain, "xi"))
52
    # alternatively to the block above one can do:
53
    #correlated_field = ift.CorrelatedField(position_space, A)
Jakob Knollmueller's avatar
Jakob Knollmueller committed
54 55

    # apply some nonlinearity
Martin Reinecke's avatar
Martin Reinecke committed
56
    signal = ift.positive_tanh(correlated_field)
Martin Reinecke's avatar
Martin Reinecke committed
57

Jakob Knollmueller's avatar
Jakob Knollmueller committed
58
    # Building the Line of Sight response
59
    LOS_starts, LOS_ends = get_random_LOS(100)
Philipp Arras's avatar
Philipp Arras committed
60
    R = ift.LOSResponse(position_space, starts=LOS_starts, ends=LOS_ends)
Jakob Knollmueller's avatar
Jakob Knollmueller committed
61
    # build signal response model and model likelihood
Martin Reinecke's avatar
Martin Reinecke committed
62
    signal_response = R(signal)
Jakob Knollmueller's avatar
Jakob Knollmueller committed
63 64
    # specify noise
    data_space = R.target
Jakob Knollmueller's avatar
Jakob Knollmueller committed
65
    noise = .001
66
    N = ift.ScalingOperator(noise, data_space)
Jakob Knollmueller's avatar
Jakob Knollmueller committed
67 68

    # generate mock data
Martin Reinecke's avatar
cleanup  
Martin Reinecke committed
69 70
    MOCK_POSITION = ift.from_random('normal', domain)
    data = signal_response(MOCK_POSITION) + N.draw_sample()
Jakob Knollmueller's avatar
Jakob Knollmueller committed
71 72

    # set up model likelihood
Philipp Arras's avatar
Philipp Arras committed
73
    likelihood = ift.GaussianEnergy(mean=data, covariance=N)(signal_response)
Jakob Knollmueller's avatar
Jakob Knollmueller committed
74 75

    # set up minimization and inversion schemes
Jakob Knollmueller's avatar
Jakob Knollmueller committed
76
    ic_sampling = ift.GradientNormController(iteration_limit=100)
Martin Reinecke's avatar
Martin Reinecke committed
77 78
    ic_newton = ift.GradInfNormController(
        name='Newton', tol=1e-7, iteration_limit=1000)
79
    minimizer = ift.NewtonCG(ic_newton)
Jakob Knollmueller's avatar
Jakob Knollmueller committed
80

Jakob Knollmueller's avatar
Jakob Knollmueller committed
81
    # build model Hamiltonian
Martin Reinecke's avatar
Martin Reinecke committed
82
    H = ift.Hamiltonian(likelihood, ic_sampling)
Jakob Knollmueller's avatar
Jakob Knollmueller committed
83

Martin Reinecke's avatar
cleanup  
Martin Reinecke committed
84
    INITIAL_POSITION = ift.from_random('normal', domain)
Jakob Knollmueller's avatar
Jakob Knollmueller committed
85
    position = INITIAL_POSITION
Jakob Knollmueller's avatar
Jakob Knollmueller committed
86

87
    plot = ift.Plot()
Martin Reinecke's avatar
merge  
Martin Reinecke committed
88 89
    plot.add(signal(MOCK_POSITION), title='Ground Truth')
    plot.add(R.adjoint_times(data), title='Data')
Philipp Arras's avatar
Philipp Arras committed
90
    plot.add([A(MOCK_POSITION.extract(A.domain))], title='Power Spectrum')
Martin Reinecke's avatar
merge  
Martin Reinecke committed
91
    plot.output(ny=1, nx=3, xsize=24, ysize=6, name="setup.png")
Jakob Knollmueller's avatar
Jakob Knollmueller committed
92

Jakob Knollmueller's avatar
Jakob Knollmueller committed
93
    # number of samples used to estimate the KL
94
    N_samples = 20
Martin Reinecke's avatar
Martin Reinecke committed
95
    for i in range(2):
Martin Reinecke's avatar
Martin Reinecke committed
96
        KL = ift.KL_Energy(position, H, N_samples)
Jakob Knollmueller's avatar
Jakob Knollmueller committed
97 98 99
        KL, convergence = minimizer(KL)
        position = KL.position

100
        plot = ift.Plot()
Martin Reinecke's avatar
merge  
Martin Reinecke committed
101
        plot.add(signal(KL.position), title="reconstruction")
Philipp Arras's avatar
Philipp Arras committed
102 103 104 105 106 107
        plot.add(
            [
                A(KL.position.extract(A.domain)),
                A(MOCK_POSITION.extract(A.domain))
            ],
            title="power")
Martin Reinecke's avatar
merge  
Martin Reinecke committed
108
        plot.output(ny=1, ysize=6, xsize=16, name="loop.png")
Jakob Knollmueller's avatar
Jakob Knollmueller committed
109

110
    plot = ift.Plot()
Martin Reinecke's avatar
Martin Reinecke committed
111
    sc = ift.StatCalculator()
112
    for sample in KL.samples:
Philipp Arras's avatar
Philipp Arras committed
113
        sc.add(signal(sample + KL.position))
Martin Reinecke's avatar
merge  
Martin Reinecke committed
114 115
    plot.add(sc.mean, title="Posterior Mean")
    plot.add(ift.sqrt(sc.var), title="Posterior Standard Deviation")
Martin Reinecke's avatar
Martin Reinecke committed
116

Philipp Arras's avatar
Philipp Arras committed
117
    powers = [A((s + KL.position).extract(A.domain)) for s in KL.samples]
Martin Reinecke's avatar
merge  
Martin Reinecke committed
118
    plot.add(
Philipp Arras's avatar
Philipp Arras committed
119 120
        [A(KL.position.extract(A.domain)),
         A(MOCK_POSITION.extract(A.domain))] + powers,
Philipp Arras's avatar
Philipp Arras committed
121
        title="Sampled Posterior Power Spectrum")
Martin Reinecke's avatar
merge  
Martin Reinecke committed
122
    plot.output(ny=1, nx=3, xsize=24, ysize=6, name="results.png")