wiener_filter.py 3.53 KB
Newer Older
Theo Steininger's avatar
Theo Steininger committed
1 2
# -*- coding: utf-8 -*-

Martin Reinecke's avatar
Martin Reinecke committed
3
import nifty2go as ift
Theo Steininger's avatar
Theo Steininger committed
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
import numpy as np


if __name__ == "__main__":
    # Setting up parameters    |\label{code:wf_parameters}|
    correlation_length_scale = 1.  # Typical distance over which the field is correlated
    fluctuation_scale = 2.         # Variance of field in position space
    response_sigma = 0.05          # Smoothing length of response (in same unit as L)
    signal_to_noise = 1.5          # The signal to noise ratio
    np.random.seed(43)             # Fixing the random seed
    def power_spectrum(k):         # Defining the power spectrum
        a = 4 * correlation_length_scale * fluctuation_scale**2
        return a / (1 + (k * correlation_length_scale)**2) ** 2

    # Setting up the geometry |\label{code:wf_geometry}|
    L = 2.  # Total side-length of the domain
    N_pixels = 512  # Grid resolution (pixels per axis)
    signal_space = ift.RGSpace([N_pixels, N_pixels], distances=L/N_pixels)
Martin Reinecke's avatar
Martin Reinecke committed
22
    harmonic_space = signal_space.get_default_codomain()
Martin Reinecke's avatar
Martin Reinecke committed
23
    fft = ift.FFTOperator(harmonic_space, target=signal_space)
Martin Reinecke's avatar
Martin Reinecke committed
24
    power_space = ift.PowerSpace(harmonic_space,binbounds=ift.PowerSpace.useful_binbounds(harmonic_space,logarithmic=True))
Theo Steininger's avatar
Theo Steininger committed
25 26 27

    # Creating the mock signal |\label{code:wf_mock_signal}|
    S = ift.create_power_operator(harmonic_space, power_spectrum=power_spectrum)
Martin Reinecke's avatar
Martin Reinecke committed
28
    mock_power = ift.Field(power_space, val=power_spectrum(power_space.kindex))
Theo Steininger's avatar
Theo Steininger committed
29 30 31 32 33 34 35 36 37 38 39
    mock_signal = fft(mock_power.power_synthesize(real_signal=True))

    # Setting up an exemplary response
    mask = ift.Field(signal_space, val=1.)
    N10 = int(N_pixels/10)
    mask.val[N10*5:N10*9, N10*5:N10*9] = 0.
    R = ift.ResponseOperator(signal_space, sigma=(response_sigma,), exposure=(mask,))  #|\label{code:wf_response}|
    data_domain = R.target[0]
    R_harmonic = ift.ComposedOperator([fft, R], default_spaces=[0, 0])

    # Setting up the noise covariance and drawing a random noise realization
Martin Reinecke's avatar
Martin Reinecke committed
40 41
    ndiag = ift.Field(data_domain, mock_signal.var()/signal_to_noise).weight(1)
    N = ift.DiagonalOperator(data_domain, ndiag)
Theo Steininger's avatar
Theo Steininger committed
42 43 44 45 46 47
    noise = ift.Field.from_random(domain=data_domain, random_type='normal',
                                  std=mock_signal.std()/np.sqrt(signal_to_noise), mean=0)
    data = R(mock_signal) + noise  #|\label{code:wf_mock_data}|

    # Wiener filter
    j = R_harmonic.adjoint_times(N.inverse_times(data))
48
    ctrl = ift.GradientNormController(verbose=True,tol_abs_gradnorm=0.1,iteration_limit=10)
49
    inverter = ift.ConjugateGradient(controller=ctrl)
Martin Reinecke's avatar
Martin Reinecke committed
50
    wiener_curvature = ift.library.WienerFilterCurvature(S=S, N=N, R=R_harmonic,inverter=inverter)
Theo Steininger's avatar
Theo Steininger committed
51 52 53 54 55
    m_k = wiener_curvature.inverse_times(j)  #|\label{code:wf_wiener_filter}|
    m = fft(m_k)

    # Probing the uncertainty |\label{code:wf_uncertainty_probing}|
    class Proby(ift.DiagonalProberMixin, ift.Prober): pass
Martin Reinecke's avatar
tweaks  
Martin Reinecke committed
56
    proby = Proby(signal_space, probe_count=1,ncpu=1)
Theo Steininger's avatar
Theo Steininger committed
57 58
    proby(lambda z: fft(wiener_curvature.inverse_times(fft.inverse_times(z))))  #|\label{code:wf_variance_fft_wrap}|

Martin Reinecke's avatar
Martin Reinecke committed
59
    sm = ift.FFTSmoothingOperator(signal_space, sigma=0.03)
Theo Steininger's avatar
Theo Steininger committed
60
    variance = ift.sqrt(sm(proby.diagonal.weight(-1)))  #|\label{code:wf_variance_weighting}|
61

Theo Steininger's avatar
Theo Steininger committed
62
    # Plotting #|\label{code:wf_plotting}|
Martin Reinecke's avatar
Martin Reinecke committed
63 64 65
    ift.plotting.plot(variance,name="uncertainty.pdf",xlabel='Pixel index', ylabel='Pixel index')
    ift.plotting.plot(mock_signal,name="mock_signal.pdf",xlabel='Pixel index', ylabel='Pixel index')
    ift.plotting.plot(ift.Field(signal_space, val=data.val),name="data.pdf",xlabel='Pixel index', ylabel='Pixel index')
Martin Reinecke's avatar
tweaks  
Martin Reinecke committed
66 67
    ift.plotting.plot(m,name="map.pdf",xlabel='Pixel index', ylabel='Pixel index')