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

4
np.random.seed(42)
5

Martin Reinecke's avatar
Martin Reinecke committed
6

Martin Reinecke's avatar
Martin Reinecke committed
7
class AdjointFFTResponse(ift.LinearOperator):
8
9
10
11
12
13
14
15
16
17
18
19
    def __init__(self, FFT, R, default_spaces=None):
        super(AdjointFFTResponse, self).__init__(default_spaces)
        self._domain = FFT.target
        self._target = R.target
        self.R = R
        self.FFT = FFT

    def _times(self, x, spaces=None):
        return self.R(self.FFT.adjoint_times(x))

    def _adjoint_times(self, x, spaces=None):
        return self.FFT(self.R.adjoint_times(x))
Martin Reinecke's avatar
Martin Reinecke committed
20

21
22
23
24
25
26
27
28
29
30
31
32
    @property
    def domain(self):
        return self._domain

    @property
    def target(self):
        return self._target

    @property
    def unitary(self):
        return False

33

34
if __name__ == "__main__":
Jakob Knollmueller's avatar
Jakob Knollmueller committed
35
    # Set up position space
Martin Reinecke's avatar
Martin Reinecke committed
36
    s_space = ift.RGSpace([128, 128])
Jakob Knollmueller's avatar
Jakob Knollmueller committed
37
38
39
    # s_space = HPSpace(32)

    # Define harmonic transformation and associated harmonic space
Martin Reinecke's avatar
Martin Reinecke committed
40
    fft = ift.FFTOperator(s_space)
41
    h_space = fft.target[0]
Jakob Knollmueller's avatar
Jakob Knollmueller committed
42
43

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

Martin Reinecke's avatar
Martin Reinecke committed
46
47
    # Choosing the prior correlation structure and defining
    # correlation operator
48
    p_spec = (lambda k: (42 / (k + 1) ** 3))
Jakob Knollmueller's avatar
Jakob Knollmueller committed
49

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

Jakob Knollmueller's avatar
Jakob Knollmueller committed
52
    # Drawing a sample sh from the prior distribution in harmonic space
Martin Reinecke's avatar
Martin Reinecke committed
53
    sp = ift.Field(p_space, val=p_spec(p_space.kindex))
54
    sh = sp.power_synthesize(real_signal=True)
55
56
    ss = fft.adjoint_times(sh)

Jakob Knollmueller's avatar
Jakob Knollmueller committed
57
    # Choosing the measurement instrument
Jakob Knollmueller's avatar
Jakob Knollmueller committed
58
    # Instrument = SmoothingOperator(s_space, sigma=0.05)
Martin Reinecke's avatar
Martin Reinecke committed
59
    Instrument = ift.DiagonalOperator(s_space, diagonal=1.)
60
#    Instrument._diagonal.val[200:400, 200:400] = 0
Jakob Knollmueller's avatar
Jakob Knollmueller committed
61

Martin Reinecke's avatar
Martin Reinecke committed
62
    # Adding a harmonic transformation to the instrument
63
    R = AdjointFFTResponse(fft, Instrument)
Jakob Knollmueller's avatar
Jakob Knollmueller committed
64
    signal_to_noise = 1.
Martin Reinecke's avatar
Martin Reinecke committed
65
66
    N = ift.DiagonalOperator(s_space, diagonal=ss.var()/signal_to_noise, bare=True)
    n = ift.Field.from_random(domain=s_space,
67
68
                          random_type='normal',
                          std=ss.std()/np.sqrt(signal_to_noise),
69
                          mean=0)
70

Jakob Knollmueller's avatar
Jakob Knollmueller committed
71
    # Creating the mock data
72
    d = R(sh) + n
73
    j = R.adjoint_times(N.inverse_times(d))
74

Jakob Knollmueller's avatar
Jakob Knollmueller committed
75
76
    # Choosing the minimization strategy

Martin Reinecke's avatar
Martin Reinecke committed
77
78
    ctrl = ift.DefaultIterationController(verbose=True,tol_abs_gradnorm=0.1)
    inverter = ift.ConjugateGradient(controller=ctrl, preconditioner=S.times)
Jakob Knollmueller's avatar
Jakob Knollmueller committed
79
    # Setting starting position
Martin Reinecke's avatar
Martin Reinecke committed
80
    m0 = ift.Field(h_space, val=.0)
Jakob Knollmueller's avatar
Jakob Knollmueller committed
81
82

    # Initializing the Wiener Filter energy
Martin Reinecke's avatar
Martin Reinecke committed
83
84
    energy = ift.library.WienerFilterEnergy(position=m0, d=d, R=R, N=N, S=S,
                                inverter=inverter)
85
    D0 = energy.curvature
86

Jakob Knollmueller's avatar
Jakob Knollmueller committed
87
    # Solving the problem analytically
88
    m0 = D0.inverse_times(j)
89

Martin Reinecke's avatar
Martin Reinecke committed
90
91
    sample_variance = ift.Field(sh.domain, val=0.)
    sample_mean = ift.Field(sh.domain, val=0.)
92
93

    # sampling the uncertainty map
Martin Reinecke's avatar
Martin Reinecke committed
94
    n_samples = 50
95
    for i in range(n_samples):
Martin Reinecke's avatar
Martin Reinecke committed
96
        sample = fft(ift.sugar.generate_posterior_sample(0., D0))
97
98
        sample_variance += sample**2
        sample_mean += sample
99
    variance = (sample_variance - sample_mean**2)/n_samples