-
Notifications
You must be signed in to change notification settings - Fork 520
/
dti.py
288 lines (257 loc) · 9.66 KB
/
dti.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
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
# -*- coding: utf-8 -*-
# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*-
# vi: set ft=python sts=4 ts=4 sw=4 et:
"""Provides interfaces to various commands provided by diffusion toolkit
"""
import os
import re
from ...utils.filemanip import fname_presuffix, split_filename, copyfile
from ..base import (
TraitedSpec,
File,
traits,
CommandLine,
CommandLineInputSpec,
isdefined,
)
__docformat__ = "restructuredtext"
class DTIReconInputSpec(CommandLineInputSpec):
DWI = File(
desc="Input diffusion volume",
argstr="%s",
exists=True,
mandatory=True,
position=1,
)
out_prefix = traits.Str(
"dti", desc="Output file prefix", argstr="%s", usedefault=True, position=2
)
output_type = traits.Enum(
"nii",
"analyze",
"ni1",
"nii.gz",
argstr="-ot %s",
desc="output file type",
usedefault=True,
)
bvecs = File(exists=True, desc="b vectors file", argstr="-gm %s", mandatory=True)
bvals = File(exists=True, desc="b values file", mandatory=True)
n_averages = traits.Int(desc="Number of averages", argstr="-nex %s")
image_orientation_vectors = traits.List(
traits.Float(),
minlen=6,
maxlen=6,
desc="""\
Specify image orientation vectors. if just one argument given,
will treat it as filename and read the orientation vectors from
the file. If 6 arguments are given, will treat them as 6 float
numbers and construct the 1st and 2nd vector and calculate the 3rd
one automatically.
This information will be used to determine image orientation,
as well as to adjust gradient vectors with oblique angle when.""",
argstr="-iop %f",
)
oblique_correction = traits.Bool(
desc="""\
When oblique angle(s) applied, some SIEMENS DTI protocols do not
adjust gradient accordingly, thus it requires adjustment for correct
diffusion tensor calculation""",
argstr="-oc",
)
b0_threshold = traits.Float(
desc="""\
Program will use b0 image with the given threshold to mask out high
background of fa/adc maps. by default it will calculate threshold
automatically. but if it failed, you need to set it manually.""",
argstr="-b0_th",
)
class DTIReconOutputSpec(TraitedSpec):
ADC = File(exists=True)
B0 = File(exists=True)
L1 = File(exists=True)
L2 = File(exists=True)
L3 = File(exists=True)
exp = File(exists=True)
FA = File(exists=True)
FA_color = File(exists=True)
tensor = File(exists=True)
V1 = File(exists=True)
V2 = File(exists=True)
V3 = File(exists=True)
class DTIRecon(CommandLine):
"""Use dti_recon to generate tensors and other maps"""
input_spec = DTIReconInputSpec
output_spec = DTIReconOutputSpec
_cmd = "dti_recon"
def _create_gradient_matrix(self, bvecs_file, bvals_file):
_gradient_matrix_file = "gradient_matrix.txt"
with open(bvals_file) as fbvals:
bvals = [val for val in re.split(r"\s+", fbvals.readline().strip())]
with open(bvecs_file) as fbvecs:
bvecs_x = fbvecs.readline().split()
bvecs_y = fbvecs.readline().split()
bvecs_z = fbvecs.readline().split()
with open(_gradient_matrix_file, "w") as gradient_matrix_f:
for i in range(len(bvals)):
gradient_matrix_f.write(
"%s, %s, %s, %s\n" % (bvecs_x[i], bvecs_y[i], bvecs_z[i], bvals[i])
)
return _gradient_matrix_file
def _format_arg(self, name, spec, value):
if name == "bvecs":
new_val = self._create_gradient_matrix(self.inputs.bvecs, self.inputs.bvals)
return super(DTIRecon, self)._format_arg("bvecs", spec, new_val)
return super(DTIRecon, self)._format_arg(name, spec, value)
def _list_outputs(self):
out_prefix = self.inputs.out_prefix
output_type = self.inputs.output_type
outputs = self.output_spec().get()
outputs["ADC"] = os.path.abspath(
fname_presuffix("", prefix=out_prefix, suffix="_adc." + output_type)
)
outputs["B0"] = os.path.abspath(
fname_presuffix("", prefix=out_prefix, suffix="_b0." + output_type)
)
outputs["L1"] = os.path.abspath(
fname_presuffix("", prefix=out_prefix, suffix="_e1." + output_type)
)
outputs["L2"] = os.path.abspath(
fname_presuffix("", prefix=out_prefix, suffix="_e2." + output_type)
)
outputs["L3"] = os.path.abspath(
fname_presuffix("", prefix=out_prefix, suffix="_e3." + output_type)
)
outputs["exp"] = os.path.abspath(
fname_presuffix("", prefix=out_prefix, suffix="_exp." + output_type)
)
outputs["FA"] = os.path.abspath(
fname_presuffix("", prefix=out_prefix, suffix="_fa." + output_type)
)
outputs["FA_color"] = os.path.abspath(
fname_presuffix("", prefix=out_prefix, suffix="_fa_color." + output_type)
)
outputs["tensor"] = os.path.abspath(
fname_presuffix("", prefix=out_prefix, suffix="_tensor." + output_type)
)
outputs["V1"] = os.path.abspath(
fname_presuffix("", prefix=out_prefix, suffix="_v1." + output_type)
)
outputs["V2"] = os.path.abspath(
fname_presuffix("", prefix=out_prefix, suffix="_v2." + output_type)
)
outputs["V3"] = os.path.abspath(
fname_presuffix("", prefix=out_prefix, suffix="_v3." + output_type)
)
return outputs
class DTITrackerInputSpec(CommandLineInputSpec):
tensor_file = File(exists=True, desc="reconstructed tensor file")
input_type = traits.Enum(
"nii",
"analyze",
"ni1",
"nii.gz",
desc="""\
Input and output file type. Accepted values are:
* analyze -> analyze format 7.5
* ni1 -> nifti format saved in seperate .hdr and .img file
* nii -> nifti format with one .nii file
* nii.gz -> nifti format with compression
Default type is 'nii'
""",
argstr="-it %s",
)
tracking_method = traits.Enum(
"fact",
"rk2",
"tl",
"sl",
desc="""\
Tracking algorithm.
* fact -> use FACT method for tracking. This is the default method.
* rk2 -> use 2nd order Runge-Kutta method for tracking.
* tl -> use tensorline method for tracking.
* sl -> use interpolated streamline method with fixed step-length
""",
argstr="-%s",
)
step_length = traits.Float(
desc="""\
Step length, in the unit of minimum voxel size.
default value is 0.5 for interpolated streamline method
and 0.1 for other methods""",
argstr="-l %f",
)
angle_threshold = traits.Float(
desc="set angle threshold. default value is 35 degree", argstr="-at %f"
)
angle_threshold_weight = traits.Float(
desc="set angle threshold weighting factor. weighting will be be applied "
"on top of the angle_threshold",
argstr="-atw %f",
)
random_seed = traits.Int(
desc="use random location in a voxel instead of the center of the voxel "
"to seed. can also define number of seed per voxel. default is 1",
argstr="-rseed %d",
)
invert_x = traits.Bool(desc="invert x component of the vector", argstr="-ix")
invert_y = traits.Bool(desc="invert y component of the vector", argstr="-iy")
invert_z = traits.Bool(desc="invert z component of the vector", argstr="-iz")
swap_xy = traits.Bool(desc="swap x & y vectors while tracking", argstr="-sxy")
swap_yz = traits.Bool(desc="swap y & z vectors while tracking", argstr="-syz")
swap_zx = traits.Bool(desc="swap x & z vectors while tracking", argstr="-szx")
mask1_file = File(
desc="first mask image", mandatory=True, argstr="-m %s", position=2
)
mask1_threshold = traits.Float(
desc="threshold value for the first mask image, if not given, the program will "
"try automatically find the threshold",
position=3,
)
mask2_file = File(desc="second mask image", argstr="-m2 %s", position=4)
mask2_threshold = traits.Float(
desc="threshold value for the second mask image, if not given, the program will "
"try automatically find the threshold",
position=5,
)
input_data_prefix = traits.Str(
"dti",
desc="for internal naming use only",
position=0,
argstr="%s",
usedefault=True,
)
output_file = File(
"tracks.trk", "file containing tracks", argstr="%s", position=1, usedefault=True
)
output_mask = File(
desc="output a binary mask file in analyze format", argstr="-om %s"
)
primary_vector = traits.Enum(
"v2",
"v3",
desc="which vector to use for fibre tracking: v2 or v3. If not set use v1",
argstr="-%s",
)
class DTITrackerOutputSpec(TraitedSpec):
track_file = File(exists=True)
mask_file = File(exists=True)
class DTITracker(CommandLine):
input_spec = DTITrackerInputSpec
output_spec = DTITrackerOutputSpec
_cmd = "dti_tracker"
def _run_interface(self, runtime):
_, _, ext = split_filename(self.inputs.tensor_file)
copyfile(
self.inputs.tensor_file,
os.path.abspath(self.inputs.input_data_prefix + "_tensor" + ext),
copy=False,
)
return super(DTITracker, self)._run_interface(runtime)
def _list_outputs(self):
outputs = self.output_spec().get()
outputs["track_file"] = os.path.abspath(self.inputs.output_file)
if isdefined(self.inputs.output_mask) and self.inputs.output_mask:
outputs["mask_file"] = os.path.abspath(self.inputs.output_mask)
return outputs