Skip to content

Commit

Permalink
ENH: Added pipeline for BRAINSTools to BRAINSTools
Browse files Browse the repository at this point in the history
  • Loading branch information
hjmjohnson committed Mar 30, 2012
1 parent 1ea9336 commit ad67fbf
Show file tree
Hide file tree
Showing 51 changed files with 3,587 additions and 0 deletions.
91 changes: 91 additions & 0 deletions AutoWorkup/BRAINSTools/ANTSWrapper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
#! /usr/bin/env python
# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*-
# vi: set ft=python sts=4 ts=4 sw=4 et:
"""
Program: PINC/nipype/Wrappers/ants.py
Date: Tue Jan 3 10:47:45 2012
Author: David Welch, [email protected] #
Purpose: Wrap a Bash script to interface ANTS with Nipype
Requirements: <<< Interface specifications >>>
$ ANTS_wrapper.sh <FixedT1Image> <FixedT2Image> <MovingT1Image> <MovingT2Image> <OutputPrefix>
where the script implicitly generates three output files:
<OutputPrefix>Affine.txt
<OutputPrefix>Warp.nii.gz
<OutputPrefix>InverseWarp.nii.gz
"""
from nipype.interfaces.base import ( File, TraitedSpec, Interface, CommandLineInputSpec, CommandLine, traits )

### Node Interface
class ANTSWrapperInputSpec( TraitedSpec ):
fixed_T1_image = File( desc = "T1 Fixed Volume", exists = True, mandatory = True)
fixed_T2_image = File( desc = "T2 Fixed Volume", exists = True, mandatory = True)
moving_T1_image = File( desc = "T1 Moving Volume", exists = True, mandatory = True)
moving_T2_image = File( desc = "T2 Moving Volume", exists = True, mandatory = True)
prefix_desc = "Output volume prefix for Affine.txt, Warp.nii.gz, and InverseWarp.nii.gz"
output_prefix = traits.Str( desc = prefix_desc, exists = True, mandatory = False,
default_value = "./ANTS_")

class ANTSWrapperOutputSpec(TraitedSpec):
output_affine = File( desc = "Affine Transform Text File")
output_warp = File( desc = "Warped Output Volume")
output_invwarp = File( desc = "Inverse Warp Output Volume")

class ANTSWrapper(Interface):
input_spec = ANTSWrapperInputSpec
output_spec = ANTSWrapperOutputSpec

def _list_outputs(self):
"""
The ANTS package implicitly outputs three files based on the prefix given at the commandline:
{prefix}Affine.txt, {prefix}Warp.nii.gz, and {prefix}InverseWarp.nii.gz. This function exposes
them for subsequent nodes.
"""
from os.path import abspath
outputs = self.output_spec().get()
outputs['output_affine'] = abspath( self.inputs.output_prefix + 'Affine.txt')
outputs['output_warp'] = abspath( self.inputs.output_prefix + 'Warp.nii.gz')
outputs['output_invwarp'] = abspath( self.inputs.output_prefix + 'InverseWarp.nii.gz')
return outputs

### CommandLine
class ANTSWrapperCLInputSpec(CommandLineInputSpec):
fixed_T1_image = File(desc="T1 Fixed Volume", exists=True, mandatory=True, position=0, argstr="%s" )
fixed_T2_image = File(desc="T2 Fixed Volume", exists=True, mandatory=True, position=1, argstr="%s" )
moving_T1_image = File(desc="T1 Moving Volume", exists=True, mandatory=True, position=2, argstr="%s" )
moving_T2_image = File(desc="T2 Moving Volume", exists=True, mandatory=True, position=3, argstr="%s" )
prefix_desc = "Output volume prefix for Affine.txt, Warp.nii.gz, and InverseWarp.nii.gz"
output_prefix = traits.Str(desc=prefix_desc, exists=True, mandatory=False, position=4, argstr="%s",
default_value="./ANTS_" )

class ANTSWrapperCLOutputSpec(CommandLineInputSpec):
output_affine = File( desc = "Affine Transform Text File", exists = True, mandatory = False)
output_warp = File( desc = "Warped Output Volume", exists = True, mandatory = True )
output_invwarp = File( desc = "Inverse Warp Output Volume", exists = True, mandatory = False)

class ANTSWrapper(CommandLine):
_cmd = 'ANTS_wrapper.sh'
input_spec = ANTSWrapperCLInputSpec
output_spec = ANTSWrapperCLOutputSpec

def _list_outputs(self):
"""
The ANTS package implicitly outputs three files based on the prefix given at the commandline:
{prefix}Affine.txt, {prefix}Warp.nii.gz, and {prefix}InverseWarp.nii.gz. This function exposes
them for subsequent nodes.
"""
from os.path import abspath
outputs = self.output_spec().get()
outputs['output_affine'] = abspath( self.inputs.output_prefix + 'Affine.txt')
outputs['output_warp'] = abspath( self.inputs.output_prefix + 'Warp.nii.gz')
outputs['output_invwarp'] = abspath( self.inputs.output_prefix + 'InverseWarp.nii.gz')
return outputs

if __name__ == '__main__':
ants = ANTSWrapper(sys.argv)
ants.run()
73 changes: 73 additions & 0 deletions AutoWorkup/BRAINSTools/BRAINSABC.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
from nipype.interfaces.base import CommandLine, CommandLineInputSpec, TraitedSpec, File, Directory, traits, isdefined, InputMultiPath, OutputMultiPath
import os

class BRAINSABCInputSpec(CommandLineInputSpec):
inputVolumes = InputMultiPath(File(exists=True), argstr = "--inputVolumes %s...")
atlasDefinition = File( exists = True,argstr = "--atlasDefinition %s")
inputVolumeTypes = InputMultiPath(traits.Str, sep = ",",argstr = "--inputVolumeTypes %s")
outputDir = traits.Either(traits.Bool, Directory(), hash_files = False,argstr = "--outputDir %s")
atlasToSubjectTransformType = traits.Enum("ID","Rigid","Affine","BSpline", argstr = "--atlasToSubjectTransformType %s")
atlasToSubjectTransform = traits.Either(traits.Bool, File(), hash_files = False,argstr = "--atlasToSubjectTransform %s")
atlasToSubjectInitialTransform = traits.Either(traits.Bool, File(), hash_files = False,argstr = "--atlasToSubjectInitialTransform %s")
subjectIntermodeTransformType = traits.Enum("ID","Rigid","Affine","BSpline", argstr = "--subjectIntermodeTransformType %s")
outputVolumes = traits.Either(traits.Bool, InputMultiPath(File(),), hash_files = False,argstr = "--outputVolumes %s...")
outputLabels = traits.Either(traits.Bool, File(), hash_files = False,argstr = "--outputLabels %s")
outputDirtyLabels = traits.Either(traits.Bool, File(), hash_files = False,argstr = "--outputDirtyLabels %s")
posteriorTemplate = traits.Str( argstr = "--posteriorTemplate %s")
outputFormat = traits.Enum("NIFTI","Meta","Nrrd", argstr = "--outputFormat %s")
resamplerInterpolatorType = traits.Enum("BSpline","NearestNeighbor","WindowedSinc","Linear","ResampleInPlace","Hamming","Cosine","Welch","Lanczos","Blackman", argstr = "--interpolationMode %s")
maxIterations = traits.Int( argstr = "--maxIterations %d")
medianFilterSize = InputMultiPath(traits.Int, sep = ",",argstr = "--medianFilterSize %s")
filterIteration = traits.Int( argstr = "--filterIteration %d")
filterTimeStep = traits.Float( argstr = "--filterTimeStep %f")
filterMethod = traits.Enum("None","CurvatureFlow","GradientAnisotropicDiffusion","Median", argstr = "--filterMethod %s")
maxBiasDegree = traits.Int( argstr = "--maxBiasDegree %d")
atlasWarpingOff = traits.Bool( argstr = "--atlasWarpingOff ")
gridSize = InputMultiPath(traits.Int, sep = ",",argstr = "--gridSize %s")
defaultSuffix = traits.Str( argstr = "--defaultSuffix %s")
implicitOutputs = traits.Either(traits.Bool, InputMultiPath(File(),), hash_files = False,argstr = "--implicitOutputs %s...")
debuglevel = traits.Int( argstr = "--debuglevel %d")
writeLess = traits.Bool( argstr = "--writeLess ")
numberOfThreads = traits.Int( argstr = "--numberOfThreads %d")


class BRAINSABCOutputSpec(TraitedSpec):
outputDir = Directory( exists = True)
atlasToSubjectTransform = File( exists = True)
atlasToSubjectInitialTransform = File( exists = True)
outputVolumes = OutputMultiPath(File(exists=True), exists = True)
outputLabels = File( exists = True)
outputDirtyLabels = File( exists = True)
implicitOutputs = OutputMultiPath(File(exists=True), exists = True)


class BRAINSABC(CommandLine):

input_spec = BRAINSABCInputSpec
output_spec = BRAINSABCOutputSpec
_cmd = " BRAINSABC "
_outputs_filenames = {'outputLabels':'outputLabels.nii.gz','atlasToSubjectTransform':'atlasToSubjectTransform.mat','atlasToSubjectInitialTransform':'atlasToSubjectInitialTransform.mat','outputDirtyLabels':'outputDirtyLabels.nii.gz','outputVolumes':'outputVolumes.nii.gz','outputDir':'outputDir','implicitOutputs':'implicitOutputs.nii.gz'}

def _list_outputs(self):
outputs = self.output_spec().get()
for name in outputs.keys():
coresponding_input = getattr(self.inputs, name)
if isdefined(coresponding_input):
if isinstance(coresponding_input, bool) and coresponding_input == True:
outputs[name] = os.path.abspath(self._outputs_filenames[name])
else:
if isinstance(coresponding_input, list):
outputs[name] = [os.path.abspath(inp) for inp in coresponding_input]
else:
outputs[name] = os.path.abspath(coresponding_input)
return outputs

def _format_arg(self, name, spec, value):
if name in self._outputs_filenames.keys():
if isinstance(value, bool):
if value == True:
value = os.path.abspath(self._outputs_filenames[name])
else:
return ""
return super(BRAINSABC, self)._format_arg(name, spec, value)

53 changes: 53 additions & 0 deletions AutoWorkup/BRAINSTools/BRAINSAlignMSP.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
from nipype.interfaces.base import CommandLine, CommandLineInputSpec, TraitedSpec, File, Directory, traits, isdefined, InputMultiPath, OutputMultiPath
import os

class BRAINSAlignMSPInputSpec(CommandLineInputSpec):
inputVolume = File( exists = True,argstr = "--inputVolume %s")
resampleMSP = traits.Either(traits.Bool, File(), hash_files = False,argstr = "--OutputresampleMSP %s")
verbose = traits.Bool( argstr = "--verbose ")
resultsDir = traits.Either(traits.Bool, Directory(), hash_files = False,argstr = "--resultsDir %s")
writedebuggingImagesLevel = traits.Int( argstr = "--writedebuggingImagesLevel %d")
mspQualityLevel = traits.Int( argstr = "--mspQualityLevel %d")
rescaleIntensities = traits.Bool( argstr = "--rescaleIntensities ")
trimRescaledIntensities = traits.Float( argstr = "--trimRescaledIntensities %f")
rescaleIntensitiesOutputRange = InputMultiPath(traits.Int, sep = ",",argstr = "--rescaleIntensitiesOutputRange %s")
backgroundFillValueString = traits.Str( argstr = "--BackgroundFillValue %s")
interpolationMode = traits.Enum("NearestNeighbor","Linear","ResampleInPlace","BSpline","WindowedSinc","Hamming","Cosine","Welch","Lanczos","Blackman", argstr = "--interpolationMode %s")
numberOfThreads = traits.Int( argstr = "--numberOfThreads %d")


class BRAINSAlignMSPOutputSpec(TraitedSpec):
resampleMSP = File( exists = True)
resultsDir = Directory( exists = True)


class BRAINSAlignMSP(CommandLine):

input_spec = BRAINSAlignMSPInputSpec
output_spec = BRAINSAlignMSPOutputSpec
_cmd = " BRAINSAlignMSP "
_outputs_filenames = {'resampleMSP':'resampleMSP.nii','resultsDir':'resultsDir'}

def _list_outputs(self):
outputs = self.output_spec().get()
for name in outputs.keys():
coresponding_input = getattr(self.inputs, name)
if isdefined(coresponding_input):
if isinstance(coresponding_input, bool) and coresponding_input == True:
outputs[name] = os.path.abspath(self._outputs_filenames[name])
else:
if isinstance(coresponding_input, list):
outputs[name] = [os.path.abspath(inp) for inp in coresponding_input]
else:
outputs[name] = os.path.abspath(coresponding_input)
return outputs

def _format_arg(self, name, spec, value):
if name in self._outputs_filenames.keys():
if isinstance(value, bool):
if value == True:
value = os.path.abspath(self._outputs_filenames[name])
else:
return ""
return super(BRAINSAlignMSP, self)._format_arg(name, spec, value)

45 changes: 45 additions & 0 deletions AutoWorkup/BRAINSTools/BRAINSClipInferior.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
from nipype.interfaces.base import CommandLine, CommandLineInputSpec, TraitedSpec, File, Directory, traits, isdefined, InputMultiPath, OutputMultiPath
import os

class BRAINSClipInferiorInputSpec(CommandLineInputSpec):
inputVolume = File( exists = True,argstr = "--inputVolume %s")
outputVolume = traits.Either(traits.Bool, File(), hash_files = False,argstr = "--outputVolume %s")
acLowerBound = traits.Float( argstr = "--acLowerBound %f")
backgroundFillValueString = traits.Str( argstr = "--BackgroundFillValue %s")
numberOfThreads = traits.Int( argstr = "--numberOfThreads %d")


class BRAINSClipInferiorOutputSpec(TraitedSpec):
outputVolume = File( exists = True)


class BRAINSClipInferior(CommandLine):

input_spec = BRAINSClipInferiorInputSpec
output_spec = BRAINSClipInferiorOutputSpec
_cmd = " BRAINSClipInferior "
_outputs_filenames = {'outputVolume':'outputVolume.nii'}

def _list_outputs(self):
outputs = self.output_spec().get()
for name in outputs.keys():
coresponding_input = getattr(self.inputs, name)
if isdefined(coresponding_input):
if isinstance(coresponding_input, bool) and coresponding_input == True:
outputs[name] = os.path.abspath(self._outputs_filenames[name])
else:
if isinstance(coresponding_input, list):
outputs[name] = [os.path.abspath(inp) for inp in coresponding_input]
else:
outputs[name] = os.path.abspath(coresponding_input)
return outputs

def _format_arg(self, name, spec, value):
if name in self._outputs_filenames.keys():
if isinstance(value, bool):
if value == True:
value = os.path.abspath(self._outputs_filenames[name])
else:
return ""
return super(BRAINSClipInferior, self)._format_arg(name, spec, value)

Loading

0 comments on commit ad67fbf

Please sign in to comment.