Skip to content

Commit 1fb8c9d

Browse files
committed
merged some fixes and added mask to output
1 parent 531e6e9 commit 1fb8c9d

File tree

1 file changed

+19
-13
lines changed

1 file changed

+19
-13
lines changed

examples/rsfmri_conn_spm_preprocessing.py

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,10 @@
5858

5959
mlab.MatlabCommand.set_default_matlab_cmd("matlab -nodisplay")
6060
# If SPM is not in your MATLAB path you should add it here
61-
# mlab.MatlabCommand.set_default_paths('/path/to/your/spm8')
61+
mlab.MatlabCommand.set_default_paths('/cm/shared/openmind/spm/spm12b/spm12b_r5918/')
6262

6363
from nipype.algorithms.rapidart import ArtifactDetect
64-
from nipype.interfaces.utility import Rename
64+
from nipype.interfaces.utility import Rename, Merge
6565
from nipype.utils.filemanip import filename_to_list
6666
from nipype.interfaces.io import DataSink
6767

@@ -366,29 +366,34 @@ def create_workflow(files,
366366
(smooth, art, [('smoothed_files', 'realigned_files')]),
367367
])
368368

369+
def selectN(files, N=1):
370+
from nipype.utils.filemanip import filename_to_list, list_to_filename
371+
return list_to_filename(filename_to_list(files)[:N])
372+
373+
mask = Node(fsl.BET(), name='getmask')
374+
mask.inputs.mask = True
375+
wf.connect(normalize_func, ('normalized_files', selectN, 1), mask, 'in_file')
369376
# get segmentation in normalized functional space
370377

371378
segment.inputs.wm_output_type = [False, False, True]
372379
segment.inputs.csf_output_type = [False, False, True]
380+
segment.inputs.gm_output_type = [False, False, True]
373381

374382
def merge_files(in1, in2):
375383
out_files = filename_to_list(in1)
376384
out_files.extend(filename_to_list(in2))
377385
return out_files
378386

379-
merge = Node(Function(input_names=['in1', 'in2'],
380-
output_names=['out_file'],
381-
function=merge_files,
382-
imports=imports),
383-
name='merge')
387+
merge = Node(Merge(3), name='merge')
384388
wf.connect(segment, 'native_wm_image', merge, 'in1')
385389
wf.connect(segment, 'native_csf_image', merge, 'in2')
390+
wf.connect(segment, 'native_gm_image', merge, 'in3')
386391

387392
normalize_segs = Node(interface=spm.Normalize(), name = "normalize_segs")
388393
normalize_segs.inputs.jobtype = "write"
389394
normalize_segs.inputs.write_voxel_sizes = [2., 2., 2.]
390395

391-
wf.connect(merge, 'out_file', normalize_segs, 'apply_to_files')
396+
wf.connect(merge, 'out', normalize_segs, 'apply_to_files')
392397
wf.connect(segment, 'transformation_mat', normalize_segs, 'parameter_file')
393398

394399
# binarize and erode
@@ -448,7 +453,7 @@ def merge_files(in1, in2):
448453
createfilter2.inputs.num_components = num_components
449454
wf.connect(createfilter1, 'out_files', createfilter2, 'extra_regressors')
450455
wf.connect(filter1, 'out_res', createfilter2, 'realigned_file')
451-
wf.connect(bin_and_erode, 'out_file', createfilter2, 'mask_file')
456+
wf.connect(bin_and_erode, ('out_file', selectN, 2), createfilter2, 'mask_file')
452457

453458
# Filter noise components from unsmoothed data
454459
filter2 = MapNode(fsl.GLM(out_f_name='F.nii',
@@ -460,7 +465,7 @@ def merge_files(in1, in2):
460465
wf.connect(normalize_func, ('normalized_files', rename, '_unsmooth_cleaned'),
461466
filter2, 'out_res_name')
462467
wf.connect(createfilter2, 'out_files', filter2, 'design')
463-
#wf.connect(masktransform, 'transformed_file', filter2, 'mask')
468+
wf.connect(mask, 'mask_file', filter2, 'mask')
464469

465470
# Filter noise components from smoothed data
466471
filter3 = MapNode(fsl.GLM(out_f_name='F.nii',
@@ -472,7 +477,7 @@ def merge_files(in1, in2):
472477
filter3, 'out_res_name')
473478
wf.connect(smooth, 'smoothed_files', filter3, 'in_file')
474479
wf.connect(createfilter2, 'out_files', filter3, 'design')
475-
# wf.connect(masktransform, 'transformed_file', filter3, 'mask')
480+
wf.connect(mask, 'mask_file', filter3, 'mask')
476481

477482
# Bandpass filter the data
478483
bandpass1 = Node(Function(input_names=['files', 'lowpass_freq',
@@ -504,12 +509,13 @@ def merge_files(in1, in2):
504509
datasink.inputs.container = subject_id
505510
#datasink.inputs.substitutions = [('_target_subject_', '')]
506511
#datasink.inputs.regexp_substitutions = (r'(/_.*(\d+/))', r'/run\2')
507-
wf.connect(realign, 'par_file', datasink, 'resting.qa.motion')
512+
wf.connect(realign, 'realignment_parameters', datasink, 'resting.qa.motion')
508513
wf.connect(art, 'norm_files', datasink, 'resting.qa.art.@norm')
509514
wf.connect(art, 'intensity_files', datasink, 'resting.qa.art.@intensity')
510515
wf.connect(art, 'outlier_files', datasink, 'resting.qa.art.@outlier_files')
511-
wf.connect(smooth, 'out_file', datasink, 'resting.timeseries.fullpass')
516+
wf.connect(smooth, 'smoothed_files', datasink, 'resting.timeseries.fullpass')
512517
wf.connect(bin_and_erode, 'out_file', datasink, 'resting.mask_files')
518+
wf.connect(mask, 'mask_file', datasink, 'resting.mask_files.@brainmask')
513519
wf.connect(filter1, 'out_f', datasink, 'resting.qa.compmaps.@mc_F')
514520
wf.connect(filter1, 'out_pf', datasink, 'resting.qa.compmaps.@mc_pF')
515521
wf.connect(filter2, 'out_f', datasink, 'resting.qa.compmaps')

0 commit comments

Comments
 (0)