krylov_sampling.py 2.47 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))
33
D_inv = ift.SandwichOperator(R_p, N.inverse) + S.inverse
34

Philipp Arras's avatar
PEP8    
Philipp Arras committed
35

36
37
N_samps = 200
N_iter = 10
38
m, samps = ift.library.generate_krylov_samples(D_inv, S, j, N_samps, N_iter)
39
m_x = sky(m)
Philipp Arras's avatar
PEP8    
Philipp Arras committed
40
IC = ift.GradientNormController(iteration_limit=N_iter)
41
inverter = ift.ConjugateGradient(IC)
Philipp Arras's avatar
PEP8    
Philipp Arras committed
42
curv = ift.library.WienerFilterCurvature(S=S, N=N, R=R_p, inverter=inverter)
43
44
45
46
samps_old = []
for i in range(N_samps):
    samps_old += [curv.draw_sample(from_inverse=True)]

Philipp Arras's avatar
Philipp Arras committed
47
plt.plot(d.val, '+', label="data", alpha=.5)
Philipp Arras's avatar
PEP8    
Philipp Arras committed
48
plt.plot(s_x.val, label="original")
49
50
plt.plot(m_x.val, label="reconstruction")
plt.legend()
Philipp Arras's avatar
Philipp Arras committed
51
52
plt.savefig('Krylov_reconstruction.png')
plt.close()
53

Philipp Arras's avatar
Philipp Arras committed
54
pltdict = {'alpha': .3, 'linewidth': .2}
55
for i in range(N_samps):
Philipp Arras's avatar
Philipp Arras committed
56
    if i == 0:
Martin Reinecke's avatar
Martin Reinecke committed
57
58
59
60
61
62
        plt.plot(sky(samps_old[i]).val, color='b',
                 label='Traditional samples (residuals)',
                 **pltdict)
        plt.plot(sky(samps[i]).val, color='r',
                 label='Krylov samples (residuals)',
                 **pltdict)
Philipp Arras's avatar
Philipp Arras committed
63
64
65
66
67
68
    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
69
70
plt.close()

71
72
73
74
75
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
76
plt.plot(np.sqrt(D_hat_old / N_samps), 'r--', label='Traditional uncertainty')
Philipp Arras's avatar
PEP8    
Philipp Arras committed
77
78
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(
Martin Reinecke's avatar
Martin Reinecke committed
79
80
    D_hat_new / N_samps), facecolor='0.5', alpha=0.5,
    label='Krylov uncertainty')
Philipp Arras's avatar
PEP8    
Philipp Arras committed
81
plt.plot((s_x - m_x).val, color='k', label='signal - mean')
Philipp Arras's avatar
Philipp Arras committed
82
83
84
plt.legend()
plt.savefig('Krylov_uncertainty.png')
plt.close()
85