Пример #1
0
def heat_finite_elt_2D_tensor2(x0, t_final, t_step, h, g):
    """

    :param x0: vector x (at t = 0)
    :param t_final: time
    :param t_step: time step (must satisfy the CFL max(lambda) < 2)
    :param h:
    :param g: metric tensor
    :return: vector x (at t = t_final)

    """
    import clinica.pipelines.machine_learning_spatial_svm.spatial_svm_utils as utils
    import numpy as np

    # parameters
    nb_step = np.ceil(t_final / t_step)  # number of time step
    t_step = t_final / nb_step

    # tensors
    detg = utils.tensor_determinant(g)
    detg = np.sqrt(detg)
    ginv = utils.tensor_inverse(g)
    ginv = utils.tensor_scalar_product(detg, ginv)
    detg2 = detg[1:-1, 1:-1]

    # LOOP
    x = x0
    for i in range(nb_step):
        m = t_step / h / h
        x = np.sum(x, -(
            utils.tensor_scalar_product(m,
                                        np.divide(utils.tensor_scalar_product(h, utils.operateur(x, ginv, detg)), detg2, dtype=object))))

    return x
Пример #2
0
def largest_eigenvalue_heat_3D_tensor2(g, h, epsilon):
    """

    :param g: metric tensor
    :param h: space step
    :param epsilon: stop criterion
    :return: lamba = the largest eigenvalues

    """
    import clinica.pipelines.machine_learning_spatial_svm.spatial_svm_utils as utils

    import numpy as np
    import cmath

    # parameters
    if epsilon is None:
        epsilon = 1e-6
    erreur = 1 + epsilon

    # tensors

    detg = utils.tensor_determinant(g)
    detg = np.array(detg, dtype=np.complex128)  # complex tensor
    detg = np.sqrt(detg)
    detg = detg[0]
    ginv = utils.tensor_inverse(g)

    if len(ginv.shape) == 6:
        ginv = ginv[:, :, 0, :, :, :]

    ginv = utils.tensor_scalar_product(detg, ginv)
    detg2 = detg[1:-1, 1:-1, 1:-1]  # 141*121*141
    detg2[detg2 != detg2] = 0
    detg[detg != detg] = 0
    ginv[ginv != ginv] = 0

    # initialisation

    s = [g[0][0].shape[0] - 2, g[0][0].shape[1] - 2, g[0][0].shape[2] - 2]
    b1 = np.ones([s[0], s[1], s[2]])

    b1 = np.divide(b1, np.array(cmath.sqrt(np.dot(b1.flatten('F').transpose(), b1.flatten('F'))), dtype=np.complex128))

    print("Computation of the largest eigenvalue ...")
    while erreur > epsilon:
        b0 = b1
        b2 = np.array(np.divide(np.array(utils.operateur(b1, ginv, detg)) * h, detg2) / h / h / h, dtype=np.complex128)
        b1 = np.divide(b2, np.array(cmath.sqrt(np.dot(b2.flatten('F').transpose(), b2.flatten('F')))),
                       dtype=np.complex128)

        erreur = np.linalg.norm(b1.flatten('F') - b0.flatten('F'))

    print("done")

    lam = (cmath.sqrt(np.dot(b2.flatten('F').transpose(), b2.flatten('F'))))

    return lam
Пример #3
0
def heat_finite_elt_3D_tensor2(x0, t_final, t_step, h, g):
    """

    :param x0: vector x (at t = 0)
    :param t_final: time
    :param t_step: time step (must satisfy the CFL max(lambda) < 2)
    :param h:
    :param g: metric tensor
    :return: vector x (at t = t_final)

    """
    import numpy as np

    import clinica.pipelines.machine_learning_spatial_svm.spatial_svm_utils as utils

    if len(x0.shape) == 4:
        x0 = x0[0, :, :, :]

    # parameters
    nb_step = np.ceil(t_final / t_step)  # number of time step
    nb_step = nb_step.astype(int)
    t_step = t_final / nb_step

    # tensors
    detg = utils.tensor_determinant(g)
    detg = np.sqrt(detg)
    ginv = utils.tensor_inverse(g)
    ginv = utils.tensor_scalar_product(detg, ginv)
    detg2 = detg[:, 1:-1, 1:-1, 1:-1]
    if len(ginv.shape) == 6:
        ginv = ginv[:, :, 0, :, :, :]
    if len(detg.shape) == 4:
        detg = detg[0, :, :, :]
    ginv = np.array(ginv.real, dtype="float64")
    detg = np.array(detg.real, dtype="float64")
    detg2 = np.array(detg2.real, dtype="float64")

    # LOOP
    x = x0
    for i in range(nb_step):
        x = np.array(
            x
            - t_step
            * (np.divide(np.array(utils.operateur(x, ginv, detg)) * h, detg2))
            / h
            / h
            / h
        )

    return x