-
Notifications
You must be signed in to change notification settings - Fork 92
/
t2smap.py
343 lines (311 loc) · 11.1 KB
/
t2smap.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
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
"""
Estimate T2 and S0, and optimally combine data across TEs.
"""
import argparse
import logging
import os
import os.path as op
import numpy as np
from scipy import stats
from threadpoolctl import threadpool_limits
from tedana import __version__, combine, decay, io, utils
from tedana.workflows.parser_utils import is_valid_file
LGR = logging.getLogger("GENERAL")
RepLGR = logging.getLogger("REPORT")
def _get_parser():
"""
Parses command line inputs for tedana
Returns
-------
parser.parse_args() : argparse dict
"""
parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter)
# Argument parser follow template provided by RalphyZ
# https://stackoverflow.com/a/43456577
optional = parser._action_groups.pop()
required = parser.add_argument_group("Required Arguments")
required.add_argument(
"-d",
dest="data",
nargs="+",
metavar="FILE",
type=lambda x: is_valid_file(parser, x),
help=(
"Multi-echo dataset for analysis. May be a "
"single file with spatially concatenated data "
"or a set of echo-specific files, in the same "
"order as the TEs are listed in the -e "
"argument."
),
required=True,
)
required.add_argument(
"-e",
dest="tes",
nargs="+",
metavar="TE",
type=float,
help="Echo times (in ms). E.g., 15.0 39.0 63.0",
required=True,
)
optional.add_argument(
"--out-dir",
dest="out_dir",
type=str,
metavar="PATH",
help="Output directory.",
default=".",
)
optional.add_argument(
"--mask",
dest="mask",
metavar="FILE",
type=lambda x: is_valid_file(parser, x),
help=(
"Binary mask of voxels to include in TE "
"Dependent ANAlysis. Must be in the same "
"space as `data`."
),
default=None,
)
optional.add_argument(
"--prefix", dest="prefix", type=str, help="Prefix for filenames generated.", default=""
)
optional.add_argument(
"--convention",
dest="convention",
action="store",
choices=["orig", "bids"],
help=("Filenaming convention. bids will use the latest BIDS derivatives version."),
default="bids",
)
optional.add_argument(
"--fittype",
dest="fittype",
action="store",
choices=["loglin", "curvefit"],
help=(
"Desired T2*/S0 fitting method. "
'"loglin" means that a linear model is fit '
"to the log of the data. "
'"curvefit" means that a more computationally '
"demanding monoexponential model is fit "
"to the raw data. "
),
default="loglin",
)
optional.add_argument(
"--fitmode",
dest="fitmode",
action="store",
choices=["all", "ts"],
help=(
"Monoexponential model fitting scheme. "
'"all" means that the model is fit, per voxel, '
"across all timepoints. "
'"ts" means that the model is fit, per voxel '
"and per timepoint."
),
default="all",
)
optional.add_argument(
"--combmode",
dest="combmode",
action="store",
choices=["t2s", "paid"],
help=("Combination scheme for TEs: t2s (Posse 1999), paid (Poser)"),
default="t2s",
)
optional.add_argument(
"--n-threads",
dest="n_threads",
type=int,
action="store",
help=(
"Number of threads to use. Used by "
"threadpoolctl to set the parameter outside "
"of the workflow function. Higher numbers of "
"threads tend to slow down performance on "
"typical datasets."
),
default=1,
)
optional.add_argument(
"--debug", dest="debug", help=argparse.SUPPRESS, action="store_true", default=False
)
optional.add_argument(
"--quiet", dest="quiet", help=argparse.SUPPRESS, action="store_true", default=False
)
parser._action_groups.append(optional)
return parser
def t2smap_workflow(
data,
tes,
out_dir=".",
mask=None,
prefix="",
convention="bids",
fittype="loglin",
fitmode="all",
combmode="t2s",
debug=False,
quiet=False,
):
"""
Estimate T2 and S0, and optimally combine data across TEs.
Please remember to cite :footcite:t:`dupre2021te`.
Parameters
----------
data : :obj:`str` or :obj:`list` of :obj:`str`
Either a single z-concatenated file (single-entry list or str) or a
list of echo-specific files, in ascending order.
tes : :obj:`list`
List of echo times associated with data in milliseconds.
out_dir : :obj:`str`, optional
Output directory.
mask : :obj:`str`, optional
Binary mask of voxels to include in TE Dependent ANAlysis. Must be spatially
aligned with `data`.
fittype : {'loglin', 'curvefit'}, optional
Monoexponential fitting method.
'loglin' means to use the the default linear fit to the log of
the data.
'curvefit' means to use a monoexponential fit to the raw data,
which is slightly slower but may be more accurate.
fitmode : {'all', 'ts'}, optional
Monoexponential model fitting scheme.
'all' means that the model is fit, per voxel, across all timepoints.
'ts' means that the model is fit, per voxel and per timepoint.
Default is 'all'.
combmode : {'t2s', 'paid'}, optional
Combination scheme for TEs: 't2s' (Posse 1999, default), 'paid' (Poser).
Other Parameters
----------------
debug : :obj:`bool`, optional
Whether to run in debugging mode or not. Default is False.
quiet : :obj:`bool`, optional
If True, suppress logging/printing of messages. Default is False.
Notes
-----
This workflow writes out several files, which are described below:
============================= =================================================
Filename Content
============================= =================================================
T2starmap.nii.gz Estimated T2* 3D map or 4D timeseries.
Will be a 3D map if ``fitmode`` is 'all' and a
4D timeseries if it is 'ts'.
S0map.nii.gz S0 3D map or 4D timeseries.
desc-limited_T2starmap.nii.gz Limited T2* map/timeseries. The difference between
the limited and full maps is that, for voxels
affected by dropout where only one echo contains
good data, the full map uses the T2* estimate
from the first two echos, while the limited map
will have a NaN.
desc-limited_S0map.nii.gz Limited S0 map/timeseries. The difference between
the limited and full maps is that, for voxels
affected by dropout where only one echo contains
good data, the full map uses the S0 estimate
from the first two echos, while the limited map
will have a NaN.
desc-optcom_bold.nii.gz Optimally combined timeseries.
============================= =================================================
References
----------
.. footbibliography::
"""
out_dir = op.abspath(out_dir)
if not op.isdir(out_dir):
os.mkdir(out_dir)
utils.setup_loggers(quiet=quiet, debug=debug)
LGR.info("Using output directory: {}".format(out_dir))
# ensure tes are in appropriate format
tes = [float(te) for te in tes]
n_echos = len(tes)
# coerce data to samples x echos x time array
if isinstance(data, str):
data = [data]
LGR.info("Loading input data: {}".format([f for f in data]))
catd, ref_img = io.load_data(data, n_echos=n_echos)
io_generator = io.OutputGenerator(
ref_img,
convention=convention,
out_dir=out_dir,
prefix=prefix,
config="auto",
make_figures=False,
)
n_samp, n_echos, n_vols = catd.shape
LGR.debug("Resulting data shape: {}".format(catd.shape))
if mask is None:
LGR.info("Computing adaptive mask")
else:
LGR.info("Using user-defined mask")
mask, masksum = utils.make_adaptive_mask(catd, mask=mask, getsum=True, threshold=1)
LGR.info("Computing adaptive T2* map")
if fitmode == "all":
(t2s_limited, s0_limited, t2s_full, s0_full) = decay.fit_decay(
catd, tes, mask, masksum, fittype
)
else:
(t2s_limited, s0_limited, t2s_full, s0_full) = decay.fit_decay_ts(
catd, tes, mask, masksum, fittype
)
# set a hard cap for the T2* map/timeseries
# anything that is 10x higher than the 99.5 %ile will be reset to 99.5 %ile
cap_t2s = stats.scoreatpercentile(t2s_full.flatten(), 99.5, interpolation_method="lower")
cap_t2s_sec = utils.millisec2sec(cap_t2s * 10.0)
LGR.debug("Setting cap on T2* map at {:.5f}s".format(cap_t2s_sec))
t2s_full[t2s_full > cap_t2s * 10] = cap_t2s
LGR.info("Computing optimal combination")
# optimally combine data
OCcatd = combine.make_optcom(catd, tes, masksum, t2s=t2s_full, combmode=combmode)
# clean up numerical errors
for arr in (OCcatd, s0_full, t2s_full):
np.nan_to_num(arr, copy=False)
s0_full[s0_full < 0] = 0
t2s_full[t2s_full < 0] = 0
io_generator.save_file(
utils.millisec2sec(t2s_full),
"t2star img",
)
io_generator.save_file(s0_full, "s0 img")
io_generator.save_file(
utils.millisec2sec(t2s_limited),
"limited t2star img",
)
io_generator.save_file(
s0_limited,
"limited s0 img",
)
io_generator.save_file(OCcatd, "combined img")
# Write out BIDS-compatible description file
derivative_metadata = {
"Name": "t2smap Outputs",
"BIDSVersion": "1.5.0",
"DatasetType": "derivative",
"GeneratedBy": [
{
"Name": "tedana",
"Version": __version__,
"Description": (
"A pipeline estimating T2* from multi-echo fMRI data and "
"combining data across echoes."
),
"CodeURL": "https://github.com/ME-ICA/tedana",
}
],
}
io_generator.save_file(derivative_metadata, "data description json")
io_generator.save_self()
LGR.info("Workflow completed")
utils.teardown_loggers()
def _main(argv=None):
"""T2smap entry point"""
options = _get_parser().parse_args(argv)
kwargs = vars(options)
n_threads = kwargs.pop("n_threads")
n_threads = None if n_threads == -1 else n_threads
with threadpool_limits(limits=n_threads, user_api=None):
t2smap_workflow(**kwargs)
if __name__ == "__main__":
_main()