1
1
# -*- coding: utf-8 -*-
2
+ import os
2
3
from nipype .interfaces import afni
3
4
from nipype .interfaces import ants
4
5
from nipype .interfaces import fsl
9
10
from CPAC .utils .datasource import create_check_for_s3_node
10
11
from CPAC .unet .function import predict_volumes
11
12
13
+ def patch_cmass_output (lst , index = 0 ):
14
+ """
15
+ Parameters
16
+ ----------
17
+ lst : list of tuples
18
+ output of afni.CenterMass()
19
+ index : int
20
+ index in the list of tuples
21
+
22
+ Returns
23
+ -------
24
+ tuple
25
+ one set of center of mass coordinates
26
+ """
27
+ if len (lst ) <= index :
28
+ raise IndexError ("lst index out of range" )
29
+ return lst [index ]
12
30
13
31
def create_anat_preproc (method = 'afni' , already_skullstripped = False , config = None , wf_name = 'anat_preproc' ):
14
32
"""The main purpose of this workflow is to process T1 scans. Raw mprage file is deobliqued, reoriented
@@ -82,15 +100,20 @@ def create_anat_preproc(method='afni', already_skullstripped=False, config=None,
82
100
83
101
preproc = pe .Workflow (name = wf_name )
84
102
85
- inputnode = pe .Node (util .IdentityInterface (
86
- fields = ['anat' , 'brain_mask' ]), name = 'inputspec' )
103
+ inputnode = pe .Node (util .IdentityInterface (fields = ['anat' ,
104
+ 'brain_mask' ,
105
+ 'template_brain_only_for_anat' ,
106
+ 'template_skull_for_anat' ,
107
+ 'template_cmass' ]),
108
+ name = 'inputspec' )
87
109
88
110
outputnode = pe .Node (util .IdentityInterface (fields = ['refit' ,
89
111
'reorient' ,
90
112
'skullstrip' ,
91
113
'brain' ,
92
- 'brain_mask' ]),
93
- name = 'outputspec' )
114
+ 'brain_mask' ,
115
+ 'center_of_mass' ]),
116
+ name = 'outputspec' )
94
117
95
118
anat_deoblique = pe .Node (interface = afni .Refit (),
96
119
name = 'anat_deoblique' )
@@ -241,6 +264,7 @@ def create_anat_preproc(method='afni', already_skullstripped=False, config=None,
241
264
242
265
preproc .connect (anat_reorient , 'out_file' ,
243
266
anat_skullstrip , 'in_file' )
267
+
244
268
preproc .connect (skullstrip_args , 'expr' ,
245
269
anat_skullstrip , 'args' )
246
270
@@ -295,7 +319,7 @@ def create_anat_preproc(method='afni', already_skullstripped=False, config=None,
295
319
anat_skullstrip = pe .Node (
296
320
interface = fsl .BET (), name = 'anat_skullstrip' )
297
321
anat_skullstrip .inputs .output_type = 'NIFTI_GZ'
298
-
322
+
299
323
preproc .connect (anat_reorient , 'out_file' ,
300
324
anat_skullstrip , 'in_file' )
301
325
@@ -422,29 +446,28 @@ def create_anat_preproc(method='afni', already_skullstripped=False, config=None,
422
446
# flirt -v -dof 6 -in brain.nii.gz -ref NMT_SS_0.5mm.nii.gz -o brain_rot2atl -omat brain_rot2atl.mat -interp sinc
423
447
# TODO: antsRegistration -z 0 -d 3 -r [NMT_SS_0.5mm.nii.gz,brain.nii.gz,0] -o [transform,brain_rot2atl.nii.gz,brain_inv_rot2atl.nii.gz] -t Rigid[0.1] -m MI[NMT_SS_0.5mm.nii.gz,brain.nii.gz,1,32,Regular,0.25] -c [1000x500x250x100,1e-08,10] -s 3.0x2.0x1.0x0.0 -f 8x4x2x1 -u 1 -t Affine[0.1] -m MI[NMT_SS_0.5mm.nii.gz,brain.nii.gz,1,32,Regular,0.25] -c [1000x500x250x100,1e-08,10] -s 3.0x2.0x1.0x0.0 -f 8x4x2x1 -u 1
424
448
native_brain_to_template_brain = pe .Node (interface = fsl .FLIRT (), name = 'native_brain_to_template_brain' )
425
- native_brain_to_template_brain .inputs .reference = config .template_brain_only_for_anat
426
449
native_brain_to_template_brain .inputs .dof = 6
427
450
native_brain_to_template_brain .inputs .interp = 'sinc'
428
451
preproc .connect (unet_masked_brain , 'out_file' , native_brain_to_template_brain , 'in_file' )
452
+ preproc .connect (inputnode , 'template_brain_only_for_anat' , native_brain_to_template_brain , 'reference' )
429
453
430
454
# flirt -in head.nii.gz -ref NMT_0.5mm.nii.gz -o head_rot2atl -applyxfm -init brain_rot2atl.mat
431
455
# TODO: antsApplyTransforms -d 3 -i head.nii.gz -r NMT_0.5mm.nii.gz -n Linear -o head_rot2atl.nii.gz -v -t transform1Rigid.mat -t transform2Affine.mat -t transform0DerivedInitialMovingTranslation.mat
432
456
native_head_to_template_head = pe .Node (interface = fsl .FLIRT (), name = 'native_head_to_template_head' )
433
- native_head_to_template_head .inputs .reference = config .template_skull_for_anat
434
457
native_head_to_template_head .inputs .apply_xfm = True
435
458
preproc .connect (anat_reorient , 'out_file' , native_head_to_template_head , 'in_file' )
436
459
preproc .connect (native_brain_to_template_brain , 'out_matrix_file' , native_head_to_template_head , 'in_matrix_file' )
437
-
460
+ preproc .connect (inputnode , 'template_skull_for_anat' , native_head_to_template_head , 'reference' )
461
+
438
462
# fslmaths NMT_SS_0.5mm.nii.gz -bin templateMask.nii.gz
439
463
template_brain_mask = pe .Node (interface = fsl .maths .MathsCommand (), name = 'template_brain_mask' )
440
- template_brain_mask .inputs .in_file = config .template_brain_only_for_anat
441
464
template_brain_mask .inputs .args = '-bin'
465
+ preproc .connect (inputnode , 'template_brain_only_for_anat' , template_brain_mask , 'in_file' )
442
466
443
467
# ANTS 3 -m CC[head_rot2atl.nii.gz,NMT_0.5mm.nii.gz,1,5] -t SyN[0.25] -r Gauss[3,0] -o atl2T1rot -i 60x50x20 --use-Histogram-Matching --number-of-affine-iterations 10000x10000x10000x10000x10000 --MI-option 32x16000
444
468
ants_template_head_to_template = pe .Node (interface = ants .Registration (), name = 'template_head_to_template' )
445
469
ants_template_head_to_template .inputs .metric = ['CC' ]
446
470
ants_template_head_to_template .inputs .metric_weight = [1 ,5 ]
447
- ants_template_head_to_template .inputs .moving_image = config .template_skull_for_anat
448
471
ants_template_head_to_template .inputs .transforms = ['SyN' ]
449
472
ants_template_head_to_template .inputs .transform_parameters = [(0.25 ,)]
450
473
ants_template_head_to_template .inputs .interpolation = 'NearestNeighbor'
@@ -453,7 +476,7 @@ def create_anat_preproc(method='afni', already_skullstripped=False, config=None,
453
476
ants_template_head_to_template .inputs .shrink_factors = [[4 ,2 ,1 ]]
454
477
ants_template_head_to_template .inputs .convergence_threshold = [1.e-8 ]
455
478
preproc .connect (native_head_to_template_head , 'out_file' , ants_template_head_to_template , 'fixed_image' )
456
-
479
+ preproc . connect ( inputnode , 'template_skull_for_anat' , ants_template_head_to_template , 'moving_image' )
457
480
# antsApplyTransforms -d 3 -i templateMask.nii.gz -t atl2T1rotWarp.nii.gz atl2T1rotAffine.txt -r brain_rot2atl.nii.gz -o brain_rot2atl_mask.nii.gz
458
481
template_head_transform_to_template = pe .Node (interface = ants .ApplyTransforms (), name = 'template_head_transform_to_template' )
459
482
template_head_transform_to_template .inputs .dimension = 3
@@ -491,3 +514,4 @@ def create_anat_preproc(method='afni', already_skullstripped=False, config=None,
491
514
preproc .connect (refined_brain , 'out_file' , outputnode , 'brain' )
492
515
493
516
return preproc
517
+
0 commit comments