/
t2s.py
223 lines (188 loc) · 7.55 KB
/
t2s.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
# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*-
# vi: set ft=python sts=4 ts=4 sw=4 et:
#
# Copyright 2023 The NiPreps Developers <nipreps@gmail.com>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# We support and encourage derived works from this project, please read
# about our expectations at
#
# https://www.nipreps.org/community/licensing/
#
"""
Generate T2* map from multi-echo BOLD images
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
.. autofunction:: init_bold_t2s_wf
"""
import typing as ty
from nipype.interfaces import utility as niu
from nipype.pipeline import engine as pe
from ... import config
from ...interfaces.maths import Clip, Label2Mask
from ...interfaces.multiecho import T2SMap
from ...interfaces.reports import LabeledHistogram
LOGGER = config.loggers.workflow
# pylint: disable=R0914
def init_bold_t2s_wf(
echo_times: ty.Sequence[float],
mem_gb: float,
omp_nthreads: int,
name: str = 'bold_t2s_wf',
):
r"""
Combine multiple echos of :abbr:`ME-EPI (multi-echo echo-planar imaging)`.
This workflow wraps the `tedana`_ `T2* workflow`_ to optimally
combine multiple preprocessed echos and derive a T2\ :sup:`★` map.
The following steps are performed:
#. Compute the T2\ :sup:`★` map
#. Create an optimally combined ME-EPI time series
.. _tedana: https://github.com/me-ica/tedana
.. _`T2* workflow`: https://tedana.readthedocs.io/en/latest/generated/tedana.workflows.t2smap_workflow.html#tedana.workflows.t2smap_workflow # noqa
Parameters
----------
echo_times : :obj:`list` or :obj:`tuple`
list of TEs associated with each echo
mem_gb : :obj:`float`
Size of BOLD file in GB
omp_nthreads : :obj:`int`
Maximum number of threads an individual process may use
name : :obj:`str`
Name of workflow (default: ``bold_t2s_wf``)
Inputs
------
bold_file
list of individual echo files
bold_mask
a binary mask to apply to the BOLD files
Outputs
-------
bold
the optimally combined time series for all supplied echos
t2star_map
the calculated T2\ :sup:`★` map
"""
from niworkflows.engine.workflows import LiterateWorkflow as Workflow
from niworkflows.interfaces.morphology import BinaryDilation
workflow = Workflow(name=name)
if config.workflow.me_t2s_fit_method == "curvefit":
fit_str = (
"nonlinear regression. "
"The T2<sup>★</sup>/S<sub>0</sub> estimates from a log-linear regression fit "
"were used for initial values"
)
else:
fit_str = "log-linear regression"
workflow.__desc__ = f"""\
A T2<sup>★</sup> map was estimated from the preprocessed EPI echoes, by voxel-wise fitting
the maximal number of echoes with reliable signal in that voxel to a monoexponential signal
decay model with {fit_str}.
The calculated T2<sup>★</sup> map was then used to optimally combine preprocessed BOLD across
echoes following the method described in [@posse_t2s].
The optimally combined time series was carried forward as the *preprocessed BOLD*.
"""
inputnode = pe.Node(niu.IdentityInterface(fields=['bold_file', 'bold_mask']), name='inputnode')
outputnode = pe.Node(niu.IdentityInterface(fields=['bold', 't2star_map']), name='outputnode')
LOGGER.log(25, 'Generating T2* map and optimally combined ME-EPI time series.')
dilate_mask = pe.Node(BinaryDilation(radius=2), name='dilate_mask')
t2smap_node = pe.Node(
T2SMap(echo_times=list(echo_times), fittype=config.workflow.me_t2s_fit_method),
name='t2smap_node',
mem_gb=2.5 * mem_gb * len(echo_times),
)
workflow.connect([
(inputnode, dilate_mask, [('bold_mask', 'in_mask')]),
(inputnode, t2smap_node, [('bold_file', 'in_files')]),
(dilate_mask, t2smap_node, [('out_mask', 'mask_file')]),
(t2smap_node, outputnode, [('optimal_comb', 'bold'),
('t2star_map', 't2star_map')]),
]) # fmt:skip
return workflow
def init_t2s_reporting_wf(name: str = 't2s_reporting_wf'):
r"""
Generate T2\*-map reports.
This workflow generates a histogram of estimated T2\* values (in seconds) in the
cortical and subcortical gray matter mask.
Parameters
----------
mem_gb : :obj:`float`
Size of BOLD file in GB
omp_nthreads : :obj:`int`
Maximum number of threads an individual process may use
name : :obj:`str`
Name of workflow (default: ``t2s_reporting_wf``)
Inputs
------
t2star_file
estimated T2\* map
boldref
reference BOLD file
label_file
an integer label file identifying gray matter with value ``1``
boldref2anat_xfm
Affine matrix that maps images in the native bold space into the
anatomical space of ``label_file``; can be ``"identity"`` if label
file is already aligned
Outputs
-------
t2star_hist
an SVG histogram showing estimated T2\* values in gray matter
t2s_comp_report
a before/after figure comparing the reference BOLD image and T2\* map
"""
from nipype.pipeline import engine as pe
from nireports.interfaces.reporting.base import (
SimpleBeforeAfterRPT as SimpleBeforeAfter,
)
from niworkflows.interfaces.fixes import FixHeaderApplyTransforms as ApplyTransforms
workflow = pe.Workflow(name=name)
inputnode = pe.Node(
niu.IdentityInterface(fields=['t2star_file', 'boldref', 'label_file', 'boldref2anat_xfm']),
name='inputnode',
)
outputnode = pe.Node(
niu.IdentityInterface(fields=['t2star_hist', 't2s_comp_report']), name='outputnode'
)
label_tfm = pe.Node(
ApplyTransforms(interpolation="MultiLabel", invert_transform_flags=[True]),
name="label_tfm",
)
gm_mask = pe.Node(Label2Mask(label_val=1), name="gm_mask")
clip_t2star = pe.Node(Clip(maximum=0.1), name="clip_t2star")
t2s_hist = pe.Node(
LabeledHistogram(mapping={1: "Gray matter"}, xlabel='T2* (s)'), name='t2s_hist'
)
t2s_comparison = pe.Node(
SimpleBeforeAfter(
before_label="BOLD Reference",
after_label="T2* Map",
dismiss_affine=True,
),
name="t2s_comparison",
mem_gb=0.1,
)
workflow.connect([
(inputnode, label_tfm, [('label_file', 'input_image'),
('t2star_file', 'reference_image'),
('boldref2anat_xfm', 'transforms')]),
(inputnode, clip_t2star, [('t2star_file', 'in_file')]),
(clip_t2star, t2s_hist, [('out_file', 'in_file')]),
(label_tfm, gm_mask, [('output_image', 'in_file')]),
(gm_mask, t2s_hist, [('out_file', 'label_file')]),
(inputnode, t2s_comparison, [('boldref', 'before'),
('t2star_file', 'after')]),
(gm_mask, t2s_comparison, [('out_file', 'wm_seg')]),
(t2s_hist, outputnode, [('out_report', 't2star_hist')]),
(t2s_comparison, outputnode, [('out_report', 't2s_comp_report')]),
]) # fmt:skip
return workflow