critical_filtering.py 5.83 KB
Newer Older
1 2 3 4 5 6 7 8 9 10

from nifty import *

import plotly.offline as pl
import plotly.graph_objs as go

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

11
np.random.seed(42)
12

Jakob Knollmueller's avatar
Jakob Knollmueller committed
13 14

def plot_parameters(m,t,t_true, t_real, t_d):
Jakob Knollmueller's avatar
Jakob Knollmueller committed
15 16

    x = log(t.domain[0].kindex)
Jakob Knollmueller's avatar
Jakob Knollmueller committed
17 18 19 20 21 22 23
    m = fft.adjoint_times(m)
    m_data = m.val.get_full_data().real
    t_data = t.val.get_full_data().real
    t_d_data = t_d.val.get_full_data().real
    t_true_data = t_true.val.get_full_data().real
    t_real_data = t_real.val.get_full_data().real
    pl.plot([go.Heatmap(z=m_data)], filename='map.html')
Jakob Knollmueller's avatar
Jakob Knollmueller committed
24 25 26
    pl.plot([go.Scatter(x=x,y=t_data), go.Scatter(x=x ,y=t_true_data),
             go.Scatter(x=x, y=t_real_data), go.Scatter(x=x, y=t_d_data)], filename="t.html")

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

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

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

    def _adjoint_times(self, x, spaces=None):
Jakob Knollmueller's avatar
Jakob Knollmueller committed
40
        return self.FFT(self.R.adjoint_times(x))
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
    @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
Jakob Knollmueller's avatar
Jakob Knollmueller committed
66
    p_space = PowerSpace(h_space, logarithmic=False,
Jakob Knollmueller's avatar
Jakob Knollmueller committed
67
                         distribution_strategy=distribution_strategy)#, nbin=5)
68 69

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

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


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


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

Jakob Knollmueller's avatar
Jakob Knollmueller committed
90
    noise = .1
91 92 93 94 95 96 97 98
    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
99 100

    realized_power = log(sh.power_analyze(logarithmic=p_space.config["logarithmic"],
Jakob Knollmueller's avatar
Jakob Knollmueller committed
101
                                          nbin=p_space.config["nbin"]))
102
    data_power = log(fft(d).power_analyze(logarithmic=p_space.config["logarithmic"],
Jakob Knollmueller's avatar
Jakob Knollmueller committed
103
                                          nbin=p_space.config["nbin"]))
104 105 106 107
    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
108
    #  minimization strategy
109 110 111 112 113 114 115 116

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


    minimizer1 = RelaxedNewton(convergence_tolerance=0,
                              convergence_level=1,
117
                              iteration_limit=3,
118
                              callback=convergence_measure)
Jakob Knollmueller's avatar
Jakob Knollmueller committed
119
    minimizer2 = VL_BFGS(convergence_tolerance=0,
Jakob Knollmueller's avatar
Jakob Knollmueller committed
120
                       iteration_limit=7,
Jakob Knollmueller's avatar
Jakob Knollmueller committed
121
                       callback=convergence_measure,
Jakob Knollmueller's avatar
Jakob Knollmueller committed
122
                       max_history_length=3)
123 124 125 126 127

    # Setting starting position
    flat_power = Field(p_space,val=10e-8)
    m0 = flat_power.power_synthesize(real_signal=True)

128 129
    # t0 = Field(p_space, val=log(1./(1+p_space.kindex)**2))
    t0 = data_power- 1.
Jakob Knollmueller's avatar
Jakob Knollmueller committed
130

131

132
    for i in range(500):
Jakob Knollmueller's avatar
Jakob Knollmueller committed
133
        S0 = create_power_operator(h_space, power_spectrum=exp(t0),
134 135 136
                              distribution_strategy=distribution_strategy)

        # Initializing the  nonlinear Wiener Filter energy
Jakob Knollmueller's avatar
Jakob Knollmueller committed
137
        map_energy = WienerFilterEnergy(position=m0, d=d, R=R, N=N, S=S0)
138
        # Minimization with chosen minimizer
Jakob Knollmueller's avatar
Jakob Knollmueller committed
139 140
        map_energy = map_energy.analytic_solution()

141 142 143 144
        # Updating parameters for correlation structure reconstruction
        m0 = map_energy.position
        D0 = map_energy.curvature
        # Initializing the power energy with updated parameters
Jakob Knollmueller's avatar
Jakob Knollmueller committed
145
        power_energy = CriticalPowerEnergy(position=t0, m=m0, D=D0, sigma=100., samples=5)
146 147 148

        (power_energy, convergence) = minimizer1(power_energy)

149
        # Setting new power spectrum
150
        t0.val  = power_energy.position.val.real
Jakob Knollmueller's avatar
Jakob Knollmueller committed
151

Jakob Knollmueller's avatar
Jakob Knollmueller committed
152
        # Plotting current estimate
Jakob Knollmueller's avatar
Jakob Knollmueller committed
153
        plot_parameters(m0,t0,log(sp),realized_power, data_power)
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182

    # Transforming fields to position space for plotting

    ss = fft.adjoint_times(sh)
    m = fft.adjoint_times(map_energy.position)


    # Plotting

    d_data = d.val.get_full_data().real
    if rank == 0:
        pl.plot([go.Heatmap(z=d_data)], filename='data.html')

    tt_data = power_energy.position.val.get_full_data().real
    t_data = log(sp**2).val.get_full_data().real
    if rank == 0:
        pl.plot([go.Scatter(y=t_data),go.Scatter(y=tt_data)], filename="t.html")
    ss_data = ss.val.get_full_data().real
    if rank == 0:
        pl.plot([go.Heatmap(z=ss_data)], filename='ss.html')

    sh_data = sh.val.get_full_data().real
    if rank == 0:
        pl.plot([go.Heatmap(z=sh_data)], filename='sh.html')


    m_data = m.val.get_full_data().real
    if rank == 0:
        pl.plot([go.Heatmap(z=m_data)], filename='map.html')
Jakob Knollmueller's avatar
Jakob Knollmueller committed
183