def ants_apply_warp(template, input_image, input_warp, input_affine, output_image, switches='', ants_apply=False, **exec_options): if ants_apply: cmd = os.path.join(this_path, 'tools', 'WarpImageMultiTransform.py')+' %s %s %s %s %s %s' % (switches, input_image, output_image, template, input_warp, input_affine) else: cmd = 'WarpImageMultiTransform 3 %s %s %s %s -R %s %s' % (input_image, output_image, input_warp, input_affine, template, switches) command(cmd, **exec_options) return output_image, cmd
def ants_rigid_registration(fixed, moving, cost='MI', **exec_options): cmd = 'antsRegistration -d 3 --float 0 --output rigid -t Rigid[0.1] -r [%s,%s,1] --metric %s[%s,%s,1,32,Regular,0.25] --convergence [1000x500x250x100, 5e-7,10] -v -f 8x4x2x1 -s 3x2x1x0vox' % ( fixed, moving, cost, fixed, moving) output_warp = 'rigid.nii.gz' output_rigid = 'rigidGeneric0Affine.txt' command(cmd, **exec_options) return output_warp, output_rigid, cmd
def ants_v0_nonlinear_registration(template, input_image, output, switches='', **exec_options): """Do nonlinear registration with antsRegistration but no -r option """ cmd = 'antsRegistration -d 3 --float 0 --output %s -t Affine[0.1] --metric MI[%s,%s,1,32,Regular,0.25] --convergence [1000x500x250x100,1e-6,10] -f 8x4x2x1 -s 3x2x1x0vox -t SyN[0.1,3.0] --metric CC[%s,%s,1,4] --convergence [70x70x20,1e-6,10] -f 4x2x1 -s 2x1x0vox' % (output, template, input_image, template, input_image) output_warp = output+'Warp.nii.gz' output_affine = output+'Affine.txt' command(cmd, **exec_options) return output_warp, output_affine, cmd
def sanitize_label_image(input_image, output_image, **exec_options): # Slicer puts data in LR convention for some reason cmd = 'fslswapdim %s LR PA IS %s; ' % (input_image, output_image) # Sometimes values are > 1 cmd += 'fslmaths %s -bin %s' % (output_image, output_image) command(cmd, **exec_options) return output_image, cmd
def ants_linear_registration(template, input_image, cost='CC', **exec_options): cmd = 'ANTS 3 -m %s[%s,%s,1,5] -o linear -i 0 --use-Histogram-Matching --number-of-affine-iterations 10000x10000x10000x10000x10000 --MI-option 32x16000 --rigid-affine true' % ( cost, template, input_image) output_warp = 'linearWarp.nii.gz' output_affine = 'linearAffine.txt' command(cmd, **exec_options) return output_warp, output_affine, cmd
def copy_header(reference, target, output, switches='1 1 1', echo=False): """ Copies NIFTI header information from reference to target resulting in output. Usage: CopyImageHeaderInformation refimage.ext imagetocopyrefimageinfoto.ext imageout.ext boolcopydirection boolcopyorigin boolcopyspacing {bool-Image2-IsTensor} """ cmd = 'CopyImageHeaderInformation %s %s %s %s' % (reference, target, output, switches) command(cmd, echo=echo) return output, cmd
def sanitize_input(input_image, output_image, command=os.system): """ Standardizes the input to neurological coordinates and can flip to segment right thalamus. """ if sys.platform == 'linux2' or sys.platform == 'darwin': command('fslreorient2std %s %s' % (input_image, output_image)) return output_image
def create_atlas(label, path, subjects, target, output_atlas, echo=False): # Create 4D atlas for a label previously registered to a target subject label_paths = [os.path.join(path, subj, target, label+'.nii.gz') for subj in subjects] cmd = 'fslmerge -t %s %s' % (output_atlas, ' '.join(label_paths)) # if echo: # print cmd # else: # os.system(cmd) command(cmd, echo=echo) return output_atlas, cmd
def label_fusion_steps(input_image, image_atlas, label_atlas, output_label, sigma, X, mrf=0., echo=False): # Perform steps label fusion. Parameter naming comes from Cardoso et al. 2013 # verbose and only consider non-consensus voxels. cmd = 'seg_LabFusion -v -unc -in %s -STEPS %s %s %s %s -out %s' % (label_atlas, sigma, X, input_image, image_atlas, output_label) if 0 < mrf <= 5: cmd += ' -MRF_beta %g' % mrf # command(cmd, echo=echo) if not echo: command(cmd) return output_label, cmd
def ants_apply_only_warp(template, input_image, input_warp, output_image, switches='', **exec_options): cmd = 'WarpImageMultiTransform 3 %s %s %s -R %s %s' % ( input_image, output_image, input_warp, template, switches) command(cmd, **exec_options) return output_image, cmd
def ants_nonlinear_registration(template, input_image, output, switches='', linear=False, cost='CC', **exec_options): """Do nonlinear registration with ANTS as in buildtemplateparallel.sh""" if linear: iterations = '0' else: iterations = '30x90x20' cmd = 'ANTS 3 -m %s[%s,%s,1,5] -t SyN[0.25] -r Gauss[3,0] -o %s -i %s --use-Histogram-Matching --number-of-affine-iterations 10000x10000x10000x10000x10000 --MI-option 32x16000 %s' % (cost, template, input_image, output, iterations, switches) output_warp = output+'Warp.nii.gz' output_affine = output+'Affine.txt' command(cmd, **exec_options) return output_warp, output_affine, cmd
def label_fusion_picsl(input_image, atlas_images, atlas_labels, output_label, rp=[2, 2, 2], rs=[3, 3, 3], alpha=0.1, beta=2, **exec_options): """ H Wang. Multi-Atlas Sementation with Joint Label Fusion. 2013. Joint Label Fusion: usage: jointfusion dim mod [options] output_image required options: dim Image dimension (2 or 3) mod Number of modalities or features -g atlas1_mod1.nii atlas1_mod2.nii ...atlasN_mod1.nii atlasN_mod2.nii ... Warped atlas images -tg target_mod1.nii ... target_modN.nii Target image(s) -l label1.nii ... labelN.nii Warped atlas segmentation -m <method> [parameters] Select voting method. Options: Joint (Joint Label Fusion) May be followed by optional parameters in brackets, e.g., -m Joint[0.1,2]. See below for parameters other options: -rp radius Patch radius for similarity measures, scalar or vector (AxBxC) Default: 2x2x2 -rs radius Local search radius. Default: 3x3x3 -x label image.nii Specify an exclusion region for the given label. -p filenamePattern Save the posterior maps (probability that each voxel belongs to each label) as images. The number of images saved equals the number of labels. The filename pattern must be in C printf format, e.g. posterior%04d.nii.gz Parameters for -m Joint option: alpha Regularization term added to matrix Mx for inverse Default: 0.1 beta Exponent for mapping intensity difference to joint error Default: 2 """ dim = 3 mod = 1 g = ' '.join(atlas_images) tg = input_image l = ' '.join(atlas_labels) m = 'Joint[%g,%g]' % (alpha, beta) rp = '%dx%dx%d' % tuple(rp) rs = '%dx%dx%d' % tuple(rs) cmd = 'jointfusion %s %s -g %s -tg %s -l %s -m %s -rp %s -rs %s %s' % ( dim, mod, g, tg, l, m, rp, rs, output_label) command(cmd, **exec_options) # if not echo: # command(cmd) return output_label, cmd
def ants_new_compose_a_to_b(a_transform_prefix, b_path, b_transform_prefix, output, **exec_options): """ Compose a to b via an intermediate template space """ a_affine = a_transform_prefix+'Affine.txt' a_warp = a_transform_prefix+'Warp.nii.gz' b_affine = '-i '+b_transform_prefix+'0GenericAffine.mat' b_warp = b_transform_prefix+'1InverseWarp.nii.gz' cmd = 'ComposeMultiTransform 3 %s %s %s %s %s -R %s' % (output, b_affine, b_warp, a_warp, a_affine, b_path) command(cmd, **exec_options) return output, cmd
def ants_mi_nonlinear_registration(template, input_image, output, switches='', **exec_options): """Do nonlinear registration with antsRegistration MI syn """ cmd = 'antsRegistration -v -d 3 --float 0 --output %s --use-histogram-matching 1 -t Rigid[0.1] --metric Mattes[%s,%s,1,32,None] --convergence [500x500x500x500x500,1e-6,10] -f 5x5x5x5x4 -s 1.685x1.4771x1.256x1.0402x0.82235mm -r [rigid0GenericAffine.mat,1] -t Affine[0.1] --metric Mattes[%s,%s,1,64, None] --convergence [450x150x50,1e-7,10] -f 3x2x1 -s 0.60056x0.3677x0mm -t SyN[0.4,3.0] --metric MI[%s,%s,1,32,None] --convergence [200x200x90x50,1e-10,10] -f 4x3x2x1 -s 0.82x0.6x0.3677x0.0mm' % ( output, template, input_image, template, input_image, template, input_image) output_warp = output + 'Warp.nii.gz' output_affine = output + 'Affine.txt' command(cmd, **exec_options) return output_warp, output_affine, cmd
def label_fusion_picsl(input_image, atlas_images, atlas_labels, output_label, rp=[2, 2, 2], rs=[3, 3, 3], alpha=0.1, beta=2, **exec_options): """ H Wang. Multi-Atlas Sementation with Joint Label Fusion. 2013. Joint Label Fusion: usage: jointfusion dim mod [options] output_image required options: dim Image dimension (2 or 3) mod Number of modalities or features -g atlas1_mod1.nii atlas1_mod2.nii ...atlasN_mod1.nii atlasN_mod2.nii ... Warped atlas images -tg target_mod1.nii ... target_modN.nii Target image(s) -l label1.nii ... labelN.nii Warped atlas segmentation -m <method> [parameters] Select voting method. Options: Joint (Joint Label Fusion) May be followed by optional parameters in brackets, e.g., -m Joint[0.1,2]. See below for parameters other options: -rp radius Patch radius for similarity measures, scalar or vector (AxBxC) Default: 2x2x2 -rs radius Local search radius. Default: 3x3x3 -x label image.nii Specify an exclusion region for the given label. -p filenamePattern Save the posterior maps (probability that each voxel belongs to each label) as images. The number of images saved equals the number of labels. The filename pattern must be in C printf format, e.g. posterior%04d.nii.gz Parameters for -m Joint option: alpha Regularization term added to matrix Mx for inverse Default: 0.1 beta Exponent for mapping intensity difference to joint error Default: 2 """ dim = 3 mod = 1 g = ' '.join(atlas_images) tg = input_image l = ' '.join(atlas_labels) m = 'Joint[%g,%g]' % (alpha, beta) rp = '%dx%dx%d' % tuple(rp) rs = '%dx%dx%d' % tuple(rs) cmd = 'jointfusion %s %s -g %s -tg %s -l %s -m %s -rp %s -rs %s %s' % (dim, mod, g, tg, l, m, rp, rs, output_label) command(cmd, **exec_options) # if not echo: # command(cmd) return output_label, cmd
def ants_WarpImageMultiTransform(input_image, output_image, template, **exec_options): cmd = 'WarpImageMultiTransform 3 %s %s -R %s -i linearAffine.txt' % (input_image, output_image, template) command(cmd, **exec_options) return output_image, cmd
def flip_lr(input_image, output_image, command=os.system): if sys.platform == 'linux2' or sys.platform == 'darwin': command('fslswapdim %s -x y z %s' % (input_image, output_image)) # else: # command('fsl5.0-fslswapdim %s -x y z %s' % (input_image, output_image)) return output_image
def label_fusion_picsl_ants(input_image, atlas_images, atlas_labels, output_label, rp=[2, 2, 2], rs=[3, 3, 3], alpha=0.1, beta=2, mask='', **exec_options): """ COMMAND: antsJointFusion antsJointFusion is an image fusion algorithm developed by Hongzhi Wang and Paul Yushkevich which won segmentation challenges at MICCAI 2012 and MICCAI 2013. The original label fusion framework was extended to accommodate intensities by Brian Avants. This implementation is based on Paul's original ITK-style implementation and Brian's ANTsR implementation. References include 1) H. Wang, J. W. Suh, S. Das, J. Pluta, C. Craige, P. Yushkevich, Multi-atlas segmentation with joint label fusion IEEE Trans. on Pattern Analysis and Machine Intelligence, 35(3), 611-623, 2013. and 2) H. Wang and P. A. Yushkevich, Multi-atlas segmentation with joint label fusion and corrective learning--an open source implementation, Front. Neuroinform., 2013. OPTIONS: -d, --image-dimensionality 2/3/4 This option forces the image to be treated as a specified-dimensional image. If not specified, the program tries to infer the dimensionality from the input image. -t, --target-image targetImage [targetImageModality0,targetImageModality1,...,targetImageModalityN] The target image (or multimodal target images) assumed to be aligned to a common image domain. -g, --atlas-image atlasImage [atlasImageModality0,atlasImageModality1,...,atlasImageModalityN] The atlas image (or multimodal atlas images) assumed to be aligned to a common image domain. -l, --atlas-segmentation atlasSegmentation The atlas segmentation images. For performing label fusion the number of specified segmentations should be identical to the number of atlas image sets. -a, --alpha 0.1 Regularization term added to matrix Mx for calculating the inverse. Default = 0.1 -b, --beta 2.0 Exponent for mapping intensity difference to the joint error. Default = 2.0 -r, --retain-label-posterior-images (0)/1 Retain label posterior probability images. Requires atlas segmentations to be specified. Default = false -f, --retain-atlas-voting-images (0)/1 Retain atlas voting images. Default = false -a, --constrain-nonnegative (0)/1 Constrain solution to non-negative weights. -p, --patch-radius 2 2x2x2 Patch radius for similarity measures. Default = 2x2x2 -m, --patch-metric (PC)/MSQ Metric to be used in determining the most similar neighborhood patch. Options include Pearson's correlation (PC) and mean squares (MSQ). Default = PC (Pearson correlation). -s, --search-radius 3 3x3x3 searchRadiusMap.nii.gz Search radius for similarity measures. Default = 3x3x3. One can also specify an image where the value at the voxel specifies the isotropic search radius at that voxel. -e, --exclusion-image label[exclusionImage] Specify an exclusion region for the given label. -x, --mask-image maskImageFilename If a mask image is specified, fusion is only performed in the mask region. -o, --output labelFusionImage intensityFusionImageFileNameFormat [labelFusionImage,intensityFusionImageFileNameFormat,<labelPosteriorProbabilityImageFileNameFormat>,<atlasVotingWeightImageFileNameFormat>] The output is the intensity and/or label fusion image. Additional optional outputs include the label posterior probability images and the atlas voting weight images. --version Get version information. -v, --verbose (0)/1 Verbose output. -h Print the help menu (short version). --help Print the help menu. """ dim = 3 g = ' '.join(atlas_images) tg = input_image l = ' '.join(atlas_labels) rp = 'x'.join(['%d' % el for el in rp]) rs = 'x'.join(['%d' % el for el in rs]) if mask: mask = '-x '+mask cmd = 'antsJointFusion -d %s -g %s -t %s -l %s -a %g -b %g -p %s -s %s %s -o %s' % (dim, g, tg, l, alpha, beta, rp, rs, mask, output_label) command(cmd, **exec_options) return output_label, cmd
def label_fusion_picsl_ants(input_image, atlas_images, atlas_labels, output_label, rp=[2, 2, 2], rs=[3, 3, 3], alpha=0.1, beta=2, mask='', **exec_options): """ COMMAND: antsJointFusion antsJointFusion is an image fusion algorithm developed by Hongzhi Wang and Paul Yushkevich which won segmentation challenges at MICCAI 2012 and MICCAI 2013. The original label fusion framework was extended to accommodate intensities by Brian Avants. This implementation is based on Paul's original ITK-style implementation and Brian's ANTsR implementation. References include 1) H. Wang, J. W. Suh, S. Das, J. Pluta, C. Craige, P. Yushkevich, Multi-atlas segmentation with joint label fusion IEEE Trans. on Pattern Analysis and Machine Intelligence, 35(3), 611-623, 2013. and 2) H. Wang and P. A. Yushkevich, Multi-atlas segmentation with joint label fusion and corrective learning--an open source implementation, Front. Neuroinform., 2013. OPTIONS: -d, --image-dimensionality 2/3/4 This option forces the image to be treated as a specified-dimensional image. If not specified, the program tries to infer the dimensionality from the input image. -t, --target-image targetImage [targetImageModality0,targetImageModality1,...,targetImageModalityN] The target image (or multimodal target images) assumed to be aligned to a common image domain. -g, --atlas-image atlasImage [atlasImageModality0,atlasImageModality1,...,atlasImageModalityN] The atlas image (or multimodal atlas images) assumed to be aligned to a common image domain. -l, --atlas-segmentation atlasSegmentation The atlas segmentation images. For performing label fusion the number of specified segmentations should be identical to the number of atlas image sets. -a, --alpha 0.1 Regularization term added to matrix Mx for calculating the inverse. Default = 0.1 -b, --beta 2.0 Exponent for mapping intensity difference to the joint error. Default = 2.0 -r, --retain-label-posterior-images (0)/1 Retain label posterior probability images. Requires atlas segmentations to be specified. Default = false -f, --retain-atlas-voting-images (0)/1 Retain atlas voting images. Default = false -c, --constrain-nonnegative (0)/1 Constrain solution to non-negative weights. -p, --patch-radius 2 2x2x2 Patch radius for similarity measures. Default = 2x2x2 -m, --patch-metric (PC)/MSQ Metric to be used in determining the most similar neighborhood patch. Options include Pearson's correlation (PC) and mean squares (MSQ). Default = PC (Pearson correlation). -s, --search-radius 3 3x3x3 searchRadiusMap.nii.gz Search radius for similarity measures. Default = 3x3x3. One can also specify an image where the value at the voxel specifies the isotropic search radius at that voxel. -e, --exclusion-image label[exclusionImage] Specify an exclusion region for the given label. -x, --mask-image maskImageFilename If a mask image is specified, fusion is only performed in the mask region. -o, --output labelFusionImage intensityFusionImageFileNameFormat [labelFusionImage,intensityFusionImageFileNameFormat,<labelPosteriorProbabilityImageFileNameFormat>,<atlasVotingWeightImageFileNameFormat>] The output is the intensity and/or label fusion image. Additional optional outputs include the label posterior probability images and the atlas voting weight images. --version Get version information. -v, --verbose (0)/1 Verbose output. -h Print the help menu (short version). --help Print the help menu. """ dim = 3 g = ' '.join(atlas_images) tg = input_image l = ' '.join(atlas_labels) rp = 'x'.join(['%d' % el for el in rp]) rs = 'x'.join(['%d' % el for el in rs]) if mask: mask = '-x '+mask cmd = 'antsJointFusion -d %s -g %s -t %s -l %s -a %g -b %g -p %s -s %s %s -o %s' % (dim, g, tg, l, alpha, beta, rp, rs, mask, output_label) command(cmd, **exec_options) return output_label, cmd
def ants_ApplyTransforms(input_image, reference, output_image, **exec_options): cmd = 'antsApplyTransforms -d 3 -i %s -r %s -o %s -t rigid0GenericAffine.mat' % (input_image, reference, output_image) command(cmd, **exec_options) return output_image, cmd
def bias_correct(input_image, output_image, **exec_options): cmd = 'N4BiasFieldCorrection -d 3 -i %s -o %s -b [200] -s 3 -c [50x50x30x20,1e-6]' % (input_image, output_image) command(cmd, **exec_options) return output_image, cmd
def bias_correct(input_image, output_image, **exec_options): cmd = 'N4BiasFieldCorrection -d 3 -i %s -o %s -b [200] -s 3 -c [50x50x30x20,1e-6]' % ( input_image, output_image) command(cmd, **exec_options) return output_image, cmd