wiener_filter_via_curvature.py 3.29 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
9
    #nu.reset_units(43)
    dimensionality = 1
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
19
    # smoothing length of response
    response_sigma = 0.01*nu.m
Martin Reinecke's avatar
Martin Reinecke committed
20
    # The signal to noise ratio ***CURRENTLY BROKEN***
Martin Reinecke's avatar
Martin Reinecke committed
21
    signal_to_noise = 70.7
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
tweaks    
Martin Reinecke committed
42
    fft = 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)
Martin Reinecke's avatar
adjust    
Martin Reinecke committed
51
    mock_harmonic = ift.power_synthesize(mock_power, real_signal=True)
52
53
    mock_signal = fft(mock_harmonic)

Martin Reinecke's avatar
Martin Reinecke committed
54
    sensitivity = (1./nu.m)**dimensionality/nu.K
Martin Reinecke's avatar
tweaks    
Martin Reinecke committed
55
    R = ift.ResponseOperator(signal_space, sigma=(response_sigma,),
Martin Reinecke's avatar
Martin Reinecke committed
56
                             sensitivity=(sensitivity,))
57
    data_domain = R.target[0]
Martin Reinecke's avatar
Martin Reinecke committed
58
    R_harmonic = R*fft
59

Martin Reinecke's avatar
Martin Reinecke committed
60
    noise_amplitude = 1./signal_to_noise*field_sigma*sensitivity*((L/N_pixels)**dimensionality)
Martin Reinecke's avatar
tweaks    
Martin Reinecke committed
61
    print "noise amplitude:", noise_amplitude
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
tweaks    
Martin Reinecke committed
67
    data = R(mock_signal) + noise
Martin Reinecke's avatar
Martin Reinecke committed
68
     # Wiener filter
69
70

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

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

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

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