critical_filtering.py 5.07 KB
Newer Older
1
2

from nifty import *
3
4
from nifty.library.wiener_filter import WienerFilterEnergy
from nifty.library.critical_filter import CriticalPowerEnergy
5
6
7
8
9
10
11
import plotly.offline as pl
import plotly.graph_objs as go

from mpi4py import MPI
comm = MPI.COMM_WORLD
rank = comm.rank

12
np.random.seed(42)
13

Jakob Knollmueller's avatar
Jakob Knollmueller committed
14

15
def plot_parameters(m,t,p, p_d):
Jakob Knollmueller's avatar
Jakob Knollmueller committed
16
17

    x = log(t.domain[0].kindex)
Jakob Knollmueller's avatar
Jakob Knollmueller committed
18
    m = fft.adjoint_times(m)
19
20
21
22
23
24
    m = m.val.get_full_data().real
    t = t.val.get_full_data().real
    p = p.val.get_full_data().real
    p_d = p_d.val.get_full_data().real
    pl.plot([go.Heatmap(z=m)], filename='map.html')
    pl.plot([go.Scatter(x=x,y=t), go.Scatter(x=x ,y=p), go.Scatter(x=x, y=p_d)], filename="t.html")
Jakob Knollmueller's avatar
Jakob Knollmueller committed
25

Jakob Knollmueller's avatar
Jakob Knollmueller committed
26
27
28
29

class AdjointFFTResponse(LinearOperator):
    def __init__(self, FFT, R, default_spaces=None):
        super(AdjointFFTResponse, self).__init__(default_spaces)
30
        self._domain = FFT.target
Jakob Knollmueller's avatar
Jakob Knollmueller committed
31
32
        self._target = R.target
        self.R = R
33
34
35
        self.FFT = FFT

    def _times(self, x, spaces=None):
Jakob Knollmueller's avatar
Jakob Knollmueller committed
36
        return self.R(self.FFT.adjoint_times(x))
37
38

    def _adjoint_times(self, x, spaces=None):
Jakob Knollmueller's avatar
Jakob Knollmueller committed
39
        return self.FFT(self.R.adjoint_times(x))
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
    @property
    def domain(self):
        return self._domain

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

    @property
    def unitary(self):
        return False

if __name__ == "__main__":

    distribution_strategy = 'not'

    # Set up position space
    s_space = RGSpace([128,128])
    # s_space = HPSpace(32)

    # Define harmonic transformation and associated harmonic space
    fft = FFTOperator(s_space)
    h_space = fft.target[0]

    # Setting up power space
65
    p_space = PowerSpace(h_space, logarithmic=True,
66
                         distribution_strategy=distribution_strategy)
67
68

    # Choosing the prior correlation structure and defining correlation operator
69
    p_spec = (lambda k: (.5 / (k + 1) ** 3))
Jakob Knollmueller's avatar
Jakob Knollmueller committed
70
    S = create_power_operator(h_space, power_spectrum=p_spec,
71
72
73
                              distribution_strategy=distribution_strategy)

    # Drawing a sample sh from the prior distribution in harmonic space
Jakob Knollmueller's avatar
Jakob Knollmueller committed
74
    sp = Field(p_space,  val=p_spec,
75
76
77
78
79
               distribution_strategy=distribution_strategy)
    sh = sp.power_synthesize(real_signal=True)


    # Choosing the measurement instrument
Jakob Knollmueller's avatar
Jakob Knollmueller committed
80
81
    # Instrument = SmoothingOperator(s_space, sigma=0.01)
    Instrument = DiagonalOperator(s_space, diagonal=1.)
Jakob Knollmueller's avatar
Jakob Knollmueller committed
82
    # Instrument._diagonal.val[200:400, 200:400] = 0
Jakob Knollmueller's avatar
Jakob Knollmueller committed
83
    #Instrument._diagonal.val[64:512-64, 64:512-64] = 0
84
85
86


    #Adding a harmonic transformation to the instrument
Jakob Knollmueller's avatar
Jakob Knollmueller committed
87
88
    R = AdjointFFTResponse(fft, Instrument)

89
    noise = 1.
90
91
92
93
94
95
96
97
    N = DiagonalOperator(s_space, diagonal=noise, bare=True)
    n = Field.from_random(domain=s_space,
                          random_type='normal',
                          std=sqrt(noise),
                          mean=0)

    # Creating the mock data
    d = R(sh) + n
Jakob Knollmueller's avatar
Jakob Knollmueller committed
98

99
100
    # The information source
    j = R.adjoint_times(N.inverse_times(d))
Jakob Knollmueller's avatar
Jakob Knollmueller committed
101
    realized_power = log(sh.power_analyze(logarithmic=p_space.config["logarithmic"],
Jakob Knollmueller's avatar
Jakob Knollmueller committed
102
                                          nbin=p_space.config["nbin"]))
103
    data_power = log(fft(d).power_analyze(logarithmic=p_space.config["logarithmic"],
Jakob Knollmueller's avatar
Jakob Knollmueller committed
104
                                          nbin=p_space.config["nbin"]))
105
106
107
108
    d_data = d.val.get_full_data().real
    if rank == 0:
        pl.plot([go.Heatmap(z=d_data)], filename='data.html')

Jakob Knollmueller's avatar
Jakob Knollmueller committed
109
    #  minimization strategy
110
111
112
113
114
115

    def convergence_measure(a_energy, iteration): # returns current energy
        x = a_energy.value
        print (x, iteration)


116
117
    minimizer1 = RelaxedNewton(convergence_tolerance=10e-2,
                              convergence_level=2,
118
                              iteration_limit=3,
119
                              callback=convergence_measure)
120

Jakob Knollmueller's avatar
Jakob Knollmueller committed
121
    minimizer2 = VL_BFGS(convergence_tolerance=0,
Jakob Knollmueller's avatar
Jakob Knollmueller committed
122
                       iteration_limit=7,
Jakob Knollmueller's avatar
Jakob Knollmueller committed
123
                       callback=convergence_measure,
Jakob Knollmueller's avatar
Jakob Knollmueller committed
124
                       max_history_length=3)
125

126
127
128
    inverter = ConjugateGradient(convergence_level=1,
                                 convergence_tolerance=10e-4,
                                 preconditioner=None)
129
130
131
132
    # Setting starting position
    flat_power = Field(p_space,val=10e-8)
    m0 = flat_power.power_synthesize(real_signal=True)

133
134
    t0 = Field(p_space, val=log(1./(1+p_space.kindex)**2))

Jakob Knollmueller's avatar
Jakob Knollmueller committed
135

136

137
    for i in range(500):
Jakob Knollmueller's avatar
Jakob Knollmueller committed
138
        S0 = create_power_operator(h_space, power_spectrum=exp(t0),
139
140
141
                              distribution_strategy=distribution_strategy)

        # Initializing the  nonlinear Wiener Filter energy
142
143
        map_energy = WienerFilterEnergy(position=m0, d=d, R=R, N=N, S=S0, inverter=inverter)
        # Solving the Wiener Filter analytically
144
        D0 = map_energy.curvature
145
        m0 = D0.inverse_times(j)
146
        # Initializing the power energy with updated parameters
147
        power_energy = CriticalPowerEnergy(position=t0, m=m0, D=D0, sigma=10., samples=3, inverter=inverter)
148
149
150

        (power_energy, convergence) = minimizer1(power_energy)

151

152
        # Setting new power spectrum
153
        t0.val  = power_energy.position.val.real
Jakob Knollmueller's avatar
Jakob Knollmueller committed
154

Jakob Knollmueller's avatar
Jakob Knollmueller committed
155
        # Plotting current estimate
156
        plot_parameters(m0,t0,log(sp), data_power)
157

Jakob Knollmueller's avatar
Jakob Knollmueller committed
158