Exemplo n.º 1
0
 def curvature(self):
     iteration_controller = nifty5.GradientNormController(
             iteration_limit=300,
             tol_abs_gradnorm=1e-3,
             name=None)
     return nifty5.InversionEnabler(
         BetaCurvature(
                 domain=self.s_space,
                 beta=self.beta,
                 B=self.B,
                 rho=self.rho),
             iteration_controller=iteration_controller)
Exemplo n.º 2
0
        def metric(self):
            class RBCurv(ift.EndomorphicOperator):
                def __init__(self, loc):
                    self._loc = loc.to_global_data_rw()
                    self._capability = self.TIMES
                    self._domain = space

                def apply(self, x, mode):
                    self._check_input(x, mode)
                    inp = x.to_global_data_rw()
                    out = ift.Field.from_global_data(
                        space, rosen_hess_prod(self._loc.copy(), inp))
                    return out

            t1 = ift.GradientNormController(
                tol_abs_gradnorm=1e-5, iteration_limit=1000)
            return ift.InversionEnabler(RBCurv(self._position), t1)
Exemplo n.º 3
0
def wf_test(signal, noise, signal_boost, npix = 400):
    
    pixel_space = ift.RGSpace([npix, npix]) 
    fourier_space = pixel_space.get_default_codomain()

    signal_field = ift.Field.from_global_data(pixel_space, signal.astype(float))
    
    HT = ift.HartleyOperator(fourier_space, target=pixel_space) 
    power_field = ift.power_analyze(HT.inverse(signal_field), binbounds=ift.PowerSpace.useful_binbounds(fourier_space, True))

    Sh = ift.create_power_operator(fourier_space, power_spectrum=power_field) 
    R = HT
 
    noise_field = ift.Field.from_global_data(pixel_space, noise.astype(float))
    noise_power_field = ift.power_analyze(HT.inverse(noise_field), binbounds=ift.PowerSpace.useful_binbounds(fourier_space, True))

    N = ift.create_power_operator(HT.domain, noise_power_field)
    N_inverse = HT@[email protected]
    
    amplify = len(signal_boost)
    
    s_data = np.zeros((amplify, npix, npix))
    m_data = np.zeros((amplify, npix, npix))
    d_data = np.zeros((amplify, npix, npix))

    for i in np.arange(amplify):
        
        data = noise_field 

        # Wiener filtering the data

        j = (R.adjoint @N_inverse.inverse)(data)
        D_inv = R.adjoint @ N_inverse.inverse @ R + Sh.inverse

        IC = ift.GradientNormController(iteration_limit=500, tol_abs_gradnorm=1e-3)
        D = ift.InversionEnabler(D_inv, IC, approximation=Sh.inverse).inverse
        m = D(j)

        s_data[i,:,:] = (signal_field * signal_boost[i]).to_global_data()
        m_data[i,:,:] = HT(m).to_global_data()
        d_data[i,:,:] = data.to_global_data()

    return (s_data, m_data, d_data)
Exemplo n.º 4
0
    # Set the noise covariance N
    noise = 5.
    N = ift.ScalingOperator(noise, data_space)

    # Create mock data
    MOCK_SIGNAL = S.draw_sample()
    MOCK_NOISE = N.draw_sample()
    data = R(MOCK_SIGNAL) + MOCK_NOISE

    # Build inverse propagator D and information source j
    D_inv = R.adjoint @ N.inverse @ R + S.inverse
    j = R.adjoint_times(N.inverse_times(data))
    # Make D_inv invertible (via Conjugate Gradient)
    IC = ift.GradientNormController(iteration_limit=500, tol_abs_gradnorm=1e-3)
    D = ift.InversionEnabler(D_inv, IC, approximation=S.inverse).inverse

    # Calculate WIENER FILTER solution
    m = D(j)

    # Plotting
    rg = isinstance(position_space, ift.RGSpace)
    plot = ift.Plot()
    filename = "getting_started_1_mode_{}.png".format(mode)
    if rg and len(position_space.shape) == 1:
        plot.add([HT(MOCK_SIGNAL), GR.adjoint(data),
                  HT(m)],
                 label=['Mock signal', 'Data', 'Reconstruction'],
                 alpha=[1, .3, 1])
        plot.add(mask_to_nan(mask, HT(m - MOCK_SIGNAL)), title='Residuals')
        plot.output(nx=2, ny=1, xsize=10, ysize=4, name=filename)
Exemplo n.º 5
0
data_space = R.target
data = ift.from_global_data(data_space, data)

ground_truth = ift.from_global_data(position_space, ground_truth)
plot_WF('data', ground_truth, data)

N = ift.ScalingOperator(0.1, data_space)

harmonic_space = position_space.get_default_codomain()
HT = ift.HartleyOperator(harmonic_space, target=position_space)

S_h = ift.create_power_operator(harmonic_space, prior_spectrum)
S = HT @ S_h @ HT.adjoint

D_inv = S.inverse + R.adjoint @ N.inverse @ R
j = (R.adjoint @ N.inverse)(data)

IC = ift.GradientNormController(iteration_limit=100, tol_abs_gradnorm=1e-7)
D = ift.InversionEnabler(D_inv.inverse, IC, approximation=S)

m = D(j)

plot_WF('result', ground_truth, data, m)

S = ift.SandwichOperator.make(HT.adjoint, S_h)
D = ift.WienerFilterCurvature(R, N, S, IC, IC).inverse
N_samples = 10
samples = [D.draw_sample() + m for i in range(N_samples)]

plot_WF('result', ground_truth, data, m=m, samples=samples)
Exemplo n.º 6
0
def nifty_wf(signal, noise, y_map, npix = 400, pxsize = 1.5, kernel = 9.68, n = 10, smooth = False):
    
    cmb_mocks = noise.shape[0]
    
    A = (2*np.sqrt(2*np.log(2)))
    
    if smooth is True:
        signal_smooth = np.zeros((cmb_mocks, npix, npix))
        noise_smooth = np.zeros((cmb_mocks, npix, npix))
        
        for i in np.arange(cmb_mocks):
            noise_data = ndimage.gaussian_filter(noise[i], sigma= kernel/A/pxsize, order=0, mode = "reflect", truncate = 10)
            #signal_data = ndimage.gaussian_filter(signal[i], sigma= kernel/A/pxsize, order=0, mode = "reflect", truncate = 10)
            signal_data = signal[i] #uncomment here if smoothing signal and noise
            noise_smooth[i,:,:] = noise_data
            signal_smooth[i,:,:] = signal_data
    else:
        noise_smooth = noise
        signal_smooth = signal
                
    pixel_space = ift.RGSpace([npix, npix]) 
    fourier_space = pixel_space.get_default_codomain()

    s_data = np.zeros((cmb_mocks, npix, npix))
    m_data = np.zeros((cmb_mocks, npix, npix))
    d_data = np.zeros((cmb_mocks, npix, npix))


    for i in np.arange(cmb_mocks):
        
        signal_field = ift.Field.from_global_data(pixel_space, signal_smooth.astype(float)) #[i] for mock_data
        HT = ift.HartleyOperator(fourier_space, target=pixel_space) 
        power_field = ift.power_analyze(HT.inverse(signal_field), binbounds=ift.PowerSpace.useful_binbounds(fourier_space, True))
        Sh = ift.create_power_operator(fourier_space, power_spectrum=power_field) 
        R = HT
           
        noise_field = ift.Field.from_global_data(pixel_space, noise_smooth[i].astype(float))
        noise_power_field = ift.power_analyze(HT.inverse(noise_field), binbounds=ift.PowerSpace.useful_binbounds(fourier_space, True))

        N = ift.create_power_operator(HT.domain, noise_power_field)
        N_inverse = HT@[email protected]

        data = signal_field + noise_field # --->when using mock_data

        # Wiener filtering the data

        j = (R.adjoint @N_inverse.inverse)(data)
        D_inv = R.adjoint @ N_inverse.inverse @ R + Sh.inverse

        IC = ift.GradientNormController(iteration_limit=500, tol_abs_gradnorm=1e-3)
        D = ift.InversionEnabler(D_inv, IC, approximation=Sh.inverse).inverse
        m = D(j)

        #s_data[i,:,:] = (signal_field).to_global_data()
        m_data[i,:,:] = HT(m).to_global_data()
        #d_data[i,:,:] = data.to_global_data()    
    
    #Squaring the filtered map and also taking the absoute val of filtered map
       
    
    # uncomment here for no cross correlation 
    squared_m_data = np.zeros((cmb_mocks, npix, npix))
    abs_m_data = np.zeros((cmb_mocks, npix, npix))
    
    for i in np.arange(m_data.shape[0]):
        squared_m_data[i,:,:]  = m_data[i,:,:] * m_data[i,:,:]
        abs_m_data[i,:,:] = np.abs(m_data[i,:,:])
    
    #Stacking all filtered maps
    stack1  = np.sum(squared_m_data, axis = 0)/m_data.shape[0]
    stack2  = np.sum(abs_m_data, axis = 0)/m_data.shape[0]
       
    return (m_data, squared_m_data, abs_m_data, stack1, stack2) #change here to return the right values ---->, stack_square, stack_abs