Esempio n. 1
0
def computeDwiMaskFromFreesurfer(source, reference, sourceToResample, target,
                                 extraArgs):
    """

    Args:
        source:    an input image into the dwi space
        reference: usually the freesurfer normalize image
        sourceToResample: The image to apply the transform matrix: usually the freesurfer mask
        target: the output image name

        extraArgs: extra parameters to pass during the resampling computation step

    return
        A mask into the dwi native space

    """
    randomNumber = "{0:.6g}".format(random.randint(0, 999999))

    #@TODO add dof as arguments parameters
    dummyTarget = "flirt_{}_target.nii.gz".format(randomNumber)
    matrix = "b0ToFressurfer_{}_transformation.mat".format(randomNumber)
    freesurferToB0 = 'freesurferToB0_{}_transformation.mat'.format(
        randomNumber)

    cmd = "flirt -in {} -ref {} -omat {} -out {} {}".format(
        source, reference, matrix, dummyTarget, extraArgs)
    util.launchCommand(cmd)
    invertMatrix(matrix, freesurferToB0)
    cmd = "flirt -in {} -ref {} -applyxfm -init {} -out {} ".format(
        sourceToResample, source, freesurferToB0, target)
    util.launchCommand(cmd)
    return target
Esempio n. 2
0
def computeDwiMaskFromFreesurfer(source, reference, sourceToResample, target, extraArgs):
    """

    Args:
        source:    an input image into the dwi space
        reference: usually the freesurfer normalize image
        sourceToResample: The image to apply the transform matrix: usually the freesurfer mask
        target: the output image name

        extraArgs: extra parameters to pass during the resampling computation step

    return
        A mask into the dwi native space

    """
    randomNumber = "{0:.6g}".format(random.randint(0,999999))

    #@TODO add dof as arguments parameters
    dummyTarget = "flirt_{}_target.nii.gz".format(randomNumber)
    matrix = "b0ToFressurfer_{}_transformation.mat".format(randomNumber)
    freesurferToB0 ='freesurferToB0_{}_transformation.mat'.format(randomNumber)

    cmd = "flirt -in {} -ref {} -omat {} -out {} {}".format(source, reference, matrix, dummyTarget, extraArgs)
    util.launchCommand(cmd)
    invertMatrix(matrix, freesurferToB0)
    cmd = "flirt -in {} -ref {} -applyxfm -init {} -out {} ".format(sourceToResample, source, freesurferToB0, target)
    util.launchCommand(cmd)
    return target
Esempio n. 3
0
def convertAndRestride(source, target, orientation):
    """Utility for converting between different file formats

    Args:
        source: The input source file
        target: The name of the resulting output file name

    """
    cmd = "mrconvert {} {} -stride {} -force -quiet".format(source, target, orientation)
    util.launchCommand(cmd)
    return target
Esempio n. 4
0
def convertAndRestride(source, target, orientation):
    """Utility for converting between different file formats

    Args:
        source: The input source file
        target: The name of the resulting output file name

    """
    cmd = "mrconvert {} {} -stride {} -force -quiet"\
        .format(source, target, orientation)
    util.launchCommand(cmd)
    return target
Esempio n. 5
0
def tckresample(source, downsample, target, launch=True):
    """ perform resample on track files

    Args:
        source: the input track file
        downsample: decrease the density of points along the length of the
            streamline by some factor
        target: the output track file

    Returns: the executed command line
    """
    cmd = "tckresample -downsample {} {} {}"
    cmd = cmd.format(downsample, source, target)
    if launch: util.launchCommand(cmd)
    return cmd
Esempio n. 6
0
def tckresample(source, downsample, target, launch=True):
    """ perform resample on track files

    Args:
        source: the input track file
        downsample: decrease the density of points along the length of the
            streamline by some factor
        target: the output track file

    Returns: the executed command line
    """
    cmd = "tckresample -downsample {} {} {}"
    cmd = cmd.format(downsample, source, target)
    if launch: util.launchCommand(cmd)
    return cmd
Esempio n. 7
0
def applyResampleFsl(source, reference, matrix, target, nearest=False):
    """Register an image with symmetric normalization and mutual information metric

    Args:
        source:
        reference: use this image as reference
        matrix:
        target: the output file name
        nearest: A boolean, process nearest neighbour interpolation

    Returns:
        return a file containing the resulting image transform
    """
    cmd = "flirt -in {} -ref {} -applyxfm -init {} -out {} ".format(source, reference, matrix, target)
    if nearest:
        cmd += "-interp nearestneighbour"
    util.launchCommand(cmd)
    return target
Esempio n. 8
0
def applyResampleFsl(source, reference, matrix, target, nearest=False):
    """Register an image with symmetric normalization and mutual information metric

    Args:
        source:
        reference: use this image as reference
        matrix:
        target: the output file name
        nearest: A boolean, process nearest neighbour interpolation

    Returns:
        return a file containing the resulting image transform
    """
    cmd = "flirt -in {} -ref {} -applyxfm -init {} -out {} ".format(
        source, reference, matrix, target)
    if nearest:
        cmd += "-interp nearestneighbour"
    util.launchCommand(cmd)
    return target
Esempio n. 9
0
def tckedit(source, roi, target, launch=True):
    """ perform various editing operations on track files.

    Args:
        source: the input track file(s)
        roi:    specify an inclusion region of interest, as either a binary mask image, or as a sphere
                using 4 comma-separared values (x,y,z,radius)
        target: the output track file

    Returns: the executed command line
    """
    cmd = "tckedit {} {} -quiet".format(source, target)

    if isinstance(roi, basestring):
        cmd += " -include {}".format(roi)
    else:
        for element in roi:
            cmd += " -include {}".format(element)
    if launch: util.launchCommand(cmd)
    return cmd
Esempio n. 10
0
def tckedit(source, roi, target, launch=True):
    """ perform various editing operations on track files.

    Args:
        source: the input track file(s)
        roi:    specify an inclusion region of interest, as either a binary mask image, or as a sphere
                using 4 comma-separared values (x,y,z,radius)
        target: the output track file

    Returns: the executed command line
    """
    cmd = "tckedit {} {} -quiet".format(source, target)

    if isinstance(roi, basestring):
        cmd += " -include {}".format(roi)
    else:
        for element in roi:
            cmd += " -include {}".format(element)
    if launch: util.launchCommand(cmd)
    return cmd
Esempio n. 11
0
def invertMatrix(source, target):
    """ invert a transformation matrices

    Args:
        source: an input transformation matrices
        target: an output transformation matrices name

    Returns:
        the resulting output transformation matrices name

    """
    cmd = "convert_xfm -inverse {} -omat {}".format(source, target)
    return util.launchCommand(cmd)
Esempio n. 12
0
def stride3DImage(source, target, layout="1,2,3"):
    """perform a reorientation of the axes and flip the image into a different layout

    Args:
        source:           the input image
        layout:           comma-separated list that specify the strides.
        outputNamePrefix: a prefix to rename target filename

    Returns:
        A 3 elements tuples representing the command line launch, the standards output and the standard error message
    """
    cmd = "mrconvert {} {} -stride {}".format(source, target, layout)
    return util.launchCommand(cmd)
Esempio n. 13
0
def stride3DImage(source, target, layout="1,2,3" ):
    """perform a reorientation of the axes and flip the image into a different layout

    Args:
        source:           the input image
        layout:           comma-separated list that specify the strides.
        outputNamePrefix: a prefix to rename target filename

    Returns:
        A 3 elements tuples representing the command line launch, the standards output and the standard error message
    """
    cmd = "mrconvert {} {} -stride {}".format(source, target, layout)
    return util.launchCommand(cmd)
Esempio n. 14
0
def invertMatrix(source, target):
    """ invert a transformation matrices

    Args:
        source: an input transformation matrices
        target: an output transformation matrices name

    Returns:
        the resulting output transformation matrices name

    """
    cmd = "convert_xfm -inverse {} -omat {}".format(source, target)
    return util.launchCommand(cmd)
Esempio n. 15
0
def extractB0sFromDwi(source, target, bVals, bVecs, nthreads = "1"):
    """Perform an extraction of all b0s image found into a DWI image

    Args:
        source: The input image
        target: The resulting output name
        bvals:   BValue table
        bvecs: Gradient table

    Returns:
         A tuple of 3 elements representing (the command launch, the stdout and stderr of the execution)
    """
    cmd = 'dwiextract -bzero {} {} -fslgrad {} {} -nthreads {} -quiet'.format(source, target, bVecs, bVals, nthreads)
    return util.launchCommand(cmd)
Esempio n. 16
0
def extractSubVolume(source, target, extractAtAxis, extractAtCoordinate, nthreads = "1"):
    """Perform an extraction of a subset of the source image

    Args:
        source: The input image
        target: The resulting output name
        extractAtAxis: extract data only in the axis of interest
        extractAtCoordinate: extract data only in the coordinates of interest

    Returns:
         A tuple of 3 elements representing (the command launch, the stdout and stderr of the execution)
    """
    cmd = "mrconvert -coord {} {} {} {} -nthreads {} -quiet".format(extractAtAxis, extractAtCoordinate, source, target, nthreads)
    return util.launchCommand(cmd)
Esempio n. 17
0
def fslToMrtrixEncoding(dwi, bVecs, bVals, target):
    """Create a B encoding gradient file base on bVals and bVecs encoding file

    Args:
        dwi:           the input diffusion weight images
        bVecs: a vector value file.
        bVals: a gradient b value encoding file.
        target: the output file name
    Returns:
        A 3 elements tuples representing the command line launch, the standards output and the standard error message

    """
    cmd = "mrinfo {} -fslgrad {} {} -export_grad_mrtrix {} --quiet".format(dwi, bVecs, bVals, target)
    return util.launchCommand(cmd)
Esempio n. 18
0
def mrtrixToFslEncoding(dwi, bEncs, bVecsTarget, bValsTarget):
    """Create a B encoding gradient file base on bVals and bVecs encoding file

    Args:
        dwi:           the input diffusion weight images
        bEncs: a mrtrix encoding gradient direction file.
        bVecsTarget: a output vector file name.
        bValsTarget: a output value file name.
    Returns:
        A 3 elements tuples representing the command line launch, the standards output and the standard error message

    """
    cmd = "mrinfo {} -grad {} -export_grad_fsl {} {} --quiet".format(dwi, bEncs, bVecsTarget, bValsTarget)
    return util.launchCommand(cmd)
Esempio n. 19
0
def fslToMrtrixEncoding(dwi, bVecs, bVals, target):
    """Create a B encoding gradient file base on bVals and bVecs encoding file

    Args:
        dwi:           the input diffusion weight images
        bVecs: a vector value file.
        bVals: a gradient b value encoding file.
        target: the output file name
    Returns:
        A 3 elements tuples representing the command line launch, the standards output and the standard error message

    """
    cmd = "mrinfo {} -fslgrad {} {} -export_grad_mrtrix {} --quiet".format(
        dwi, bVecs, bVals, target)
    return util.launchCommand(cmd)
Esempio n. 20
0
def mrtrixToFslEncoding(dwi, bEncs, bVecsTarget, bValsTarget):
    """Create a B encoding gradient file base on bVals and bVecs encoding file

    Args:
        dwi:           the input diffusion weight images
        bEncs: a mrtrix encoding gradient direction file.
        bVecsTarget: a output vector file name.
        bValsTarget: a output value file name.
    Returns:
        A 3 elements tuples representing the command line launch, the standards output and the standard error message

    """
    cmd = "mrinfo {} -grad {} -export_grad_fsl {} {} --quiet".format(
        dwi, bEncs, bVecsTarget, bValsTarget)
    return util.launchCommand(cmd)
Esempio n. 21
0
def mrinfo(source):
    """display or extract specific information from the source header.

    Args:
        source: the input image

    Returns:
        An array of information generate by the standard output command line

    Raises:
        ValueError: if mrinfo binary is not found
    """

    cmd = "mrinfo {}".format(source)
    (executedCmd, stdout, stderr) = util.launchCommand(cmd)
    return stdout.splitlines()
Esempio n. 22
0
def mrinfo(source):
    """display or extract specific information from the source header.

    Args:
        source: the input image

    Returns:
        An array of information generate by the standard output command line

    Raises:
        ValueError: if mrinfo binary is not found
    """

    cmd = "mrinfo {}".format(source)
    (executedCmd, stdout, stderr) = util.launchCommand(cmd)
    return stdout.splitlines()
Esempio n. 23
0
def getBValues(source, grad):
    """Use mrinfo to get BValues

    Args:
        Source: dwi file

    Returns:
        An array of bValues

    Raises:
        ValueError: if mrinfo binary is not found

    """
    cmd = "mrinfo {} -grad {} -shells".format(source, grad)
    (executedCmd, stdout, stderr) = util.launchCommand(cmd)
    return stdout.split()
Esempio n. 24
0
def extractSubVolume(source,
                     target,
                     extractAtAxis,
                     extractAtCoordinate,
                     nthreads="1"):
    """Perform an extraction of a subset of the source image

    Args:
        source: The input image
        target: The resulting output name
        extractAtAxis: extract data only in the axis of interest
        extractAtCoordinate: extract data only in the coordinates of interest

    Returns:
         A tuple of 3 elements representing (the command launch, the stdout and stderr of the execution)
    """
    cmd = "mrconvert -coord {} {} {} {} -nthreads {} -quiet".format(
        extractAtAxis, extractAtCoordinate, source, target, nthreads)
    return util.launchCommand(cmd)
Esempio n. 25
0
def fslmaths(source1, target, operator="bin", source2=None):
    """Perform a mathematical operations using a second image or a numeric value

    Args:
        source1: he input image
        target:  Name of the resulting output image
        operator: Operator to apply
        source2: Image associate with the operator if Binary operation is specified

    Returns:
        A tuple of 3 elements representing (the command launch, the stdout and stderr of the execution)

    """
    if source2 is None:
        cmd = "fslmaths {} -{} {} ".format(source1, operator, target)
    else:
        cmd = "fslmaths {} -{} {} {}".format(source1, operator, source2, target)

    return util.launchCommand(cmd)
Esempio n. 26
0
def tckedit(source, roi, target, downsample= "2"):
    """ perform various editing operations on track files.

    Args:
        source: the input track file(s)
        roi:    specify an inclusion region of interest, as either a binary mask image, or as a sphere
                using 4 comma-separared values (x,y,z,radius)
        target: the output track file
        downsample: increase the density of points along the length of the streamline by some factor

    Returns:
        the output track file
    """
    cmd = "tckedit {} {} -downsample {} -quiet ".format(source, target, downsample)
    if isinstance(roi, basestring):
        cmd += " -include {}".format(roi)
    else:
        for element in roi:
            cmd += " -include {}".format(element)
    return util.launchCommand(cmd)
Esempio n. 27
0
def fslmaths(source1, target, operator="bin", source2=None):
    """Perform a mathematical operations using a second image or a numeric value

    Args:
        source1: he input image
        target:  Name of the resulting output image
        operator: Operator to apply
        source2: Image associate with the operator if Binary operation is specified

    Returns:
        A tuple of 3 elements representing (the command launch, the stdout and stderr of the execution)

    """
    if source2 is None:
        cmd = "fslmaths {} -{} {} ".format(source1, operator, target)
    else:
        cmd = "fslmaths {} -{} {} {}".format(source1, operator, source2,
                                             target)

    return util.launchCommand(cmd)
Esempio n. 28
0
def extractFirstB0sFromDWI(source, target, bVals, nthreads):
    """Extract first n b0s from DWI using bVal

    Ex: BVal contains: 0 0 0 1000 1000 1000 0 1000
        Each value corresponds to one volume
        Output would the first 3 b0s  

    """
    vals = open(bVals, 'r')
    bvals = vals.readlines()
    vals.close()
    index = 0
    for currVals in bvals[0].split(' '):
        if  int(currVals) == 0:
            index+=1
        else:
            break

    if index == 0:
        raise ValueError
    else:
        cmd = 'mrconvert -coord 3 0:{} {} {} -nthreads {} -quiet'.format(str(index-1), source, target, nthreads)
        return util.launchCommand(cmd)
Esempio n. 29
0
def mrcalc(source, value, target):
    """
    #@TODO comment the purpose of this function
    """
    cmd = "mrcalc {} {} -eq {} -quiet".format(source, value, target)
    return util.launchCommand(cmd)
Esempio n. 30
0
def applyRegistrationMrtrix(source, matrix, target):
    cmd = "mrtransform  {} -linear {} {} -quiet".format(source, matrix, target)
    util.launchCommand(cmd)
    return target
Esempio n. 31
0
def applyRegistrationMrtrix(source, matrix, target):
    cmd = "mrtransform  {} -linear {} {} -quiet".format(source, matrix, target)
    util.launchCommand(cmd)
    return target
Esempio n. 32
0
def mrcalc(source, value, target):
    """
    #@TODO comment the purpose of this function
    """
    cmd = "mrcalc {} {} -eq {} -quiet".format(source, value, target)
    return util.launchCommand(cmd)