Child pages
  • BRAINSSnapShotWriter Python Wrapping
Skip to end of metadata
Go to start of metadata

BRAINSSnapShotWriter.py

Python wrapping for Nipype pipelining

#! /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:       BAW_Nipype/BRAINSTools/BRAINSSnapShotWriter.py
Date:          Thu Jan  5 17:29:38 2012
Author:        David Welch, dmwelch@NOSPAM.uiowa.edu                                                #
Purpose:       Wrap BRAINSSnapShotWriter.sh to interact with Nipype

Requirements:  see BRAINSSnapShotWriter --help


"""

from nipype.interfaces.base import ( File, TraitedSpec, Interface, CommandLineInputSpec, CommandLine, traits , InputMultiPath )

### Node Interface
class BRAINSSnapShotWriterInputSpec( TraitedSpec ):
    inputVolumes = InputMultiPath( File(exists = True),
                                   sep = ',',
                                   desc = 'Input image volume list to be extracted as 2D image. Multiple input is possible. \
                                   At least one input is required.',
                                   mandatory = True )
    inputBinaryVolumes = InputMultiPath( File,
                                         sep = ',',
                                         desc = 'Input mask (binary) volume list to be extracted as 2D image. Multiple input is possible.' )
    inputSliceToExtractInPhysicalPoint = InputMultiPath( traits.Float,
                                                         sep = ",",
                                                         desc = '2D slice number of input images. For autoWorkUp output, which AC-PC\
                                                         aligned, 0,0,0 will be the center.' )
    inputSliceToExtractInIndex = InputMultiPath( traits.Int,
                                                 sep = ",",
                                                 desc = '2D slice number of input images. For size of 256*256*256 image, 128 is \
                                                 usually used.')
    inputSliceToExtractInPercent = InputMultiPath( traits.Int,
                                                   sep = ",",
                                                   desc = '2D slice number of input images. Percentage input from 0%-100%. \
                                                   (ex. --inputSliceToExtractInPercent 50,50,50')
    inputPlaneDirection = InputMultiPath( traits.Int,
                                          sep = ",",
                                          desc = 'Plane to display. In general, 0=saggital, 1=coronal, and 2=axial plane.')

class BRAINSSnapShotWriterOutputSpec(TraitedSpec):
    outputFilename = File( desc = '2D file name of input images. Required.')

class BRAINSSnapShotWriter(Interface):
    """
    Example
    -------

    import BRAINSSnapShotWriter as BSSW
    bssw = BSSW()
    bssw.inputs.inputVolumes = ['file1.nii.gz', 'file2.nii.gz', 'file3.nii.gz']
    bssw.inputs.inputBinaryVolumes = ['binaryfile1.nii.gz']

    """
    input_spec = BRAINSSnapShotWriterInputSpec
    output_spec = BRAINSSnapShotWriterOutputSpec

    def _list_outputs(self):
        """
        The BRAINSSnapShot script
        """
        from os.path import abspath
        outputs = self.output_spec().get()
        outputs['outputFilename'] = abspath( self.inputs.outputFilename )
        return outputs

### CommandLine
class BRAINSSnapShotWriterCLInputSpec( TraitedSpec ):
    inputVolumes = InputMultiPath( File(exists = True),
                                   sep = ',',
                                   desc = 'Input image volume list to be extracted as 2D image. Multiple input is possible. \
                                   At least one input is required.',
                                   mandatory = True,
                                   argstr = '--inputVolumes %s ...')
    inputBinaryVolumes = InputMultiPath( File,
                                         sep = ',',
                                         desc = 'Input mask (binary) volume list to be extracted as 2D image. Multiple input is possible.',
                                         argstr = '--inputBinaryVolumes %s ...')
    inputSliceToExtractInPhysicalPoint = InputMultiPath( traits.Float,
                                                         sep = ",",
                                                         desc = '2D slice number of input images. For autoWorkUp output, which AC-PC\
                                                         aligned, 0,0,0 will be the center.',
                                                         argstr = '--inputSliceToExtractInPhysicalPoint %s')
    inputSliceToExtractInIndex = InputMultiPath( traits.Int,
                                                 sep = ",",
                                                 desc = '2D slice number of input images. For size of 256*256*256 image, 128 is \
                                                 usually used.',
                                                 argstr = '--inputSliceToExtractInIndex %s')
    inputSliceToExtractInPercent = InputMultiPath( traits.Int,
                                                   sep = ",",
                                                   desc = '2D slice number of input images. Percentage input from 0%-100%. \
                                                   (ex. --inputSliceToExtractInPercent 50,50,50',
                                                   argstr = '--inputSliceToExtractInPercent %s')
    inputPlaneDirection = InputMultiPath( traits.Int,
                                          sep = ",",
                                          desc = 'Plane to display. In general, 0=saggital, 1=coronal, and 2=axial plane.',
                                          argstr = '--inputPlaneDirection %s')

class BRAINSSnapShotWriterCLOutputSpec(TraitedSpec):
    outputFilename = File( desc = '2D file name of input images. Required.')

class BRAINSSnapShotWriter(CommandLine):
    _cmd = 'BRAINSSnapShotWriter.sh'
    input_spec = BRAINSSnapShotWriterCLInputSpec
    output_spec = BRAINSSnapShotWriterCLOutputSpec

    def _list_outputs(self):
        """
        The BRAINSSnapShot script
        """
        from os.path import abspath
        outputs = self.output_spec().get()
        outputs['outputFilename'] = abspath( self.inputs.outputFilename )
        return outputs

if __name__ == '__main__':
    bssw = BRAINSSnapShotWriter(sys.argv)
    bssw.run()

Note:
The inputVolumes and inputBinaryVolumes will need to be gathered in Nipype using a Merge node