wiener_filter_via_hamiltonian.py 2.99 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

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.k_lengths))
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
    ndiag = ift.Field(s_space, ss.var()/signal_to_noise).weight(1)
    N = ift.DiagonalOperator(s_space, ndiag)
Martin Reinecke's avatar
Martin Reinecke committed
67
    n = ift.Field.from_random(domain=s_space,
68
69
                          random_type='normal',
                          std=ss.std()/np.sqrt(signal_to_noise),
70
                          mean=0)
71

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

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

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

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

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

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

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