wiener_filter_advanced.py 4.25 KB
Newer Older
1
2

from nifty import *
3

4
5
import plotly.offline as pl
import plotly.graph_objs as go
6
7
8
9
10

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

11
np.random.seed(42)
12

13
14
class AdjointFFTResponse(LinearOperator):
    def __init__(self, FFT, R, default_spaces=None):
Jakob Knollmueller's avatar
test    
Jakob Knollmueller committed
15
        super(AdjointFFTResponse, self).__init__(default_spaces)
16
        self._domain = FFT.target
Jakob Knollmueller's avatar
test    
Jakob Knollmueller committed
17
        self._target = R.target
18
19
20
        self.R = R
        self.FFT = FFT

Jakob Knollmueller's avatar
test    
Jakob Knollmueller committed
21
    def _times(self, x, spaces=None):
22
23
        return self.R(self.FFT.adjoint_times(x))

Jakob Knollmueller's avatar
test    
Jakob Knollmueller committed
24
    def _adjoint_times(self, x, spaces=None):
25
        return self.FFT(self.R.adjoint_times(x))
Jakob Knollmueller's avatar
test    
Jakob Knollmueller committed
26
27
28
29
30
31
32
33
34
35
36
    @property
    def domain(self):
        return self._domain

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

    @property
    def unitary(self):
        return False
37

38
39


40
41
if __name__ == "__main__":

Martin Reinecke's avatar
Martin Reinecke committed
42
    distribution_strategy = 'not'
43

Jakob Knollmueller's avatar
Jakob Knollmueller committed
44
    # Set up position space
Jakob Knollmueller's avatar
Jakob Knollmueller committed
45
    s_space = RGSpace([1024,1024])
Jakob Knollmueller's avatar
Jakob Knollmueller committed
46
47
48
    # s_space = HPSpace(32)

    # Define harmonic transformation and associated harmonic space
49
50
    fft = FFTOperator(s_space)
    h_space = fft.target[0]
Jakob Knollmueller's avatar
Jakob Knollmueller committed
51
52

    # Setting up power space
53
54
    p_space = PowerSpace(h_space, distribution_strategy=distribution_strategy)

Jakob Knollmueller's avatar
Jakob Knollmueller committed
55
    # Choosing the prior correlation structure and defining correlation operator
56
    pow_spec = (lambda k: (42 / (k + 1) ** 3))
Jakob Knollmueller's avatar
Jakob Knollmueller committed
57
58
    sqr_pow_spec = lambda z: pow_spec(z) ** (1. / 2)
    S = create_power_operator(h_space, power_spectrum=sqr_pow_spec,
59
60
                              distribution_strategy=distribution_strategy)

Jakob Knollmueller's avatar
Jakob Knollmueller committed
61
    # Drawing a sample sh from the prior distribution in harmonic space
62
63
64
    sp = Field(p_space, val=lambda z: pow_spec(z)**(1./2),
               distribution_strategy=distribution_strategy)
    sh = sp.power_synthesize(real_signal=True)
Jakob Knollmueller's avatar
Jakob Knollmueller committed
65
    ss = fft.adjoint_times(sh)
66

Jakob Knollmueller's avatar
Jakob Knollmueller committed
67
    # Choosing the measurement instrument
Jakob Knollmueller's avatar
Jakob Knollmueller committed
68
69
    # Instrument = SmoothingOperator(s_space, sigma=0.05)
    Instrument = DiagonalOperator(s_space, diagonal=1.)
70
#    Instrument._diagonal.val[200:400, 200:400] = 0
Jakob Knollmueller's avatar
Jakob Knollmueller committed
71
72

    #Adding a harmonic transformation to the instrument
73
    R = AdjointFFTResponse(fft, Instrument)
Jakob Knollmueller's avatar
Jakob Knollmueller committed
74
    signal_to_noise = 1.
75
76
77
78
79
80
    N = DiagonalOperator(s_space, diagonal=ss.var()/signal_to_noise, bare=True)
    n = Field.from_random(domain=s_space,
                          random_type='normal',
                          std=ss.std()/np.sqrt(signal_to_noise),
                          mean=0)

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

Jakob Knollmueller's avatar
Jakob Knollmueller committed
84
85
86
    # Choosing the minimization strategy

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

90
91
#    minimizer = SteepestDescent(convergence_tolerance=0,
#                                iteration_limit=50,
Jakob Knollmueller's avatar
Jakob Knollmueller committed
92
#                                callback=convergence_measure)
93
94

    minimizer = RelaxedNewton(convergence_tolerance=0,
Jakob Knollmueller's avatar
Jakob Knollmueller committed
95
                              iteration_limit=1,
Jakob Knollmueller's avatar
Jakob Knollmueller committed
96
97
98
                              callback=convergence_measure)
    #
    # minimizer = VL_BFGS(convergence_tolerance=0,
Jakob Knollmueller's avatar
Jakob Knollmueller committed
99
    #                    iteration_limit=50,
Jakob Knollmueller's avatar
Jakob Knollmueller committed
100
101
102
    #                    callback=convergence_measure,
    #                    max_history_length=3)
    #
Jakob Knollmueller's avatar
Jakob Knollmueller committed
103

Jakob Knollmueller's avatar
Jakob Knollmueller committed
104
105

    # Setting starting position
Jakob Knollmueller's avatar
Jakob Knollmueller committed
106
    m0 = Field(h_space, val=.0)
Jakob Knollmueller's avatar
Jakob Knollmueller committed
107
108
109
110
111

    # Initializing the Wiener Filter energy
    energy = WienerFilterEnergy(position=m0, d=d, R=R, N=N, S=S)

    # Solving the problem analytically
Jakob Knollmueller's avatar
test    
Jakob Knollmueller committed
112
    solution = energy.analytic_solution()
Jakob Knollmueller's avatar
Jakob Knollmueller committed
113
114

    # Solving the problem with chosen minimization strategy
115
116
    (energy, convergence) = minimizer(energy)

Jakob Knollmueller's avatar
Jakob Knollmueller committed
117
118
119
    # Transforming fields to position space for plotting

    ss = fft.adjoint_times(sh)
120
    m = fft.adjoint_times(energy.position)
Jakob Knollmueller's avatar
Jakob Knollmueller committed
121
122
123
    m_wf = fft.adjoint_times(solution.position)

    # Plotting
124

125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
    d_data = d.val.get_full_data().real
    if rank == 0:
        pl.plot([go.Heatmap(z=d_data)], filename='data.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')
142

Jakob Knollmueller's avatar
Jakob Knollmueller committed
143
144
145
146
    m_wf_data = m_wf.val.get_full_data().real
    if rank == 0:
        pl.plot([go.Heatmap(z=m_wf_data)], filename='map_wf.html')