Nipype Preprocessing fail

nipype

#1

import os
import nipype.interfaces.spm as spm
import nipype.pipeline.engine as pe
import nipype.interfaces.io as nio
from nipype import SelectFiles, IdentityInterface, DataSink

from nipype.interfaces.matlab import MatlabCommand

MatlabCommand.set_default_paths(’//usr/local/MATLAB/R2012b/SPM/spm12b’)
MatlabCommand.set_default_matlab_cmd(“matlab -nodesktop -nosplash”)

participantDir = ‘/home/Output’
participant_list = [‘fp1’, ‘fp2’ …]
session_list = [‘test1’, ‘test2’]

infosource = pe.Node(interface=IdentityInterface(fields=[‘participant_list’, ‘session_list’]),
name = “infosource”, base_dir=participantDir)
infosource.iterables = [(‘participant_list’, participant_list),
(‘session_list’, session_list)]

templates = {“T1”: os.path.join(participantDir, “{participant_list}/t1/t1.nii”),
“epi”: os.path.join(participantDir, “{participant_list}/{session_list}/{session_list}.nii”)}
selectfiles = pe.Node(SelectFiles(templates), name=“selectfiles”, base_dir= participantDir)

datasink = pe.Node(interface=DataSink(),
name = “datasink”)
datasink.base_dir = participantDir

DataSink output substitutions

substitutions = [(‘participant_list’, ‘’),
(‘session_list’, ‘’)]

datasink.inputs.substitutions = substitutions

Realign

realign = pe.Node(interface=spm.Realign(register_to_mean = True),
name=‘realign’)

preprocess = pe.Workflow(name=‘preproc’)
preprocess.connect([(infosource, selectfiles, [(‘participant_list’, ‘participant_list’),
(‘session_list’, ‘session_list’)]),
(selectfiles, realign, [(‘epi’, ‘in_files’)]),
(realign, datasink, [(‘mean_image’, ‘realign.mean’),
(‘realignment_parameters’, ‘realign.parameters’)])])

preprocess.run()

I get a lot of output information and crash files. It seems that it is looking in the wrong directory.

/tmp/tmpjG5LYz/preproc/_participant_list_fp1_session_list_test1/selectfiles/fp1/test1/test1.nii

So, basically, whyy is it using this directory /tmp/tmp…/preproc/_participant_list_fp1_session_list_test1/selectfiles?
The absolute last part seems ok, namely /fp1/test1/test1.nii.


#2

Try to set base_dir for you workflow, e.g. preprocess.base_dir = /output/working_dir


#3

I have tested some variations and now the output of realign shows up in the directory preproc next to the participants folders. I would like to have each preprocessing step to output in the folder where the nifti file is located. So if the folder structure looks like: ParticipantDirectory >> participant1 >> experiment1 >> niftifile located here. When a preprocessing step is completed it should exist here. Is it datasink that I regulate or does realign( ) have an output_dir?


#4

I’m not sure if I fully understand, but you should explore DataSink, it controls the output directory format. Here you can find some examples. I guess the notation preproc.@in_file can solve some of your problems.


#5

From the link you sent:

wf.connect([(smooth, sinker, [('out_file', 'preproc.@in_file')]),
        (mcflirt, sinker, [('mean_img', 'preproc.@mean_img'),
                           ('par_file', 'preproc.@par_file')]),
        ])
wf.run()

This saves the files to the folder “base_directory/preproc”.
To put each file in the folder where the original nii-file is, do I have to write a for-loop and then run a workflow for each participant?


#6

oh, I see, you want to put everything in the input directory. I think that in general we advice to not change the directory with your original input and use DataSink to create a separate directory for output. You can consider connecting your original file with your DataSink, so it will be copied to your output directory.

Of course if you really have to do it, you can ask DataSink to point to your input directory. Answering your question regarding multiple subjects, you should use iterable (examples can be found here). Probably you would have to use multiple substitutions for your DataSink to match your input directory structure.