bernoulli_demo.py 3.03 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


if __name__ == '__main__':
Philipp Arras's avatar
Philipp Arras committed
24
    # FIXME ABOUT THIS CODE
Jakob Knollmueller's avatar
Jakob Knollmueller committed
25
26
27
28
29
30
31
32
33
34
35
    np.random.seed(41)

    # Set up the position space of the signal
    #
    # # One dimensional regular grid with uniform exposure
    # position_space = ift.RGSpace(1024)
    # exposure = np.ones(position_space.shape)

    # Two-dimensional regular grid with inhomogeneous exposure
    position_space = ift.RGSpace([512, 512])

Martin Reinecke's avatar
Martin Reinecke committed
36
    #  Sphere with uniform exposure
Jakob Knollmueller's avatar
Jakob Knollmueller committed
37
38
39
40
41
42
43
    # position_space = ift.HPSpace(128)
    # exposure = ift.Field.full(position_space, 1.)

    # Defining harmonic space and transform
    harmonic_space = position_space.get_default_codomain()
    HT = ift.HarmonicTransformOperator(harmonic_space, position_space)

Martin Reinecke's avatar
Martin Reinecke committed
44
    position = ift.from_random('normal', harmonic_space)
Jakob Knollmueller's avatar
Jakob Knollmueller committed
45
46
47
48
49

    # Define power spectrum and amplitudes
    def sqrtpspec(k):
        return 1. / (20. + k**2)

Martin Reinecke's avatar
tweaks    
Martin Reinecke committed
50
    A = ift.create_power_operator(harmonic_space, sqrtpspec)
Jakob Knollmueller's avatar
Jakob Knollmueller committed
51
52

    # Set up a sky model
Martin Reinecke's avatar
tweaks    
Martin Reinecke committed
53
    sky = ift.positive_tanh(HT(A))
Jakob Knollmueller's avatar
Jakob Knollmueller committed
54
55
56
57
58
59

    GR = ift.GeometryRemover(position_space)
    # Set up instrumental response
    R = GR

    # Generate mock data
Martin Reinecke's avatar
Martin Reinecke committed
60
    p = R(sky)
Martin Reinecke's avatar
Martin Reinecke committed
61
    mock_position = ift.from_random('normal', harmonic_space)
Martin Reinecke's avatar
Martin Reinecke committed
62
63
64
    tmp = p(mock_position).to_global_data().astype(np.float64)
    data = np.random.binomial(1, tmp)
    data = ift.Field.from_global_data(R.target, data)
Jakob Knollmueller's avatar
Jakob Knollmueller committed
65
66

    # Compute likelihood and Hamiltonian
Martin Reinecke's avatar
Martin Reinecke committed
67
    position = ift.from_random('normal', harmonic_space)
Jakob Knollmueller's avatar
Jakob Knollmueller committed
68
    likelihood = ift.BernoulliEnergy(p, data)
69
70
71
    ic_newton = ift.DeltaEnergyController(name='Newton', iteration_limit=100,
                                          tol_rel_deltaE=1e-8)
    minimizer = ift.NewtonCG(ic_newton)
Jakob Knollmueller's avatar
Jakob Knollmueller committed
72
73
74
75
    ic_sampling = ift.GradientNormController(iteration_limit=100)

    # Minimize the Hamiltonian
    H = ift.Hamiltonian(likelihood, ic_sampling)
Martin Reinecke's avatar
fixes    
Martin Reinecke committed
76
    H = ift.EnergyAdapter(position, H, want_metric=True)
Martin Reinecke's avatar
tweaks    
Martin Reinecke committed
77
    # minimizer = ift.L_BFGS(ic_newton)
Jakob Knollmueller's avatar
Jakob Knollmueller committed
78
    H, convergence = minimizer(H)
Martin Reinecke's avatar
PEP8    
Martin Reinecke committed
79

Martin Reinecke's avatar
Martin Reinecke committed
80
    reconstruction = sky(H.position)
Jakob Knollmueller's avatar
Jakob Knollmueller committed
81

82
83
84
85
86
87
    plot = ift.Plot()
    plot.add(reconstruction, title='reconstruction')
    plot.add(GR.adjoint_times(data), title='data')
    plot.add(sky(mock_position), title='truth')
    plot.output(nx=3, xsize=16, ysize=5, title="results",
                name="bernoulli.png")