cartesian_wiener_filter.py 4.61 KB
Newer Older
Theo Steininger's avatar
Theo Steininger committed
1
import numpy as np
Martin Reinecke's avatar
Martin Reinecke committed
2
import nifty4 as ift
Theo Steininger's avatar
Theo Steininger committed
3
4

if __name__ == "__main__":
Martin Reinecke's avatar
Martin Reinecke committed
5
    signal_to_noise = 0.5  # The signal to noise ratio
Theo Steininger's avatar
Theo Steininger committed
6

Martin Reinecke's avatar
Martin Reinecke committed
7
    # Setting up parameters
Martin Reinecke's avatar
Martin Reinecke committed
8
9
10
11
12
13
14
15
16
17
18
19
    L_1 = 2.                   # Total side-length of the domain
    N_pixels_1 = 512           # Grid resolution (pixels per axis)
    L_2 = 2.                   # Total side-length of the domain
    N_pixels_2 = 512           # Grid resolution (pixels per axis)
    correlation_length_1 = 1.
    field_variance_1 = 2.      # Variance of field in position space
    response_sigma_1 = 0.05    # Smoothing length of response
    correlation_length_2 = 1.
    field_variance_2 = 2.      # Variance of field in position space
    response_sigma_2 = 0.01    # Smoothing length of response

    def power_spectrum_1(k):   # note: field_variance**2 = a*k_0/4.
Theo Steininger's avatar
Theo Steininger committed
20
21
22
23
        a = 4 * correlation_length_1 * field_variance_1**2
        return a / (1 + k * correlation_length_1) ** 4.

    signal_space_1 = ift.RGSpace([N_pixels_1], distances=L_1/N_pixels_1)
Martin Reinecke's avatar
Martin Reinecke committed
24
    harmonic_space_1 = signal_space_1.get_default_codomain()
25
26
27
28
29
    signal_space_2 = ift.RGSpace([N_pixels_2], distances=L_2/N_pixels_2)
    harmonic_space_2 = signal_space_2.get_default_codomain()

    signal_domain = ift.DomainTuple.make((signal_space_1, signal_space_2))
    mid_domain = ift.DomainTuple.make((signal_space_1, harmonic_space_2))
Martin Reinecke's avatar
Martin Reinecke committed
30
31
    harmonic_domain = ift.DomainTuple.make((harmonic_space_1,
                                            harmonic_space_2))
32

Martin Reinecke's avatar
Martin Reinecke committed
33
    ht_1 = ift.HarmonicTransformOperator(harmonic_domain, space=0)
Martin Reinecke's avatar
Martin Reinecke committed
34
    power_space_1 = ift.PowerSpace(harmonic_space_1)
Theo Steininger's avatar
Theo Steininger committed
35

Martin Reinecke's avatar
Martin Reinecke committed
36
    mock_power_1 = ift.PS_field(power_space_1, power_spectrum_1)
Theo Steininger's avatar
Theo Steininger committed
37

Martin Reinecke's avatar
Martin Reinecke committed
38
    def power_spectrum_2(k):  # note: field_variance**2 = a*k_0/4.
Theo Steininger's avatar
Theo Steininger committed
39
40
41
        a = 4 * correlation_length_2 * field_variance_2**2
        return a / (1 + k * correlation_length_2) ** 2.5

Martin Reinecke's avatar
Martin Reinecke committed
42
    ht_2 = ift.HarmonicTransformOperator(mid_domain, space=1)
Martin Reinecke's avatar
Martin Reinecke committed
43
    power_space_2 = ift.PowerSpace(harmonic_space_2)
Theo Steininger's avatar
Theo Steininger committed
44

Martin Reinecke's avatar
Martin Reinecke committed
45
    mock_power_2 = ift.PS_field(power_space_2, power_spectrum_2)
Theo Steininger's avatar
Theo Steininger committed
46

Martin Reinecke's avatar
Martin Reinecke committed
47
    ht = ht_2*ht_1
Theo Steininger's avatar
Theo Steininger committed
48

49
    mock_power = ift.Field.from_global_data(
Martin Reinecke's avatar
Martin Reinecke committed
50
        (power_space_1, power_space_2),
51
52
        np.outer(mock_power_1.to_global_data(),
                 mock_power_2.to_global_data()))
Theo Steininger's avatar
Theo Steininger committed
53

Martin Reinecke's avatar
tweaks    
Martin Reinecke committed
54
    diagonal = ift.power_synthesize_nonrandom(mock_power, spaces=(0, 1))**2
Theo Steininger's avatar
Theo Steininger committed
55

56
    S = ift.DiagonalOperator(diagonal)
Theo Steininger's avatar
Theo Steininger committed
57
58

    np.random.seed(10)
Martin Reinecke's avatar
Martin Reinecke committed
59
    mock_signal = ift.power_synthesize(mock_power, real_signal=True)
Theo Steininger's avatar
Theo Steininger committed
60
61
62

    # Setting up a exemplary response
    N1_10 = int(N_pixels_1/10)
Martin Reinecke's avatar
Martin Reinecke committed
63
64
    mask_1 = np.ones(signal_space_1.shape)
    mask_1[N1_10*7:N1_10*9] = 0.
65
    mask_1 = ift.Field.from_global_data(signal_space_1, mask_1)
Theo Steininger's avatar
Theo Steininger committed
66
67

    N2_10 = int(N_pixels_2/10)
Martin Reinecke's avatar
Martin Reinecke committed
68
69
    mask_2 = np.ones(signal_space_2.shape)
    mask_2[N2_10*7:N2_10*9] = 0.
70
    mask_2 = ift.Field.from_global_data(signal_space_2, mask_2)
Theo Steininger's avatar
Theo Steininger committed
71

Martin Reinecke's avatar
Martin Reinecke committed
72
    R = ift.GeometryRemover(signal_domain)
Martin Reinecke's avatar
Martin Reinecke committed
73
74
    R = R*ift.DiagonalOperator(mask_1, signal_domain, spaces=0)
    R = R*ift.DiagonalOperator(mask_2, signal_domain, spaces=1)
Martin Reinecke's avatar
Martin Reinecke committed
75
76
77
78
79
    R = R*ht
    R = R * ift.create_harmonic_smoothing_operator(harmonic_domain, 0,
                                                   response_sigma_1)
    R = R * ift.create_harmonic_smoothing_operator(harmonic_domain, 1,
                                                   response_sigma_2)
Theo Steininger's avatar
Theo Steininger committed
80
81
    data_domain = R.target

Martin Reinecke's avatar
Martin Reinecke committed
82
    noiseless_data = R(mock_signal)
Martin Reinecke's avatar
Martin Reinecke committed
83
    noise_amplitude = noiseless_data.val.std()/signal_to_noise
Theo Steininger's avatar
Theo Steininger committed
84
    # Setting up the noise covariance and drawing a random noise realization
85
    N = ift.ScalingOperator(noise_amplitude**2, data_domain)
Martin Reinecke's avatar
Martin Reinecke committed
86
87
    noise = ift.Field.from_random(
        domain=data_domain, random_type='normal',
Martin Reinecke's avatar
Martin Reinecke committed
88
89
        std=noise_amplitude, mean=0)
    data = noiseless_data + noise
Theo Steininger's avatar
Theo Steininger committed
90
91

    # Wiener filter
Martin Reinecke's avatar
Martin Reinecke committed
92
    j = R.adjoint_times(N.inverse_times(data))
93
    ctrl = ift.GradientNormController(name="inverter", tol_abs_gradnorm=0.1)
94
    inverter = ift.ConjugateGradient(controller=ctrl)
95
    wiener_curvature = ift.library.WienerFilterCurvature(
Martin Reinecke's avatar
Martin Reinecke committed
96
        S=S, N=N, R=R, inverter=inverter)
Theo Steininger's avatar
Theo Steininger committed
97

Martin Reinecke's avatar
Martin Reinecke committed
98
    m_k = wiener_curvature.inverse_times(j)
Martin Reinecke's avatar
Martin Reinecke committed
99
    m = ht(m_k)
Theo Steininger's avatar
Theo Steininger committed
100

101
    plotdict = {"colormap": "Planck-like"}
Martin Reinecke's avatar
Martin Reinecke committed
102
    plot_space = ift.RGSpace((N_pixels_1, N_pixels_2))
Martin Reinecke's avatar
Martin Reinecke committed
103
    ift.plot(ht(mock_signal).cast_domain(plot_space),
Martin Reinecke's avatar
Martin Reinecke committed
104
             name='mock_signal.png', **plotdict)
Martin Reinecke's avatar
Martin Reinecke committed
105
106
    ift.plot(data.cast_domain(plot_space), name='data.png', **plotdict)
    ift.plot(m.cast_domain(plot_space), name='map.png', **plotdict)
Martin Reinecke's avatar
Martin Reinecke committed
107
    # sampling the uncertainty map
108
    mean, variance = ift.probe_with_posterior_samples(wiener_curvature, ht, 10)
Martin Reinecke's avatar
Martin Reinecke committed
109
    ift.plot(ift.sqrt(variance).cast_domain(plot_space),
Martin Reinecke's avatar
Martin Reinecke committed
110
             name="uncertainty.png", **plotdict)
Martin Reinecke's avatar
Martin Reinecke committed
111
    ift.plot((mean+m).cast_domain(plot_space),
Martin Reinecke's avatar
Martin Reinecke committed
112
             name="posterior_mean.png", **plotdict)