wiener_filter_via_curvature.py 3.22 KB
Newer Older
1
import numpy as np
Martin Reinecke's avatar
Martin Reinecke committed
2
import nifty4 as ift
3
import numericalunits as nu
4

Martin Reinecke's avatar
tweaks    
Martin Reinecke committed
5

6
if __name__ == "__main__":
Martin Reinecke's avatar
Martin Reinecke committed
7
    # In MPI mode, the random seed for numericalunits must be set by hand
Martin Reinecke's avatar
Martin Reinecke committed
8
    #nu.reset_units(43)
9
    dimensionality = 2
Martin Reinecke's avatar
Martin Reinecke committed
10
    np.random.seed(43)
11
12
13
14

    # Setting up variable parameters

    # Typical distance over which the field is correlated
15
16
    correlation_length = 0.05*nu.m
    # sigma of field in position space sqrt(<|s_x|^2>)
Martin Reinecke's avatar
adjust    
Martin Reinecke committed
17
    field_sigma = 2. * nu.K
18
    # smoothing length of response
Martin Reinecke's avatar
Martin Reinecke committed
19
20
21
    response_sigma = 0.03*nu.m
    # The signal to noise ratio
    signal_to_noise = 1
22
23
24
25

    # note that field_variance**2 = a*k_0/4. for this analytic form of power
    # spectrum
    def power_spectrum(k):
Martin Reinecke's avatar
Martin Reinecke committed
26
        #RL FIXME: signal_amplitude is not how much signal varies
27
28
        cldim = correlation_length**(2*dimensionality)
        a = 4/(2*np.pi) * cldim * field_sigma**2
Martin Reinecke's avatar
adjust    
Martin Reinecke committed
29
30
        # to be integrated over spherical shells later on
        return a / (1 + (k*correlation_length)**(2*dimensionality)) ** 2
31
32
33
34

    # Setting up the geometry

    # Total side-length of the domain
35
    L = 2.*nu.m
36
37
    # Grid resolution (pixels per axis)
    N_pixels = 512
38
    shape = [N_pixels]*dimensionality
39

40
    signal_space = ift.RGSpace(shape, distances=L/N_pixels)
Martin Reinecke's avatar
Martin Reinecke committed
41
    harmonic_space = signal_space.get_default_codomain()
Martin Reinecke's avatar
Martin Reinecke committed
42
    ht = ift.HarmonicTransformOperator(harmonic_space, target=signal_space)
Martin Reinecke's avatar
Martin Reinecke committed
43
    power_space = ift.PowerSpace(harmonic_space)
44
45

    # Creating the mock data
Martin Reinecke's avatar
adjust    
Martin Reinecke committed
46
47
    S = ift.create_power_operator(harmonic_space,
                                  power_spectrum=power_spectrum)
Martin Reinecke's avatar
Martin Reinecke committed
48
    np.random.seed(43)
49

Martin Reinecke's avatar
Martin Reinecke committed
50
    mock_power = ift.PS_field(power_space, power_spectrum)
51
    mock_signal = ift.power_synthesize(mock_power, real_signal=True)
52

Martin Reinecke's avatar
Martin Reinecke committed
53
    sensitivity = (1./nu.m)**dimensionality/nu.K
54
55
    R = ift.GeometryRemover(signal_space)
    R = R*ift.ScalingOperator(sensitivity, signal_space)
Martin Reinecke's avatar
Martin Reinecke committed
56
    R = R*ht
57
    R = R * ift.create_harmonic_smoothing_operator((harmonic_space,),0,response_sigma)
58
59
    data_domain = R.target[0]

Martin Reinecke's avatar
Martin Reinecke committed
60
    noiseless_data = R(mock_signal)
Martin Reinecke's avatar
Martin Reinecke committed
61
    noise_amplitude = noiseless_data.val.std()/signal_to_noise
62
    N = ift.DiagonalOperator(
Martin Reinecke's avatar
Martin Reinecke committed
63
        ift.Field.full(data_domain, noise_amplitude**2))
Martin Reinecke's avatar
adjust    
Martin Reinecke committed
64
65
    noise = ift.Field.from_random(
        domain=data_domain, random_type='normal',
Martin Reinecke's avatar
Martin Reinecke committed
66
        std=noise_amplitude, mean=0)
Martin Reinecke's avatar
Martin Reinecke committed
67
    data = noiseless_data + noise
Martin Reinecke's avatar
Martin Reinecke committed
68
     # Wiener filter
69

70
    j = R.adjoint_times(N.inverse_times(data))
Martin Reinecke's avatar
adjust    
Martin Reinecke committed
71
    ctrl = ift.GradientNormController(
72
        name="inverter", tol_abs_gradnorm=1e-5/(nu.K*(nu.m**dimensionality)))
Martin Reinecke's avatar
Martin Reinecke committed
73
    inverter = ift.ConjugateGradient(controller=ctrl)
74
    wiener_curvature = ift.library.WienerFilterCurvature(
75
        S=S, N=N, R=R, inverter=inverter)
76
77

    m = wiener_curvature.inverse_times(j)
Martin Reinecke's avatar
Martin Reinecke committed
78
    m_s = ht(m)
79

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

Martin Reinecke's avatar
Martin Reinecke committed
82
    ift.plot(ift.Field(sspace2, ht(mock_signal).val)/nu.K, name="mock_signal.png")
Martin Reinecke's avatar
tweaks    
Martin Reinecke committed
83
84
    #data = ift.dobj.to_global_data(data.val).reshape(sspace2.shape)
    #data = ift.Field(sspace2, val=ift.dobj.from_global_data(data))
Martin Reinecke's avatar
Martin Reinecke committed
85
86
    ift.plot(ift.Field(sspace2, val=data.val), name="data.png")
    print "msig",np.min(ht(mock_signal).val)/nu.K, np.max(ht(mock_signal).val)/nu.K
Martin Reinecke's avatar
tweaks    
Martin Reinecke committed
87
    print "map",np.min(m_s.val)/nu.K, np.max(m_s.val)/nu.K
88
    ift.plot(ift.Field(sspace2, m_s.val)/nu.K, name="map.png")