Skip to content

Commit f907499

Browse files
committed
added 3dCM interface
1 parent 2f422f0 commit f907499

File tree

2 files changed

+95
-1
lines changed

2 files changed

+95
-1
lines changed

nipype/interfaces/afni/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
TShift, Volreg, Warp, QwarpPlusMinus, Qwarp)
2121
from .svm import (SVMTest, SVMTrain)
2222
from .utils import (ABoverlap, AFNItoNIFTI, Autobox, Axialize, BrickStat,
23-
Bucket, Calc, Cat, CatMatvec, Copy, Dot,
23+
Bucket, Calc, Cat, CatMatvec, CenterMass, Copy, Dot,
2424
Edge3, Eval, FWHMx, MaskTool, Merge, Notes, NwarpApply,
2525
OneDToolPy,
2626
Refit, Resample, TCat, TCatSubBrick, TStat, To3D, Unifize,

nipype/interfaces/afni/utils.py

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -642,6 +642,100 @@ def _format_arg(self, name, spec, value):
642642
return spec.argstr%(' '.join([i[0]+' -'+i[1] for i in value]))
643643
return super(CatMatvec, self)._format_arg(name, spec, value)
644644

645+
646+
class CenterMassInputSpec(CommandLineInputSpec):
647+
in_file = File(
648+
desc='input file to 3dCM',
649+
argstr='%s',
650+
position=-2,
651+
mandatory=True,
652+
exists=True,
653+
copyfile=True)
654+
cm_file = File(
655+
name_source='in_file',
656+
name_template='%s_cm.out',
657+
keep_extension=False,
658+
descr="File to write center of mass to",
659+
argstr=" > %s",
660+
position=-1)
661+
mask_file = File(
662+
desc='Only voxels with nonzero values in the provided mask will be '
663+
'averaged.',
664+
argstr='-mask %s',
665+
exists=True)
666+
automask = traits.Bool(
667+
desc='Generate the mask automatically',
668+
argstr='-automask')
669+
set_cm = traits.Tuple(
670+
(traits.Float(), traits.Float(), traits.Float()),
671+
desc='After computing the center of mass, set the origin fields in '
672+
'the header so that the center of mass will be at (x,y,z) in '
673+
'DICOM coords.',
674+
argstr='-set %f %f %f')
675+
local_ijk = traits.Bool(
676+
desc='Output values as (i,j,k) in local orienation',
677+
argstr='-local_ijk')
678+
roi_vals = traits.List(
679+
traits.Int,
680+
desc='Compute center of mass for each blob with voxel value of v0, '
681+
'v1, v2, etc. This option is handy for getting ROI centers of '
682+
'mass.',
683+
argstr='-roi_vals %s')
684+
automask = traits.Bool(
685+
desc='Don\'t bother listing the values of ROIs you want: The program '
686+
'will find all of them and produce a full list',
687+
argstr='-all_rois')
688+
689+
690+
class CenterMassOutputSpec(TraitedSpec):
691+
out_file = File(
692+
exists=True,
693+
desc='output file')
694+
cm_file = File(
695+
desc='file with the center of mass coordinates')
696+
cm = traits.Tuple(
697+
traits.Float(), traits.Float(), traits.Float(),
698+
desc='center of mass')
699+
700+
701+
class CenterMass(AFNICommandBase):
702+
"""Computes center of mass using 3dCM command
703+
704+
.. note::
705+
706+
By default, the output is (x,y,z) values in DICOM coordinates. But
707+
as of Dec, 2016, there are now command line switches for other options.
708+
709+
710+
For complete details, see the `3dCM Documentation.
711+
<https://afni.nimh.nih.gov/pub/dist/doc/program_help/3dCM.html>`_
712+
713+
Examples
714+
========
715+
716+
>>> from nipype.interfaces import afni
717+
>>> cm = afni.CenterMass()
718+
>>> cm.inputs.in_file = 'structural.nii'
719+
>>> cm.inputs.out_file = 'cm.txt'
720+
>>> cm.inputs.set_cm = (0, 0, 0)
721+
>>> cm.cmdline # doctest: +ALLOW_UNICODE
722+
'3dcm -set 0 0 0 structural.nii > cm.txt'
723+
>>> res = 3dcm.run() # doctest: +SKIP
724+
"""
725+
726+
_cmd = '3dCM'
727+
input_spec = CenterMassInputSpec
728+
output_spec = CenterMassOutputSpec
729+
730+
def _list_outputs(self):
731+
outputs = super(CenterMass, self)._list_outputs()
732+
outputs['out_file'] = os.path.abspath(self.inputs.in_file)
733+
outputs['cm_file'] = os.path.abspath(self.inputs.cm_file)
734+
sout = np.loadtxt(outputs['cm_file']) # pylint: disable=E1101
735+
outputs['cm'] = tuple(sout)
736+
return outputs
737+
738+
645739
class CopyInputSpec(AFNICommandInputSpec):
646740
in_file = File(
647741
desc='input file to 3dcopy',

0 commit comments

Comments
 (0)