Performing a basic two-sample t-test in SPM using nipype.interfaces.spm

Hi all,
I’m performing an analysis in Jupyter and would like to run a straightforward two-sample t-test using SPM. I already have two lists of input files (single 3D r>Z fcMRI maps) for my two groups, and am trying to use nipype.interfaces.spm to do this.

Any thoughts on an easier way to do this, and or what I’m doing wrong?

-Alex

Here is what I have so far:

import nipype.interfaces.spm as spm

TargetGroup_Fz_nii_files = ["./Functional_Connectivity_nii/"+str(entry)+"_seed_AvgR_Fz.nii" for entry in TargetGroup]
Controls_Fz_nii_files =  ["./Functional_Connectivity_nii/"+str(entry)+"_seed_AvgR_Fz.nii" for entry in Controls]
    
ttest = spm.TwoSampleTTestDesign()
ttest.inputs.group1_files = TargetGroup_Fz_nii_files
ttest.inputs.group2_files = Controls_Fz_nii_files
ttest.run()

est = spm.EstimateModel()
est.inputs.spm_mat_file = 'SPM.mat'
est.inputs.estimation_method = {'Classical': 1}
est.run()

est = spm.EstimateContrast()
est.inputs.spm_mat_file = 'SPM.mat'
est.inputs.residual_image = 'ResMS.nii'
est.inputs.beta_images = ['beta_0001.nii','beta_0002.nii']
cont1 = ('TargetGroup','T', ['Group_{1}'],[1])
cont2 = ('Controls','T', ['Group_{2}'],[1])
cont3 = ('TargetGroup>Controls','T', ['Group_{1}','Group_{2}'],[1,-1])
cont4 = ('Controls>TargetGroup','T', ['Group_{1}','Group_{2}'],[-1,1])
contrasts = [cont1,cont2,cont3,cont4]
est.inputs.contrasts = contrasts
est.run()

Which gives me the following error:

---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-458-751309d3240a> in <module>()
     22 contrasts = [cont1,cont2,cont3,cont4]
     23 est.inputs.contrasts = contrasts
---> 24 est.run()

/usr/lib/python2.7/site-packages/nipype/interfaces/base.pyc in run(self, **inputs)
   1041                         version=self.version)
   1042         try:
-> 1043             runtime = self._run_wrapper(runtime)
   1044             outputs = self.aggregate_outputs(runtime)
   1045             runtime.endTime = dt.isoformat(dt.utcnow())

/usr/lib/python2.7/site-packages/nipype/interfaces/base.pyc in _run_wrapper(self, runtime)
    998             runtime.environ['DISPLAY'] = ':%d' % vdisp_num
    999 
-> 1000         runtime = self._run_interface(runtime)
   1001 
   1002         if self._redirect_x:

/usr/lib/python2.7/site-packages/nipype/interfaces/spm/base.pyc in _run_interface(self, runtime)
    309         self.mlab.inputs.script = self._make_matlab_command(
    310             deepcopy(self._parse_inputs()))
--> 311         results = self.mlab.run()
    312         runtime.returncode = results.runtime.returncode
    313         if self.mlab.inputs.uses_mcr:

/usr/lib/python2.7/site-packages/nipype/interfaces/base.pyc in run(self, **inputs)
   1041                         version=self.version)
   1042         try:
-> 1043             runtime = self._run_wrapper(runtime)
   1044             outputs = self.aggregate_outputs(runtime)
   1045             runtime.endTime = dt.isoformat(dt.utcnow())

/usr/lib/python2.7/site-packages/nipype/interfaces/base.pyc in _run_wrapper(self, runtime)
   1658 
   1659     def _run_wrapper(self, runtime):
-> 1660         runtime = self._run_interface(runtime)
   1661         return runtime
   1662 

/usr/lib/python2.7/site-packages/nipype/interfaces/matlab.pyc in _run_interface(self, runtime)
    147             pass
    148         if 'MATLAB code threw an exception' in runtime.stderr:
--> 149             self.raise_exception(runtime)
    150         return runtime
    151 

/usr/lib/python2.7/site-packages/nipype/interfaces/base.pyc in raise_exception(self, runtime)
   1616         message += "Standard error:\n" + runtime.stderr + "\n"
   1617         message += "Return code: " + str(runtime.returncode)
-> 1618         raise RuntimeError(message)
   1619 
   1620     @classmethod

RuntimeError: Command:
matlab -nodesktop -nosplash -singleCompThread -r "addpath('/home/user/projects/test');pyscript_estimatecontrast;exit"
Standard output:
MATLAB is selecting SOFTWARE OPENGL rendering.

                            < M A T L A B (R) >
                  Copyright 1984-2016 The MathWorks, Inc.
                   R2016b (9.1.0.441655) 64-bit (glnxa64)
                             September 7, 2016

 
To get started, type one of these: helpwin, helpdesk, or demo.
For product information, visit www.mathworks.com.
 
Executing pyscript_estimatecontrast at 31-May-2017 15:35:44:
----------------------------------------------------------------------------------------------------
MATLAB Version: 9.1.0.441655 (R2016b)
MATLAB License Number: 339659
Operating System: Linux 3.10.0-514.6.2.el7.x86_64 #1 SMP Thu Feb 23 03:04:39 UTC 2017 x86_64
Java Version: Java 1.7.0_60-b19 with Oracle Corporation Java HotSpot(TM) 64-Bit Server VM mixed mode
----------------------------------------------------------------------------------------------------
MATLAB                                                Version 9.1         (R2016b)
Curve Fitting Toolbox                                 Version 3.5.4       (R2016b)
Image Processing Toolbox                              Version 9.5         (R2016b)
Mapping Toolbox                                       Version 4.4         (R2016b)
Optimization Toolbox                                  Version 7.5         (R2016b)
Parallel Computing Toolbox                            Version 6.9         (R2016b)
Signal Processing Toolbox                             Version 7.3         (R2016b)
Statistical Parametric Mapping                        Version 6906        (SPM12) 
Statistics and Machine Learning Toolbox               Version 11.0        (R2016b)
Direct calls to spm_defauts are deprecated.
Please use spm('Defaults',modality) or spm_get_defaults instead.

Standard error:
MATLAB code threw an exception:
Index exceeds matrix dimensions.
File:/home/user/projects/test/pyscript_estimatecontrast.m
Name:pyscript_estimatecontrast
Line:14

Return code: 0
Interface MatlabCommand failed to run. 
Interface EstimateContrast failed to run.

hi, i am new to fmri data analysis and getting into the same trouble with spm.twosamplettestdes, if you’ve got the solution, could you please give me any suggestion:grin:

not sure if still relevant.
In the estimate contrasts node you should add ‘group_contrast = True’
You can take a look at this link for an example of working code (not mine, but I found it out while searching for a solution myself)