forked from NeuroanatomyAndConnectivity/7T_preprocessing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
linear_coreg.py
128 lines (107 loc) · 6.24 KB
/
linear_coreg.py
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
from nipype.pipeline.engine import Node, Workflow
import nipype.interfaces.utility as util
import nipype.interfaces.fsl as fsl
import nipype.interfaces.c3 as c3
import nipype.interfaces.freesurfer as fs
import nipype.interfaces.ants as ants
import nipype.interfaces.io as nio
import os
def create_coreg_pipeline(name='coreg'):
# fsl output type
fsl.FSLCommand.set_default_output_type('NIFTI_GZ')
# initiate workflow
coreg = Workflow(name='coreg')
#inputnode
inputnode=Node(util.IdentityInterface(fields=['epi_median',
'fs_subjects_dir',
'fs_subject_id',
'uni_highres',
'uni_lowres',
]),
name='inputnode')
# outputnode
outputnode=Node(util.IdentityInterface(fields=['epi2lowres',
'epi2lowres_mat',
'epi2lowres_dat',
'highres2lowres',
'highres2lowres_mat',
'highres2lowres_dat',
'highres2lowres_itk',
'epi2highres_lin',
'epi2highres_lin_mat',
'epi2highres_lin_itk',
]),
name='outputnode')
# linear registration epi median to lowres mp2rage (downsampled from freesurfer) with bbregister
bbregister_epi = Node(fs.BBRegister(contrast_type='t2',
out_fsl_file='epi2lowres.mat',
out_reg_file='epi2lowres.dat',
registered_file='epi2lowres.nii.gz',
init='fsl',
epi_mask=True
),
name='bbregister_epi')
coreg.connect([(inputnode, bbregister_epi, [('fs_subjects_dir', 'subjects_dir'),
('fs_subject_id', 'subject_id'),
('epi_median', 'source_file')]),
(bbregister_epi, outputnode, [('out_fsl_file', 'epi2lowres_mat'),
('out_reg_file', 'epi2lowres_dat'),
('registered_file', 'epi2lowres')
])
])
# linear register original highres mp2rage to lowres mp2rage
bbregister_anat = Node(fs.BBRegister(contrast_type='t1',
out_fsl_file='highres2lowres.mat',
out_reg_file='highres2lowres.dat',
registered_file='highres2lowres.nii.gz',
init='fsl'
),
name='bbregister_anat')
coreg.connect([(inputnode, bbregister_anat, [('fs_subjects_dir', 'subjects_dir'),
('fs_subject_id', 'subject_id'),
('uni_highres', 'source_file')]),
(bbregister_anat, outputnode, [('out_fsl_file', 'highres2lowres_mat'),
('out_reg_file', 'highres2lowres_dat'),
('registered_file', 'highres2lowres')
])
])
# convert highres2lowres transform to itk
itk_anat = Node(interface=c3.C3dAffineTool(fsl2ras=True,
itk_transform='highres2lowres.txt'),
name='itk_anat')
coreg.connect([(inputnode, itk_anat, [('uni_highres', 'source_file'),
('uni_lowres', 'reference_file')]),
(bbregister_anat, itk_anat, [('out_fsl_file', 'transform_file')]),
(itk_anat, outputnode, [('itk_transform', 'highres2lowres_itk')])
])
# invert highres2lowres transform
invert = Node(fsl.ConvertXFM(invert_xfm=True),
name='invert')
coreg.connect([(bbregister_anat, invert, [('out_fsl_file', 'in_file')])])
# concatenate epi2highres transforms
concat = Node(fsl.ConvertXFM(concat_xfm=True,
out_file='epi2highres_lin.mat'),
name='concat')
coreg.connect([(bbregister_epi, concat, [('out_fsl_file', 'in_file')]),
(invert, concat, [('out_file', 'in_file2')]),
(concat, outputnode, [('out_file', 'epi2higres_lin_mat')])])
# convert epi2highres transform into itk format
itk = Node(interface=c3.C3dAffineTool(fsl2ras=True,
itk_transform='epi2highres_lin.txt'),
name='itk')
coreg.connect([(inputnode, itk, [('epi_median', 'source_file'),
('uni_highres', 'reference_file')]),
(concat, itk, [('out_file', 'transform_file')]),
(itk, outputnode, [('itk_transform', 'epi2highres_lin_itk')])
])
# transform epi to highres
epi2highres = Node(ants.ApplyTransforms(dimension=3,
output_image='epi2highres_lin.nii.gz',
interpolation = 'BSpline',
),
name='epi2highres')
coreg.connect([(inputnode, epi2highres, [('uni_highres', 'reference_image'),
('epi_median', 'input_image')]),
(itk, epi2highres, [('itk_transform', 'transforms')]),
(epi2highres, outputnode, [('output_image', 'epi2highres_lin')])])
return coreg