wiener_filter_via_curvature.py 3.49 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

Martin Reinecke's avatar
Martin Reinecke committed
6
7
8
9
10
11
12
13
def init_nu():
    if ift.dobj.ntask > 1:
        from mpi4py import MPI
        comm = MPI.COMM_WORLD
        data = np.random.randint(10000000, size=1)
        data = comm.bcast(data, root=0)
        nu.reset_units(data[0])

14
if __name__ == "__main__":
Martin Reinecke's avatar
Martin Reinecke committed
15
    # In MPI mode, the random seed for numericalunits must be set by hand
Martin Reinecke's avatar
Martin Reinecke committed
16
    init_nu()
Martin Reinecke's avatar
Martin Reinecke committed
17
18
19
20

    # the number of dimensions for the problem
    # For dimensionality>2, you should probably reduce N_pixels, otherwise
    # the code may run out of memory
21
    dimensionality = 2
Martin Reinecke's avatar
Martin Reinecke committed
22
23
    # Grid resolution (pixels per axis)
    N_pixels = 512
Martin Reinecke's avatar
Martin Reinecke committed
24
    np.random.seed(43)
25
26
27
28

    # Setting up variable parameters

    # Typical distance over which the field is correlated
29
30
    correlation_length = 0.05*nu.m
    # sigma of field in position space sqrt(<|s_x|^2>)
Martin Reinecke's avatar
adjust    
Martin Reinecke committed
31
    field_sigma = 2. * nu.K
32
    # smoothing length of response
Martin Reinecke's avatar
Martin Reinecke committed
33
34
35
    response_sigma = 0.03*nu.m
    # The signal to noise ratio
    signal_to_noise = 1
36
37
38
39

    # 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
40
        # RL FIXME: signal_amplitude is not how much signal varies
41
42
        cldim = correlation_length**(2*dimensionality)
        a = 4/(2*np.pi) * cldim * field_sigma**2
Martin Reinecke's avatar
adjust    
Martin Reinecke committed
43
44
        # to be integrated over spherical shells later on
        return a / (1 + (k*correlation_length)**(2*dimensionality)) ** 2
45
46
47
48

    # Setting up the geometry

    # Total side-length of the domain
49
50
    L = 2.*nu.m
    shape = [N_pixels]*dimensionality
51

52
    signal_space = ift.RGSpace(shape, distances=L/N_pixels)
Martin Reinecke's avatar
Martin Reinecke committed
53
    harmonic_space = signal_space.get_default_codomain()
Martin Reinecke's avatar
Martin Reinecke committed
54
    HT = ift.HarmonicTransformOperator(harmonic_space, target=signal_space)
Martin Reinecke's avatar
Martin Reinecke committed
55
    power_space = ift.PowerSpace(harmonic_space)
56
57

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

Martin Reinecke's avatar
Martin Reinecke committed
62
    mock_power = ift.PS_field(power_space, power_spectrum)
63
    mock_signal = ift.power_synthesize(mock_power, real_signal=True)
64

Martin Reinecke's avatar
Martin Reinecke committed
65
    sensitivity = (1./nu.m)**dimensionality/nu.K
66
67
    R = ift.GeometryRemover(signal_space)
    R = R*ift.ScalingOperator(sensitivity, signal_space)
Martin Reinecke's avatar
Martin Reinecke committed
68
    R = R*HT
Martin Reinecke's avatar
Martin Reinecke committed
69
70
    R = R * ift.create_harmonic_smoothing_operator(
        (harmonic_space,), 0, response_sigma)
71
72
    data_domain = R.target[0]

Martin Reinecke's avatar
Martin Reinecke committed
73
    noiseless_data = R(mock_signal)
Martin Reinecke's avatar
Martin Reinecke committed
74
    noise_amplitude = noiseless_data.val.std()/signal_to_noise
75
    N = ift.ScalingOperator(noise_amplitude**2, data_domain)
Martin Reinecke's avatar
adjust    
Martin Reinecke committed
76
77
    noise = ift.Field.from_random(
        domain=data_domain, random_type='normal',
Martin Reinecke's avatar
Martin Reinecke committed
78
        std=noise_amplitude, mean=0)
Martin Reinecke's avatar
Martin Reinecke committed
79
    data = noiseless_data + noise
80

81
    j = R.adjoint_times(N.inverse_times(data))
Martin Reinecke's avatar
adjust    
Martin Reinecke committed
82
    ctrl = ift.GradientNormController(
83
        name="inverter", tol_abs_gradnorm=1e-5/(nu.K*(nu.m**dimensionality)))
Martin Reinecke's avatar
Martin Reinecke committed
84
    inverter = ift.ConjugateGradient(controller=ctrl)
85
    wiener_curvature = ift.library.WienerFilterCurvature(
86
        S=S, N=N, R=R, inverter=inverter)
87
88

    m = wiener_curvature.inverse_times(j)
Martin Reinecke's avatar
Martin Reinecke committed
89
    m_s = HT(m)
90

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

Martin Reinecke's avatar
Martin Reinecke committed
93
94
    zmax = max(m_s.max(), HT(mock_signal).max())
    zmin = min(m_s.min(), HT(mock_signal).min())
95
96
    plotdict = {"zmax": zmax/nu.K, "zmin": zmin/nu.K}

Martin Reinecke's avatar
Martin Reinecke committed
97
98
    ift.plot(ift.Field(sspace2, HT(mock_signal).val)/nu.K,
             name="mock_signal.png", **plotdict)
Martin Reinecke's avatar
Martin Reinecke committed
99
    ift.plot(ift.Field(sspace2, val=data.val), name="data.png")
Martin Reinecke's avatar
Martin Reinecke committed
100
101
    ift.plot(ift.Field(sspace2, m_s.val)/nu.K, name="reconstruction.png",
             **plotdict)