krylov_sampling.py 3.11 KB
Newer Older
1
2
3
4
5
import nifty4 as ift
import numpy as np
import matplotlib.pyplot as plt
from nifty4.sugar import create_power_operator

Philipp Arras's avatar
Philipp Arras committed
6
7
np.random.seed(42)

8
9
10
11
x_space = ift.RGSpace(1024)
h_space = x_space.get_default_codomain()

d_space = x_space
Philipp Arras's avatar
PEP8    
Philipp Arras committed
12
N_hat = ift.Field(d_space, 10.)
13
14
15
16
17
N_hat.val[400:450] = 0.0001
N = ift.DiagonalOperator(N_hat, d_space)

FFT = ift.HarmonicTransformOperator(h_space, target=x_space)
R = ift.ScalingOperator(1., x_space)
Philipp Arras's avatar
PEP8    
Philipp Arras committed
18
19
20
21
22
23


def ampspec(k): return 1. / (1. + k**2.)


S = ift.ScalingOperator(1., h_space)
24
25
A = create_power_operator(h_space, ampspec)
s_h = S.draw_sample()
Philipp Arras's avatar
PEP8    
Philipp Arras committed
26
sky = FFT * A
27
28
29
30
s_x = sky(s_h)
n = N.draw_sample()
d = R(s_x) + n

Philipp Arras's avatar
PEP8    
Philipp Arras committed
31
R_p = R * FFT * A
32
j = R_p.adjoint(N.inverse(d))
Philipp Arras's avatar
PEP8    
Philipp Arras committed
33
34
D_inv = R_p.adjoint * N.inverse * R_p + S.inverse

35
36
37
38
39
40
41
42
43
44

def sample(D_inv, S, j,  N_samps, N_iter):
    space = D_inv.domain
    x = ift.Field.zeros(space)
    r = j.copy()
    p = r.copy()
    d = p.vdot(D_inv(p))
    y = []
    for i in range(N_samps):
        y += [S.draw_sample()]
Philipp Arras's avatar
PEP8    
Philipp Arras committed
45
46
    for k in range(1, 1 + N_iter):
        gamma = r.vdot(r) / d
47
48
        if gamma == 0.:
            break
Philipp Arras's avatar
PEP8    
Philipp Arras committed
49
        x += gamma * p
50
51
        print(p.vdot(D_inv(j)))
        for i in range(N_samps):
Philipp Arras's avatar
PEP8    
Philipp Arras committed
52
53
            y[i] -= p.vdot(D_inv(y[i])) * p / d
            y[i] += np.random.randn() / np.sqrt(d) * p
54
        #r_new = j - D_inv(x)
Philipp Arras's avatar
PEP8    
Philipp Arras committed
55
56
        r_new = r - gamma * D_inv(p)
        beta = r_new.vdot(r_new) / (r.vdot(r))
57
        r = r_new
Philipp Arras's avatar
PEP8    
Philipp Arras committed
58
        p = r + beta * p
59
60
        d = p.vdot(D_inv(p))
        if d == 0.:
Philipp Arras's avatar
PEP8    
Philipp Arras committed
61
            break
62
63
    return x, y

Philipp Arras's avatar
PEP8    
Philipp Arras committed
64

65
66
N_samps = 200
N_iter = 10
Philipp Arras's avatar
PEP8    
Philipp Arras committed
67
m, samps = sample(D_inv, S, j, N_samps, N_iter)
68
m_x = sky(m)
Philipp Arras's avatar
PEP8    
Philipp Arras committed
69
IC = ift.GradientNormController(iteration_limit=N_iter)
70
inverter = ift.ConjugateGradient(IC)
Philipp Arras's avatar
PEP8    
Philipp Arras committed
71
curv = ift.library.WienerFilterCurvature(S=S, N=N, R=R_p, inverter=inverter)
72
73
74
75
samps_old = []
for i in range(N_samps):
    samps_old += [curv.draw_sample(from_inverse=True)]

Philipp Arras's avatar
Philipp Arras committed
76
plt.plot(d.val, '+', label="data", alpha=.5)
Philipp Arras's avatar
PEP8    
Philipp Arras committed
77
plt.plot(s_x.val, label="original")
78
79
plt.plot(m_x.val, label="reconstruction")
plt.legend()
Philipp Arras's avatar
Philipp Arras committed
80
81
plt.savefig('Krylov_reconstruction.png')
plt.close()
82

Philipp Arras's avatar
Philipp Arras committed
83
pltdict = {'alpha': .3, 'linewidth': .2}
84
for i in range(N_samps):
Philipp Arras's avatar
Philipp Arras committed
85
    if i == 0:
Philipp Arras's avatar
Philipp Arras committed
86
87
        plt.plot(sky(samps_old[i]).val, color='b', **pltdict, label='Traditional samples (residuals)')
        plt.plot(sky(samps[i]).val, color='r', **pltdict, label='Krylov samples (residuals)')
Philipp Arras's avatar
Philipp Arras committed
88
89
90
91
92
93
    else:
        plt.plot(sky(samps_old[i]).val, color='b', **pltdict)
        plt.plot(sky(samps[i]).val, color='r', **pltdict)
plt.plot((s_x - m_x).val, color='k', label='signal - mean')
plt.legend()
plt.savefig('Krylov_samples_residuals.png')
Philipp Arras's avatar
Philipp Arras committed
94
95
plt.close()

96
97
98
99
100
D_hat_old = ift.Field.zeros(x_space).val
D_hat_new = ift.Field.zeros(x_space).val
for i in range(N_samps):
    D_hat_old += sky(samps_old[i]).val**2
    D_hat_new += sky(samps[i]).val**2
Philipp Arras's avatar
Philipp Arras committed
101
plt.plot(np.sqrt(D_hat_old / N_samps), 'r--', label='Traditional uncertainty')
Philipp Arras's avatar
PEP8    
Philipp Arras committed
102
103
plt.plot(-np.sqrt(D_hat_old / N_samps), 'r--')
plt.fill_between(range(len(D_hat_new)), -np.sqrt(D_hat_new / N_samps), np.sqrt(
Philipp Arras's avatar
Philipp Arras committed
104
    D_hat_new / N_samps), facecolor='0.5', alpha=0.5, label='Krylov unvertainty')
Philipp Arras's avatar
PEP8    
Philipp Arras committed
105
plt.plot((s_x - m_x).val, color='k', label='signal - mean')
Philipp Arras's avatar
Philipp Arras committed
106
107
108
plt.legend()
plt.savefig('Krylov_uncertainty.png')
plt.close()