critical_filtering.py 4.52 KB
Newer Older
Philipp Arras's avatar
Philipp Arras committed
1
import numpy as np
Martin Reinecke's avatar
updates    
Martin Reinecke committed
2
import nifty2go as ift
3

4
np.random.seed(42)
5
#np.seterr(all="raise",under="ignore")
Jakob Knollmueller's avatar
Jakob Knollmueller committed
6

Philipp Arras's avatar
Philipp Arras committed
7
def plot_parameters(m, t, p, p_d):
Martin Reinecke's avatar
Martin Reinecke committed
8
    x = np.log(t.domain[0].k_lengths)
Jakob Knollmueller's avatar
Jakob Knollmueller committed
9
    m = fft.adjoint_times(m)
Martin Reinecke's avatar
updates    
Martin Reinecke committed
10
11
12
    t = t.val.real
    p = p.val.real
    p_d = p_d.val.real
Martin Reinecke's avatar
Martin Reinecke committed
13
14
15
    ift.plotting.plot(m.real, name='map.pdf')
    #pl.plot([go.Scatter(x=x, y=t), go.Scatter(x=x, y=p),
    #         go.Scatter(x=x, y=p_d)], filename="t.html", auto_open=False)
Jakob Knollmueller's avatar
Jakob Knollmueller committed
16

Jakob Knollmueller's avatar
Jakob Knollmueller committed
17

Martin Reinecke's avatar
updates    
Martin Reinecke committed
18
class AdjointFFTResponse(ift.LinearOperator):
19
20
    def __init__(self, FFT, R):
        super(AdjointFFTResponse, self).__init__()
21
        self._domain = FFT.target
Jakob Knollmueller's avatar
Jakob Knollmueller committed
22
23
        self._target = R.target
        self.R = R
24
25
        self.FFT = FFT

26
    def _times(self, x):
Jakob Knollmueller's avatar
Jakob Knollmueller committed
27
        return self.R(self.FFT.adjoint_times(x))
28

29
    def _adjoint_times(self, x):
Jakob Knollmueller's avatar
Jakob Knollmueller committed
30
        return self.FFT(self.R.adjoint_times(x))
Philipp Arras's avatar
Philipp Arras committed
31

32
33
34
35
36
37
38
39
40
41
42
43
    @property
    def domain(self):
        return self._domain

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

    @property
    def unitary(self):
        return False

Philipp Arras's avatar
Philipp Arras committed
44

45
46
if __name__ == "__main__":
    # Set up position space
Martin Reinecke's avatar
updates    
Martin Reinecke committed
47
48
    s_space = ift.RGSpace([128, 128])
    # s_space = ift.HPSpace(32)
49
50

    # Define harmonic transformation and associated harmonic space
Martin Reinecke's avatar
updates    
Martin Reinecke committed
51
    fft = ift.FFTOperator(s_space)
52
53
    h_space = fft.target[0]

Philipp Arras's avatar
Philipp Arras committed
54
    # Set up power space
Martin Reinecke's avatar
Martin Reinecke committed
55
    p_space = ift.PowerSpace(h_space,binbounds=ift.PowerSpace.useful_binbounds(h_space,logarithmic=True))
56

Philipp Arras's avatar
Philipp Arras committed
57
    # Choose the prior correlation structure and defining correlation operator
58
    p_spec = (lambda k: (.5 / (k + 1) ** 3))
Martin Reinecke's avatar
updates    
Martin Reinecke committed
59
    S = ift.create_power_operator(h_space, power_spectrum=p_spec)
60

Philipp Arras's avatar
Philipp Arras committed
61
    # Draw a sample sh from the prior distribution in harmonic space
Martin Reinecke's avatar
Martin Reinecke committed
62
    sp = ift.Field(p_space,  val=p_spec(p_space.k_lengths))
63
64
    sh = sp.power_synthesize(real_signal=True)

Philipp Arras's avatar
Philipp Arras committed
65
    # Choose the measurement instrument
Martin Reinecke's avatar
Martin Reinecke committed
66
    # Instrument = SmoothingOperator(s_space, sigma=0.01)
Martin Reinecke's avatar
Martin Reinecke committed
67
    Instrument = ift.DiagonalOperator(ift.Field(s_space, 1.))
Jakob Knollmueller's avatar
Jakob Knollmueller committed
68
    # Instrument._diagonal.val[200:400, 200:400] = 0
Philipp Arras's avatar
Philipp Arras committed
69
    # Instrument._diagonal.val[64:512-64, 64:512-64] = 0
70

Philipp Arras's avatar
Philipp Arras committed
71
    # Add a harmonic transformation to the instrument
Jakob Knollmueller's avatar
Jakob Knollmueller committed
72
73
    R = AdjointFFTResponse(fft, Instrument)

74
    noise = 1.
75
    N = ift.DiagonalOperator(ift.Field.full(s_space,noise))
Martin Reinecke's avatar
updates    
Martin Reinecke committed
76
    n = ift.Field.from_random(domain=s_space,
77
                          random_type='normal',
Martin Reinecke's avatar
Martin Reinecke committed
78
                          std=np.sqrt(noise),
79
80
                          mean=0)

Philipp Arras's avatar
Philipp Arras committed
81
    # Create mock data
82
    d = R(sh) + n
Jakob Knollmueller's avatar
Jakob Knollmueller committed
83

84
85
    # The information source
    j = R.adjoint_times(N.inverse_times(d))
Martin Reinecke's avatar
updates    
Martin Reinecke committed
86
87
88
    realized_power = ift.log(sh.power_analyze(binbounds=p_space.binbounds))
    data_power = ift.log(fft(d).power_analyze(binbounds=p_space.binbounds))
    d_data = d.val.real
Martin Reinecke's avatar
Martin Reinecke committed
89
    ift.plotting.plot(d.real, name="data.pdf")
90

91
    IC1 = ift.GradientNormController(verbose=True,iteration_limit=100,tol_abs_gradnorm=0.1)
Martin Reinecke's avatar
updates    
Martin Reinecke committed
92
    minimizer1 = ift.RelaxedNewton(IC1)
93
    IC2 = ift.GradientNormController(verbose=True,iteration_limit=100,tol_abs_gradnorm=0.1)
Martin Reinecke's avatar
updates    
Martin Reinecke committed
94
    minimizer2 = ift.VL_BFGS(IC2, max_history_length=20)
95
    IC3 = ift.GradientNormController(verbose=True,iteration_limit=100,tol_abs_gradnorm=0.1)
Martin Reinecke's avatar
updates    
Martin Reinecke committed
96
    minimizer3 = ift.SteepestDescent(IC3)
97

Philipp Arras's avatar
Philipp Arras committed
98
    # Set starting position
99
    flat_power = ift.Field.full(p_space, 1e-8)
100
101
    m0 = flat_power.power_synthesize(real_signal=True)

Martin Reinecke's avatar
updates    
Martin Reinecke committed
102
103
    def ps0(k):
        return (1./(1.+k)**2)
Martin Reinecke's avatar
Martin Reinecke committed
104
    t0 = ift.Field(p_space, val=np.log(1./(1+p_space.k_lengths)**2))
105

106
    for i in range(500):
Martin Reinecke's avatar
updates    
Martin Reinecke committed
107
        S0 = ift.create_power_operator(h_space, power_spectrum=ps0)
108

Philipp Arras's avatar
Philipp Arras committed
109
        # Initialize non-linear Wiener Filter energy
110
        ICI = ift.GradientNormController(verbose=False,iteration_limit=500,tol_abs_gradnorm=0.1)
111
112
        map_inverter = ift.ConjugateGradient(controller=ICI)
        map_energy = ift.library.WienerFilterEnergy(position=m0, d=d, R=R, N=N, S=S0, inverter=map_inverter)
Philipp Arras's avatar
Philipp Arras committed
113
        # Solve the Wiener Filter analytically
114
        D0 = map_energy.curvature
115
        m0 = D0.inverse_times(j)
Philipp Arras's avatar
Philipp Arras committed
116
        # Initialize power energy with updated parameters
117
        ICI2 = ift.GradientNormController(name="powI",verbose=True,iteration_limit=200,tol_abs_gradnorm=1e-5)
118
        power_inverter = ift.ConjugateGradient(controller=ICI2)
Martin Reinecke's avatar
updates    
Martin Reinecke committed
119
        power_energy = ift.library.CriticalPowerEnergy(position=t0, m=m0, D=D0,
120
                                           smoothness_prior=10., samples=3, inverter=power_inverter)
121

Martin Reinecke's avatar
Martin Reinecke committed
122
        (power_energy, convergence) = minimizer1(power_energy)
123

Philipp Arras's avatar
Philipp Arras committed
124
        # Set new power spectrum
Martin Reinecke's avatar
Martin Reinecke committed
125
        t0 = power_energy.position.real
126

Philipp Arras's avatar
Philipp Arras committed
127
128
129
        # Plot current estimate
        print(i)
        if i % 5 == 0:
Martin Reinecke's avatar
updates    
Martin Reinecke committed
130
            plot_parameters(m0, t0, ift.log(sp), data_power)