-
Notifications
You must be signed in to change notification settings - Fork 21
/
concatenation.py
415 lines (380 loc) · 13.6 KB
/
concatenation.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
"""Workflows for concatenating postprocessed data."""
from nipype.interfaces import utility as niu
from nipype.pipeline import engine as pe
from niworkflows.engine.workflows import LiterateWorkflow as Workflow
from xcp_d.interfaces.bids import DerivativesDataSink
from xcp_d.interfaces.concatenation import (
CleanNameSource,
ConcatenateInputs,
FilterOutFailedRuns,
)
from xcp_d.utils.doc import fill_doc
from xcp_d.utils.utils import _select_first
from xcp_d.workflows.plotting import init_qc_report_wf
@fill_doc
def init_concatenate_data_wf(
output_dir,
motion_filter_type,
mem_gb,
omp_nthreads,
TR,
head_radius,
smoothing,
cifti,
dcan_qc,
name="concatenate_data_wf",
):
"""Concatenate postprocessed data.
Workflow Graph
.. workflow::
:graph2use: orig
:simple_form: yes
from xcp_d.workflows.concatenation import init_concatenate_data_wf
wf = init_concatenate_data_wf(
output_dir=".",
motion_filter_type=None,
mem_gb=0.1,
omp_nthreads=1,
TR=2,
head_radius=50,
smoothing=None,
cifti=False,
dcan_qc=True,
name="concatenate_data_wf",
)
Parameters
----------
%(output_dir)s
%(motion_filter_type)s
%(mem_gb)s
%(omp_nthreads)s
%(TR)s
%(head_radius)s
%(smoothing)s
%(cifti)s
%(dcan_qc)s
%(name)s
Default is "concatenate_data_wf".
Inputs
------
%(name_source)s
One list entry for each run.
These are used as the bases for concatenated output filenames.
preprocessed_bold : :obj:`list` of :obj:`str`
The preprocessed BOLD files, after dummy volume removal.
%(filtered_motion)s
One list entry for each run.
%(temporal_mask)s
One list entry for each run.
%(uncensored_denoised_bold)s
One list entry for each run.
%(interpolated_filtered_bold)s
One list entry for each run.
%(censored_denoised_bold)s
One list entry for each run.
bold_mask : :obj:`list` of :obj:`str` or :obj:`~nipype.interfaces.base.Undefined`
Brain mask files for each of the BOLD runs.
This will be a list of paths for NIFTI inputs, or a list of Undefineds for CIFTI ones.
anat_brainmask : :obj:`str`
%(template_to_anat_xfm)s
%(boldref)s
%(atlas_names)s
This will be a list of lists, with one sublist for each run.
%(timeseries)s
This will be a list of lists, with one sublist for each run.
%(timeseries_ciftis)s
This will be a list of lists, with one sublist for each run.
"""
workflow = Workflow(name=name)
workflow.__desc__ = """
Postprocessing derivatives from multi-run tasks were then concatenated across runs.
"""
inputnode = pe.Node(
niu.IdentityInterface(
fields=[
"name_source",
"preprocessed_bold",
"fmriprep_confounds_file",
"filtered_motion",
"temporal_mask",
"uncensored_denoised_bold",
"interpolated_filtered_bold",
"censored_denoised_bold",
"smoothed_denoised_bold",
"bold_mask", # only for niftis, from postproc workflows
"boldref", # only for niftis, from postproc workflows
"anat_to_native_xfm", # only for niftis, from postproc workflows
"anat_brainmask", # only for niftis, from data collection
"template_to_anat_xfm", # only for niftis, from data collection
"atlas_names", # this will be exactly the same across runs
"timeseries",
"timeseries_ciftis", # only for ciftis, from postproc workflows
],
),
name="inputnode",
)
clean_name_source = pe.Node(
CleanNameSource(),
name="clean_name_source",
)
# fmt:off
workflow.connect([(inputnode, clean_name_source, [("name_source", "name_source")])])
# fmt:on
filter_out_failed_runs = pe.Node(
FilterOutFailedRuns(),
name="filter_out_failed_runs",
)
# fmt:off
workflow.connect([
(inputnode, filter_out_failed_runs, [
("preprocessed_bold", "preprocessed_bold"),
("fmriprep_confounds_file", "fmriprep_confounds_file"),
("filtered_motion", "filtered_motion"),
("temporal_mask", "temporal_mask"),
("uncensored_denoised_bold", "uncensored_denoised_bold"),
("interpolated_filtered_bold", "interpolated_filtered_bold"),
("censored_denoised_bold", "censored_denoised_bold"),
("smoothed_denoised_bold", "smoothed_denoised_bold"),
("bold_mask", "bold_mask"),
("boldref", "boldref"),
("anat_to_native_xfm", "anat_to_native_xfm"),
("atlas_names", "atlas_names"),
("timeseries", "timeseries"),
("timeseries_ciftis", "timeseries_ciftis"),
])
])
# fmt:on
concatenate_inputs = pe.Node(
ConcatenateInputs(),
name="concatenate_inputs",
)
# fmt:off
workflow.connect([
(filter_out_failed_runs, concatenate_inputs, [
("preprocessed_bold", "preprocessed_bold"),
("fmriprep_confounds_file", "fmriprep_confounds_file"),
("filtered_motion", "filtered_motion"),
("temporal_mask", "temporal_mask"),
("uncensored_denoised_bold", "uncensored_denoised_bold"),
("interpolated_filtered_bold", "interpolated_filtered_bold"),
("censored_denoised_bold", "censored_denoised_bold"),
("smoothed_denoised_bold", "smoothed_denoised_bold"),
("timeseries", "timeseries"),
("timeseries_ciftis", "timeseries_ciftis"),
]),
])
# fmt:on
# Now, run the QC report workflow on the concatenated BOLD file.
qc_report_wf = init_qc_report_wf(
output_dir=output_dir,
TR=TR,
head_radius=head_radius,
mem_gb=mem_gb,
omp_nthreads=omp_nthreads,
cifti=cifti,
dcan_qc=dcan_qc,
name="concat_qc_report_wf",
)
qc_report_wf.inputs.inputnode.dummy_scans = 0
# fmt:off
workflow.connect([
(inputnode, qc_report_wf, [
("template_to_anat_xfm", "inputnode.template_to_anat_xfm"),
("anat_brainmask", "inputnode.anat_brainmask"),
]),
(clean_name_source, qc_report_wf, [("name_source", "inputnode.name_source")]),
(filter_out_failed_runs, qc_report_wf, [
# nifti-only inputs
(("bold_mask", _select_first), "inputnode.bold_mask"),
(("boldref", _select_first), "inputnode.boldref"),
(("anat_to_native_xfm", _select_first), "inputnode.anat_to_native_xfm"),
]),
(concatenate_inputs, qc_report_wf, [
("preprocessed_bold", "inputnode.preprocessed_bold"),
("uncensored_denoised_bold", "inputnode.uncensored_denoised_bold"),
("interpolated_filtered_bold", "inputnode.interpolated_filtered_bold"),
("censored_denoised_bold", "inputnode.censored_denoised_bold"),
("fmriprep_confounds_file", "inputnode.fmriprep_confounds_file"),
("filtered_motion", "inputnode.filtered_motion"),
("temporal_mask", "inputnode.temporal_mask"),
("run_index", "inputnode.run_index"),
]),
])
# fmt:on
ds_filtered_motion = pe.Node(
DerivativesDataSink(
base_directory=output_dir,
dismiss_entities=["atlas", "den", "res", "space", "cohort", "desc"],
desc="filtered" if motion_filter_type else None,
suffix="motion",
extension=".tsv",
),
name="ds_filtered_motion",
run_without_submitting=True,
mem_gb=1,
)
# fmt:off
workflow.connect([
(clean_name_source, ds_filtered_motion, [("name_source", "source_file")]),
(concatenate_inputs, ds_filtered_motion, [("filtered_motion", "in_file")]),
])
# fmt:on
ds_temporal_mask = pe.Node(
DerivativesDataSink(
base_directory=output_dir,
dismiss_entities=["atlas", "den", "res", "space", "cohort", "desc"],
suffix="outliers",
extension=".tsv",
),
name="ds_temporal_mask",
run_without_submitting=True,
mem_gb=1,
)
# fmt:off
workflow.connect([
(clean_name_source, ds_temporal_mask, [("name_source", "source_file")]),
(concatenate_inputs, ds_temporal_mask, [("temporal_mask", "in_file")]),
])
# fmt:on
ds_timeseries = pe.MapNode(
DerivativesDataSink(
base_directory=output_dir,
dismiss_entities=["desc"],
suffix="timeseries",
extension=".tsv",
),
name="ds_timeseries",
run_without_submitting=True,
mem_gb=1,
iterfield=["atlas", "in_file"],
)
# fmt:off
workflow.connect([
(clean_name_source, ds_timeseries, [("name_source", "source_file")]),
(filter_out_failed_runs, ds_timeseries, [(("atlas_names", _select_first), "atlas")]),
(concatenate_inputs, ds_timeseries, [("timeseries", "in_file")]),
])
# fmt:on
if cifti:
ds_censored_filtered_bold = pe.Node(
DerivativesDataSink(
base_directory=output_dir,
dismiss_entities=["den"],
desc="denoised",
den="91k",
extension=".dtseries.nii",
),
name="ds_censored_filtered_bold",
run_without_submitting=True,
mem_gb=2,
)
ds_timeseries_cifti_files = pe.MapNode(
DerivativesDataSink(
base_directory=output_dir,
check_hdr=False,
dismiss_entities=["desc", "den"],
den="91k",
suffix="timeseries",
extension=".ptseries.nii",
),
name="ds_timeseries_cifti_files",
run_without_submitting=True,
mem_gb=1,
iterfield=["atlas", "in_file"],
)
# fmt:off
workflow.connect([
(clean_name_source, ds_timeseries_cifti_files, [("name_source", "source_file")]),
(filter_out_failed_runs, ds_timeseries_cifti_files, [
(("atlas_names", _select_first), "atlas"),
]),
(concatenate_inputs, ds_timeseries_cifti_files, [("timeseries_ciftis", "in_file")]),
])
# fmt:on
if smoothing:
ds_smoothed_denoised_bold = pe.Node(
DerivativesDataSink(
base_directory=output_dir,
dismiss_entities=["den"],
desc="denoisedSmoothed",
den="91k",
extension=".dtseries.nii",
),
name="ds_smoothed_denoised_bold",
run_without_submitting=True,
mem_gb=2,
)
if dcan_qc:
ds_interpolated_filtered_bold = pe.Node(
DerivativesDataSink(
base_directory=output_dir,
dismiss_entities=["den"],
desc="interpolated",
den="91k",
extension=".dtseries.nii",
),
name="ds_interpolated_filtered_bold",
run_without_submitting=True,
mem_gb=2,
)
else:
ds_censored_filtered_bold = pe.Node(
DerivativesDataSink(
base_directory=output_dir,
desc="denoised",
extension=".nii.gz",
compression=True,
),
name="ds_censored_filtered_bold",
run_without_submitting=True,
mem_gb=2,
)
if smoothing:
ds_smoothed_denoised_bold = pe.Node(
DerivativesDataSink(
base_directory=output_dir,
desc="denoisedSmoothed",
extension=".nii.gz",
compression=True,
),
name="ds_smoothed_denoised_bold",
run_without_submitting=True,
mem_gb=2,
)
if dcan_qc:
ds_interpolated_filtered_bold = pe.Node(
DerivativesDataSink(
base_directory=output_dir,
desc="interpolated",
extension=".nii.gz",
compression=True,
),
name="ds_interpolated_filtered_bold",
run_without_submitting=True,
mem_gb=2,
)
# fmt:off
workflow.connect([
(clean_name_source, ds_censored_filtered_bold, [("name_source", "source_file")]),
(concatenate_inputs, ds_censored_filtered_bold, [("censored_denoised_bold", "in_file")]),
])
# fmt:on
if smoothing:
# fmt:off
workflow.connect([
(clean_name_source, ds_smoothed_denoised_bold, [("name_source", "source_file")]),
(concatenate_inputs, ds_smoothed_denoised_bold, [
("smoothed_denoised_bold", "in_file"),
]),
])
# fmt:on
if dcan_qc:
# fmt:off
workflow.connect([
(clean_name_source, ds_interpolated_filtered_bold, [("name_source", "source_file")]),
(concatenate_inputs, ds_interpolated_filtered_bold, [
("interpolated_filtered_bold", "in_file"),
]),
])
# fmt:on
return workflow