17
17
18
18
from __future__ import print_function
19
19
from builtins import range
20
+ import os .path as op # system functions
20
21
21
- import nipype .interfaces .io as nio # Data i/o
22
- import nipype .interfaces .spm as spm # spm
23
- import nipype .interfaces .matlab as mlab # how to run matlab
24
- import nipype .interfaces .fsl as fsl # fsl
25
- import nipype .interfaces .utility as niu # utility
26
- import nipype .pipeline .engine as pe # pypeline engine
27
- import nipype .algorithms .rapidart as ra # artifact detection
28
- import nipype .algorithms .modelgen as model # model specification
29
- import os # system functions
22
+ from nipype .interfaces import io as nio # Data i/o
23
+ from nipype .interfaces import spm as spm # spm
24
+ # from nipype.interfaces import matlab as mlab # how to run matlab
25
+ from nipype .interfaces import fsl as fsl # fsl
26
+ from nipype .interfaces import utility as niu # utility
27
+ from nipype .pipeline import engine as pe # pypeline engine
28
+ from nipype .algorithms import rapidart as ra # artifact detection
29
+ from nipype .algorithms import modelgen as model # model specification
30
30
31
31
"""
32
32
65
65
66
66
preproc = pe .Workflow (name = 'preproc' )
67
67
68
+ """
69
+ A node called :code:`inputnode` is set to designate the path in which input data
70
+ are located:
71
+ """
72
+
73
+ inputnode = pe .Node (niu .IdentityInterface (fields = ['in_data' ]), name = 'inputnode' )
74
+
75
+
68
76
"""Use :class:`nipype.interfaces.spm.Realign` for motion correction
69
77
and register all images to the mean image.
70
78
"""
71
79
72
- realign = pe .Node (interface = spm .Realign (), name = "realign" )
80
+ realign = pe .Node (spm .Realign (), name = "realign" )
73
81
realign .inputs .register_to_mean = True
74
82
75
83
"""Use :class:`nipype.algorithms.rapidart` to determine which of the
76
84
images in the functional series are outliers based on deviations in
77
85
intensity or movement.
78
86
"""
79
87
80
- art = pe .Node (interface = ra .ArtifactDetect (), name = "art" )
88
+ art = pe .Node (ra .ArtifactDetect (), name = "art" )
81
89
art .inputs .use_differences = [True , False ]
82
90
art .inputs .use_norm = True
83
91
art .inputs .norm_threshold = 1
89
97
:class:`nipype.interfaces.fsl.BET`.
90
98
"""
91
99
92
- skullstrip = pe .Node (interface = fsl .BET (), name = "skullstrip" )
100
+ skullstrip = pe .Node (fsl .BET (), name = "skullstrip" )
93
101
skullstrip .inputs .mask = True
94
102
95
103
"""Use :class:`nipype.interfaces.spm.Coregister` to perform a rigid
96
104
body registration of the functional data to the structural data.
97
105
"""
98
106
99
- coregister = pe .Node (interface = spm .Coregister (), name = "coregister" )
107
+ coregister = pe .Node (spm .Coregister (), name = "coregister" )
100
108
coregister .inputs .jobtype = 'estimate'
101
109
102
110
105
113
includes the template image, T1.nii.
106
114
"""
107
115
108
- normalize = pe .Node (interface = spm .Normalize (), name = "normalize" )
109
- normalize .inputs .template = os .path .abspath ('nipype-tutorial/data/T1.nii' )
116
+ normalize = pe .Node (spm .Normalize (), name = "normalize" )
110
117
111
118
112
119
"""Smooth the functional data using
113
120
:class:`nipype.interfaces.spm.Smooth`.
114
121
"""
115
122
116
- smooth = pe .Node (interface = spm .Smooth (), name = "smooth" )
123
+ smooth = pe .Node (spm .Smooth (), name = "smooth" )
117
124
fwhmlist = [4 ]
118
125
smooth .iterables = ('fwhm' , fwhmlist )
119
126
120
- preproc .connect ([(realign , coregister , [('mean_image' , 'source' ),
127
+ preproc .connect ([(inputnode , normalize , [(('in_data' , _template_path ), 'template' )]),
128
+ (realign , coregister , [('mean_image' , 'source' ),
121
129
('realigned_files' , 'apply_to_files' )]),
122
130
(coregister , normalize , [('coregistered_files' , 'apply_to_files' )]),
123
131
(normalize , smooth , [('normalized_files' , 'in_files' )]),
140
148
:class:`nipype.interfaces.spm.SpecifyModel`.
141
149
"""
142
150
143
- modelspec = pe .Node (interface = model .SpecifySPMModel (), name = "modelspec" )
151
+ modelspec = pe .Node (model .SpecifySPMModel (), name = "modelspec" )
144
152
modelspec .inputs .concatenate_runs = True
145
153
146
154
"""Generate a first level SPM.mat file for analysis
147
155
:class:`nipype.interfaces.spm.Level1Design`.
148
156
"""
149
157
150
- level1design = pe .Node (interface = spm .Level1Design (), name = "level1design" )
158
+ level1design = pe .Node (spm .Level1Design (), name = "level1design" )
151
159
level1design .inputs .bases = {'hrf' : {'derivs' : [0 , 0 ]}}
152
160
153
161
"""Use :class:`nipype.interfaces.spm.EstimateModel` to determine the
154
162
parameters of the model.
155
163
"""
156
164
157
- level1estimate = pe .Node (interface = spm .EstimateModel (), name = "level1estimate" )
165
+ level1estimate = pe .Node (spm .EstimateModel (), name = "level1estimate" )
158
166
level1estimate .inputs .estimation_method = {'Classical' : 1 }
159
167
160
168
"""Use :class:`nipype.interfaces.spm.EstimateContrast` to estimate the
161
169
first level contrasts specified in a few steps above.
162
170
"""
163
171
164
- contrastestimate = pe .Node (interface = spm .EstimateContrast (), name = "contrastestimate" )
172
+ contrastestimate = pe .Node (spm .EstimateContrast (), name = "contrastestimate" )
165
173
166
174
"""Use :class: `nipype.interfaces.utility.Select` to select each contrast for
167
175
reporting.
168
176
"""
169
177
170
- selectcontrast = pe .Node (interface = niu .Select (), name = "selectcontrast" )
178
+ selectcontrast = pe .Node (niu .Select (), name = "selectcontrast" )
171
179
172
180
"""Use :class:`nipype.interfaces.fsl.Overlay` to combine the statistical output of
173
181
the contrast estimate and a background image into one volume.
174
182
"""
175
183
176
- overlaystats = pe .Node (interface = fsl .Overlay (), name = "overlaystats" )
184
+ overlaystats = pe .Node (fsl .Overlay (), name = "overlaystats" )
177
185
overlaystats .inputs .stat_thresh = (3 , 10 )
178
186
overlaystats .inputs .show_negative_stats = True
179
187
overlaystats .inputs .auto_thresh_bg = True
182
190
statistical volumes for a report of the first-level results.
183
191
"""
184
192
185
- slicestats = pe .Node (interface = fsl .Slicer (), name = "slicestats" )
193
+ slicestats = pe .Node (fsl .Slicer (), name = "slicestats" )
186
194
slicestats .inputs .all_axial = True
187
195
slicestats .inputs .image_width = 750
188
196
243
251
info = dict (func = [['subject_id' , ['f3' , 'f5' , 'f7' , 'f10' ]]],
244
252
struct = [['subject_id' , 'struct' ]])
245
253
246
- inputnode = pe .Node (niu .IdentityInterface (fields = ['in_data' ]), name = 'inputnode' )
247
- infosource = pe .Node (interface = niu .IdentityInterface (fields = ['subject_id' ]), name = "infosource" )
254
+ infosource = pe .Node (niu .IdentityInterface (fields = ['subject_id' ]), name = "infosource" )
248
255
249
256
"""Here we set up iteration over all the subjects. The following line
250
257
is a particular example of the flexibility of the system. The
265
272
functionality.
266
273
"""
267
274
268
- datasource = pe .Node (interface = nio .DataGrabber (infields = ['subject_id' ],
275
+ datasource = pe .Node (nio .DataGrabber (infields = ['subject_id' ],
269
276
outfields = ['func' , 'struct' ]),
270
277
name = 'datasource' )
271
278
datasource .inputs .template = 'nipype-tutorial/data/%s/%s.nii'
@@ -357,7 +364,7 @@ def subjectinfo(subject_id):
357
364
"""
358
365
359
366
level1 = pe .Workflow (name = "level1" )
360
- level1 .base_dir = os . path .abspath ('spm_tutorial2/workingdir' )
367
+ level1 .base_dir = op .abspath ('spm_tutorial2/workingdir' )
361
368
362
369
level1 .connect ([(inputnode , datasource , [('in_data' , 'base_directory' )]),
363
370
(infosource , datasource , [('subject_id' , 'subject_id' )]),
@@ -389,16 +396,16 @@ def subjectinfo(subject_id):
389
396
the mean image would be copied to that directory.
390
397
"""
391
398
392
- datasink = pe .Node (interface = nio .DataSink (), name = "datasink" )
393
- datasink .inputs .base_directory = os . path .abspath ('spm_tutorial2/l1output' )
394
- report = pe .Node (interface = nio .DataSink (), name = 'report' )
395
- report .inputs .base_directory = os . path .abspath ('spm_tutorial2/report' )
399
+ datasink = pe .Node (nio .DataSink (), name = "datasink" )
400
+ datasink .inputs .base_directory = op .abspath ('spm_tutorial2/l1output' )
401
+ report = pe .Node (nio .DataSink (), name = 'report' )
402
+ report .inputs .base_directory = op .abspath ('spm_tutorial2/report' )
396
403
report .inputs .parameterization = False
397
404
398
405
399
406
def getstripdir (subject_id ):
400
- import os
401
- return os . path . join (os . path .abspath ('spm_tutorial2/workingdir' ), '_subject_id_%s' % subject_id )
407
+ import os . path as op
408
+ return op . join (op .abspath ('spm_tutorial2/workingdir' ), '_subject_id_%s' % subject_id )
402
409
403
410
# store relevant outputs from various stages of the 1st level analysis
404
411
level1 .connect ([(infosource , datasink , [('subject_id' , 'container' ),
@@ -440,7 +447,7 @@ def getstripdir(subject_id):
440
447
contrast_ids = list (range (1 , len (contrasts ) + 1 ))
441
448
l2source = pe .Node (nio .DataGrabber (infields = ['fwhm' , 'con' ]), name = "l2source" )
442
449
# we use .*i* to capture both .img (SPM8) and .nii (SPM12)
443
- l2source .inputs .template = os . path .abspath ('spm_tutorial2/l1output/*/con*/*/_fwhm_%d/con_%04d.*i*' )
450
+ l2source .inputs .template = op .abspath ('spm_tutorial2/l1output/*/con*/*/_fwhm_%d/con_%04d.*i*' )
444
451
# iterate over all contrast images
445
452
l2source .iterables = [('fwhm' , fwhmlist ),
446
453
('con' , contrast_ids )]
@@ -453,10 +460,10 @@ def getstripdir(subject_id):
453
460
"""
454
461
455
462
# setup a 1-sample t-test node
456
- onesamplettestdes = pe .Node (interface = spm .OneSampleTTestDesign (), name = "onesampttestdes" )
457
- l2estimate = pe .Node (interface = spm .EstimateModel (), name = "level2estimate" )
463
+ onesamplettestdes = pe .Node (spm .OneSampleTTestDesign (), name = "onesampttestdes" )
464
+ l2estimate = pe .Node (spm .EstimateModel (), name = "level2estimate" )
458
465
l2estimate .inputs .estimation_method = {'Classical' : 1 }
459
- l2conestimate = pe .Node (interface = spm .EstimateContrast (), name = "level2conestimate" )
466
+ l2conestimate = pe .Node (spm .EstimateContrast (), name = "level2conestimate" )
460
467
cont1 = ('Group' , 'T' , ['mean' ], [1 ])
461
468
l2conestimate .inputs .contrasts = [cont1 ]
462
469
l2conestimate .inputs .group_contrast = True
@@ -467,14 +474,22 @@ def getstripdir(subject_id):
467
474
"""
468
475
469
476
l2pipeline = pe .Workflow (name = "level2" )
470
- l2pipeline .base_dir = os . path .abspath ('spm_tutorial2/l2output' )
477
+ l2pipeline .base_dir = op .abspath ('spm_tutorial2/l2output' )
471
478
l2pipeline .connect ([(l2source , onesamplettestdes , [('outfiles' , 'in_files' )]),
472
479
(onesamplettestdes , l2estimate , [('spm_mat_file' , 'spm_mat_file' )]),
473
480
(l2estimate , l2conestimate , [('spm_mat_file' , 'spm_mat_file' ),
474
481
('beta_images' , 'beta_images' ),
475
482
('residual_image' , 'residual_image' )]),
476
483
])
477
484
485
+ """
486
+ Inlining functions in connect()
487
+ -------------------------------
488
+ """
489
+ def _template_path (in_data ):
490
+ return op .abspath (op .join (in_data , 'nipype-tutorial/data/T1.nii' ))
491
+
492
+
478
493
"""
479
494
Execute the second level pipeline
480
495
---------------------------------
0 commit comments