Hi, I have followd Michael Notters tutorial on fmri preprocessing found here http://miykael.github.io/nipype-beginner-s-guide/firstLevel.html.
Although, I stop before he continues on to do a first-level.
I want to get the preprocessing working first. I tried with the code below.
I have just added the last few lines, trying to run it with preproc.run( ).
I seem to have a problem with nodes not running. Its something with ‘iterfield’.
Now it is specified as [‘in_file’].
Should I replace this with something?
import os
from nipype.interfaces.afni import Despike
from nipype.interfaces.freesurfer import (BBRegister, ApplyVolTransform,
Binarize, MRIConvert, FSCommand)
from nipype.interfaces.spm import (SliceTiming, Realign, Smooth, Level1Design,
EstimateModel, EstimateContrast)
from nipype.interfaces.utility import Function, IdentityInterface
from nipype.interfaces.io import FreeSurferSource, SelectFiles, DataSink
from nipype.algorithms.rapidart import ArtifactDetect
from nipype.algorithms.misc import TSNR, Gunzip
from nipype.algorithms.modelgen import SpecifySPMModel
from nipype.pipeline.engine import Workflow, Node, MapNode
MATLAB - Specify path to current SPM and the MATLAB’s default mode
from nipype.interfaces.matlab import MatlabCommand
MatlabCommand.set_default_paths(’//usr/local/MATLAB/R2012b/SPM/spm12b’)
MatlabCommand.set_default_matlab_cmd(“matlab -nodesktop -nosplash”)
FreeSurfer - Specify the location of the freesurfer folder
fs_dir = ‘/usr/local/freesurfer’
FSCommand.set_default_subjects_dir(fs_dir)
participantDir = ‘/disk2/home/granit/Link_to_TwinMR/Twins/Granits/Nipype/Output/twin_9389_20170211_102958’
os.chdir(participantDir)
#########################################
Define experiment specific parameters
#########################################
experiment_dir = ‘~/nipype_tutorial’ # location of experiment folder
subject_list = os.listdir(participantDir) # list of subject identifiers
output_dir = ‘output_fMRI_example_1st’ # name of 1st-level output folder
working_dir = ‘workingdir_fMRI_example_1st’ # name of 1st-level working directory
number_of_slices = 47 # number of slices in volume
TR = 2.396 # time repetition of volume
fwhm_size = 6 # size of FWHM in mm
#################################
Create preprocessing pipeline
#################################
Despike - Removes ‘spikes’ from the 3D+time input dataset
despike = MapNode(Despike(outputtype=‘NIFTI’),
name=“despike”, iterfield=[‘in_file’])
Slicetiming - correct for slice wise acquisition
interleaved_order = range(1,number_of_slices+1,2) + range(2,number_of_slices+1,2)
sliceTiming = Node(SliceTiming(num_slices=number_of_slices,
time_repetition=TR,
time_acquisition=TR-TR/number_of_slices,
slice_order=interleaved_order,
ref_slice=2),
name=“sliceTiming”)
Realign - correct for motion
realign = Node(Realign(register_to_mean=True),
name=“realign”)
TSNR - remove polynomials 2nd order
tsnr = MapNode(TSNR(regress_poly=2),
name=‘tsnr’, iterfield=[‘in_file’])
Artifact Detection - determine which of the images in the functional series
are outliers. This is based on deviation in intensity or movement.
art = Node(ArtifactDetect(norm_threshold=1,
zintensity_threshold=3,
mask_type=‘file’,
parameter_source=‘SPM’,
use_differences=[True, False]),
name=“art”)
Gunzip - unzip functional
gunzip = MapNode(Gunzip(), name=“gunzip”, iterfield=[‘in_file’])
Smooth - to smooth the images with a given kernel
smooth = Node(Smooth(fwhm=fwhm_size),
name=“smooth”)
FreeSurferSource - Data grabber specific for FreeSurfer data
fssource = Node(FreeSurferSource(subjects_dir=fs_dir),
run_without_submitting=True,
name=‘fssource’)
BBRegister - coregister a volume to the Freesurfer anatomical
bbregister = Node(BBRegister(init=‘header’,
contrast_type=‘t2’,
out_fsl_file=True),
name=‘bbregister’)
Volume Transformation - transform the brainmask into functional space
applyVolTrans = Node(ApplyVolTransform(inverse=True),
name=‘applyVolTrans’)
Binarize - binarize and dilate an image to create a brainmask
binarize = Node(Binarize(min=0.5,
dilate=1,
out_type=‘nii’),
name=‘binarize’)
#########################################################################
Create the preprocessing subworkflow and add all those nodes to it and#
connect them to each other
#########################################################################
Create a preprocessing workflow
preproc = Workflow(name=‘preproc’)
Connect all components of the preprocessing workflow
preproc.connect([(despike, sliceTiming, [(‘out_file’, ‘in_files’)]),
(sliceTiming, realign, [(‘timecorrected_files’, ‘in_files’)]),
(realign, tsnr, [(‘realigned_files’, ‘in_file’)]),
(tsnr, art, [(‘detrended_file’, ‘realigned_files’)]),
(realign, art, [(‘mean_image’, ‘mask_file’),
(‘realignment_parameters’,
‘realignment_parameters’)]),
(tsnr, gunzip, [(‘detrended_file’, ‘in_file’)]),
(gunzip, smooth, [(‘out_file’, ‘in_files’)]),
(realign, bbregister, [(‘mean_image’, ‘source_file’)]),
(fssource, applyVolTrans, [(‘brainmask’, ‘target_file’)]),
(bbregister, applyVolTrans, [(‘out_reg_file’, ‘reg_file’)]),
(realign, applyVolTrans, [(‘mean_image’, ‘source_file’)]),
(applyVolTrans, binarize, [(‘transformed_file’, ‘in_file’)]),
])
#########################
Run the preprocessing
#########################
preproc.base_dir = os.path.abspath(’/disk2/home/granit/Link_to_TwinMR/Twins/Granits/Nipype/Output/twin_9389_20170211_102958’)
preproc.inputs.inputspec.func = os.path.abspath(’/disk2/home/granit/Output/fp1/prox1.nii’)
preproc.inputs.inputspec.struct = os.path.abspath(’/disk2/home/granit/Output/fp1/t1.nii’)
preproc.write_graph(graph2use = ‘colored’)
preproc.run()