Nipype: SPM realignment multiple sessions

Ahoi hoi @Sebastian,

in the end it kinda depends on your dataset and it’s structure, as well as the preprocessing and subsequent analyses steps you’ve planed.
But for now, assuming you have a dataset (of course in BIDS) that contains 6 functional runs of the same task for a bunch of participants and you want to apply realignment (via SPM) and coregistration (via FreeSurfer) something like the following should work:

import nipype.interfaces.freesurfer as fs  
import nipype.interfaces.spm as spm  
import nipype.interfaces.utility as util 
import nipype.interfaces.io as io   
import nipype.algorithms.misc as mc  
import nipype.pipeline.engine as eng  

# Gunzip node - unzip functional images, as SPM can't read .gz
gunzip = eng.MapNode(mc.Gunzip(), name="gunzip", iterfield=['in_file'])

# realign node - register functional images to the mean functional
realign = eng.Node(spm.Realign(register_to_mean=True),
                   name="realign")

# coregistration node - coregister the mean functional to the anatomical image
bbregister = eng.Node(fs.BBRegister(init='spm',
                             contrast_type='t2',
                             out_fsl_file=True),
                      name='bbregister')

# Create a preprocessing workflow
preproc = eng.Workflow(name='preproc')
preproc.base_dir = opj(experiment_dir, working_dir)

# Connect all components of the preprocessing workflow  
preproc_masks.connect([(gunzip, realign, [('out_file', 'in_files')]), 
                       (realign, bbregister, [('mean_image', 'source_file')]),]) 

# Infosource - a function free node to iterate over the list of subject names
infosource = eng.Node(util.IdentityInterface(fields=['subject_id']),
                      name="infosource")
infosource.iterables = [('subject_id', subject_list)]

# SelectFiles - to grab the data 
templates = {'func': 'bids_dataset/{subject_id}/func/task-test_run-*_bold.nii.gz'}
selectfiles = eng.Node(io.SelectFiles(templates,
                                      base_directory=experiment_dir),
                       name="selectfiles")

# connect Infosource and SelectFiles to the preprocessing workflow
preproc.connect([(infosource, selectfiles, [('subject_id', 'subject_id')]),
                 (infosource, bbregister, [('subject_id', 'subject_id')]),
                 (selectfiles, gunzip, [('func', 'in_file')]),])

(This of course doesn’t include necessary paths like the experiment, working and FreeSurfer directory, or the subject list.)

Based on that you can continue by e.g. coregister & transform the functional runs to a certain reference space via ANTs.

HTH, best, Peer

1 Like