How to use the clinica.pipelines.machine_learning_spatial_svm.spatial_svm_utils function in clinica

To help you get started, we’ve selected a few clinica examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github aramis-lab / clinica / clinica / pipelines / machine_learning_spatial_svm / spatial_svm_utils.py View on Github external
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[np.isnan(detg2)] = 0
    detg[np.isnan(detg)] = 0
    ginv[np.isnan(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)
github aramis-lab / clinica / clinica / pipelines / machine_learning_spatial_svm / spatial_svm_utils.py View on Github external
def tensor_inverse(g):
    """

    :param g: tensor
    :return: inverse of the tensor
    """
    import clinica.pipelines.machine_learning_spatial_svm.spatial_svm_utils as utils
    import numpy as np

    h = utils.tensor_transpose(utils.tensor_commatrix(g))
    detg = utils.tensor_determinant(g)

    h = h * (1 / (detg))
    mask = np.isnan(h)
    h[mask] = 0
    return h
github aramis-lab / clinica / clinica / pipelines / machine_learning_spatial_svm / spatial_svm_utils.py View on Github external
"""

    :param x:
    :param ginv:
    :param detg:
    :return:
    """
    import clinica.pipelines.machine_learning_spatial_svm.spatial_svm_utils as utils
    import numpy as np

    if len(x.shape) == 4:
        x = x[0, :, :, :]
    y = np.zeros([x.shape[0] + 2, x.shape[1] + 2, x.shape[2] + 2])
    y = np.array(y, dtype=np.complex_)
    y[1:-1, 1:-1, 1:-1] = x
    y = utils.tensor_helmholtz(y, ginv, detg, 0)

    return y
github aramis-lab / clinica / clinica / pipelines / machine_learning_spatial_svm / spatial_svm_utils.py View on Github external
for i in range(s[0]):

            if np.mod(i, 2) == 0:
                epsilon = 1
            else:
                epsilon = -1

            if i == 0:
                g1 = [[g[1][1], g[1][2]], [g[2][1], g[2][2]]]
            elif i == 1:
                g1 = [[g[0][1], g[0][2]], [g[2][1], g[2][2]]]
            else:
                g1 = [[g[0][1], g[0][2]], [g[1][1], g[1][2]]]
            # it's a recursive function
            prod = epsilon * g[i][0] * utils.tensor_determinant(g1)
            d = d + prod

    elif s[0] == 2:
        # if the tensor is 2*2
        for i in range(s[0]):
            if np.mod(i, 2) == 0:
                epsilon = 1
            else:
                epsilon = -1
            if i == 0:
                g1 = [g[1][1]]
            elif i == 1:
                g1 = [g[0][1]]
            prod = epsilon * g[i][0] * utils.tensor_determinant(g1)
            d = d + prod
github aramis-lab / clinica / clinica / pipelines / machine_learning_spatial_svm / spatial_svm_utils.py View on Github external
rts = np.array([rts1, rts2])

    elif C.shape[0] < 5:
        # implementation of the method of Cardan

        a = C[0, :]
        b = C[1, :]
        c = C[2, :]
        d = C[3, :]

        p = - b * b * (1 / (3 * a * a)) + c * (1 / a)
        q = b * (1 / (27. * a)) * (2 * b * b * (1 / (a * a)) - 9 * c * (1 / a)) + d * (1 / a)

        new_roots = np.array([np.ones((q.shape[0])), q, - (p * p * p) / 27])

        rts = utils.roots_poly(new_roots)

        u = rts[0, :]
        u_mod = abs(u) ** (1 / 3)
        u_angle = np.angle(u) * (1 / 3)

        v = rts[1, :]
        v_mod = abs(v) ** (1 / 3)
        v_angle = np.angle(v) * (1 / 3)

        rts = np.zeros([C.shape[1], 3], dtype='complex128')

        ind = np.zeros([u.shape[0]], dtype='int32')

        for k in [0, 1, 2]:
            u = u_mod * np.power(math.e, 1j * (u_angle + k * 2 * math.pi * (1 / 3)))
            u = np.array(u)
github aramis-lab / clinica / clinica / pipelines / machine_learning_spatial_svm / spatial_svm_utils.py View on Github external
import clinica.pipelines.machine_learning_spatial_svm.spatial_svm_utils as utils

    import numpy as np
    g = np.array(g)
    g_com = []

    for i in range(g.shape[0]):
        for j in range(g.shape[0]):
            if np.mod(i + j, 2) == 0:
                epsilon = 1
            else:
                epsilon = -1
            if i == 0:
                if j == 0:
                    n = [[g[1][1], g[1][2]], [g[2][1], g[2][2]]]
                    a0 = epsilon * utils.tensor_determinant(n)
                elif j == 1:
                    n = [[g[1][0], g[1][2]], [g[2][0], g[2][2]]]
                    a1 = epsilon * utils.tensor_determinant(n)
                else:
                    n = [[g[1][0], g[1][1]], [g[2][0], g[2][1]]]
                    a2 = epsilon * utils.tensor_determinant(n)
            elif i == 1:
                if j == 0:
                    n = [[g[0][1], g[0][2]], [g[2][1], g[2][2]]]
                    b0 = epsilon * utils.tensor_determinant(n)
                elif j == 1:
                    n = [[g[0][0], g[0][2]], [g[2][0], g[2][2]]]
                    b1 = epsilon * utils.tensor_determinant(n)
                else:
                    n = [[g[0][0], g[0][1]], [g[2][0], g[2][1]]]
                    b2 = epsilon * utils.tensor_determinant(n)
github aramis-lab / clinica / clinica / pipelines / machine_learning_spatial_svm / spatial_svm_pipeline.py View on Github external
import clinica.pipelines.machine_learning_spatial_svm.spatial_svm_utils as utils
        import nipype.interfaces.utility as nutil
        import nipype.pipeline.engine as npe
        import nipype.interfaces.io as nio

        fisher_tensor_generation = npe.Node(name="obtain_g_fisher_tensor",
                                            interface=nutil.Function(input_names=['dartel_input', 'FWHM'],
                                                                     output_names=['fisher_tensor', 'fisher_tensor_path'],
                                                                     function=utils.obtain_g_fisher_tensor))
        fisher_tensor_generation.inputs.FWHM = self.parameters['fwhm']

        time_step_generation = npe.Node(name='estimation_time_step',
                                        interface=nutil.Function(input_names=['dartel_input', 'FWHM', 'g'],
                                                                 output_names=['t_step', 'json_file'],
                                                                 function=utils.obtain_time_step_estimation))
        time_step_generation.inputs.FWHM = self.parameters['fwhm']

        heat_solver_equation = npe.MapNode(name='heat_solver_equation',
                                           interface=nutil.Function(input_names=['input_image', 'g',
                                                                                 'FWHM', 't_step', 'dartel_input'],
                                                                    output_names=['regularized_image'],
                                                                    function=utils.heat_solver_equation),
                                           iterfield=['input_image'])
        heat_solver_equation.inputs.FWHM = self.parameters['fwhm']

        datasink = npe.Node(nio.DataSink(),
                            name='sinker')
        datasink.inputs.base_directory = self.caps_directory
        datasink.inputs.parameterization = True
        if self.parameters['image_type'] == 't1':
            datasink.inputs.regexp_substitutions = [
github aramis-lab / clinica / clinica / pipelines / machine_learning_spatial_svm / spatial_svm_utils.py View on Github external
def tensor_eigenvalues(g):
    """

    :param g: tensor
    :return: eigenvalues of the tensor

    """
    import numpy as np
    import clinica.pipelines.machine_learning_spatial_svm.spatial_svm_utils as utils
    g = np.array(g)

    if g.shape[0] < 4:
        # condition if we have a tensor

        C1 = np.ones(len(np.ravel(g[0][0])))
        buff = -utils.tensor_trace(g)
        C2 = buff.flatten('F')
        buff = utils.tensor_trace(utils.tensor_product(g, g))
        buff = (- 0.5 * (buff.flatten('F') - np.multiply(C2, C2)))
        C3 = buff.flatten('F')
        buff = -utils.tensor_determinant(g)
        C4 = buff.flatten('F')

        C = np.array([C1, C2, C3, C4])
        rts = utils.roots_poly(C)

    else:
        print('Degree too big : not still implemented')

    rts2 = rts.real.copy()
    rts2.sort()
github aramis-lab / clinica / clinica / pipelines / machine_learning_spatial_svm / spatial_svm_utils.py View on Github external
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[np.isnan(detg2)] = 0
    detg[np.isnan(detg)] = 0
    ginv[np.isnan(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))
github aramis-lab / clinica / clinica / pipelines / machine_learning_spatial_svm / spatial_svm_utils.py View on Github external
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