getting_started_3.py 5.39 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 17 18
# NIFTy is being developed at the Max-Planck-Institut fuer Astrophysik.

############################################################
Philipp Arras's avatar
Philipp Arras committed
19
# Non-linear tomography
Torsten Ensslin's avatar
Torsten Ensslin committed
20 21 22 23 24 25
#
# The signal is a sigmoid-normal distributed field.
# The data is the field integrated along lines of sight that are
# randomly (set mode=0) or radially (mode=1) distributed
#
# Demo takes a while to compute
26
#############################################################
27

Jakob Knollmueller's avatar
Jakob Knollmueller committed
28 29
import numpy as np

Philipp Arras's avatar
Philipp Arras committed
30 31
import nifty5 as ift

Jakob Knollmueller's avatar
Jakob Knollmueller committed
32

Philipp Arras's avatar
Philipp Arras committed
33
def random_los(n_los):
34
    starts = list(np.random.uniform(0, 1, (n_los, 2)).T)
Torsten Ensslin's avatar
Torsten Ensslin committed
35
    ends = list(np.random.uniform(0, 1, (n_los, 2)).T)
Philipp Arras's avatar
Philipp Arras committed
36 37 38 39 40
    return starts, ends


def radial_los(n_los):
    starts = list(np.random.uniform(0, 1, (n_los, 2)).T)
Torsten Ensslin's avatar
Torsten Ensslin committed
41
    ends = list(0.5 + 0*np.random.uniform(0, 1, (n_los, 2)).T)
Jakob Knollmueller's avatar
Jakob Knollmueller committed
42 43
    return starts, ends

Martin Reinecke's avatar
cleanup  
Martin Reinecke committed
44

Jakob Knollmueller's avatar
Jakob Knollmueller committed
45
if __name__ == '__main__':
Torsten Ensslin's avatar
Torsten Ensslin committed
46
    np.random.seed(420)  # picked for a nice field realization
Philipp Arras's avatar
Philipp Arras committed
47

Torsten Ensslin's avatar
Torsten Ensslin committed
48 49 50
    # Choose between random line-of-sight response (mode=0) and radial lines
    # of sight (mode=1)
    mode = 0
Philipp Arras's avatar
Philipp Arras committed
51

52
    position_space = ift.RGSpace([128, 128])
53 54 55
    harmonic_space = position_space.get_default_codomain()
    ht = ift.HarmonicTransformOperator(harmonic_space, position_space)
    power_space = ift.PowerSpace(harmonic_space)
Jakob Knollmueller's avatar
Jakob Knollmueller committed
56

Philipp Arras's avatar
Philipp Arras committed
57
    # Set up an amplitude operator for the field
58 59 60 61 62 63 64 65 66 67 68
    dct = {
        'target': power_space,
        'n_pix': 64,  # 64 spectral bins

        # Spectral smoothness (affects Gaussian process part)
        'a': 3,  # relatively high variance of spectral curbvature
        'k0': .4,  # quefrency mode below which cepstrum flattens

        # Power-law part of spectrum:
        'sm': -5,  # preferred power-law slope
        'sv': .5,  # low variance of power-law slope
Torsten Ensslin's avatar
Torsten Ensslin committed
69 70
        'im':  0,  # y-intercept mean, in-/decrease for more/less contrast
        'iv': .3   # y-intercept variance
71
    }
72
    A = ift.SLAmplitude(**dct)
Philipp Arras's avatar
Philipp Arras committed
73

Philipp Arras's avatar
Philipp Arras committed
74
    # Build the operator for a correlated signal
Jakob Knollmueller's avatar
Jakob Knollmueller committed
75
    power_distributor = ift.PowerDistributor(harmonic_space, power_space)
76 77 78
    vol = harmonic_space.scalar_dvol**-0.5
    xi = ift.ducktape(harmonic_space, None, 'xi')
    correlated_field = ht(vol*power_distributor(A)*xi)
Philipp Arras's avatar
Philipp Arras committed
79 80
    # Alternatively, one can use:
    # correlated_field = ift.CorrelatedField(position_space, A)
Jakob Knollmueller's avatar
Jakob Knollmueller committed
81

Philipp Arras's avatar
Philipp Arras committed
82
    # Apply a nonlinearity
Jakob Knollmueller's avatar
Jakob Knollmueller committed
83
    signal = ift.sigmoid(correlated_field)
Martin Reinecke's avatar
Martin Reinecke committed
84

Philipp Arras's avatar
Philipp Arras committed
85
    # Build the line-of-sight response and define signal response
Torsten Ensslin's avatar
Torsten Ensslin committed
86
    LOS_starts, LOS_ends = random_los(100) if mode == 0 else radial_los(100)
Philipp Arras's avatar
Philipp Arras committed
87
    R = ift.LOSResponse(position_space, starts=LOS_starts, ends=LOS_ends)
Martin Reinecke's avatar
Martin Reinecke committed
88
    signal_response = R(signal)
Philipp Arras's avatar
Philipp Arras committed
89 90

    # Specify noise
Jakob Knollmueller's avatar
Jakob Knollmueller committed
91
    data_space = R.target
Jakob Knollmueller's avatar
Jakob Knollmueller committed
92
    noise = .001
93
    N = ift.ScalingOperator(noise, data_space)
Jakob Knollmueller's avatar
Jakob Knollmueller committed
94

Philipp Arras's avatar
Philipp Arras committed
95 96 97
    # Generate mock signal and data
    mock_position = ift.from_random('normal', signal_response.domain)
    data = signal_response(mock_position) + N.draw_sample()
Jakob Knollmueller's avatar
Jakob Knollmueller committed
98

Philipp Arras's avatar
Philipp Arras committed
99
    # Minimization parameters
Jakob Knollmueller's avatar
Jakob Knollmueller committed
100
    ic_sampling = ift.GradientNormController(iteration_limit=100)
Martin Reinecke's avatar
Martin Reinecke committed
101
    ic_newton = ift.GradInfNormController(
102
        name='Newton', tol=1e-7, iteration_limit=35)
103
    minimizer = ift.NewtonCG(ic_newton)
Jakob Knollmueller's avatar
Jakob Knollmueller committed
104

Philipp Arras's avatar
Philipp Arras committed
105
    # Set up likelihood and information Hamiltonian
Philipp Arras's avatar
Philipp Arras committed
106
    likelihood = ift.GaussianEnergy(mean=data, covariance=N)(signal_response)
107
    H = ift.StandardHamiltonian(likelihood, ic_sampling)
Jakob Knollmueller's avatar
Jakob Knollmueller committed
108

109 110
    initial_mean = ift.MultiField.full(H.domain, 0.)
    mean = initial_mean
Jakob Knollmueller's avatar
Jakob Knollmueller committed
111

112
    plot = ift.Plot()
Philipp Arras's avatar
Philipp Arras committed
113
    plot.add(signal(mock_position), title='Ground Truth')
Martin Reinecke's avatar
merge  
Martin Reinecke committed
114
    plot.add(R.adjoint_times(data), title='Data')
Philipp Arras's avatar
Philipp Arras committed
115
    plot.add([A.force(mock_position)], title='Power Spectrum')
Martin Reinecke's avatar
merge  
Martin Reinecke committed
116
    plot.output(ny=1, nx=3, xsize=24, ysize=6, name="setup.png")
Jakob Knollmueller's avatar
Jakob Knollmueller committed
117

Jakob Knollmueller's avatar
Jakob Knollmueller committed
118
    # number of samples used to estimate the KL
119
    N_samples = 20
Philipp Arras's avatar
Philipp Arras committed
120 121

    # Draw new samples to approximate the KL five times
122
    for i in range(5):
Philipp Arras's avatar
Philipp Arras committed
123
        # Draw new samples and minimize KL
124
        KL = ift.MetricGaussianKL(mean, H, N_samples)
Jakob Knollmueller's avatar
Jakob Knollmueller committed
125
        KL, convergence = minimizer(KL)
126
        mean = KL.position
Philipp Arras's avatar
Philipp Arras committed
127 128

        # Plot current reconstruction
129
        plot = ift.Plot()
Martin Reinecke's avatar
merge  
Martin Reinecke committed
130
        plot.add(signal(KL.position), title="reconstruction")
Philipp Arras's avatar
Philipp Arras committed
131
        plot.add([A.force(KL.position), A.force(mock_position)], title="power")
Lukas Platz's avatar
Lukas Platz committed
132
        plot.output(ny=1, ysize=6, xsize=16, name="loop-{:02}.png".format(i))
Jakob Knollmueller's avatar
Jakob Knollmueller committed
133

Philipp Arras's avatar
Philipp Arras committed
134
    # Draw posterior samples
135
    KL = ift.MetricGaussianKL(mean, H, N_samples)
Martin Reinecke's avatar
Martin Reinecke committed
136
    sc = ift.StatCalculator()
137
    for sample in KL.samples:
Philipp Arras's avatar
Philipp Arras committed
138
        sc.add(signal(sample + KL.position))
Philipp Arras's avatar
Philipp Arras committed
139 140 141

    # Plotting
    plot = ift.Plot()
Martin Reinecke's avatar
merge  
Martin Reinecke committed
142 143
    plot.add(sc.mean, title="Posterior Mean")
    plot.add(ift.sqrt(sc.var), title="Posterior Standard Deviation")
Martin Reinecke's avatar
Martin Reinecke committed
144

Philipp Arras's avatar
Philipp Arras committed
145
    powers = [A.force(s + KL.position) for s in KL.samples]
Martin Reinecke's avatar
merge  
Martin Reinecke committed
146
    plot.add(
Philipp Arras's avatar
Philipp Arras committed
147 148
        powers + [A.force(KL.position),
                  A.force(mock_position)],
Lukas Platz's avatar
Lukas Platz committed
149 150
        title="Sampled Posterior Power Spectrum",
        linewidth=[1.]*len(powers) + [3., 3.])
Martin Reinecke's avatar
merge  
Martin Reinecke committed
151
    plot.output(ny=1, nx=3, xsize=24, ysize=6, name="results.png")