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