Skip to content

Instantly share code, notes, and snippets.

@satra
Created August 11, 2011 17:17
Show Gist options
  • Select an option

  • Save satra/1140223 to your computer and use it in GitHub Desktop.

Select an option

Save satra/1140223 to your computer and use it in GitHub Desktop.
3dcalc test interface
In [2]: dcalc = Threedcalc()
In [3]: dcalc.inputs.infile_a = 'struct.nii'
In [4]: dcalc.inputs.expr = 'a'
In [5]: dcalc.cmdline
Out[5]: '3dcalc -a struct.nii -expr a -prefix /software/temp/struct_3dcalc.nii'
In [6]: dcalc.inputs.start_idx = 1
/software/pysoft/nipype/nipype/interfaces/base.py:349: UserWarning: Input start_idx requires inputs: stop_idx
warn(msg)
In [7]: dcalc.inputs.stop_idx = 4
In [8]: dcalc.cmdline
Out[8]: '3dcalc -a struct.nii[1...4] -expr a -prefix /software/temp/struct_3dcalc.nii'
import os
from nipype.interfaces.base import (CommandLineInputSpec, CommandLine,
TraitedSpec, traits, isdefined, File)
from nipype.utils.filemanip import split_filename
"""
3dcalc -a ${rest}.nii.gz[${TRstart}..${TRend}] -expr 'a' -prefix $
{rest}_dr.nii.gz
3dcalc -a ${rest}_mc.nii.gz -b ${rest}_mask.nii.gz -expr 'a*b' -prefix
${rest}_ss.nii.gz
"""
class ThreedcalcInputSpec(CommandLineInputSpec):
infile_a = File(desc = 'input file to 3dcalc',
argstr = '-a %s', position = 0, mandatory = True)
infile_b = File(desc = 'operand file to 3dcalc',
argstr = ' -b %s', position = 1)
expr = traits.Str(desc = 'expr', argstr = '-expr %s', position = 2,
mandatory = True)
out_file = File(desc = 'output file from 3dFourier', argstr = '-prefix %s',
position = -1, genfile=True)
start_idx = traits.Int(desc='start index for infile_a',
requires=['stop_idx'])
stop_idx = traits.Int(desc='stop index for infile_a',
requires=['start_idx'])
other = File(desc = 'other options', argstr = '')
class ThreedcalcOutputSpec(TraitedSpec):
out_file = File(desc = ' output file', exists = True)
class Threedcalc(CommandLine):
"""Merge or edit volumes using AFNI 3dmerge command.
For complete details, see the `3dcalc Documentation.
<http://afni.nimh.nih.gov/pub/dist/doc/program_help/3dcalc.html>`_
"""
_cmd = '3dcalc'
input_spec = ThreedcalcInputSpec
output_spec = ThreedcalcOutputSpec
def _list_outputs(self):
outputs = self.output_spec().get()
outputs['out_file'] = os.path.abspath(self.inputs.out_file)
if not isdefined(outputs['out_file']):
outputs['out_file'] = self._gen_filename('out_file')
return outputs
def _format_arg(self, name, trait_spec, value):
if name == 'infile_a':
arg = trait_spec.argstr%value
if isdefined(self.inputs.start_idx):
arg += '[%d...%d]'%(self.inputs.start_idx, self.inputs.stop_idx)
return arg
return super(Threedcalc, self)._format_arg(name, trait_spec, value)
def _parse_inputs(self, skip=None):
"""Skip the arguments without argstr metadata
"""
return super(Threedcalc, self)._parse_inputs(skip=('start_idx',
'stop_idx',
'other'))
def _gen_filename(self, name):
"""Generate output file name
"""
if name == 'out_file':
_, fname, ext = split_filename(self.inputs.infile_a)
return os.path.join(os.getcwd(), ''.join((fname, '_3dcalc',
ext)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment