def test_simplification():
    from nifty5.operators.operator import _ConstantOperator
    f1 = ift.Field.full(ift.RGSpace(10), 2.)
    op = ift.FFTOperator(f1.domain)
    _, op2 = op.simplify_for_constant_input(f1)
    assert_equal(isinstance(op2, _ConstantOperator), True)
    assert_allclose(op(f1).local_data, op2(f1).local_data)

    dom = {"a": ift.RGSpace(10)}
    f1 = ift.full(dom, 2.)
    op = ift.FFTOperator(f1.domain["a"]).ducktape("a")
    _, op2 = op.simplify_for_constant_input(f1)
    assert_equal(isinstance(op2, _ConstantOperator), True)
    assert_allclose(op(f1).local_data, op2(f1).local_data)

    dom = {"a": ift.RGSpace(10), "b": ift.RGSpace(5)}
    f1 = ift.full(dom, 2.)
    pdom = {"a": ift.RGSpace(10)}
    f2 = ift.full(pdom, 2.)
    o1 = ift.FFTOperator(f1.domain["a"])
    o2 = ift.FFTOperator(f1.domain["b"])
    op = (o1.ducktape("a").ducktape_left("a") +
          o2.ducktape("b").ducktape_left("b"))
    _, op2 = op.simplify_for_constant_input(f2)
    assert_equal(isinstance(op2._op1, _ConstantOperator), True)
    assert_allclose(op(f1)["a"].local_data, op2(f1)["a"].local_data)
    assert_allclose(op(f1)["b"].local_data, op2(f1)["b"].local_data)
    lin = ift.Linearization.make_var(ift.MultiField.full(op2.domain, 2.), True)
    assert_allclose(op(lin).val["a"].local_data, op2(lin).val["a"].local_data)
    assert_allclose(op(lin).val["b"].local_data, op2(lin).val["b"].local_data)
示例#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 test_fft2D(dim1, dim2, d1, d2, dtype, op, fftw):
    if fftw:
        ift.fft.enable_fftw()
    tol = _get_rtol(dtype)
    a = ift.RGSpace([dim1, dim2], distances=[d1, d2])
    b = ift.RGSpace([dim1, dim2],
                    distances=[1. / (dim1 * d1), 1. / (dim2 * d2)],
                    harmonic=True)

    fft = op(domain=a, target=b)
    inp = ift.Field.from_random(domain=a,
                                random_type='normal',
                                std=7,
                                mean=3,
                                dtype=dtype)
    out = fft.inverse_times(fft.times(inp))
    assert_allclose(inp.local_data, out.local_data, rtol=tol, atol=tol)

    a, b = b, a

    fft = ift.FFTOperator(domain=a, target=b)
    inp = ift.Field.from_random(domain=a,
                                random_type='normal',
                                std=7,
                                mean=3,
                                dtype=dtype)
    out = fft.inverse_times(fft.times(inp))
    assert_allclose(inp.local_data, out.local_data, rtol=tol, atol=tol)
    ift.fft.disable_fftw()
示例#4
0
def test_fft1D(d, dtype, op, fftw):
    if fftw:
        ift.fft.enable_fftw()
    dim1 = 16
    tol = _get_rtol(dtype)
    a = ift.RGSpace(dim1, distances=d)
    b = ift.RGSpace(dim1, distances=1. / (dim1 * d), harmonic=True)
    np.random.seed(16)

    fft = op(domain=a, target=b)
    inp = ift.Field.from_random(domain=a,
                                random_type='normal',
                                std=7,
                                mean=3,
                                dtype=dtype)
    out = fft.inverse_times(fft.times(inp))
    assert_allclose(inp.local_data, out.local_data, rtol=tol, atol=tol)

    a, b = b, a

    fft = ift.FFTOperator(domain=a, target=b)
    inp = ift.Field.from_random(domain=a,
                                random_type='normal',
                                std=7,
                                mean=3,
                                dtype=dtype)
    out = fft.inverse_times(fft.times(inp))
    assert_allclose(inp.local_data, out.local_data, rtol=tol, atol=tol)
    ift.fft.disable_fftw()
示例#5
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()
    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()
 def __init__(
         self,
         domain,
         beta,
         B,
         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.rho = rho
     self.B = B
示例#8
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
示例#9
0
def testBinary(type1, type2, space, seed):
    dom1 = ift.MultiDomain.make({'s1': space})
    dom2 = ift.MultiDomain.make({'s2': space})

    # FIXME Remove this?
    _make_linearization(type1, dom1, seed)
    _make_linearization(type2, dom2, seed)

    dom = ift.MultiDomain.union((dom1, dom2))
    select_s1 = ift.ducktape(None, dom1, "s1")
    select_s2 = ift.ducktape(None, dom2, "s2")
    model = select_s1 * select_s2
    pos = ift.from_random("normal", dom)
    ift.extra.check_jacobian_consistency(model, pos, ntries=20)
    model = select_s1 + select_s2
    pos = ift.from_random("normal", dom)
    ift.extra.check_jacobian_consistency(model, pos, ntries=20)
    model = select_s1.scale(3.)
    pos = ift.from_random("normal", dom1)
    ift.extra.check_jacobian_consistency(model, pos, ntries=20)
    model = ift.ScalingOperator(2.456, space)(select_s1 * select_s2)
    pos = ift.from_random("normal", dom)
    ift.extra.check_jacobian_consistency(model, pos, ntries=20)
    model = ift.sigmoid(2.456 * (select_s1 * select_s2))
    pos = ift.from_random("normal", dom)
    ift.extra.check_jacobian_consistency(model, pos, ntries=20)
    pos = ift.from_random("normal", dom)
    model = ift.OuterProduct(pos['s1'], ift.makeDomain(space))
    ift.extra.check_jacobian_consistency(model, pos['s2'], ntries=20)
    model = select_s1**2
    pos = ift.from_random("normal", dom1)
    ift.extra.check_jacobian_consistency(model, pos, ntries=20)
    model = select_s1.clip(-1, 1)
    pos = ift.from_random("normal", dom1)
    ift.extra.check_jacobian_consistency(model, pos, ntries=20)
    f = ift.from_random("normal", space)
    model = select_s1.clip(f - 0.1, f + 1.)
    pos = ift.from_random("normal", dom1)
    ift.extra.check_jacobian_consistency(model, pos, ntries=20)
    if isinstance(space, ift.RGSpace):
        model = ift.FFTOperator(space)(select_s1 * select_s2)
        pos = ift.from_random("normal", dom)
        ift.extra.check_jacobian_consistency(model, pos, ntries=20)
示例#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
def testFFT(sp, dtype):
    _check_repr(ift.FFTOperator(sp))
    _check_repr(ift.FFTOperator(sp.get_default_codomain()))
示例#12
0
def testFFT(sp, dtype):
    op = ift.FFTOperator(sp)
    ift.extra.consistency_check(op, dtype, dtype)
    op = ift.FFTOperator(sp.get_default_codomain())
    ift.extra.consistency_check(op, dtype, dtype)
示例#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()