-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathanatomical_preproc.py
More file actions
688 lines (533 loc) · 26.5 KB
/
anatomical_preproc.py
File metadata and controls
688 lines (533 loc) · 26.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
base_test_dir = "/tdata/QAP/qc_test"
def anatomical_reorient_workflow(workflow, resource_pool, config, name="_"):
"""Build a Nipype workflow to deoblique and reorient an anatomical scan
from a NIFTI file.
- This is a seminal workflow that can only take an input directly from
disk (i.e. no Nipype workflow connections/pointers, and this is where
the pipeline will actually begin). For the sake of building the
pipeline in reverse, if this workflow is called when there is no input
file available, this function will return the unmodified workflow and
resource pool directly back.
- In conjunction with the other workflow-building functions, if this
function returns the workflow and resource pool unmodified, each
function up will do the same until it reaches the top level, allowing
the pipeline builder to continue "searching" for a base-level input
without crashing at this one.
Expected Resources in Resource Pool
- anatomical_scan: The raw anatomical scan in a NIFTI image.
New Resources Added to Resource Pool
- anatomical_reorient: The deobliqued, reoriented anatomical scan.
Workflow Steps
1. AFNI's 3drefit to deoblique the anatomical scan.
2. AFNI's 3dresample to reorient the deobliqued anatomical scan to RPI.
:type workflow: Nipype workflow object
:param workflow: A Nipype workflow object which can already contain other
connected nodes; this function will insert the following
workflow into this one provided.
:type resource_pool: dict
:param resource_pool: A dictionary defining input files and pointers to
Nipype node outputs / workflow connections; the keys
are the resource names.
:type config: dict
:param config: A dictionary defining the configuration settings for the
workflow, such as directory paths or toggled options.
:type name: str
:param name: (default: "_") A string to append to the end of each node
name.
:rtype: Nipype workflow object
:return: The Nipype workflow originally provided, but with this function's
sub-workflow connected into it.
:rtype: dict
:return: The resource pool originally provided, but updated (if
applicable) with the newest outputs and connections.
"""
import nipype.pipeline.engine as pe
from nipype.interfaces.afni import preprocess
if "anatomical_scan" not in resource_pool.keys():
return workflow, resource_pool
anat_deoblique = pe.Node(interface=preprocess.Refit(),
name='anat_deoblique%s' % name)
anat_deoblique.inputs.in_file = resource_pool["anatomical_scan"]
anat_deoblique.inputs.deoblique = True
anat_reorient = pe.Node(interface=preprocess.Resample(),
name='anat_reorient%s' % name)
anat_reorient.inputs.orientation = 'RPI'
anat_reorient.inputs.outputtype = 'NIFTI_GZ'
workflow.connect(anat_deoblique, 'out_file', anat_reorient, 'in_file')
resource_pool["anatomical_reorient"] = (anat_reorient, 'out_file')
return workflow, resource_pool
def run_anatomical_reorient(anatomical_scan, out_dir=None, run=True):
"""Run the 'anatomical_reorient_workflow' function to execute the modular
workflow with the provided inputs.
:type anatomical_scan: str
:param anatomical_scan: The filepath to the raw anatomical image in a
NIFTI file.
:type out_dir: str
:param out_dir: (default: None) The output directory to write the results
to; if left as None, will write to the current directory.
:type run: bool
:param run: (default: True) Will run the workflow; if set to False, will
connect the Nipype workflow and return the workflow object
instead.
:rtype: str
:return: (if run=True) The filepath of the generated anatomical_reorient
file.
:rtype: Nipype workflow object
:return: (if run=False) The connected Nipype workflow object.
:rtype: str
:return: (if run=False) The base directory of the workflow if it were to
be run.
"""
import os
import glob
import nipype.interfaces.io as nio
import nipype.pipeline.engine as pe
output = "anatomical_reorient"
workflow = pe.Workflow(name='anatomical_reorient_workflow')
if not out_dir:
out_dir = os.getcwd()
workflow_dir = os.path.join(out_dir, "workflow_output", output)
workflow.base_dir = workflow_dir
resource_pool = {}
config = {}
num_cores_per_subject = 1
resource_pool["anatomical_scan"] = anatomical_scan
workflow, resource_pool = \
anatomical_reorient_workflow(workflow, resource_pool, config)
ds = pe.Node(nio.DataSink(), name='datasink_anatomical_reorient')
ds.inputs.base_directory = workflow_dir
node, out_file = resource_pool["anatomical_reorient"]
workflow.connect(node, out_file, ds, 'anatomical_reorient')
if run == True:
workflow.run(plugin='MultiProc', plugin_args= \
{'n_procs': num_cores_per_subject})
outpath = glob.glob(os.path.join(workflow_dir, "anatomical_reorient",\
"*"))[0]
return outpath
else:
return workflow, workflow.base_dir
def anatomical_skullstrip_workflow(workflow, resource_pool, config, name="_"):
"""Build a Nipype workflow to skullstrip an anatomical image using AFNI's
3dSkullStrip.
- If any resources/outputs required by this workflow are not in the
resource pool, this workflow will call pre-requisite workflow builder
functions to further populate the pipeline with workflows which will
calculate/generate these necessary pre-requisites.
Expected Resources in Resource Pool
- anatomical_reorient: The deobliqued, reoriented anatomical scan.
New Resources Added to Resource Pool
- anatomical_brain: The skull-stripped anatomical image (brain only).
Workflow Steps
1. AFNI 3dSkullStrip to create a binary mask selecting only the brain.
2. AFNI 3dcalc to multiply the anatomical image with this mask.
:type workflow: Nipype workflow object
:param workflow: A Nipype workflow object which can already contain other
connected nodes; this function will insert the following
workflow into this one provided.
:type resource_pool: dict
:param resource_pool: A dictionary defining input files and pointers to
Nipype node outputs / workflow connections; the keys
are the resource names.
:type config: dict
:param config: A dictionary defining the configuration settings for the
workflow, such as directory paths or toggled options.
:type name: str
:param name: (default: "_") A string to append to the end of each node
name.
:rtype: Nipype workflow object
:return: The Nipype workflow originally provided, but with this function's
sub-workflow connected into it.
:rtype: dict
:return: The resource pool originally provided, but updated (if
applicable) with the newest outputs and connections.
"""
import copy
import nipype.pipeline.engine as pe
from nipype.interfaces.afni import preprocess
if "anatomical_reorient" not in resource_pool.keys():
from anatomical_preproc import anatomical_reorient_workflow
old_rp = copy.copy(resource_pool)
workflow, new_resource_pool = \
anatomical_reorient_workflow(workflow, resource_pool, config, name)
if resource_pool == old_rp:
return workflow, resource_pool
anat_skullstrip = pe.Node(interface=preprocess.SkullStrip(),
name='anat_skullstrip%s' % name)
anat_skullstrip.inputs.outputtype = 'NIFTI_GZ'
anat_skullstrip_orig_vol = pe.Node(interface=preprocess.Calc(),
name='anat_skullstrip_orig_vol%s' % name)
anat_skullstrip_orig_vol.inputs.expr = 'a*step(b)'
anat_skullstrip_orig_vol.inputs.outputtype = 'NIFTI_GZ'
if len(resource_pool["anatomical_reorient"]) == 2:
node, out_file = resource_pool["anatomical_reorient"]
workflow.connect(node, out_file, anat_skullstrip, 'in_file')
else:
anat_skullstrip.inputs.in_file = \
resource_pool["anatomical_reorient"]
if len(resource_pool["anatomical_reorient"]) == 2:
node, out_file = resource_pool["anatomical_reorient"]
workflow.connect(node, out_file,
anat_skullstrip_orig_vol, 'in_file_a')
else:
anat_skullstrip_orig_vol.inputs.in_file_a = \
resource_pool["anatomical_reorient"]
workflow.connect(anat_skullstrip, 'out_file',
anat_skullstrip_orig_vol, 'in_file_b')
resource_pool["anatomical_brain"] = (anat_skullstrip_orig_vol, 'out_file')
return workflow, resource_pool
def run_anatomical_skullstrip(anatomical_reorient, out_dir=None, run=True):
"""Run the 'anatomical_skullstrip_workflow' function to execute the
modular workflow with the provided inputs.
:type anatomical_reorient: str
:param anatomical_reorient: The filepath of the deobliqued, reoriented
anatomical image NIFTI file.
:type out_dir: str
:param out_dir: (default: None) The output directory to write the results
to; if left as None, will write to the current directory.
:type run: bool
:param run: (default: True) Will run the workflow; if set to False, will
connect the Nipype workflow and return the workflow object
instead.
:rtype: str
:return: (if run=True) The filepath of the generated anatomical_reorient
file.
:rtype: Nipype workflow object
:return: (if run=False) The connected Nipype workflow object.
:rtype: str
:return: (if run=False) The base directory of the workflow if it were to
be run.
"""
import os
import glob
import nipype.interfaces.io as nio
import nipype.pipeline.engine as pe
output = "anatomical_brain"
workflow = pe.Workflow(name='anatomical_skullstrip_workflow')
if not out_dir:
out_dir = os.getcwd()
workflow_dir = os.path.join(out_dir, "workflow_output", output)
workflow.base_dir = workflow_dir
resource_pool = {}
config = {}
num_cores_per_subject = 1
resource_pool["anatomical_reorient"] = anatomical_reorient
workflow, resource_pool = \
anatomical_skullstrip_workflow(workflow, resource_pool, config)
ds = pe.Node(nio.DataSink(), name='datasink_anatomical_skullstrip')
ds.inputs.base_directory = workflow_dir
node, out_file = resource_pool["anatomical_brain"]
workflow.connect(node, out_file, ds, 'anatomical_brain')
if run == True:
workflow.run(plugin='MultiProc', plugin_args= \
{'n_procs': num_cores_per_subject})
outpath = glob.glob(os.path.join(workflow_dir, "anatomical_brain", \
"*"))[0]
return outpath
else:
return workflow, workflow.base_dir
def afni_anatomical_linear_registration(workflow, resource_pool, \
config, name="_"):
"""Build Nipype workflow to calculate the linear registration (participant
to template) of an anatomical image using AFNI's 3dAllineate.
- If any resources/outputs required by this workflow are not in the
resource pool, this workflow will call pre-requisite workflow builder
functions to further populate the pipeline with workflows which will
calculate/generate these necessary pre-requisites.
Expected Settings in Configuration
- skull_on_registration: (optional- default: True) Whether or not to
accept anatomical_reorient or anatomical_brain
as the input for registration.
- template_head_for_anat: (for skull-on registration) The reference
template of the whole head.
- template_brain_for_anat: (for skull-off registration) The reference
template of the brain without skull.
Expected Resources in Resource Pool
- anatomical_reorient: The deobliqued, reoriented anatomical scan.
OR
- anatomical_brain: The skull-stripped anatomical image (brain only).
New Resources Added to Resource Pool
- afni_linear_warped_image: The anatomical image transformed to the
template (using linear warps).
- allineate_linear_xfm: The text file containing the linear warp matrix
produced by AFNI's 3dAllineate.
Workflow Steps
1. AFNI's 3dAllineate to calculate the linear registration.
:type workflow: Nipype workflow object
:param workflow: A Nipype workflow object which can already contain other
connected nodes; this function will insert the following
workflow into this one provided.
:type resource_pool: dict
:param resource_pool: A dictionary defining input files and pointers to
Nipype node outputs / workflow connections; the keys
are the resource names.
:type config: dict
:param config: A dictionary defining the configuration settings for the
workflow, such as directory paths or toggled options.
:type name: str
:param name: (default: "_") A string to append to the end of each node
name.
:rtype: Nipype workflow object
:return: The Nipype workflow originally provided, but with this function's
sub-workflow connected into it.
:rtype: dict
:return: The resource pool originally provided, but updated (if
applicable) with the newest outputs and connections.
"""
import copy
import nipype.pipeline.engine as pe
import nipype.interfaces.afni as afni
if "skull_on_registration" not in config.keys():
config["skull_on_registration"] = True
calc_allineate_warp = pe.Node(interface=afni.Allineate(),
name='calc_3dAllineate_warp%s' % name)
calc_allineate_warp.inputs.outputtype = "NIFTI_GZ"
if config["skull_on_registration"]:
if "anatomical_reorient" not in resource_pool.keys():
from anatomical_preproc import anatomical_reorient_workflow
old_rp = copy.copy(resource_pool)
workflow, new_resource_pool = \
anatomical_reorient_workflow(workflow, resource_pool,
config, name)
if resource_pool == old_rp:
return workflow, resource_pool
if len(resource_pool["anatomical_reorient"]) == 2:
node, out_file = resource_pool["anatomical_reorient"]
workflow.connect(node, out_file, calc_allineate_warp, 'in_file')
else:
calc_allineate_warp.inputs.in_file = \
resource_pool["anatomical_reorient"]
calc_allineate_warp.inputs.reference = \
config["template_head_for_anat"]
calc_allineate_warp.inputs.out_file = "allineate_warped_head.nii.gz"
else:
if "anatomical_brain" not in resource_pool.keys():
from anatomical_preproc import anatomical_skullstrip_workflow
old_rp = copy.copy(resource_pool)
workflow, new_resource_pool = \
anatomical_skullstrip_workflow(workflow, resource_pool, \
config, name)
if resource_pool == old_rp:
return workflow, resource_pool
if len(resource_pool["anatomical_brain"]) == 2:
node, out_file = resource_pool["anatomical_brain"]
workflow.connect(node, out_file, calc_allineate_warp, 'in_file')
else:
calc_allineate_warp.inputs.in_file = \
resource_pool["anatomical_brain"]
calc_allineate_warp.inputs.reference = \
config["template_brain_for_anat"]
calc_allineate_warp.inputs.out_file = \
"allineate_warped_brain.nii.gz"
calc_allineate_warp.inputs.out_matrix = "3dallineate_warp"
resource_pool["allineate_linear_xfm"] = \
(calc_allineate_warp, 'matrix')
resource_pool["afni_linear_warped_image"] = \
(calc_allineate_warp, 'out_file')
return workflow, resource_pool
def run_afni_anatomical_linear_registration(input_image, reference_image,
skull_on=True, out_dir=None,
run=True):
"""Run the 'afni_anatomical_linear_registration' function to execute the
modular workflow with the provided inputs.
:type input_image: str
:param input_image: Filepath to either the deobliqued, reoriented
anatomical image with skull, or the skullstripped
brain.
:type reference_image: str
:param reference_image: Filepath to the template image to calculate the
registration towards.
:type skull_on: bool
:param skull_on: (default: True) Whether to run the registration using the
whole head or just the brain (use this to mark which
type of input_image you are providing).
:type out_dir: str
:param out_dir: (default: None) The output directory to write the results
to; if left as None, will write to the current directory.
:type run: bool
:param run: (default: True) Will run the workflow; if set to False, will
connect the Nipype workflow and return the workflow object
instead.
:rtype: str
:return: (if run=True) The filepath of the generated anatomical_reorient
file.
:rtype: Nipype workflow object
:return: (if run=False) The connected Nipype workflow object.
:rtype: str
:return: (if run=False) The base directory of the workflow if it were to
be run.
"""
import os
import glob
import nipype.interfaces.io as nio
import nipype.pipeline.engine as pe
output = "afni_linear_warped_image"
workflow = pe.Workflow(name='3dallineate_workflow')
if not out_dir:
out_dir = os.getcwd()
workflow_dir = os.path.join(out_dir, "workflow_output", output)
workflow.base_dir = workflow_dir
resource_pool = {}
config = {}
num_cores_per_subject = 1
config["skull_on_registration"] = skull_on
if skull_on:
resource_pool["anatomical_reorient"] = input_image
config["template_head_for_anat"] = reference_image
else:
resource_pool["anatomical_brain"] = input_image
config["template_brain_for_anat"] = reference_image
workflow, resource_pool = \
afni_anatomical_linear_registration(workflow, resource_pool,
config)
ds = pe.Node(nio.DataSink(), name='datasink_3dallineate')
ds.inputs.base_directory = workflow_dir
node, out_file = resource_pool["afni_linear_warped_image"]
workflow.connect(node, out_file, ds, 'afni_linear_warped_image')
node, out_file = resource_pool["allineate_linear_xfm"]
workflow.connect(node, out_file, ds, 'allineate_linear_xfm')
if run:
workflow.run(plugin='MultiProc', plugin_args=
{'n_procs': num_cores_per_subject})
outpath = glob.glob(os.path.join(workflow_dir,
"afni_linear_warped_image",
"*"))[0]
return outpath
else:
return workflow, workflow.base_dir
def afni_segmentation_workflow(workflow, resource_pool, config, name="_"):
"""Build a Nipype workflow to generate anatomical tissue segmentation maps
using AFNI's 3dSeg.
- If any resources/outputs required by this workflow are not in the
resource pool, this workflow will call pre-requisite workflow builder
functions to further populate the pipeline with workflows which will
calculate/generate these necessary pre-requisites.
Expected Resources in Resource Pool
anatomical_brain: The skull-stripped anatomical image (brain only).
New Resources Added to Resource Pool
anatomical_csf_mask: The binary mask mapping the CSF voxels.
anatomical_gm_mask: The binary mask mapping the gray matter voxels.
anatomical_wm_mask: The binary mask mapping the white matter voxels.
Workflow Steps
1. AFNI 3dSeg to run tissue segmentation on the anatomical brain.
2. AFNI 3dAFNItoNIFTI to convert the AFNI-format 3dSeg output into a
NIFTI file (as of Oct 2016 3dSeg cannot be configured to write to
NIFTI).
3. AFNI 3dcalc to separate the three masks within the output file into
three separate images.
:type workflow: Nipype workflow object
:param workflow: A Nipype workflow object which can already contain other
connected nodes; this function will insert the following
workflow into this one provided.
:type resource_pool: dict
:param resource_pool: A dictionary defining input files and pointers to
Nipype node outputs / workflow connections; the keys
are the resource names.
:type config: dict
:param config: A dictionary defining the configuration settings for the
workflow, such as directory paths or toggled options.
:type name: str
:param name: (default: "_") A string to append to the end of each node
name.
:rtype: Nipype workflow object
:return: The Nipype workflow originally provided, but with this function's
sub-workflow connected into it.
:rtype: dict
:return: The resource pool originally provided, but updated (if
applicable) with the newest outputs and connections.
"""
import copy
import nipype.pipeline.engine as pe
from nipype.interfaces.afni import preprocess
if "anatomical_brain" not in resource_pool.keys():
from anatomical_preproc import anatomical_skullstrip_workflow
old_rp = copy.copy(resource_pool)
workflow, new_resource_pool = \
anatomical_skullstrip_workflow(workflow, resource_pool, config,
name)
if resource_pool == old_rp:
return workflow, resource_pool
segment = pe.Node(interface=preprocess.Seg(), name='segmentation%s' % name)
segment.inputs.mask = 'AUTO'
if len(resource_pool["anatomical_brain"]) == 2:
node, out_file = resource_pool["anatomical_brain"]
workflow.connect(node, out_file, segment, 'in_file')
else:
segment.inputs.in_file = resource_pool["anatomical_brain"]
# output processing
AFNItoNIFTI = pe.Node(interface=preprocess.AFNItoNIFTI(),
name="segment_AFNItoNIFTI%s" % name)
AFNItoNIFTI.inputs.out_file = "classes.nii.gz"
workflow.connect(segment, 'out_file', AFNItoNIFTI, 'in_file')
# break out each of the three tissue types into
# three separate NIFTI files
extract_CSF = pe.Node(interface=preprocess.Calc(),
name='extract_CSF_mask%s' % name)
extract_CSF.inputs.expr = "within(a,1,1)"
extract_CSF.inputs.out_file = "anatomical_csf_mask.nii.gz"
extract_GM = pe.Node(interface=preprocess.Calc(),
name='extract_GM_mask%s' % name)
extract_GM.inputs.expr = "within(a,2,2)"
extract_GM.inputs.out_file = "anatomical_gm_mask.nii.gz"
extract_WM = pe.Node(interface=preprocess.Calc(),
name='extract_WM_mask%s' % name)
extract_WM.inputs.expr = "within(a,3,3)"
extract_WM.inputs.out_file = "anatomical_wm_mask.nii.gz"
workflow.connect(AFNItoNIFTI, 'out_file', extract_CSF, 'in_file_a')
workflow.connect(AFNItoNIFTI, 'out_file', extract_GM, 'in_file_a')
workflow.connect(AFNItoNIFTI, 'out_file', extract_WM, 'in_file_a')
resource_pool["anatomical_csf_mask"] = (extract_CSF, 'out_file')
resource_pool["anatomical_gm_mask"] = (extract_GM, 'out_file')
resource_pool["anatomical_wm_mask"] = (extract_WM, 'out_file')
return workflow, resource_pool
def run_afni_segmentation(anatomical_brain, out_dir=None, run=True):
"""Run the 'afni_segmentation_workflow' function to execute the modular
workflow with the provided inputs.
:type anatomical_brain: str
:param anatomical_brain: Filepath to the skull-stripped brain image in a
NIFTI file.
:type out_dir: str
:param out_dir: (default: None) The output directory to write the results
to; if left as None, will write to the current directory.
:type run: bool
:param run: (default: True) Will run the workflow; if set to False, will
connect the Nipype workflow and return the workflow object
instead.
:rtype: str
:return: (if run=True) The filepath of the generated anatomical_reorient
file.
:rtype: Nipype workflow object
:return: (if run=False) The connected Nipype workflow object.
:rtype: str
:return: (if run=False) The base directory of the workflow if it were to
be run.
"""
import os
import glob
import nipype.interfaces.io as nio
import nipype.pipeline.engine as pe
output = "anatomical_afni_segmentation_masks"
workflow = pe.Workflow(name='afni_segmentation_workflow')
if not out_dir:
out_dir = os.getcwd()
workflow_dir = os.path.join(out_dir, "workflow_output", output)
workflow.base_dir = workflow_dir
resource_pool = {}
config = {}
num_cores_per_subject = 1
resource_pool["anatomical_brain"] = anatomical_brain
workflow, resource_pool = \
afni_segmentation_workflow(workflow, resource_pool, config)
ds = pe.Node(nio.DataSink(), name='datasink_afni_segmentation')
ds.inputs.base_directory = workflow_dir
seg_types = ["gm", "wm", "csf"]
for seg in seg_types:
node, out_file = resource_pool["anatomical_%s_mask" % seg]
workflow.connect(node, out_file, ds, 'anatomical_%s_mask' % seg)
if run == True:
workflow.run(plugin='MultiProc', plugin_args= \
{'n_procs': num_cores_per_subject})
outpath = glob.glob(os.path.join(workflow_dir, "anatomical_*_mask", \
"*"))
return outpath
else:
return workflow, workflow.base_dir