wiener_filter_via_curvature.py 4.45 KB
Newer Older
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
1
2
use_nifty2go = True

3
import numpy as np
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
4
5
6
7
if use_nifty2go:
    import nifty2go as ift
else:
    import nifty as ift
8
import numericalunits as nu
9
10

if __name__ == "__main__":
Martin Reinecke's avatar
Martin Reinecke committed
11
    # In MPI mode, the random seed for numericalunits must be set by hand
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
12
13
14
    if not use_nifty2go:
        ift.nifty_configuration['default_distribution_strategy'] = 'fftw'
        ift.nifty_configuration['harmonic_rg_base'] = 'real'
Martin Reinecke's avatar
Martin Reinecke committed
15
    nu.reset_units(43)
16
    dimensionality = 2
Martin Reinecke's avatar
Martin Reinecke committed
17
    np.random.seed(43)
18
19
20
21

    # Setting up variable parameters

    # Typical distance over which the field is correlated
22
23
    correlation_length = 0.05*nu.m
    # sigma of field in position space sqrt(<|s_x|^2>)
Martin Reinecke's avatar
adjust    
Martin Reinecke committed
24
    field_sigma = 2. * nu.K
25
26
    # smoothing length of response
    response_sigma = 0.01*nu.m
27
28
29
30
31
32
    # The signal to noise ratio
    signal_to_noise = 0.7

    # note that field_variance**2 = a*k_0/4. for this analytic form of power
    # spectrum
    def power_spectrum(k):
33
34
        cldim = correlation_length**(2*dimensionality)
        a = 4/(2*np.pi) * cldim * field_sigma**2
Martin Reinecke's avatar
adjust    
Martin Reinecke committed
35
36
        # to be integrated over spherical shells later on
        return a / (1 + (k*correlation_length)**(2*dimensionality)) ** 2
37
38
39
40

    # Setting up the geometry

    # Total side-length of the domain
41
    L = 2.*nu.m
42
    # Grid resolution (pixels per axis)
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
43
    N_pixels = 4096
44
    shape = [N_pixels]*dimensionality
45

46
    signal_space = ift.RGSpace(shape, distances=L/N_pixels)
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
47
48
49
50
    if use_nifty2go:
        harmonic_space = signal_space.get_default_codomain()
    else:
        harmonic_space = ift.FFTOperator.get_default_codomain(signal_space)
Martin Reinecke's avatar
Martin Reinecke committed
51
52
    fft = ift.FFTOperator(harmonic_space, target=signal_space)
    power_space = ift.PowerSpace(harmonic_space)
53
54

    # Creating the mock data
Martin Reinecke's avatar
adjust    
Martin Reinecke committed
55
56
    S = ift.create_power_operator(harmonic_space,
                                  power_spectrum=power_spectrum)
Martin Reinecke's avatar
Martin Reinecke committed
57
    np.random.seed(43)
58

Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
59
60
61
62
63
64
    if use_nifty2go:
        mock_power = ift.PS_field(power_space, power_spectrum)
        mock_harmonic = ift.power_synthesize(mock_power, real_signal=True)
    else:
        mock_power = ift.Field(power_space, val=power_spectrum)
        mock_harmonic = mock_power.power_synthesize(real_signal=True)
Martin Reinecke's avatar
Martin Reinecke committed
65
    mock_harmonic = mock_harmonic.real
66
67
    mock_signal = fft(mock_harmonic)

68
    exposure = 1.
Martin Reinecke's avatar
adjust    
Martin Reinecke committed
69
70
    R = ift.ResponseOperator(signal_space, sigma=(response_sigma,),
                             exposure=(exposure,))
71
    data_domain = R.target[0]
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
72
73
74
75
76
77
78
79
80
81
82
83
    if use_nifty2go:
        R_harmonic = ift.ComposedOperator([fft, R])
    else:
        R_harmonic = ift.ComposedOperator([fft, R], default_spaces=[0, 0])

    if use_nifty2go:
        N = ift.DiagonalOperator(
            ift.Field.full(data_domain,
                           mock_signal.var()/signal_to_noise).weight(1))
    else:
        ndiag = ift.Field(data_domain, mock_signal.var()/signal_to_noise).weight(1)
        N = ift.DiagonalOperator(data_domain, ndiag)
84

Martin Reinecke's avatar
adjust    
Martin Reinecke committed
85
86
87
    noise = ift.Field.from_random(
        domain=data_domain, random_type='normal',
        std=mock_signal.std()/np.sqrt(signal_to_noise), mean=0)
88
89
90
91
92
    data = R(mock_signal) + noise

    # Wiener filter

    j = R_harmonic.adjoint_times(N.inverse_times(data))
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
93
94
95
96
97
98
99
100
101
102
    if use_nifty2go:
        ctrl = ift.GradientNormController(
            verbose=True, iteration_limit=10, tol_abs_gradnorm=1e-4/nu.K/(nu.m**(0.5*dimensionality)))
    else:
        def print_stats(a_energy, iteration):  # returns current energy
            x = a_energy.value
            print(x, iteration)
        ctrl = ift.GradientNormController(
            callback=print_stats, iteration_limit=10, tol_abs_gradnorm=1e-4/nu.K/(nu.m**(0.5*dimensionality)))

Martin Reinecke's avatar
Martin Reinecke committed
103
    inverter = ift.ConjugateGradient(controller=ctrl)
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
104
105
106
107
108
109
    if use_nifty2go:
        wiener_curvature = ift.library.WienerFilterCurvature(S=S, N=N,
                                                             R=R_harmonic)
        wiener_curvature = ift.InversionEnabler(wiener_curvature, inverter)
    else:
        wiener_curvature = ift.library.WienerFilterCurvature(S=S, N=N, R=R_harmonic, inverter=inverter)
110
111
112
113

    m = wiener_curvature.inverse_times(j)
    m_s = fft(m)

Martin Reinecke's avatar
adjust    
Martin Reinecke committed
114
    sspace2 = ift.RGSpace(shape, distances=L/N_pixels/nu.m)
115

Martin Reinecke's avatar
adjust    
Martin Reinecke committed
116
117
    ift.plotting.plot(ift.Field(sspace2, mock_signal.real.val)/nu.K,
                      name="mock_signal.pdf")
Martin Reinecke's avatar
Martin Reinecke committed
118
119
120
    data = ift.dobj.to_global_data(data.val.real).reshape(sspace2.shape)/nu.K
    data = ift.Field(sspace2, val=ift.dobj.from_global_data(data))/nu.K
    ift.plotting.plot(ift.Field(sspace2, val=data), name="data.pdf")
Martin Reinecke's avatar
adjust    
Martin Reinecke committed
121
    ift.plotting.plot(ift.Field(sspace2, m_s.real.val)/nu.K, name="map.pdf")