-
Notifications
You must be signed in to change notification settings - Fork 15
/
outputs.py
466 lines (430 loc) · 14.4 KB
/
outputs.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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*-
# vi: set ft=python sts=4 ts=4 sw=4 et:
"""Workflows for writing out derivative files."""
from nipype.interfaces import utility as niu
from nipype.pipeline import engine as pe
from niworkflows.engine.workflows import LiterateWorkflow as Workflow
from niworkflows.interfaces.utility import KeySelect
from smriprep.workflows.outputs import _bids_relative
from aslprep import config
from aslprep.interfaces import DerivativesDataSink
def init_asl_derivatives_wf(
bids_root,
metadata,
output_dir,
spaces,
is_multi_pld,
output_confounds=True,
scorescrub=False,
basil=False,
name="asl_derivatives_wf",
):
"""Set up a battery of datasinks to store derivatives in the right location.
Parameters
----------
bids_root : :obj:`str`
Original BIDS dataset path.
metadata : :obj:`dict`
Metadata dictionary associated to the ASL run.
output_dir : :obj:`str`
Where derivatives should be written out to.
spaces : :py:class:`~niworkflows.utils.spaces.SpatialReferences`
A container for storing, organizing, and parsing spatial normalizations. Composed of
:py:class:`~niworkflows.utils.spaces.Reference` objects representing spatial references.
Each ``Reference`` contains a space, which is a string of either TemplateFlow template IDs
(e.g., ``MNI152Lin``, ``MNI152NLin6Asym``, ``MNIPediatricAsym``), nonstandard references
(e.g., ``T1w`` or ``anat``, ``sbref``, ``run``, etc.), or a custom template located in
the TemplateFlow root directory. Each ``Reference`` may also contain a spec, which is a
dictionary with template specifications (e.g., a specification of ``{'resolution': 2}``
would lead to resampling on a 2mm resolution of the space).
is_multi_pld : :obj:`bool`
True if data are multi-delay, False otherwise.
name : :obj:`str`
This workflow's identifier (default: ``func_derivatives_wf``).
"""
nonstd_spaces = set(spaces.get_nonstandard())
workflow = Workflow(name=name)
inputnode = pe.Node(
niu.IdentityInterface(
fields=[
"source_file",
"template",
"spatial_reference",
"qc_file",
# Preprocessed ASL files
"asl_native",
"asl_t1",
"asl_std",
"aslref_native",
"aslref_t1",
"aslref_std",
"asl_mask_native",
"asl_mask_t1",
"asl_mask_std",
# Transforms
"aslref_to_anat_xfm",
"anat_to_aslref_xfm",
# Standard CBF outputs
"cbf_ts_native",
"cbf_ts_t1",
"cbf_ts_std",
"mean_cbf_native",
"mean_cbf_t1",
"mean_cbf_std",
"att_native",
"att_t1",
"att_std",
# SCORE/SCRUB outputs
"cbf_ts_score_native",
"cbf_ts_score_t1",
"cbf_ts_score_std",
"mean_cbf_score_native",
"mean_cbf_score_t1",
"mean_cbf_score_std",
"mean_cbf_scrub_native",
"mean_cbf_scrub_t1",
"mean_cbf_scrub_std",
# BASIL outputs
"mean_cbf_basil_native",
"mean_cbf_basil_t1",
"mean_cbf_basil_std",
"mean_cbf_gm_basil_native",
"mean_cbf_gm_basil_t1",
"mean_cbf_gm_basil_std",
"mean_cbf_wm_basil_native",
"mean_cbf_wm_basil_t1",
"mean_cbf_wm_basil_std",
"att_basil_native",
"att_basil_t1",
"att_basil_std",
# Parcellated CBF outputs
"atlas_names",
"mean_cbf_parcellated",
"mean_cbf_score_parcellated",
"mean_cbf_scrub_parcellated",
"mean_cbf_basil_parcellated",
"mean_cbf_gm_basil_parcellated",
# non-GE outputs
"confounds",
"confounds_metadata",
],
),
name="inputnode",
)
raw_sources = pe.Node(niu.Function(function=_bids_relative), name="raw_sources")
raw_sources.inputs.bids_root = bids_root
# fmt:off
workflow.connect([(inputnode, raw_sources, [("source_file", "in_files")])])
# fmt:on
if output_confounds:
ds_confounds = pe.Node(
DerivativesDataSink(
base_directory=output_dir,
desc="confounds",
suffix="regressors",
),
name="ds_confounds",
run_without_submitting=True,
mem_gb=config.DEFAULT_MEMORY_MIN_GB,
)
# fmt:off
workflow.connect([
(inputnode, ds_confounds, [
("source_file", "source_file"),
("confounds", "in_file"),
("confounds_metadata", "meta_dict"),
]),
])
# fmt:on
ds_qcfile = pe.Node(
DerivativesDataSink(
base_directory=output_dir,
desc="qualitycontrol",
suffix="cbf",
compress=False,
),
name="ds_qcfile",
run_without_submitting=True,
mem_gb=config.DEFAULT_MEMORY_MIN_GB,
)
# fmt:off
workflow.connect([
(inputnode, ds_qcfile, [
("source_file", "source_file"),
("qc_file", "in_file"),
]),
])
# fmt:on
# write transform matrix file between asl native space and T1w
ds_t1w_to_asl_xform = pe.Node(
DerivativesDataSink(
base_directory=output_dir,
to="scanner",
mode="image",
suffix="xfm",
extension=".txt",
dismiss_entities=("echo",),
**{"from": "T1w"},
),
name="ds_t1w_to_asl_xform",
run_without_submitting=True,
mem_gb=config.DEFAULT_MEMORY_MIN_GB,
)
# fmt:off
workflow.connect([
(inputnode, ds_t1w_to_asl_xform, [
("source_file", "source_file"),
("anat_to_aslref_xfm", "in_file"),
]),
])
# fmt:on
ds_asl_to_t1w_xform = pe.Node(
DerivativesDataSink(
base_directory=output_dir,
dismiss_entities=("echo",),
to="T1w",
mode="image",
suffix="xfm",
extension=".txt",
**{"from": "scanner"},
),
name="ds_asl_to_t1w_xform",
run_without_submitting=True,
mem_gb=config.DEFAULT_MEMORY_MIN_GB,
)
# fmt:off
workflow.connect([
(inputnode, ds_asl_to_t1w_xform, [
("source_file", "source_file"),
("aslref_to_anat_xfm", "in_file"),
]),
])
# fmt:on
# Parcellated data
PARCELLATED_INPUT_FIELDS = {
"mean_cbf_parcellated": {},
"mean_cbf_score_parcellated": {
"desc": "score",
},
"mean_cbf_scrub_parcellated": {
"desc": "scrub",
},
"mean_cbf_basil_parcellated": {
"desc": "basil",
},
"mean_cbf_gm_basil_parcellated": {
"desc": "basilGM",
},
}
parcellated_inputs = ["mean_cbf_parcellated"]
if scorescrub:
parcellated_inputs += ["mean_cbf_score_parcellated", "mean_cbf_scrub_parcellated"]
if basil:
parcellated_inputs += ["mean_cbf_basil_parcellated", "mean_cbf_gm_basil_parcellated"]
for parcellated_input in parcellated_inputs:
ds_parcellated_input = pe.MapNode(
DerivativesDataSink(
base_directory=output_dir,
suffix="cbf",
compress=False,
**PARCELLATED_INPUT_FIELDS[parcellated_input],
),
name=f"ds_{parcellated_input}",
run_without_submitting=True,
mem_gb=config.DEFAULT_MEMORY_MIN_GB,
iterfield=["atlas", "in_file"],
)
# fmt:off
workflow.connect([
(inputnode, ds_parcellated_input, [
("source_file", "source_file"),
("atlas_names", "atlas"),
(parcellated_input, "in_file"),
]),
])
# fmt:on
# Now prepare to write out primary imaging derivatives
asl_metadata = {
"SkullStripped": False,
"RepetitionTime": metadata.get("RepetitionTime"),
"RepetitionTimePreparation": metadata.get("RepetitionTimePreparation"),
}
cbf_metadata = {
"Units": "mL/100 g/min",
}
BASE_INPUT_FIELDS = {
"asl": {
"desc": "preproc",
"suffix": "asl",
**asl_metadata,
},
"aslref": {
"suffix": "aslref",
"dismiss_entities": ("echo",),
},
"asl_mask": {
"desc": "brain",
"suffix": "mask",
"dismiss_entities": ("echo",),
},
# CBF outputs
"cbf_ts": {
"desc": "timeseries",
"suffix": "cbf",
**cbf_metadata,
},
"mean_cbf": {
"suffix": "cbf",
**cbf_metadata,
},
"att": {
"suffix": "att",
"Units": "s",
},
# SCORE/SCRUB outputs
"cbf_ts_score": {
"desc": "scoreTimeseries",
"suffix": "cbf",
**cbf_metadata,
},
"mean_cbf_score": {
"desc": "score",
"suffix": "cbf",
**cbf_metadata,
},
"mean_cbf_scrub": {
"desc": "scrub",
"suffix": "cbf",
**cbf_metadata,
},
# BASIL outputs
"mean_cbf_basil": {
"desc": "basil",
"suffix": "cbf",
**cbf_metadata,
},
"mean_cbf_gm_basil": {
"desc": "pvGM",
"suffix": "cbf",
**cbf_metadata,
},
"mean_cbf_wm_basil": {
"desc": "pvWM",
"suffix": "cbf",
**cbf_metadata,
},
"att_basil": {
"desc": "basil",
"suffix": "att",
"Units": "s",
},
}
base_inputs = ["asl", "aslref", "asl_mask", "mean_cbf"]
if is_multi_pld:
# ATT is only calculated for multi-delay data
base_inputs += ["att"]
else:
# CBF time series is only calculated for single-delay data
base_inputs += ["cbf_ts"]
if scorescrub:
base_inputs += ["cbf_ts_score", "mean_cbf_score", "mean_cbf_scrub"]
if basil:
base_inputs += ["mean_cbf_basil", "mean_cbf_gm_basil", "mean_cbf_wm_basil", "att_basil"]
# Native-space derivatives
if nonstd_spaces.intersection(("func", "run", "asl", "sbref")):
for base_input in base_inputs:
base_input_native = f"{base_input}_native"
ds_base_input_native = pe.Node(
DerivativesDataSink(
base_directory=output_dir,
compress=True,
**BASE_INPUT_FIELDS[base_input],
),
name=f"ds_{base_input_native}",
run_without_submitting=True,
mem_gb=config.DEFAULT_MEMORY_MIN_GB,
)
# fmt:off
workflow.connect([
(inputnode, ds_base_input_native, [
("source_file", "source_file"),
(base_input_native, "in_file"),
]),
(raw_sources, ds_base_input_native, [("out", "RawSources")]),
])
# fmt:on
# T1w-space derivatives
if nonstd_spaces.intersection(("T1w", "anat")):
for base_input in base_inputs:
base_input_t1 = f"{base_input}_t1"
ds_base_input_t1 = pe.Node(
DerivativesDataSink(
base_directory=output_dir,
compress=True,
space="T1w",
**BASE_INPUT_FIELDS[base_input],
),
name=f"ds_{base_input_t1}",
run_without_submitting=True,
mem_gb=config.DEFAULT_MEMORY_MIN_GB,
)
# fmt:off
workflow.connect([
(inputnode, ds_base_input_t1, [
("source_file", "source_file"),
(base_input_t1, "in_file"),
]),
(raw_sources, ds_base_input_t1, [("out", "RawSources")]),
])
# fmt:on
if getattr(spaces, "_cached") is None:
return workflow
# Standard-space derivatives
from niworkflows.interfaces.space import SpaceDataSource
spacesource = pe.Node(SpaceDataSource(), name="spacesource", run_without_submitting=True)
spacesource.iterables = (
"in_tuple",
[(s.fullname, s.spec) for s in spaces.cached.get_standard(dim=(3,))],
)
select_std = pe.Node(
KeySelect(fields=[f"{base_input}_std" for base_input in base_inputs] + ["template"]),
name="select_std",
run_without_submitting=True,
mem_gb=config.DEFAULT_MEMORY_MIN_GB,
)
# fmt:off
workflow.connect([
(inputnode, select_std, [
("template", "template"),
("spatial_reference", "keys"),
]),
(spacesource, select_std, [("uid", "key")]),
])
# fmt:on
for base_input in base_inputs:
base_input_std = f"{base_input}_std"
ds_base_input_std = pe.Node(
DerivativesDataSink(
base_directory=output_dir,
compress=True,
**BASE_INPUT_FIELDS[base_input],
),
name=f"ds_{base_input_std}",
run_without_submitting=True,
mem_gb=config.DEFAULT_MEMORY_MIN_GB,
)
# fmt:off
workflow.connect([
(inputnode, ds_base_input_std, [("source_file", "source_file")]),
(raw_sources, ds_base_input_std, [("out", "RawSources")]),
(inputnode, select_std, [(base_input_std, base_input_std)]),
(select_std, ds_base_input_std, [(base_input_std, "in_file")]),
(spacesource, ds_base_input_std, [
("space", "space"),
("cohort", "cohort"),
("resolution", "resolution"),
("density", "density"),
]),
])
# fmt:on
return workflow