コード例 #1
0
ファイル: test_field.py プロジェクト: kernsuite-debian/nifty
def test_power_synthesize_analyze(space1, space2):
    np.random.seed(11)

    p1 = ift.PowerSpace(space1)
    fp1 = ift.PS_field(p1, _spec1)
    p2 = ift.PowerSpace(space2)
    fp2 = ift.PS_field(p2, _spec2)
    outer = np.outer(fp1.to_global_data(), fp2.to_global_data())
    fp = ift.Field.from_global_data((p1, p2), outer)

    op1 = ift.create_power_operator((space1, space2), _spec1, 0)
    op2 = ift.create_power_operator((space1, space2), _spec2, 1)
    opfull = op2(op1)

    samples = 500
    sc1 = ift.StatCalculator()
    sc2 = ift.StatCalculator()
    for ii in range(samples):
        sk = opfull.draw_sample()

        sp = ift.power_analyze(sk, spaces=(0, 1), keep_phase_information=False)
        sc1.add(sp.sum(spaces=1) / fp2.sum())
        sc2.add(sp.sum(spaces=0) / fp1.sum())

    assert_allclose(sc1.mean.local_data, fp1.local_data, rtol=0.2)
    assert_allclose(sc2.mean.local_data, fp2.local_data, rtol=0.2)
コード例 #2
0
    def __init__(
            self,
            domain,
            beta,
            k,
            grid,
            power_spectrum=lambda q: 2/(q**4 + 1),
            rho=1,
            verbosity=0):
        super().__init__()
        self.beta = beta
        self._domain = (domain, )
        self.fft = nifty5.FFTOperator(self._domain)
        self.h_space = self.fft.target[0]
        self.grid = grid
        self.k = k
        self.rho = rho
        B_h = nifty5.create_power_operator(
                domain=self.h_space, power_spectrum=power_spectrum)
        self.B = nifty5.SandwichOperator.make(self.fft, B_h)
        # the diagonal operator rho*e^beta

        rho_e_beta = np.zeros(domain.shape[0])
        for i, pos in enumerate(self.grid):
            rho_e_beta[pos] = (
                    self.rho*np.exp(self.beta.val[pos]))
        rho_e_beta_field = nifty5.Field(domain=domain, val=rho_e_beta)
        self.rho_e_beta_diag = nifty5.DiagonalOperator(
                domain=self._domain,
                diagonal=rho_e_beta_field)
コード例 #3
0
    def __init__(
            self,
            N_bins=1024,
            power_spectrum_beta=lambda q: 1 / (q**4 + 1),
            power_spectrum_f=lambda q: 1 / (q**4 + 1),
            noise_var=0.1,
    ):
        """
        N_bins : int
            number of bins for the sample spaces
        p_spec_beta : function
            power spectrum for beta
        p_spec_f : function
            power spectrum for f
        noise_var : scalar
            the variance of the noise variable
        """
        self.N_bins = N_bins
        self.s_space = nifty5.RGSpace([N_bins], )
        self.h_space = self.s_space.get_default_codomain()
        self.p_space = nifty5.PowerSpace(self.h_space)

        # covariance operator for the beta distribution
        self.power_spectrum_beta = power_spectrum_beta
        B_h = nifty5.create_power_operator(
            self.h_space, power_spectrum=self.power_spectrum_beta)

        fft = nifty5.FFTOperator(self.s_space)
        self.B = nifty5.SandwichOperator.make(fft, B_h)
        self.beta = self.B.draw_sample()

        # numerical values for p(x|beta) = exp(beta(x)) / sum_z exp(beta(z))
        self.p_x_val = np.exp(np.array(self.beta.to_global_data()))
        self.p_x_val = (1 / np.sum(self.p_x_val)) * self.p_x_val

        # get the covariance operator for the f distribution
        self.power_spectrum_f = power_spectrum_f
        F_h = nifty5.create_power_operator(
            self.h_space, power_spectrum=self.power_spectrum_f)
        self.F = nifty5.SandwichOperator.make(fft, F_h)

        # sample the transformation function f
        self.f = self.F.draw_sample()
        self.f_val = np.array(self.f.to_global_data())

        # set the noise variance
        self.noise_var = noise_var
コード例 #4
0
    def __init__(
        self,
        position,
        k,
        x,
        y,
        x_indices,
        s_space,
        h_space,
        p_space,
        grid,
        sigma_f=1,
        noise_variance=0.01,
        Lambda_modes_list=None,
        term_factors=[1] * 11,
    ):

        super().__init__(position=position)
        self.domain = position.domain
        self.k, self.x, self.y = k, x, y
        self.x_indices = x_indices
        self.N = len(self.x)
        self.sigma_f = sigma_f
        self.noise_variance = noise_variance
        self.tau_f = position
        self.s_space = s_space
        self.h_space = h_space
        self.p_space = p_space
        self.len_p_space = self.p_space.shape[0]
        self.grid = grid

        self.fft = nifty5.FFTOperator(domain=self.s_space, target=self.h_space)
        self.F_h = nifty5.create_power_operator(domain=self.h_space,
                                                power_spectrum=nifty5.exp(
                                                    self.tau_f))
        self.F = nifty5.SandwichOperator.make(self.fft, self.F_h)
        self.F_matrix = probe_operator(self.F)
        self.F_tilde = np.zeros((self.N, self.N))
        for i in range(self.N):
            for j in range(i + 1):
                self.F_tilde[i, j] = self.F_matrix[x_indices[i], x_indices[j]]
                if i != j:
                    self.F_tilde[j, i] = self.F_matrix[x_indices[i],
                                                       x_indices[j]]

        self.noise_covariance = np.diag(
            [self.noise_variance for i in range(self.N)])
        self.G = np.linalg.inv(self.F_tilde + self.noise_covariance)

        self.smoothness_operator_f = nifty5.SmoothnessOperator(
            domain=self.p_space, logarithmic=True, strength=1 / self.sigma_f)

        if Lambda_modes_list is None:
            self.get_Lambda_modes(speedup=True)
        else:
            self.Lambda_modes_list = Lambda_modes_list
        self.term_factors = term_factors
        self.get_Lambdas()
コード例 #5
0
ファイル: filters.py プロジェクト: LBJ-Wade/SZ-Filtering
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)
コード例 #6
0
    def get_Lambda_modes(self, speedup=False):
        # this is an array of matrices:
        #   Lambda_mode(z)_ij = (1/2pi) (cos(z(x_i-x_j)) + sin(z(x_i + x_j)) )
        self.Lambda_modes_list = list()
        if not False:
            # conventional way
            for k in range(self.len_p_space):
                p_spec = np.zeros(self.len_p_space)
                p_spec[k] = np.exp(self.tau_f.val[k])
                p_spec[k] = 1
                Lambda_h = nifty5.create_power_operator(
                    domain=self.h_space,
                    power_spectrum=nifty5.Field(domain=self.p_space,
                                                val=p_spec))
                Lambda_kernel = nifty5.SandwichOperator.make(
                    self.fft, Lambda_h)
                Lambda_kernel_matrix = probe_operator(Lambda_kernel)
                Lambda_modes = np.zeros((self.N, self.N))
                for i in range(self.N):
                    for j in range(i + 1):
                        Lambda_modes[i, j] = Lambda_kernel_matrix[
                            self.x_indices[i], self.x_indices[j]]
                        if i != j:
                            Lambda_modes[j, i] = Lambda_kernel_matrix[
                                self.x_indices[i], self.x_indices[j]]
                self.Lambda_modes_list.append(Lambda_modes)

        else:
            # based on the thought that we are just dealing with single
            #   fourier modes, we can calculate these directly
            Lambda_modes = np.zeros((self.N, self.N))
            for i in range(self.N):
                for j in range(i + 1):
                    x_i_index = self.x_indices[i]
                    x_j_index = self.x_indices[j]
                    a = (self.grid_coordinates[x_i_index] -
                         self.grid_coordinates[x_j_index])
                    # dirty hack, if we take cos((x-y)*2*pi) we get the
                    #   desired result
                    Lambda_modes[i, j] = np.cos(a * 2 * np.pi)
                    if i != j:
                        Lambda_modes[j, i] = np.cos(a * 2 * np.pi)
            # use the relation cos(nx) = T_n(cos(x)) where T_n is the nth
            #   chebyhsev polynomial
            for i, z in enumerate(self.p_space[0].k_lengths):
                factor = 2 * self.len_s_space**(-2)
                if i == 0:
                    factor = factor / 2
                if i == (self.len_p_space - 1):
                    factor = factor / 2
                z = int(z)
                self.Lambda_modes_list.append(
                    factor *
                    np.polynomial.Chebyshev(coef=[0] * z + [1])(Lambda_modes))
コード例 #7
0
ファイル: test_field.py プロジェクト: kernsuite-debian/nifty
def test_DiagonalOperator_power_analyze2(space1, space2):
    np.random.seed(11)

    fp1 = ift.PS_field(ift.PowerSpace(space1), _spec1)
    fp2 = ift.PS_field(ift.PowerSpace(space2), _spec2)

    S_1 = ift.create_power_operator((space1, space2), _spec1, 0)
    S_2 = ift.create_power_operator((space1, space2), _spec2, 1)
    S_full = S_2(S_1)

    samples = 500
    sc1 = ift.StatCalculator()
    sc2 = ift.StatCalculator()

    for ii in range(samples):
        sk = S_full.draw_sample()
        sp = ift.power_analyze(sk, spaces=(0, 1), keep_phase_information=False)
        sc1.add(sp.sum(spaces=1) / fp2.sum())
        sc2.add(sp.sum(spaces=0) / fp1.sum())

    assert_allclose(sc1.mean.local_data, fp1.local_data, rtol=0.2)
    assert_allclose(sc2.mean.local_data, fp2.local_data, rtol=0.2)
コード例 #8
0
    def __init__(
            self,
            position,
            k,
            grid,
            sigma_beta=1,
            rho=1,
            mode='multifield',
            single_fields=None,
            term_factors=[1]*5,
            ):

        super().__init__(position=position)
        self.domain = position.domain
        self.k = k
        self.sigma_beta = sigma_beta
        self.rho = rho
        self.mode = mode
        self.fields = single_fields
        self.term_factors = term_factors
        if mode == 'multifield':
            self.beta = position.val['beta']
            self.tau_beta = position.val['tau_beta']
        else:
            self.fields[mode] = position
            self.beta = self.fields['beta']
            self.tau_beta = self.fields['tau_beta']
        self.s_space = self.beta.domain[0]
        self.h_space = self.s_space.get_default_codomain()
        self.p_space = self.tau_beta.domain
        self.len_p_space = self.p_space.shape[0]
        self.len_s_space = self.s_space.shape[0]
        self.grid = grid
        self.grid_coordinates = [i*self.s_space.distances[0] for i in range(
            self.s_space.shape[0])]
        # beta_vector is the R^N_bins vector with beta field values at the grid
        #   positions
        self.beta_vector = np.array([self.beta.val[i] for i in self.grid])

        self.fft = nifty5.FFTOperator(domain=self.s_space, target=self.h_space)
        self.B_inv_h = nifty5.create_power_operator(
            domain=self.h_space,
            power_spectrum=nifty5.exp(-self.tau_beta))
        self.B_inv = nifty5.SandwichOperator.make(self.fft, self.B_inv_h)

        self.smoothness_operator_beta = nifty5.SmoothnessOperator(
                domain=self.p_space,
                strength=1/self.sigma_beta)

        self.get_del_B()
コード例 #9
0
    def get_del_B(self):
        self.del_B_inv_list = list()
        for i in range(self.len_p_space):
            p_spec = np.zeros(self.len_p_space)
            p_spec[i] = np.exp(-self.tau_beta.val[i])

            del_B_inv_h = nifty5.create_power_operator(
                    domain=self.h_space,
                    power_spectrum=nifty5.Field(
                        domain=self.p_space,
                        val=p_spec))

            del_B_inv = nifty5.SandwichOperator.make(
                    self.fft, del_B_inv_h)

            self.del_B_inv_list.append(del_B_inv)
コード例 #10
0
    def __init__(
            self,
            position,
            k,
            s_space,
            power_spectrum_beta,
            rho,
            ):
        super().__init__(position=position)
        self.k = k
        self.rho = rho
        self.power_spectrum_beta = power_spectrum_beta
        self.N = k.sum()
        self.beta = position
        self.beta_arr = position.to_global_data()
        self.s_space = s_space
        self.fft = nifty5.FFTOperator(self.s_space)
        self.h_space = self.fft.target[0]

        # B in Fourier space:
        self.B_h = nifty5.create_power_operator(
                domain=self.h_space,
                power_spectrum=power_spectrum_beta)
        self.B = nifty5.SandwichOperator.make(self.fft, self.B_h)
コード例 #11
0
prior_spectrum = lambda k: 1 / (10. + k**2.5)
data, ground_truth = generate_wf_data(position_space, prior_spectrum)

R = ift.GeometryRemover(position_space)
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
コード例 #12
0
ファイル: filters.py プロジェクト: LBJ-Wade/SZ-Filtering
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
コード例 #13
0
    def __init__(
            self,
            position,
            k,
            x,
            y,
            grid,
            sigma_f=1,
            sigma_beta=1,
            sigma_eta=1,
            rho=1,
            mode='multifield',
            single_fields=None,
            Lambda_modes_list=None,
            term_factors=[1]*11,
            ):

        super().__init__(position=position)
        self.domain = position.domain
        self.k, self.x, self.y = k, x, y
        self.N = len(self.x)
        self.sigma_beta = sigma_beta
        self.sigma_f = sigma_f
        self.sigma_eta = sigma_eta
        self.rho = rho
        self.mode = mode
        self.fields = single_fields
        if mode == 'multifield':
            self.beta = position.val['beta']
            self.tau_beta = position.val['tau_beta']
            self.tau_f = position.val['tau_f']
            self.eta = position.val['eta']
        else:
            self.fields[mode] = position
            self.beta = self.fields['beta']
            self.tau_beta = self.fields['tau_beta']
            self.tau_f = self.fields['tau_f']
            self.eta = self.fields['eta']
        self.s_space = self.beta.domain[0]
        self.h_space = self.s_space.get_default_codomain()
        self.p_space = self.tau_f.domain
        self.len_p_space = self.p_space.shape[0]
        self.len_s_space = self.s_space.shape[0]
        self.grid = grid
        self.grid_coordinates = [i*self.s_space.distances[0] for i in range(
            self.s_space.shape[0])]
        # beta_vector is the R^N_bins vector with beta field values at the grid
        #   positions
        self.beta_vector = np.array([self.beta.val[i] for i in self.grid])

        self.fft = nifty5.FFTOperator(domain=self.s_space, target=self.h_space)
        self.F_h = nifty5.create_power_operator(
            domain=self.h_space,
            power_spectrum=nifty5.exp(self.tau_f))
        self.B_inv_h = nifty5.create_power_operator(
            domain=self.h_space,
            power_spectrum=nifty5.exp(-self.tau_beta))
        self.B_inv = nifty5.SandwichOperator.make(self.fft, self.B_inv_h)
        self.F = nifty5.SandwichOperator.make(self.fft, self.F_h)
        self.F_matrix = probe_operator(self.F)
        self.F_tilde = np.zeros((self.N, self.N))
        for i in range(self.N):
            for j in range(i+1):
                self.F_tilde[i, j] = self.F_matrix[
                        self.x_indices[i], self.x_indices[j]]
                if i != j:
                    self.F_tilde[j, i] = self.F_matrix[
                            self.x_indices[i], self.x_indices[j]]

        self.exp_hat_eta_x = np.diag([np.exp(
            self.eta.val[self.x_indices[i]]) for i in range(self.N)])
        self.G = np.linalg.inv(self.F_tilde + self.exp_hat_eta_x)

        self.smoothness_operator_f = nifty5.SmoothnessOperator(
                domain=self.p_space,
                strength=1/self.sigma_f)
        self.smoothness_operator_beta = nifty5.SmoothnessOperator(
                domain=self.p_space,
                strength=1/self.sigma_beta)

        # second derivative of eta
        nabla_nabla_eta = np.zeros(self.eta.val.shape)
        scipy.ndimage.laplace(
                input=self.eta.val,
                output=nabla_nabla_eta)
        self.nabla_nabla_eta_field = nifty5.Field(
                domain=self.s_space,
                val=nabla_nabla_eta)
        if Lambda_modes_list is None:
            self.get_Lambda_modes(speedup=True)
        else:
            self.Lambda_modes_list = Lambda_modes_list
        self.term_factors = term_factors

        self.get_Lambdas()
        self.get_del_B()
        self.get_del_exp_eta_x()
コード例 #14
0
        position_space = ift.RGSpace([512, 512])
    else:
        # Sphere
        position_space = ift.HPSpace(128)

    # Define harmonic space and transform
    harmonic_space = position_space.get_default_codomain()
    HT = ift.HarmonicTransformOperator(harmonic_space, position_space)

    position = ift.from_random('normal', harmonic_space)

    # Define power spectrum and amplitudes
    def sqrtpspec(k):
        return 1. / (20. + k**2)

    A = ift.create_power_operator(harmonic_space, sqrtpspec)

    # Set up a sky operator and instrumental response
    sky = ift.sigmoid(HT(A))
    GR = ift.GeometryRemover(position_space)
    R = GR

    # Generate mock data
    p = R(sky)
    mock_position = ift.from_random('normal', harmonic_space)
    tmp = p(mock_position).to_global_data().astype(np.float64)
    data = np.random.binomial(1, tmp)
    data = ift.Field.from_global_data(R.target, data)

    # Compute likelihood and Hamiltonian
    position = ift.from_random('normal', harmonic_space)