Example #1
0
def compute_second_order_scalar_parameters(gdti6):
    """ Compute the Fractional Anisotropy [1]_ (FA),
    Mean Diffusivity (MD) and the Westion Shapes coefficients [1]_
    (cl, cp, cs) of a second order tensor.

    .. hidden-technical-block::
        :label: [+show/hide second order tensor scalars]
        :starthidden: True

        .. include:: source/_static/technical_documentation/dt2_scalars.txt

    **References**

    .. [1] C. Westin, S. Maier, H. Mamata, A. Nabavi, F. Jolesz and
           R. Kikinis : Processing and visualization of diffusion tensor
           MRI. Medical Image Analysis, 6(2):93-108, 2002.

    Parameters
    ----------
    dt33: numpy array (..., 3, 3)
        a second order symmetric tensor.

    Returns
    -------
    fa: array [...]
        fractional anisotropy map.
    md: array [...]
        mean diffusivity map.
    cl: array [...]
        linearity coefficient map.
    cp: array [...]
        planarity coefficient map.
    cs: array [...]
        sphericity coefficient map.
    """
    # Full tensor representation
    gdti33 = dti6to33(gdti6)

    # Eigenvalue decomposition
    eigenvalues, eigenvectors = decompose_second_order_tensor(gdti33)
    ev1 = eigenvalues[..., 0]
    ev2 = eigenvalues[..., 1]
    ev3 = eigenvalues[..., 2]

    # Compute denominator: avoid division by zero
    all_zero = (eigenvalues == 0).all(axis=-1)
    ev2 = ev1*ev1 + ev2*ev2 + ev3*ev3 + all_zero

    # Compute mean diffusivity map
    md = numpy.mean(eigenvalues, axis=3)

    # Compute fractional anisotropy map
    fa = numpy.sqrt(0.5 * ((ev1 - ev2) ** 2 + (ev2 - ev3) ** 2 +
                           (ev3 - ev1) ** 2) / ev2)

    # Compute lineraity coefficient map
    cl = (ev1 - ev2) / numpy.sqrt(ev2)

    # Compute planarity coefficient map
    cp = 2 * (ev2 - ev3) / numpy.sqrt(ev2)

    # Compute sphericity coefficient map
    cs = 3 * ev3 / numpy.sqrt(ev2)

    return fa, md, cl, cp, cs
Example #2
0
    bvecfile = os.path.join(output_directory, "dwi.bvec")
    bvalfile = os.path.join(output_directory, "dwi.bval")
    dwi_image.to_filename(dfile)
    numpy.savetxt(bvecfile, g)
    numpy.savetxt(bvalfile, b.T)

    gdtifile = generalized_tensor_estimation(dfile,
                                             bvalfile,
                                             bvecfile,
                                             order,
                                             maskfile=None,
                                             number_of_workers=1,
                                             odf=False,
                                             output_directory=output_directory)
    dti_params = nibabel.load(gdtifile).get_data()
    print "Tensor:"
    for index, params in enumerate(dti_params[0, 0]):
        print "nnls:", index
        print_tensor(params, order)
    for tensor in dti6to33(dti_params)[0, 0]:
        vals, vecs = numpy.linalg.eigh(tensor)
        print vals
        print vecs

    # Plot
    ren = pvtk.ren()
    for index, params in enumerate(dti_params[0, 0]):
        actor = pvtk.tensor(params, order, position=(index, 0, 0))
        pvtk.add(ren, actor)
    pvtk.show(ren)
Example #3
0
def compute_second_order_scalar_parameters(gdti6):
    """ Compute the Fractional Anisotropy [1]_ (FA),
    Mean Diffusivity (MD) and the Westion Shapes coefficients [1]_
    (cl, cp, cs) of a second order tensor.

    .. hidden-technical-block::
        :label: [+show/hide second order tensor scalars]
        :starthidden: True

        .. include:: source/_static/technical_documentation/dt2_scalars.txt

    **References**

    .. [1] C. Westin, S. Maier, H. Mamata, A. Nabavi, F. Jolesz and
           R. Kikinis : Processing and visualization of diffusion tensor
           MRI. Medical Image Analysis, 6(2):93-108, 2002.

    Parameters
    ----------
    dt33: numpy array (..., 3, 3)
        a second order symmetric tensor.

    Returns
    -------
    fa: array [...]
        fractional anisotropy map.
    md: array [...]
        mean diffusivity map.
    cl: array [...]
        linearity coefficient map.
    cp: array [...]
        planarity coefficient map.
    cs: array [...]
        sphericity coefficient map.
    """
    # Full tensor representation
    gdti33 = dti6to33(gdti6)

    # Eigenvalue decomposition
    eigenvalues, eigenvectors = decompose_second_order_tensor(gdti33)
    ev1 = eigenvalues[..., 0]
    ev2 = eigenvalues[..., 1]
    ev3 = eigenvalues[..., 2]

    # Compute denominator: avoid division by zero
    all_zero = (eigenvalues == 0).all(axis=-1)
    ev2 = ev1 * ev1 + ev2 * ev2 + ev3 * ev3 + all_zero

    # Compute mean diffusivity map
    md = numpy.mean(eigenvalues, axis=3)

    # Compute fractional anisotropy map
    fa = numpy.sqrt(0.5 * ((ev1 - ev2)**2 + (ev2 - ev3)**2 + (ev3 - ev1)**2) /
                    ev2)

    # Compute lineraity coefficient map
    cl = (ev1 - ev2) / numpy.sqrt(ev2)

    # Compute planarity coefficient map
    cp = 2 * (ev2 - ev3) / numpy.sqrt(ev2)

    # Compute sphericity coefficient map
    cs = 3 * ev3 / numpy.sqrt(ev2)

    return fa, md, cl, cp, cs
Example #4
0
    dwi[..., 1] = S0 * factor
    for index in range(len(sdwi)):
        dwi[:, :, index, 2:] = sdwi[index]
    dwi_image = nibabel.Nifti1Image(dwi, numpy.eye(4))
    dfile = os.path.join(output_directory, "dwi.nii.gz")
    bvecfile = os.path.join(output_directory, "dwi.bvec")
    bvalfile = os.path.join(output_directory, "dwi.bval")
    dwi_image.to_filename(dfile)
    numpy.savetxt(bvecfile, g)
    numpy.savetxt(bvalfile, b.T)

    gdtifile = generalized_tensor_estimation(
        dfile, bvalfile, bvecfile, order, maskfile=None, number_of_workers=1,
        odf=False, output_directory=output_directory)
    dti_params = nibabel.load(gdtifile).get_data()  
    print "Tensor:"
    for index, params in enumerate(dti_params[0, 0]):
        print "nnls:", index
        print_tensor(params, order)
    for tensor in dti6to33(dti_params)[0, 0]:
        vals, vecs = numpy.linalg.eigh(tensor)
        print vals
        print vecs

    # Plot
    ren = pvtk.ren()
    for index, params in enumerate(dti_params[0, 0]):
        actor = pvtk.tensor(params, order, position=(index, 0, 0))
        pvtk.add(ren, actor)
    pvtk.show(ren)