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
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
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
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
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
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
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
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
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)
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)
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)
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)
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)
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)
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)
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)
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)
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()
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()
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)
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)
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)
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)
def mrcalc(source, value, target): """ #@TODO comment the purpose of this function """ cmd = "mrcalc {} {} -eq {} -quiet".format(source, value, target) return util.launchCommand(cmd)
def applyRegistrationMrtrix(source, matrix, target): cmd = "mrtransform {} -linear {} {} -quiet".format(source, matrix, target) util.launchCommand(cmd) return target