wiener_filter_via_hamiltonian.py 2.79 KB
Newer Older
Martin Reinecke's avatar
Martin Reinecke committed
1
import nifty2go as ift
Martin Reinecke's avatar
Martin Reinecke committed
2
import numpy as np
3

4
np.random.seed(42)
5

Martin Reinecke's avatar
Martin Reinecke committed
6

7
if __name__ == "__main__":
Jakob Knollmueller's avatar
Jakob Knollmueller committed
8
    # Set up position space
Martin Reinecke's avatar
Martin Reinecke committed
9
    s_space = ift.RGSpace([128, 128])
Martin Reinecke's avatar
Martin Reinecke committed
10
    # s_space = ift.HPSpace(32)
Jakob Knollmueller's avatar
Jakob Knollmueller committed
11

Martin Reinecke's avatar
Martin Reinecke committed
12
13
14
    # Define associated harmonic space and harmonic transformation
    h_space = s_space.get_default_codomain()
    fft = ift.FFTOperator(h_space, s_space)
Jakob Knollmueller's avatar
Jakob Knollmueller committed
15
16

    # Setting up power space
Martin Reinecke's avatar
Martin Reinecke committed
17
    p_space = ift.PowerSpace(h_space)
18

Martin Reinecke's avatar
Martin Reinecke committed
19
20
    # Choosing the prior correlation structure and defining
    # correlation operator
21
    p_spec = (lambda k: (42 / (k + 1) ** 3))
Jakob Knollmueller's avatar
Jakob Knollmueller committed
22

Martin Reinecke's avatar
Martin Reinecke committed
23
    S = ift.create_power_operator(h_space, power_spectrum=p_spec)
24

Jakob Knollmueller's avatar
Jakob Knollmueller committed
25
    # Drawing a sample sh from the prior distribution in harmonic space
Martin Reinecke's avatar
Martin Reinecke committed
26
    sp = ift.PS_field(p_space, p_spec)
Martin Reinecke's avatar
adjust    
Martin Reinecke committed
27
    sh = ift.power_synthesize(sp, real_signal=True)
Martin Reinecke's avatar
Martin Reinecke committed
28
    ss = fft.times(sh)
29

Jakob Knollmueller's avatar
Jakob Knollmueller committed
30
    # Choosing the measurement instrument
Martin Reinecke's avatar
Martin Reinecke committed
31
    # Instrument = ift.FFTSmoothingOperator(s_space, sigma=0.05)
Martin Reinecke's avatar
tweaks    
Martin Reinecke committed
32
33
    diag = np.ones(s_space.shape)
    diag[20:80, 20:80] = 0
Martin Reinecke's avatar
Martin Reinecke committed
34
    diag = ift.Field(s_space, ift.dobj.from_global_data(diag))
35
    Instrument = ift.DiagonalOperator(diag)
Jakob Knollmueller's avatar
Jakob Knollmueller committed
36

Martin Reinecke's avatar
Martin Reinecke committed
37
    # Adding a harmonic transformation to the instrument
Martin Reinecke's avatar
Martin Reinecke committed
38
    R = ift.ComposedOperator([fft, Instrument])
Jakob Knollmueller's avatar
Jakob Knollmueller committed
39
    signal_to_noise = 1.
40
    ndiag = ift.Field.full(s_space, ss.var()/signal_to_noise)
41
    N = ift.DiagonalOperator(ndiag.weight(1))
Martin Reinecke's avatar
Martin Reinecke committed
42
    n = ift.Field.from_random(domain=s_space,
Martin Reinecke's avatar
Martin Reinecke committed
43
44
45
                              random_type='normal',
                              std=ss.std()/np.sqrt(signal_to_noise),
                              mean=0)
46

Jakob Knollmueller's avatar
Jakob Knollmueller committed
47
    # Creating the mock data
48
    d = R(sh) + n
49
    j = R.adjoint_times(N.inverse_times(d))
50

Jakob Knollmueller's avatar
Jakob Knollmueller committed
51
52
    # Choosing the minimization strategy

Martin Reinecke's avatar
Martin Reinecke committed
53
    ctrl = ift.GradientNormController(verbose=True, tol_abs_gradnorm=0.1)
54
    inverter = ift.ConjugateGradient(controller=ctrl)
Martin Reinecke's avatar
Martin Reinecke committed
55
    controller = ift.GradientNormController(verbose=True, tol_abs_gradnorm=0.1)
56
    minimizer = ift.RelaxedNewton(controller=controller)
57
    m0 = ift.Field.zeros(h_space)
Jakob Knollmueller's avatar
Jakob Knollmueller committed
58
    # Initializing the Wiener Filter energy
Martin Reinecke's avatar
Martin Reinecke committed
59
    energy = ift.library.WienerFilterEnergy(position=m0, d=d, R=R, N=N, S=S,
Martin Reinecke's avatar
Martin Reinecke committed
60
                                            inverter=inverter)
61

Martin Reinecke's avatar
Martin Reinecke committed
62
63
64
    energy, convergence = minimizer(energy)
    m = energy.position
    D = energy.curvature
Martin Reinecke's avatar
Martin Reinecke committed
65
    ift.plotting.plot(ss, name="signal.png", colormap="Planck-like")
Martin Reinecke's avatar
Martin Reinecke committed
66
    ift.plotting.plot(fft(m), name="m.png", colormap="Planck-like")
67
68

    # sampling the uncertainty map
Martin Reinecke's avatar
Martin Reinecke committed
69
70
71
72
73
    sample_variance = ift.Field.zeros(s_space)
    sample_mean = ift.Field.zeros(s_space)

    n_samples = 50
    for i in range(n_samples):
Martin Reinecke's avatar
Martin Reinecke committed
74
        sample = fft(D.generate_posterior_sample() + m)
Martin Reinecke's avatar
Martin Reinecke committed
75
76
77
78
79
        sample_variance += sample**2
        sample_mean += sample
    sample_mean /= n_samples
    sample_variance /= n_samples
    variance = sample_variance - sample_mean**2
Martin Reinecke's avatar
Martin Reinecke committed
80
    ift.plotting.plot(variance, name="variance.png", colormap="Planck-like")