/
registration.py
743 lines (640 loc) · 26.8 KB
/
registration.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
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
# 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/
#
"""
Registration workflows
++++++++++++++++++++++
.. autofunction:: init_bold_reg_wf
.. autofunction:: init_bbreg_wf
.. autofunction:: init_fsl_bbr_wf
"""
import os
import os.path as op
import typing as ty
from nipype.interfaces import c3, fsl
from nipype.interfaces import utility as niu
from nipype.pipeline import engine as pe
from ... import config, data
from ...interfaces import DerivativesDataSink
DEFAULT_MEMORY_MIN_GB = config.DEFAULT_MEMORY_MIN_GB
LOGGER = config.loggers.workflow
AffineDOF = ty.Literal[6, 9, 12]
RegistrationInit = ty.Literal['register', 'header']
def init_bold_reg_wf(
freesurfer: bool,
use_bbr: bool,
bold2t1w_dof: AffineDOF,
bold2t1w_init: RegistrationInit,
mem_gb: float,
omp_nthreads: int,
name: str = 'bold_reg_wf',
sloppy: bool = False,
):
"""
Build a workflow to run same-subject, BOLD-to-T1w image-registration.
Calculates the registration between a reference BOLD image and T1w-space
using a boundary-based registration (BBR) cost function.
If FreeSurfer-based preprocessing is enabled, the ``bbregister`` utility
is used to align the BOLD images to the reconstructed subject, and the
resulting transform is adjusted to target the T1 space.
If FreeSurfer-based preprocessing is disabled, FSL FLIRT is used with the
BBR cost function to directly target the T1 space.
Workflow Graph
.. workflow::
:graph2use: orig
:simple_form: yes
from fmriprep.workflows.bold.registration import init_bold_reg_wf
wf = init_bold_reg_wf(freesurfer=True,
mem_gb=3,
omp_nthreads=1,
use_bbr=True,
bold2t1w_dof=9,
bold2t1w_init='register')
Parameters
----------
freesurfer : :obj:`bool`
Enable FreeSurfer functional registration (bbregister)
use_bbr : :obj:`bool` or None
Enable/disable boundary-based registration refinement.
If ``None``, test BBR result for distortion before accepting.
bold2t1w_dof : 6, 9 or 12
Degrees-of-freedom for BOLD-T1w registration
bold2t1w_init : str, 'header' or 'register'
If ``'header'``, use header information for initialization of BOLD and T1 images.
If ``'register'``, align volumes by their centers.
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_reg_wf``)
Inputs
------
ref_bold_brain
Reference image to which BOLD series is aligned
If ``fieldwarp == True``, ``ref_bold_brain`` should be unwarped
t1w_brain
Skull-stripped ``t1w_preproc``
t1w_dseg
Segmentation of preprocessed structural image, including
gray-matter (GM), white-matter (WM) and cerebrospinal fluid (CSF)
subjects_dir
FreeSurfer SUBJECTS_DIR
subject_id
FreeSurfer subject ID
fsnative2t1w_xfm
LTA-style affine matrix translating from FreeSurfer-conformed subject space to T1w
Outputs
-------
itk_bold_to_t1
Affine transform from ``ref_bold_brain`` to T1 space (ITK format)
itk_t1_to_bold
Affine transform from T1 space to BOLD space (ITK format)
fallback
Boolean indicating whether BBR was rejected (mri_coreg registration returned)
See Also
--------
* :py:func:`~fmriprep.workflows.bold.registration.init_bbreg_wf`
* :py:func:`~fmriprep.workflows.bold.registration.init_fsl_bbr_wf`
"""
from niworkflows.engine.workflows import LiterateWorkflow as Workflow
workflow = Workflow(name=name)
inputnode = pe.Node(
niu.IdentityInterface(
fields=[
'ref_bold_brain',
't1w_preproc',
't1w_mask',
't1w_dseg',
'subjects_dir',
'subject_id',
'fsnative2t1w_xfm',
]
),
name='inputnode',
)
outputnode = pe.Node(
niu.IdentityInterface(fields=['itk_bold_to_t1', 'itk_t1_to_bold', 'fallback']),
name='outputnode',
)
if freesurfer:
bbr_wf = init_bbreg_wf(
use_bbr=use_bbr,
bold2t1w_dof=bold2t1w_dof,
bold2t1w_init=bold2t1w_init,
omp_nthreads=omp_nthreads,
)
else:
bbr_wf = init_fsl_bbr_wf(
use_bbr=use_bbr,
bold2t1w_dof=bold2t1w_dof,
bold2t1w_init=bold2t1w_init,
sloppy=sloppy,
omp_nthreads=omp_nthreads,
)
workflow.connect([
(inputnode, bbr_wf, [
('ref_bold_brain', 'inputnode.in_file'),
('fsnative2t1w_xfm', 'inputnode.fsnative2t1w_xfm'),
('subjects_dir', 'inputnode.subjects_dir'),
('subject_id', 'inputnode.subject_id'),
('t1w_preproc', 'inputnode.t1w_preproc'),
('t1w_mask', 'inputnode.t1w_mask'),
('t1w_dseg', 'inputnode.t1w_dseg'),
]),
(bbr_wf, outputnode, [
('outputnode.itk_bold_to_t1', 'itk_bold_to_t1'),
('outputnode.itk_t1_to_bold', 'itk_t1_to_bold'),
('outputnode.fallback', 'fallback'),
]),
]) # fmt:skip
return workflow
def init_bbreg_wf(
use_bbr: bool,
bold2t1w_dof: AffineDOF,
bold2t1w_init: RegistrationInit,
omp_nthreads: int,
name: str = 'bbreg_wf',
):
"""
Build a workflow to run FreeSurfer's ``bbregister``.
This workflow uses FreeSurfer's ``bbregister`` to register a BOLD image to
a T1-weighted structural image.
It is a counterpart to :py:func:`~fmriprep.workflows.bold.registration.init_fsl_bbr_wf`,
which performs the same task using FSL's FLIRT with a BBR cost function.
The ``use_bbr`` option permits a high degree of control over registration.
If ``False``, standard, affine coregistration will be performed using
FreeSurfer's ``mri_coreg`` tool.
If ``True``, ``bbregister`` will be seeded with the initial transform found
by ``mri_coreg`` (equivalent to running ``bbregister --init-coreg``).
If ``None``, after ``bbregister`` is run, the resulting affine transform
will be compared to the initial transform found by ``mri_coreg``.
Excessive deviation will result in rejecting the BBR refinement and
accepting the original, affine registration.
Workflow Graph
.. workflow ::
:graph2use: orig
:simple_form: yes
from fmriprep.workflows.bold.registration import init_bbreg_wf
wf = init_bbreg_wf(use_bbr=True, bold2t1w_dof=9,
bold2t1w_init='register', omp_nthreads=1)
Parameters
----------
use_bbr : :obj:`bool` or None
Enable/disable boundary-based registration refinement.
If ``None``, test BBR result for distortion before accepting.
bold2t1w_dof : 6, 9 or 12
Degrees-of-freedom for BOLD-T1w registration
bold2t1w_init : str, 'header' or 'register'
If ``'header'``, use header information for initialization of BOLD and T1 images.
If ``'register'``, align volumes by their centers.
name : :obj:`str`, optional
Workflow name (default: bbreg_wf)
Inputs
------
in_file
Reference BOLD image to be registered
fsnative2t1w_xfm
FSL-style affine matrix translating from FreeSurfer T1.mgz to T1w
subjects_dir
FreeSurfer SUBJECTS_DIR
subject_id
FreeSurfer subject ID (must have folder in SUBJECTS_DIR)
t1w_preproc
Unused (see :py:func:`~fmriprep.workflows.bold.registration.init_fsl_bbr_wf`)
t1w_mask
Unused (see :py:func:`~fmriprep.workflows.bold.registration.init_fsl_bbr_wf`)
t1w_dseg
Unused (see :py:func:`~fmriprep.workflows.bold.registration.init_fsl_bbr_wf`)
Outputs
-------
itk_bold_to_t1
Affine transform from ``ref_bold_brain`` to T1 space (ITK format)
itk_t1_to_bold
Affine transform from T1 space to BOLD space (ITK format)
fallback
Boolean indicating whether BBR was rejected (mri_coreg registration returned)
"""
from nipype.interfaces.freesurfer import BBRegister, MRICoreg
from niworkflows.engine.workflows import LiterateWorkflow as Workflow
from niworkflows.interfaces.freesurfer import PatchedLTAConvert as LTAConvert
from niworkflows.interfaces.nitransforms import ConcatenateXFMs
workflow = Workflow(name=name)
workflow.__desc__ = """\
The BOLD reference was then co-registered to the T1w reference using
`bbregister` (FreeSurfer) which implements boundary-based registration [@bbr].
Co-registration was configured with {dof} degrees of freedom{reason}.
""".format(
dof={6: 'six', 9: 'nine', 12: 'twelve'}[bold2t1w_dof],
reason=''
if bold2t1w_dof == 6
else 'to account for distortions remaining in the BOLD reference',
)
inputnode = pe.Node(
niu.IdentityInterface(
[
'in_file',
'fsnative2t1w_xfm', # BBRegister
'subjects_dir',
'subject_id',
't1w_preproc', # FLIRT BBR
't1w_mask',
't1w_dseg',
]
),
name='inputnode',
)
outputnode = pe.Node(
niu.IdentityInterface(['itk_bold_to_t1', 'itk_t1_to_bold', 'fallback']),
name='outputnode',
)
if bold2t1w_init not in ("register", "header"):
raise ValueError(f"Unknown BOLD-T1w initialization option: {bold2t1w_init}")
# For now make BBR unconditional - in the future, we can fall back to identity,
# but adding the flexibility without testing seems a bit dangerous
if bold2t1w_init == "header":
if use_bbr is False:
raise ValueError("Cannot disable BBR and use header registration")
if use_bbr is None:
LOGGER.warning("Initializing BBR with header; affine fallback disabled")
use_bbr = True
# Define both nodes, but only connect conditionally
mri_coreg = pe.Node(
MRICoreg(dof=bold2t1w_dof, sep=[4], ftol=0.0001, linmintol=0.01),
name='mri_coreg',
n_procs=omp_nthreads,
mem_gb=5,
)
bbregister = pe.Node(
BBRegister(
dof=bold2t1w_dof,
contrast_type='t2',
out_lta_file=True,
),
name='bbregister',
mem_gb=12,
)
if bold2t1w_init == "header":
bbregister.inputs.init = "header"
transforms = pe.Node(niu.Merge(2), run_without_submitting=True, name='transforms')
# In cases where Merge(2) only has `in1` or `in2` defined
# output list will just contain a single element
select_transform = pe.Node(
niu.Select(index=0), run_without_submitting=True, name='select_transform'
)
merge_ltas = pe.Node(niu.Merge(2), name='merge_ltas', run_without_submitting=True)
concat_xfm = pe.Node(ConcatenateXFMs(inverse=True), name='concat_xfm')
workflow.connect([
(inputnode, merge_ltas, [('fsnative2t1w_xfm', 'in2')]),
# Wire up the co-registration alternatives
(transforms, select_transform, [('out', 'inlist')]),
(select_transform, merge_ltas, [('out', 'in1')]),
(merge_ltas, concat_xfm, [('out', 'in_xfms')]),
(concat_xfm, outputnode, [('out_xfm', 'itk_bold_to_t1')]),
(concat_xfm, outputnode, [('out_inv', 'itk_t1_to_bold')]),
]) # fmt:skip
# Do not initialize with header, use mri_coreg
if bold2t1w_init == "register":
workflow.connect([
(inputnode, mri_coreg, [('subjects_dir', 'subjects_dir'),
('subject_id', 'subject_id'),
('in_file', 'source_file')]),
(mri_coreg, transforms, [('out_lta_file', 'in2')]),
]) # fmt:skip
# Short-circuit workflow building, use initial registration
if use_bbr is False:
outputnode.inputs.fallback = True
return workflow
# Otherwise bbregister will also be used
workflow.connect(mri_coreg, 'out_lta_file', bbregister, 'init_reg_file')
# Use bbregister
workflow.connect([
(inputnode, bbregister, [('subjects_dir', 'subjects_dir'),
('subject_id', 'subject_id'),
('in_file', 'source_file')]),
(bbregister, transforms, [('out_lta_file', 'in1')]),
]) # fmt:skip
# Short-circuit workflow building, use boundary-based registration
if use_bbr is True:
outputnode.inputs.fallback = False
return workflow
# Only reach this point if bold2t1w_init is "register" and use_bbr is None
compare_transforms = pe.Node(niu.Function(function=compare_xforms), name='compare_transforms')
workflow.connect([
(transforms, compare_transforms, [('out', 'lta_list')]),
(compare_transforms, outputnode, [('out', 'fallback')]),
(compare_transforms, select_transform, [('out', 'index')]),
]) # fmt:skip
return workflow
def init_fsl_bbr_wf(
use_bbr: bool,
bold2t1w_dof: AffineDOF,
bold2t1w_init: RegistrationInit,
omp_nthreads: int,
sloppy: bool = False,
name: str = 'fsl_bbr_wf',
):
"""
Build a workflow to run FSL's ``flirt``.
This workflow uses FSL FLIRT to register a BOLD image to a T1-weighted
structural image, using a boundary-based registration (BBR) cost function.
It is a counterpart to :py:func:`~fmriprep.workflows.bold.registration.init_bbreg_wf`,
which performs the same task using FreeSurfer's ``bbregister``.
The ``use_bbr`` option permits a high degree of control over registration.
If ``False``, standard, rigid coregistration will be performed by FLIRT.
If ``True``, FLIRT-BBR will be seeded with the initial transform found by
the rigid coregistration.
If ``None``, after FLIRT-BBR is run, the resulting affine transform
will be compared to the initial transform found by FLIRT.
Excessive deviation will result in rejecting the BBR refinement and
accepting the original, affine registration.
Workflow Graph
.. workflow ::
:graph2use: orig
:simple_form: yes
from fmriprep.workflows.bold.registration import init_fsl_bbr_wf
wf = init_fsl_bbr_wf(use_bbr=True, bold2t1w_dof=9, bold2t1w_init='register', omp_nthreads=1)
Parameters
----------
use_bbr : :obj:`bool` or None
Enable/disable boundary-based registration refinement.
If ``None``, test BBR result for distortion before accepting.
bold2t1w_dof : 6, 9 or 12
Degrees-of-freedom for BOLD-T1w registration
bold2t1w_init : str, 'header' or 'register'
If ``'header'``, use header information for initialization of BOLD and T1 images.
If ``'register'``, align volumes by their centers.
name : :obj:`str`, optional
Workflow name (default: fsl_bbr_wf)
Inputs
------
in_file
Reference BOLD image to be registered
t1w_preproc
T1-weighted structural image
t1w_mask
Brain mask of structural image
t1w_dseg
FAST segmentation of masked ``t1w_preproc``
fsnative2t1w_xfm
Unused (see :py:func:`~fmriprep.workflows.bold.registration.init_bbreg_wf`)
subjects_dir
Unused (see :py:func:`~fmriprep.workflows.bold.registration.init_bbreg_wf`)
subject_id
Unused (see :py:func:`~fmriprep.workflows.bold.registration.init_bbreg_wf`)
Outputs
-------
itk_bold_to_t1
Affine transform from ``ref_bold_brain`` to T1w space (ITK format)
itk_t1_to_bold
Affine transform from T1 space to BOLD space (ITK format)
fallback
Boolean indicating whether BBR was rejected (rigid FLIRT registration returned)
"""
from nipype.interfaces.freesurfer import MRICoreg
from niworkflows.engine.workflows import LiterateWorkflow as Workflow
from niworkflows.interfaces.freesurfer import PatchedLTAConvert as LTAConvert
from niworkflows.interfaces.nibabel import ApplyMask
from niworkflows.utils.images import dseg_label as _dseg_label
workflow = Workflow(name=name)
workflow.__desc__ = """\
The BOLD reference was then co-registered to the T1w reference using
`mri_coreg` (FreeSurfer) followed by `flirt` [FSL {fsl_ver}, @flirt]
with the boundary-based registration [@bbr] cost-function.
Co-registration was configured with {dof} degrees of freedom{reason}.
""".format(
fsl_ver=fsl.FLIRT().version or '<ver>',
dof={6: 'six', 9: 'nine', 12: 'twelve'}[bold2t1w_dof],
reason=''
if bold2t1w_dof == 6
else 'to account for distortions remaining in the BOLD reference',
)
inputnode = pe.Node(
niu.IdentityInterface(
[
'in_file',
'fsnative2t1w_xfm', # BBRegister
'subjects_dir',
'subject_id',
't1w_preproc', # FLIRT BBR
't1w_mask',
't1w_dseg',
]
),
name='inputnode',
)
outputnode = pe.Node(
niu.IdentityInterface(['itk_bold_to_t1', 'itk_t1_to_bold', 'fallback']),
name='outputnode',
)
wm_mask = pe.Node(niu.Function(function=_dseg_label), name='wm_mask')
wm_mask.inputs.label = 2 # BIDS default is WM=2
if bold2t1w_init not in ("register", "header"):
raise ValueError(f"Unknown BOLD-T1w initialization option: {bold2t1w_init}")
if bold2t1w_init == "header":
raise NotImplementedError("Header-based registration initialization not supported for FSL")
# Mask T1w_preproc with T1w_mask to make T1w_brain
mask_t1w_brain = pe.Node(ApplyMask(), name='mask_t1w_brain')
mri_coreg = pe.Node(
MRICoreg(dof=bold2t1w_dof, sep=[4], ftol=0.0001, linmintol=0.01),
name='mri_coreg',
n_procs=omp_nthreads,
mem_gb=5,
)
lta_to_fsl = pe.Node(LTAConvert(out_fsl=True), name='lta_to_fsl', mem_gb=DEFAULT_MEMORY_MIN_GB)
invt_bbr = pe.Node(
fsl.ConvertXFM(invert_xfm=True), name='invt_bbr', mem_gb=DEFAULT_MEMORY_MIN_GB
)
# BOLD to T1 transform matrix is from fsl, using c3 tools to convert to
# something ANTs will like.
fsl2itk_fwd = pe.Node(
c3.C3dAffineTool(fsl2ras=True, itk_transform=True),
name='fsl2itk_fwd',
mem_gb=DEFAULT_MEMORY_MIN_GB,
)
fsl2itk_inv = pe.Node(
c3.C3dAffineTool(fsl2ras=True, itk_transform=True),
name='fsl2itk_inv',
mem_gb=DEFAULT_MEMORY_MIN_GB,
)
# fmt:off
workflow.connect([
(inputnode, mask_t1w_brain, [('t1w_preproc', 'in_file'),
('t1w_mask', 'in_mask')]),
(inputnode, mri_coreg, [('in_file', 'source_file')]),
(inputnode, fsl2itk_fwd, [('in_file', 'source_file')]),
(inputnode, fsl2itk_inv, [('in_file', 'reference_file')]),
(mask_t1w_brain, mri_coreg, [('out_file', 'reference_file')]),
(mask_t1w_brain, fsl2itk_fwd, [('out_file', 'reference_file')]),
(mask_t1w_brain, fsl2itk_inv, [('out_file', 'source_file')]),
(mri_coreg, lta_to_fsl, [('out_lta_file', 'in_lta')]),
(invt_bbr, fsl2itk_inv, [('out_file', 'transform_file')]),
(fsl2itk_fwd, outputnode, [('itk_transform', 'itk_bold_to_t1')]),
(fsl2itk_inv, outputnode, [('itk_transform', 'itk_t1_to_bold')]),
])
# fmt:on
# Short-circuit workflow building, use rigid registration
if use_bbr is False:
# fmt:off
workflow.connect([
(lta_to_fsl, invt_bbr, [('out_fsl', 'in_file')]),
(lta_to_fsl, fsl2itk_fwd, [('out_fsl', 'transform_file')]),
])
# fmt:on
outputnode.inputs.fallback = True
return workflow
flt_bbr = pe.Node(
fsl.FLIRT(cost_func='bbr', dof=bold2t1w_dof, args="-basescale 1"),
name='flt_bbr',
)
FSLDIR = os.getenv('FSLDIR')
if FSLDIR and os.path.exists(schedule := op.join(FSLDIR, 'etc/flirtsch/bbr.sch')):
flt_bbr.inputs.schedule = schedule
else:
# Should mostly be hit while building docs
LOGGER.warning("FSLDIR unset - using packaged BBR schedule")
flt_bbr.inputs.schedule = data.load('flirtsch/bbr.sch')
# fmt:off
workflow.connect([
(inputnode, wm_mask, [('t1w_dseg', 'in_seg')]),
(inputnode, flt_bbr, [('in_file', 'in_file')]),
(lta_to_fsl, flt_bbr, [('out_fsl', 'in_matrix_file')]),
])
# fmt:on
if sloppy is True:
downsample = pe.Node(
niu.Function(
function=_conditional_downsampling, output_names=["out_file", "out_mask"]
),
name='downsample',
)
# fmt:off
workflow.connect([
(mask_t1w_brain, downsample, [("out_file", "in_file")]),
(wm_mask, downsample, [("out", "in_mask")]),
(downsample, flt_bbr, [('out_file', 'reference'),
('out_mask', 'wm_seg')]),
])
# fmt:on
else:
# fmt:off
workflow.connect([
(mask_t1w_brain, flt_bbr, [('out_file', 'reference')]),
(wm_mask, flt_bbr, [('out', 'wm_seg')]),
])
# fmt:on
# Short-circuit workflow building, use boundary-based registration
if use_bbr is True:
# fmt:off
workflow.connect([
(flt_bbr, invt_bbr, [('out_matrix_file', 'in_file')]),
(flt_bbr, fsl2itk_fwd, [('out_matrix_file', 'transform_file')]),
])
# fmt:on
outputnode.inputs.fallback = False
return workflow
transforms = pe.Node(niu.Merge(2), run_without_submitting=True, name='transforms')
compare_transforms = pe.Node(niu.Function(function=compare_xforms), name='compare_transforms')
select_transform = pe.Node(niu.Select(), run_without_submitting=True, name='select_transform')
fsl_to_lta = pe.MapNode(LTAConvert(out_lta=True), iterfield=['in_fsl'], name='fsl_to_lta')
# fmt:off
workflow.connect([
(flt_bbr, transforms, [('out_matrix_file', 'in1')]),
(lta_to_fsl, transforms, [('out_fsl', 'in2')]),
# Convert FSL transforms to LTA (RAS2RAS) transforms and compare
(inputnode, fsl_to_lta, [('in_file', 'source_file')]),
(mask_t1w_brain, fsl_to_lta, [('out_file', 'target_file')]),
(transforms, fsl_to_lta, [('out', 'in_fsl')]),
(fsl_to_lta, compare_transforms, [('out_lta', 'lta_list')]),
(compare_transforms, outputnode, [('out', 'fallback')]),
# Select output transform
(transforms, select_transform, [('out', 'inlist')]),
(compare_transforms, select_transform, [('out', 'index')]),
(select_transform, invt_bbr, [('out', 'in_file')]),
(select_transform, fsl2itk_fwd, [('out', 'transform_file')]),
])
# fmt:on
return workflow
def compare_xforms(lta_list, norm_threshold=15):
"""
Computes a normalized displacement between two affine transforms as the
maximum overall displacement of the midpoints of the faces of a cube, when
each transform is applied to the cube.
This combines displacement resulting from scaling, translation and rotation.
Although the norm is in mm, in a scaling context, it is not necessarily
equivalent to that distance in translation.
We choose a default threshold of 15mm as a rough heuristic.
Normalized displacement above 20mm showed clear signs of distortion, while
"good" BBR refinements were frequently below 10mm displaced from the rigid
transform.
The 10-20mm range was more ambiguous, and 15mm chosen as a compromise.
This is open to revisiting in either direction.
See discussion in
`GitHub issue #681`_ <https://github.com/nipreps/fmriprep/issues/681>`_
and the `underlying implementation
<https://github.com/nipy/nipype/blob/56b7c81eedeeae884ba47c80096a5f66bd9f8116/nipype/algorithms/rapidart.py#L108-L159>`_.
Parameters
----------
lta_list : :obj:`list` or :obj:`tuple` of :obj:`str`
the two given affines in LTA format
norm_threshold : :obj:`float`
the upper bound limit to the normalized displacement caused by the
second transform relative to the first (default: `15`)
"""
import nitransforms as nt
from nipype.algorithms.rapidart import _calc_norm_affine
bbr_affine = nt.linear.load(lta_list[0]).matrix
fallback_affine = nt.linear.load(lta_list[1]).matrix
norm, _ = _calc_norm_affine([fallback_affine, bbr_affine], use_differences=True)
return norm[1] > norm_threshold
def _conditional_downsampling(in_file, in_mask, zoom_th=4.0):
"""Downsamples the input dataset for sloppy mode."""
from pathlib import Path
import nibabel as nb
import nitransforms as nt
import numpy as np
from scipy.ndimage.filters import gaussian_filter
img = nb.load(in_file)
zooms = np.array(img.header.get_zooms()[:3])
if not np.any(zooms < zoom_th):
return in_file, in_mask
out_file = Path('desc-resampled_input.nii.gz').absolute()
out_mask = Path('desc-resampled_mask.nii.gz').absolute()
shape = np.array(img.shape[:3])
scaling = zoom_th / zooms
newrot = np.diag(scaling).dot(img.affine[:3, :3])
newshape = np.ceil(shape / scaling).astype(int)
old_center = img.affine.dot(np.hstack((0.5 * (shape - 1), 1.0)))[:3]
offset = old_center - newrot.dot((newshape - 1) * 0.5)
newaffine = nb.affines.from_matvec(newrot, offset)
newref = nb.Nifti1Image(np.zeros(newshape, dtype=np.uint8), newaffine)
nt.Affine(reference=newref).apply(img).to_filename(out_file)
mask = nb.load(in_mask)
mask.set_data_dtype(float)
mdata = gaussian_filter(mask.get_fdata(dtype=float), scaling)
floatmask = nb.Nifti1Image(mdata, mask.affine, mask.header)
newmask = nt.Affine(reference=newref).apply(floatmask)
hdr = newmask.header.copy()
hdr.set_data_dtype(np.uint8)
newmaskdata = (newmask.get_fdata(dtype=float) > 0.5).astype(np.uint8)
nb.Nifti1Image(newmaskdata, newmask.affine, hdr).to_filename(out_mask)
return str(out_file), str(out_mask)