getting_started_3.py 4.4 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
Martin Reinecke's avatar
cleanup  
Martin Reinecke committed
35 36
    A = ift.AmplitudeModel(position_space, 16, 1, 10, -4., 1, 0., 1.)
    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)
Martin Reinecke's avatar
cleanup  
Martin Reinecke committed
44 45
    domain = ift.MultiDomain.union(
        (A.domain, ift.MultiDomain.make({'xi': harmonic_space})))
Jakob Knollmueller's avatar
Jakob Knollmueller committed
46

Martin Reinecke's avatar
cleanup  
Martin Reinecke committed
47 48
    correlated_field = ht.chain(
        power_distributor.chain(A)*ift.FieldAdapter(domain, "xi"))
49
    # alternatively to the block above one can do:
Martin Reinecke's avatar
cleanup  
Martin Reinecke committed
50
    # correlated_field = ift.CorrelatedField(position_space, A)
Jakob Knollmueller's avatar
Jakob Knollmueller committed
51 52

    # apply some nonlinearity
Martin Reinecke's avatar
Martin Reinecke committed
53
    signal = correlated_field.positive_tanh()
Martin Reinecke's avatar
Martin Reinecke committed
54

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

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

    # set up model likelihood
Martin Reinecke's avatar
cleanup  
Martin Reinecke committed
71 72
    likelihood = ift.GaussianEnergy(
        mean=data, covariance=N).chain(signal_response)
Jakob Knollmueller's avatar
Jakob Knollmueller committed
73 74

    # set up minimization and inversion schemes
Jakob Knollmueller's avatar
Jakob Knollmueller committed
75 76
    ic_cg = ift.GradientNormController(iteration_limit=10)
    ic_sampling = ift.GradientNormController(iteration_limit=100)
Jakob Knollmueller's avatar
Jakob Knollmueller committed
77 78
    ic_newton = ift.GradientNormController(name='Newton', iteration_limit=100)
    minimizer = ift.RelaxedNewton(ic_newton)
Jakob Knollmueller's avatar
Jakob Knollmueller committed
79

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

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

Martin Reinecke's avatar
cleanup  
Martin Reinecke committed
86
    ift.plot(signal(MOCK_POSITION), title='ground truth')
Martin Reinecke's avatar
Martin Reinecke committed
87
    ift.plot(R.adjoint_times(data), title='data')
Martin Reinecke's avatar
cleanup  
Martin Reinecke committed
88
    ift.plot([A(MOCK_POSITION)], title='power')
Martin Reinecke's avatar
Martin Reinecke committed
89
    ift.plot_finish(nx=3, xsize=16, ysize=5, title="setup", name="setup.png")
Jakob Knollmueller's avatar
Jakob Knollmueller committed
90

Jakob Knollmueller's avatar
Jakob Knollmueller committed
91
    # number of samples used to estimate the KL
92
    N_samples = 20
Martin Reinecke's avatar
Martin Reinecke committed
93
    for i in range(2):
Martin Reinecke's avatar
cleanup  
Martin Reinecke committed
94 95
        metric = H(ift.Linearization.make_var(position)).metric
        samples = [metric.draw_sample(from_inverse=True)
96
                   for _ in range(N_samples)]
Jakob Knollmueller's avatar
Jakob Knollmueller committed
97

Martin Reinecke's avatar
Martin Reinecke committed
98
        KL = ift.SampledKullbachLeiblerDivergence(H, samples)
Martin Reinecke's avatar
cleanup  
Martin Reinecke committed
99
        KL = ift.EnergyAdapter(position, KL)
100
        KL = KL.make_invertible(ic_cg)
Jakob Knollmueller's avatar
Jakob Knollmueller committed
101 102 103
        KL, convergence = minimizer(KL)
        position = KL.position

Martin Reinecke's avatar
cleanup  
Martin Reinecke committed
104 105
        ift.plot(signal(position), title="reconstruction")
        ift.plot([A(position), A(MOCK_POSITION)], title="power")
Martin Reinecke's avatar
Martin Reinecke committed
106
        ift.plot_finish(nx=2, xsize=12, ysize=6, title="loop", name="loop.png")
Jakob Knollmueller's avatar
Jakob Knollmueller committed
107

Martin Reinecke's avatar
Martin Reinecke committed
108
    sc = ift.StatCalculator()
Jakob Knollmueller's avatar
Jakob Knollmueller committed
109
    for sample in samples:
Martin Reinecke's avatar
cleanup  
Martin Reinecke committed
110
        sc.add(signal(sample+position))
Martin Reinecke's avatar
Martin Reinecke committed
111 112
    ift.plot(sc.mean, title="mean")
    ift.plot(ift.sqrt(sc.var), title="std deviation")
Martin Reinecke's avatar
Martin Reinecke committed
113

Martin Reinecke's avatar
cleanup  
Martin Reinecke committed
114 115
    powers = [A(s+position) for s in samples]
    ift.plot([A(position), A(MOCK_POSITION)]+powers, title="power")
Martin Reinecke's avatar
Martin Reinecke committed
116 117
    ift.plot_finish(nx=3, xsize=16, ysize=5, title="results",
                    name="results.png")