/
anatomical.py
1368 lines (1134 loc) · 55.7 KB
/
anatomical.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
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*-
# vi: set ft=python sts=4 ts=4 sw=4 et:
"""
Anatomical reference preprocessing workflows
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
.. autofunction:: init_anat_preproc_wf
.. autofunction:: init_skullstrip_ants_wf
Surface preprocessing
+++++++++++++++++++++
``fmriprep`` uses FreeSurfer_ to reconstruct surfaces from T1w/T2w
structural images.
.. autofunction:: init_surface_recon_wf
.. autofunction:: init_autorecon_resume_wf
.. autofunction:: init_gifti_surface_wf
"""
import os.path as op
from pkg_resources import resource_filename as pkgr
from nipype.pipeline import engine as pe
from nipype.interfaces import (
io as nio,
utility as niu,
c3,
freesurfer as fs,
fsl,
image,
)
from nipype.interfaces.ants import BrainExtraction, N4BiasFieldCorrection
from niworkflows.interfaces.registration import RobustMNINormalizationRPT
import niworkflows.data as nid
from niworkflows.interfaces.masks import ROIsPlot
from niworkflows.interfaces.segmentation import ReconAllRPT
from niworkflows.interfaces.fixes import FixHeaderApplyTransforms as ApplyTransforms
from ..engine import Workflow
from ..interfaces import (
DerivativesDataSink, MakeMidthickness, FSInjectBrainExtracted,
FSDetectInputs, NormalizeSurf, GiftiNameSource, TemplateDimensions, Conform,
ConcatAffines, RefineBrainMask,
)
from ..utils.misc import fix_multi_T1w_source_name, add_suffix
TEMPLATE_MAP = {
'MNI152NLin2009cAsym': 'mni_icbm152_nlin_asym_09c',
}
# pylint: disable=R0914
def init_anat_preproc_wf(skull_strip_template, output_spaces, template, debug,
freesurfer, longitudinal, omp_nthreads, hires, reportlets_dir,
output_dir, num_t1w,
skull_strip_fixed_seed=False, name='anat_preproc_wf'):
r"""
This workflow controls the anatomical preprocessing stages of FMRIPREP.
This includes:
- Creation of a structural template
- Skull-stripping and bias correction
- Tissue segmentation
- Normalization
- Surface reconstruction with FreeSurfer
.. workflow::
:graph2use: orig
:simple_form: yes
from fmriprep.workflows.anatomical import init_anat_preproc_wf
wf = init_anat_preproc_wf(omp_nthreads=1,
reportlets_dir='.',
output_dir='.',
template='MNI152NLin2009cAsym',
output_spaces=['T1w', 'fsnative',
'template', 'fsaverage5'],
skull_strip_template='OASIS',
freesurfer=True,
longitudinal=False,
debug=False,
hires=True,
num_t1w=1)
**Parameters**
skull_strip_template : str
Name of ANTs skull-stripping template ('OASIS' or 'NKI')
output_spaces : list
List of output spaces functional images are to be resampled to.
Some pipeline components will only be instantiated for some output spaces.
Valid spaces:
- T1w
- template
- fsnative
- fsaverage (or other pre-existing FreeSurfer templates)
template : str
Name of template targeted by ``template`` output space
debug : bool
Enable debugging outputs
freesurfer : bool
Enable FreeSurfer surface reconstruction (may increase runtime)
longitudinal : bool
Create unbiased structural template, regardless of number of inputs
(may increase runtime)
omp_nthreads : int
Maximum number of threads an individual process may use
hires : bool
Enable sub-millimeter preprocessing in FreeSurfer
reportlets_dir : str
Directory in which to save reportlets
output_dir : str
Directory in which to save derivatives
name : str, optional
Workflow name (default: anat_preproc_wf)
skull_strip_fixed_seed : bool
Do not use a random seed for skull-stripping - will ensure
run-to-run replicability when used with --omp-nthreads 1 (default: ``False``)
**Inputs**
t1w
List of T1-weighted structural images
t2w
List of T2-weighted structural images
flair
List of FLAIR images
subjects_dir
FreeSurfer SUBJECTS_DIR
**Outputs**
t1_preproc
Bias-corrected structural template, defining T1w space
t1_brain
Skull-stripped ``t1_preproc``
t1_mask
Mask of the skull-stripped template image
t1_seg
Segmentation of preprocessed structural image, including
gray-matter (GM), white-matter (WM) and cerebrospinal fluid (CSF)
t1_tpms
List of tissue probability maps in T1w space
t1_2_mni
T1w template, normalized to MNI space
t1_2_mni_forward_transform
ANTs-compatible affine-and-warp transform file
t1_2_mni_reverse_transform
ANTs-compatible affine-and-warp transform file (inverse)
mni_mask
Mask of skull-stripped template, in MNI space
mni_seg
Segmentation, resampled into MNI space
mni_tpms
List of tissue probability maps in MNI space
subjects_dir
FreeSurfer SUBJECTS_DIR
subject_id
FreeSurfer subject ID
t1_2_fsnative_forward_transform
LTA-style affine matrix translating from T1w to FreeSurfer-conformed subject space
t1_2_fsnative_reverse_transform
LTA-style affine matrix translating from FreeSurfer-conformed subject space to T1w
surfaces
GIFTI surfaces (gray/white boundary, midthickness, pial, inflated)
**Subworkflows**
* :py:func:`~fmriprep.workflows.anatomical.init_skullstrip_ants_wf`
* :py:func:`~fmriprep.workflows.anatomical.init_surface_recon_wf`
"""
workflow = Workflow(name=name)
workflow.__postdesc__ = """\
Spatial normalization to the ICBM 152 Nonlinear Asymmetrical
template version 2009c [@mni, RRID:SCR_008796] was performed
through nonlinear registration with `antsRegistration`
[ANTs {ants_ver}, RRID:SCR_004757, @ants], using
brain-extracted versions of both T1w volume and template.
Brain tissue segmentation of cerebrospinal fluid (CSF),
white-matter (WM) and gray-matter (GM) was performed on
the brain-extracted T1w using `fast` [FSL {fsl_ver}, RRID:SCR_002823,
@fsl_fast].
""".format(
ants_ver=BrainExtraction().version or '<ver>',
fsl_ver=fsl.FAST().version or '<ver>',
)
desc = """Anatomical data preprocessing
: """
desc += """\
A total of {num_t1w} T1-weighted (T1w) images were found within the input
BIDS dataset.
All of them were corrected for intensity non-uniformity (INU)
using `N4BiasFieldCorrection` [@n4, ANTs {ants_ver}].
""" if num_t1w > 1 else """\
The T1-weighted (T1w) image was corrected for intensity non-uniformity (INU)
using `N4BiasFieldCorrection` [@n4, ANTs {ants_ver}],
and used as T1w-reference throughout the workflow.
"""
workflow.__desc__ = desc.format(
num_t1w=num_t1w,
ants_ver=BrainExtraction().version or '<ver>'
)
inputnode = pe.Node(
niu.IdentityInterface(fields=['t1w', 't2w', 'roi', 'flair', 'subjects_dir', 'subject_id']),
name='inputnode')
outputnode = pe.Node(niu.IdentityInterface(
fields=['t1_preproc', 't1_brain', 't1_mask', 't1_seg', 't1_tpms',
't1_2_mni', 't1_2_mni_forward_transform', 't1_2_mni_reverse_transform',
'mni_mask', 'mni_seg', 'mni_tpms',
'template_transforms',
'subjects_dir', 'subject_id', 't1_2_fsnative_forward_transform',
't1_2_fsnative_reverse_transform', 'surfaces', 't1_aseg', 't1_aparc']),
name='outputnode')
buffernode = pe.Node(niu.IdentityInterface(
fields=['t1_brain', 't1_mask']), name='buffernode')
anat_template_wf = init_anat_template_wf(longitudinal=longitudinal, omp_nthreads=omp_nthreads,
num_t1w=num_t1w)
# 3. Skull-stripping
# Bias field correction is handled in skull strip workflows.
skullstrip_ants_wf = init_skullstrip_ants_wf(name='skullstrip_ants_wf',
skull_strip_template=skull_strip_template,
debug=debug,
omp_nthreads=omp_nthreads)
workflow.connect([
(inputnode, anat_template_wf, [('t1w', 'inputnode.t1w')]),
(anat_template_wf, skullstrip_ants_wf, [('outputnode.t1_template', 'inputnode.in_file')]),
(skullstrip_ants_wf, outputnode, [('outputnode.bias_corrected', 't1_preproc')]),
(anat_template_wf, outputnode, [
('outputnode.template_transforms', 't1_template_transforms')]),
(buffernode, outputnode, [('t1_brain', 't1_brain'),
('t1_mask', 't1_mask')]),
])
# 4. Surface reconstruction
if freesurfer:
surface_recon_wf = init_surface_recon_wf(name='surface_recon_wf',
omp_nthreads=omp_nthreads, hires=hires)
applyrefined = pe.Node(fsl.ApplyMask(), name='applyrefined')
workflow.connect([
(inputnode, surface_recon_wf, [
('t2w', 'inputnode.t2w'),
('flair', 'inputnode.flair'),
('subjects_dir', 'inputnode.subjects_dir'),
('subject_id', 'inputnode.subject_id')]),
(anat_template_wf, surface_recon_wf, [('outputnode.t1_template', 'inputnode.t1w')]),
(skullstrip_ants_wf, surface_recon_wf, [
('outputnode.out_file', 'inputnode.skullstripped_t1'),
('outputnode.out_segs', 'inputnode.ants_segs'),
('outputnode.bias_corrected', 'inputnode.corrected_t1')]),
(skullstrip_ants_wf, applyrefined, [
('outputnode.bias_corrected', 'in_file')]),
(surface_recon_wf, applyrefined, [
('outputnode.out_brainmask', 'mask_file')]),
(surface_recon_wf, outputnode, [
('outputnode.subjects_dir', 'subjects_dir'),
('outputnode.subject_id', 'subject_id'),
('outputnode.t1_2_fsnative_forward_transform', 't1_2_fsnative_forward_transform'),
('outputnode.t1_2_fsnative_reverse_transform', 't1_2_fsnative_reverse_transform'),
('outputnode.surfaces', 'surfaces'),
('outputnode.out_aseg', 't1_aseg'),
('outputnode.out_aparc', 't1_aparc')]),
(applyrefined, buffernode, [('out_file', 't1_brain')]),
(surface_recon_wf, buffernode, [
('outputnode.out_brainmask', 't1_mask')]),
])
else:
workflow.connect([
(skullstrip_ants_wf, buffernode, [
('outputnode.out_file', 't1_brain'),
('outputnode.out_mask', 't1_mask')]),
])
# 5. Segmentation
t1_seg = pe.Node(fsl.FAST(segments=True, no_bias=True, probability_maps=True),
name='t1_seg', mem_gb=3)
workflow.connect([
(buffernode, t1_seg, [('t1_brain', 'in_files')]),
(t1_seg, outputnode, [('tissue_class_map', 't1_seg'),
('probability_maps', 't1_tpms')]),
])
# 6. Spatial normalization (T1w to MNI registration)
t1_2_mni = pe.Node(
RobustMNINormalizationRPT(
float=True,
generate_report=True,
flavor='testing' if debug else 'precise',
),
name='t1_2_mni',
n_procs=omp_nthreads,
mem_gb=2
)
# Resample the brain mask and the tissue probability maps into mni space
mni_mask = pe.Node(
ApplyTransforms(dimension=3, default_value=0, float=True,
interpolation='MultiLabel'),
name='mni_mask'
)
mni_seg = pe.Node(
ApplyTransforms(dimension=3, default_value=0, float=True,
interpolation='MultiLabel'),
name='mni_seg'
)
mni_tpms = pe.MapNode(
ApplyTransforms(dimension=3, default_value=0, float=True,
interpolation='Linear'),
iterfield=['input_image'],
name='mni_tpms'
)
if 'template' in output_spaces:
template_str = TEMPLATE_MAP[template]
ref_img = op.join(nid.get_dataset(template_str), '1mm_T1.nii.gz')
t1_2_mni.inputs.template = template_str
mni_mask.inputs.reference_image = ref_img
mni_seg.inputs.reference_image = ref_img
mni_tpms.inputs.reference_image = ref_img
workflow.connect([
(inputnode, t1_2_mni, [('roi', 'lesion_mask')]),
(skullstrip_ants_wf, t1_2_mni, [('outputnode.bias_corrected', 'moving_image')]),
(buffernode, t1_2_mni, [('t1_mask', 'moving_mask')]),
(buffernode, mni_mask, [('t1_mask', 'input_image')]),
(t1_2_mni, mni_mask, [('composite_transform', 'transforms')]),
(t1_seg, mni_seg, [('tissue_class_map', 'input_image')]),
(t1_2_mni, mni_seg, [('composite_transform', 'transforms')]),
(t1_seg, mni_tpms, [('probability_maps', 'input_image')]),
(t1_2_mni, mni_tpms, [('composite_transform', 'transforms')]),
(t1_2_mni, outputnode, [
('warped_image', 't1_2_mni'),
('composite_transform', 't1_2_mni_forward_transform'),
('inverse_composite_transform', 't1_2_mni_reverse_transform')]),
(mni_mask, outputnode, [('output_image', 'mni_mask')]),
(mni_seg, outputnode, [('output_image', 'mni_seg')]),
(mni_tpms, outputnode, [('output_image', 'mni_tpms')]),
])
seg2msks = pe.Node(niu.Function(function=_seg2msks), name='seg2msks')
seg_rpt = pe.Node(ROIsPlot(colors=['r', 'magenta', 'b', 'g']), name='seg_rpt')
anat_reports_wf = init_anat_reports_wf(
reportlets_dir=reportlets_dir, output_spaces=output_spaces, template=template,
freesurfer=freesurfer)
workflow.connect([
(inputnode, anat_reports_wf, [
(('t1w', fix_multi_T1w_source_name), 'inputnode.source_file')]),
(anat_template_wf, anat_reports_wf, [
('outputnode.out_report', 'inputnode.t1_conform_report')]),
(anat_template_wf, seg_rpt, [
('outputnode.t1_template', 'in_file')]),
(t1_seg, seg2msks, [('tissue_class_map', 'in_file')]),
(seg2msks, seg_rpt, [('out', 'in_rois')]),
(outputnode, seg_rpt, [('t1_mask', 'in_mask')]),
(seg_rpt, anat_reports_wf, [('out_report', 'inputnode.seg_report')]),
])
if freesurfer:
workflow.connect([
(surface_recon_wf, anat_reports_wf, [
('outputnode.out_report', 'inputnode.recon_report')]),
])
if 'template' in output_spaces:
workflow.connect([
(t1_2_mni, anat_reports_wf, [('out_report', 'inputnode.t1_2_mni_report')]),
])
anat_derivatives_wf = init_anat_derivatives_wf(output_dir=output_dir,
output_spaces=output_spaces,
template=template,
freesurfer=freesurfer)
workflow.connect([
(anat_template_wf, anat_derivatives_wf, [
('outputnode.t1w_valid_list', 'inputnode.source_files')]),
(outputnode, anat_derivatives_wf, [
('t1_template_transforms', 'inputnode.t1_template_transforms'),
('t1_preproc', 'inputnode.t1_preproc'),
('t1_mask', 'inputnode.t1_mask'),
('t1_seg', 'inputnode.t1_seg'),
('t1_tpms', 'inputnode.t1_tpms'),
('t1_2_mni_forward_transform', 'inputnode.t1_2_mni_forward_transform'),
('t1_2_mni_reverse_transform', 'inputnode.t1_2_mni_reverse_transform'),
('t1_2_mni', 'inputnode.t1_2_mni'),
('mni_mask', 'inputnode.mni_mask'),
('mni_seg', 'inputnode.mni_seg'),
('mni_tpms', 'inputnode.mni_tpms'),
('t1_2_fsnative_forward_transform', 'inputnode.t1_2_fsnative_forward_transform'),
('surfaces', 'inputnode.surfaces'),
]),
])
if freesurfer:
workflow.connect([
(surface_recon_wf, anat_derivatives_wf, [
('outputnode.out_aseg', 'inputnode.t1_fs_aseg'),
('outputnode.out_aparc', 'inputnode.t1_fs_aparc'),
]),
])
return workflow
def init_anat_template_wf(longitudinal, omp_nthreads, num_t1w, name='anat_template_wf'):
r"""
This workflow generates a canonically oriented structural template from
input T1w images.
.. workflow::
:graph2use: orig
:simple_form: yes
from fmriprep.workflows.anatomical import init_anat_template_wf
wf = init_anat_template_wf(longitudinal=False, omp_nthreads=1, num_t1w=1)
**Parameters**
longitudinal : bool
Create unbiased structural template, regardless of number of inputs
(may increase runtime)
omp_nthreads : int
Maximum number of threads an individual process may use
num_t1w : int
Number of T1w images
name : str, optional
Workflow name (default: anat_template_wf)
**Inputs**
t1w
List of T1-weighted structural images
**Outputs**
t1_template
Structural template, defining T1w space
template_transforms
List of affine transforms from ``t1_template`` to original T1w images
out_report
Conformation report
"""
workflow = Workflow(name=name)
if num_t1w > 1:
workflow.__desc__ = """\
A T1w-reference map was computed after registration of
{num_t1w} T1w images (after INU-correction) using
`mri_robust_template` [FreeSurfer {fs_ver}, @fs_template].
""".format(num_t1w=num_t1w, fs_ver=fs.Info().looseversion() or '<ver>')
inputnode = pe.Node(niu.IdentityInterface(fields=['t1w']), name='inputnode')
outputnode = pe.Node(niu.IdentityInterface(
fields=['t1_template', 't1w_valid_list', 'template_transforms', 'out_report']),
name='outputnode')
# 0. Reorient T1w image(s) to RAS and resample to common voxel space
t1_template_dimensions = pe.Node(TemplateDimensions(), name='t1_template_dimensions')
t1_conform = pe.MapNode(Conform(), iterfield='in_file', name='t1_conform')
workflow.connect([
(inputnode, t1_template_dimensions, [('t1w', 't1w_list')]),
(t1_template_dimensions, t1_conform, [
('t1w_valid_list', 'in_file'),
('target_zooms', 'target_zooms'),
('target_shape', 'target_shape')]),
(t1_template_dimensions, outputnode, [('out_report', 'out_report'),
('t1w_valid_list', 't1w_valid_list')]),
])
if num_t1w == 1:
def _get_first(in_list):
if isinstance(in_list, (list, tuple)):
return in_list[0]
return in_list
outputnode.inputs.template_transforms = [pkgr('fmriprep', 'data/itkIdentityTransform.txt')]
workflow.connect([
(t1_conform, outputnode, [(('out_file', _get_first), 't1_template')]),
])
return workflow
# 1. Template (only if several T1w images)
# 1a. Correct for bias field: the bias field is an additive factor
# in log-transformed intensity units. Therefore, it is not a linear
# combination of fields and N4 fails with merged images.
# 1b. Align and merge if several T1w images are provided
n4_correct = pe.MapNode(
N4BiasFieldCorrection(dimension=3, copy_header=True),
iterfield='input_image', name='n4_correct',
n_procs=1) # n_procs=1 for reproducibility
t1_merge = pe.Node(
fs.RobustTemplate(auto_detect_sensitivity=True,
initial_timepoint=1, # For deterministic behavior
intensity_scaling=True, # 7-DOF (rigid + intensity)
subsample_threshold=200,
fixed_timepoint=not longitudinal,
no_iteration=not longitudinal,
transform_outputs=True,
),
mem_gb=2 * num_t1w - 1,
name='t1_merge')
# 2. Reorient template to RAS, if needed (mri_robust_template may set to LIA)
t1_reorient = pe.Node(image.Reorient(), name='t1_reorient')
lta_to_fsl = pe.MapNode(fs.utils.LTAConvert(out_fsl=True), iterfield=['in_lta'],
name='lta_to_fsl')
concat_affines = pe.MapNode(
ConcatAffines(3, invert=True), iterfield=['mat_AtoB', 'mat_BtoC'],
name='concat_affines', run_without_submitting=True)
fsl_to_itk = pe.MapNode(c3.C3dAffineTool(fsl2ras=True, itk_transform=True),
iterfield=['transform_file', 'source_file'], name='fsl_to_itk')
def _set_threads(in_list, maximum):
return min(len(in_list), maximum)
workflow.connect([
(t1_conform, n4_correct, [('out_file', 'input_image')]),
(t1_conform, t1_merge, [
(('out_file', _set_threads, omp_nthreads), 'num_threads'),
(('out_file', add_suffix, '_template'), 'out_file')]),
(n4_correct, t1_merge, [('output_image', 'in_files')]),
(t1_merge, t1_reorient, [('out_file', 'in_file')]),
# Combine orientation and template transforms
(t1_merge, lta_to_fsl, [('transform_outputs', 'in_lta')]),
(t1_conform, concat_affines, [('transform', 'mat_AtoB')]),
(lta_to_fsl, concat_affines, [('out_fsl', 'mat_BtoC')]),
(t1_reorient, concat_affines, [('transform', 'mat_CtoD')]),
(t1_template_dimensions, fsl_to_itk, [('t1w_valid_list', 'source_file')]),
(t1_reorient, fsl_to_itk, [('out_file', 'reference_file')]),
(concat_affines, fsl_to_itk, [('out_mat', 'transform_file')]),
# Output
(t1_reorient, outputnode, [('out_file', 't1_template')]),
(fsl_to_itk, outputnode, [('itk_transform', 'template_transforms')]),
])
return workflow
def init_skullstrip_ants_wf(skull_strip_template, debug, omp_nthreads,
skull_strip_fixed_seed=False, name='skullstrip_ants_wf'):
r"""
This workflow performs skull-stripping using ANTs' ``BrainExtraction.sh``
.. workflow::
:graph2use: orig
:simple_form: yes
from fmriprep.workflows.anatomical import init_skullstrip_ants_wf
wf = init_skullstrip_ants_wf(skull_strip_template='OASIS', debug=False, omp_nthreads=1)
**Parameters**
skull_strip_template : str
Name of ANTs skull-stripping template ('OASIS' or 'NKI')
debug : bool
Enable debugging outputs
omp_nthreads : int
Maximum number of threads an individual process may use
skull_strip_fixed_seed : bool
Do not use a random seed for skull-stripping - will ensure
run-to-run replicability when used with --omp-nthreads 1 (default: ``False``)
**Inputs**
in_file
T1-weighted structural image to skull-strip
**Outputs**
bias_corrected
Bias-corrected ``in_file``, before skull-stripping
out_file
Skull-stripped ``in_file``
out_mask
Binary mask of the skull-stripped ``in_file``
out_report
Reportlet visualizing quality of skull-stripping
"""
from niworkflows.data.getters import get_dataset
if skull_strip_template not in ['OASIS', 'NKI']:
raise ValueError("Unknown skull-stripping template; select from {OASIS, NKI}")
workflow = Workflow(name=name)
workflow.__desc__ = """\
The T1w-reference was then skull-stripped using `antsBrainExtraction.sh`
(ANTs {ants_ver}), using {skullstrip_tpl} as target template.
""".format(ants_ver=BrainExtraction().version or '<ver>', skullstrip_tpl=skull_strip_template)
# Grabbing the appropriate template elements
template_dir = get_dataset('ants_%s_template_ras' % skull_strip_template.lower())
brain_probability_mask = op.join(
template_dir, 'T_template0_BrainCerebellumProbabilityMask.nii.gz')
# TODO: normalize these names so this is not necessary
if skull_strip_template == 'OASIS':
brain_template = op.join(template_dir, 'T_template0.nii.gz')
extraction_registration_mask = op.join(
template_dir, 'T_template0_BrainCerebellumRegistrationMask.nii.gz')
elif skull_strip_template == 'NKI':
brain_template = op.join(template_dir, 'T_template.nii.gz')
extraction_registration_mask = op.join(
template_dir, 'T_template_BrainCerebellumExtractionMask.nii.gz')
inputnode = pe.Node(niu.IdentityInterface(fields=['in_file']),
name='inputnode')
outputnode = pe.Node(niu.IdentityInterface(
fields=['bias_corrected', 'out_file', 'out_mask', 'out_segs', 'out_report']),
name='outputnode')
t1_skull_strip = pe.Node(
BrainExtraction(dimension=3, use_floatingpoint_precision=1, debug=debug,
keep_temporary_files=1, use_random_seeding=not skull_strip_fixed_seed),
name='t1_skull_strip', n_procs=omp_nthreads)
t1_skull_strip.inputs.brain_template = brain_template
t1_skull_strip.inputs.brain_probability_mask = brain_probability_mask
t1_skull_strip.inputs.extraction_registration_mask = extraction_registration_mask
workflow.connect([
(inputnode, t1_skull_strip, [('in_file', 'anatomical_image')]),
(t1_skull_strip, outputnode, [('BrainExtractionMask', 'out_mask'),
('BrainExtractionBrain', 'out_file'),
('BrainExtractionSegmentation', 'out_segs'),
('N4Corrected0', 'bias_corrected')])
])
return workflow
def init_surface_recon_wf(omp_nthreads, hires, name='surface_recon_wf'):
r"""
This workflow reconstructs anatomical surfaces using FreeSurfer's ``recon-all``.
Reconstruction is performed in three phases.
The first phase initializes the subject with T1w and T2w (if available)
structural images and performs basic reconstruction (``autorecon1``) with the
exception of skull-stripping.
For example, a subject with only one session with T1w and T2w images
would be processed by the following command::
$ recon-all -sd <output dir>/freesurfer -subjid sub-<subject_label> \
-i <bids-root>/sub-<subject_label>/anat/sub-<subject_label>_T1w.nii.gz \
-T2 <bids-root>/sub-<subject_label>/anat/sub-<subject_label>_T2w.nii.gz \
-autorecon1 \
-noskullstrip
The second phase imports an externally computed skull-stripping mask.
This workflow refines the external brainmask using the internal mask
implicit the the FreeSurfer's ``aseg.mgz`` segmentation,
to reconcile ANTs' and FreeSurfer's brain masks.
First, the ``aseg.mgz`` mask from FreeSurfer is refined in two
steps, using binary morphological operations:
1. With a binary closing operation the sulci are included
into the mask. This results in a smoother brain mask
that does not exclude deep, wide sulci.
2. Fill any holes (typically, there could be a hole next to
the pineal gland and the corpora quadrigemina if the great
cerebral brain is segmented out).
Second, the brain mask is grown, including pixels that have a high likelihood
to the GM tissue distribution:
3. Dilate and substract the brain mask, defining the region to search for candidate
pixels that likely belong to cortical GM.
4. Pixels found in the search region that are labeled as GM by ANTs
(during ``antsBrainExtraction.sh``) are directly added to the new mask.
5. Otherwise, estimate GM tissue parameters locally in patches of ``ww`` size,
and test the likelihood of the pixel to belong in the GM distribution.
This procedure is inspired on mindboggle's solution to the problem:
https://github.com/nipy/mindboggle/blob/7f91faaa7664d820fe12ccc52ebaf21d679795e2/mindboggle/guts/segment.py#L1660
The final phase resumes reconstruction, using the T2w image to assist
in finding the pial surface, if available.
See :py:func:`~fmriprep.workflows.anatomical.init_autorecon_resume_wf` for details.
Memory annotations for FreeSurfer are based off `their documentation
<https://surfer.nmr.mgh.harvard.edu/fswiki/SystemRequirements>`_.
They specify an allocation of 4GB per subject. Here we define 5GB
to have a certain margin.
.. workflow::
:graph2use: orig
:simple_form: yes
from fmriprep.workflows.anatomical import init_surface_recon_wf
wf = init_surface_recon_wf(omp_nthreads=1, hires=True)
**Parameters**
omp_nthreads : int
Maximum number of threads an individual process may use
hires : bool
Enable sub-millimeter preprocessing in FreeSurfer
**Inputs**
t1w
List of T1-weighted structural images
t2w
List of T2-weighted structural images (only first used)
flair
List of FLAIR images
skullstripped_t1
Skull-stripped T1-weighted image (or mask of image)
ants_segs
Brain tissue segmentation from ANTS ``antsBrainExtraction.sh``
corrected_t1
INU-corrected, merged T1-weighted image
subjects_dir
FreeSurfer SUBJECTS_DIR
subject_id
FreeSurfer subject ID
**Outputs**
subjects_dir
FreeSurfer SUBJECTS_DIR
subject_id
FreeSurfer subject ID
t1_2_fsnative_forward_transform
LTA-style affine matrix translating from T1w to FreeSurfer-conformed subject space
t1_2_fsnative_reverse_transform
LTA-style affine matrix translating from FreeSurfer-conformed subject space to T1w
surfaces
GIFTI surfaces for gray/white matter boundary, pial surface,
midthickness (or graymid) surface, and inflated surfaces
out_brainmask
Refined brainmask, derived from FreeSurfer's ``aseg`` volume
out_aseg
FreeSurfer's aseg segmentation, in native T1w space
out_aparc
FreeSurfer's aparc+aseg segmentation, in native T1w space
out_report
Reportlet visualizing quality of surface alignment
**Subworkflows**
* :py:func:`~fmriprep.workflows.anatomical.init_autorecon_resume_wf`
* :py:func:`~fmriprep.workflows.anatomical.init_gifti_surface_wf`
"""
workflow = Workflow(name=name)
workflow.__desc__ = """\
Brain surfaces were reconstructed using `recon-all` [FreeSurfer {fs_ver},
RRID:SCR_001847, @fs_reconall], and the brain mask estimated
previously was refined with a custom variation of the method to reconcile
ANTs-derived and FreeSurfer-derived segmentations of the cortical
gray-matter of Mindboggle [RRID:SCR_002438, @mindboggle].
""".format(fs_ver=fs.Info().looseversion() or '<ver>')
inputnode = pe.Node(
niu.IdentityInterface(
fields=['t1w', 't2w', 'flair', 'skullstripped_t1', 'corrected_t1', 'ants_segs',
'subjects_dir', 'subject_id']), name='inputnode')
outputnode = pe.Node(
niu.IdentityInterface(
fields=['subjects_dir', 'subject_id', 't1_2_fsnative_forward_transform',
't1_2_fsnative_reverse_transform', 'surfaces', 'out_brainmask',
'out_aseg', 'out_aparc', 'out_report']),
name='outputnode')
recon_config = pe.Node(FSDetectInputs(hires_enabled=hires), name='recon_config')
autorecon1 = pe.Node(
fs.ReconAll(directive='autorecon1', flags='-noskullstrip', openmp=omp_nthreads),
name='autorecon1', n_procs=omp_nthreads, mem_gb=5)
autorecon1.interface._can_resume = False
skull_strip_extern = pe.Node(FSInjectBrainExtracted(), name='skull_strip_extern')
fsnative_2_t1_xfm = pe.Node(fs.RobustRegister(auto_sens=True, est_int_scale=True),
name='fsnative_2_t1_xfm')
t1_2_fsnative_xfm = pe.Node(fs.utils.LTAConvert(out_lta=True, invert=True),
name='t1_2_fsnative_xfm')
autorecon_resume_wf = init_autorecon_resume_wf(omp_nthreads=omp_nthreads)
gifti_surface_wf = init_gifti_surface_wf()
aseg_to_native_wf = init_segs_to_native_wf()
aparc_to_native_wf = init_segs_to_native_wf(segmentation='aparc_aseg')
refine = pe.Node(RefineBrainMask(), name='refine')
workflow.connect([
# Configuration
(inputnode, recon_config, [('t1w', 't1w_list'),
('t2w', 't2w_list'),
('flair', 'flair_list')]),
# Passing subjects_dir / subject_id enforces serial order
(inputnode, autorecon1, [('subjects_dir', 'subjects_dir'),
('subject_id', 'subject_id')]),
(autorecon1, skull_strip_extern, [('subjects_dir', 'subjects_dir'),
('subject_id', 'subject_id')]),
(skull_strip_extern, autorecon_resume_wf, [('subjects_dir', 'inputnode.subjects_dir'),
('subject_id', 'inputnode.subject_id')]),
(autorecon_resume_wf, gifti_surface_wf, [
('outputnode.subjects_dir', 'inputnode.subjects_dir'),
('outputnode.subject_id', 'inputnode.subject_id')]),
# Reconstruction phases
(inputnode, autorecon1, [('t1w', 'T1_files')]),
(recon_config, autorecon1, [('t2w', 'T2_file'),
('flair', 'FLAIR_file'),
('hires', 'hires'),
# First run only (recon-all saves expert options)
('mris_inflate', 'mris_inflate')]),
(inputnode, skull_strip_extern, [('skullstripped_t1', 'in_brain')]),
(recon_config, autorecon_resume_wf, [('use_t2w', 'inputnode.use_T2'),
('use_flair', 'inputnode.use_FLAIR')]),
# Construct transform from FreeSurfer conformed image to FMRIPREP
# reoriented image
(inputnode, fsnative_2_t1_xfm, [('t1w', 'target_file')]),
(autorecon1, fsnative_2_t1_xfm, [('T1', 'source_file')]),
(fsnative_2_t1_xfm, gifti_surface_wf, [
('out_reg_file', 'inputnode.t1_2_fsnative_reverse_transform')]),
(fsnative_2_t1_xfm, t1_2_fsnative_xfm, [('out_reg_file', 'in_lta')]),
# Refine ANTs mask, deriving new mask from FS' aseg
(inputnode, refine, [('corrected_t1', 'in_anat'),
('ants_segs', 'in_ants')]),
(inputnode, aseg_to_native_wf, [('corrected_t1', 'inputnode.in_file')]),
(autorecon_resume_wf, aseg_to_native_wf, [
('outputnode.subjects_dir', 'inputnode.subjects_dir'),
('outputnode.subject_id', 'inputnode.subject_id')]),
(inputnode, aparc_to_native_wf, [('corrected_t1', 'inputnode.in_file')]),
(autorecon_resume_wf, aparc_to_native_wf, [
('outputnode.subjects_dir', 'inputnode.subjects_dir'),
('outputnode.subject_id', 'inputnode.subject_id')]),
(aseg_to_native_wf, refine, [('outputnode.out_file', 'in_aseg')]),
# Output
(autorecon_resume_wf, outputnode, [('outputnode.subjects_dir', 'subjects_dir'),
('outputnode.subject_id', 'subject_id'),
('outputnode.out_report', 'out_report')]),
(gifti_surface_wf, outputnode, [('outputnode.surfaces', 'surfaces')]),
(t1_2_fsnative_xfm, outputnode, [('out_lta', 't1_2_fsnative_forward_transform')]),
(fsnative_2_t1_xfm, outputnode, [('out_reg_file', 't1_2_fsnative_reverse_transform')]),
(refine, outputnode, [('out_file', 'out_brainmask')]),
(aseg_to_native_wf, outputnode, [('outputnode.out_file', 'out_aseg')]),
(aparc_to_native_wf, outputnode, [('outputnode.out_file', 'out_aparc')]),
])
return workflow
def init_autorecon_resume_wf(omp_nthreads, name='autorecon_resume_wf'):
r"""
This workflow resumes recon-all execution, assuming the `-autorecon1` stage
has been completed.
In order to utilize resources efficiently, this is broken down into five
sub-stages; after the first stage, the second and third stages may be run
simultaneously, and the fourth and fifth stages may be run simultaneously,
if resources permit::
$ recon-all -sd <output dir>/freesurfer -subjid sub-<subject_label> \
-autorecon2-volonly
$ recon-all -sd <output dir>/freesurfer -subjid sub-<subject_label> \
-autorecon-hemi lh \
-noparcstats -nocortparc2 -noparcstats2 -nocortparc3 \
-noparcstats3 -nopctsurfcon -nohyporelabel -noaparc2aseg \
-noapas2aseg -nosegstats -nowmparc -nobalabels
$ recon-all -sd <output dir>/freesurfer -subjid sub-<subject_label> \
-autorecon-hemi rh \
-noparcstats -nocortparc2 -noparcstats2 -nocortparc3 \
-noparcstats3 -nopctsurfcon -nohyporelabel -noaparc2aseg \
-noapas2aseg -nosegstats -nowmparc -nobalabels
$ recon-all -sd <output dir>/freesurfer -subjid sub-<subject_label> \
-autorecon3 -hemi lh -T2pial
$ recon-all -sd <output dir>/freesurfer -subjid sub-<subject_label> \
-autorecon3 -hemi rh -T2pial
The excluded steps in the second and third stages (``-no<option>``) are not
fully hemisphere independent, and are therefore postponed to the final two
stages.
.. workflow::
:graph2use: orig
:simple_form: yes
from fmriprep.workflows.anatomical import init_autorecon_resume_wf
wf = init_autorecon_resume_wf(omp_nthreads=1)
**Inputs**
subjects_dir
FreeSurfer SUBJECTS_DIR
subject_id
FreeSurfer subject ID
use_T2
Refine pial surface using T2w image
use_FLAIR
Refine pial surface using FLAIR image
**Outputs**
subjects_dir
FreeSurfer SUBJECTS_DIR
subject_id
FreeSurfer subject ID
out_report
Reportlet visualizing quality of surface alignment
"""
workflow = Workflow(name=name)
inputnode = pe.Node(
niu.IdentityInterface(
fields=['subjects_dir', 'subject_id', 'use_T2', 'use_FLAIR']),
name='inputnode')
outputnode = pe.Node(
niu.IdentityInterface(
fields=['subjects_dir', 'subject_id', 'out_report']),
name='outputnode')
autorecon2_vol = pe.Node(
fs.ReconAll(directive='autorecon2-volonly', openmp=omp_nthreads),
n_procs=omp_nthreads, mem_gb=5, name='autorecon2_vol')
autorecon_surfs = pe.MapNode(
fs.ReconAll(
directive='autorecon-hemi',
flags=['-noparcstats', '-nocortparc2', '-noparcstats2',
'-nocortparc3', '-noparcstats3', '-nopctsurfcon',
'-nohyporelabel', '-noaparc2aseg', '-noapas2aseg',
'-nosegstats', '-nowmparc', '-nobalabels'],
openmp=omp_nthreads),
iterfield='hemi', n_procs=omp_nthreads, mem_gb=5,
name='autorecon_surfs')
autorecon_surfs.inputs.hemi = ['lh', 'rh']
autorecon3 = pe.MapNode(
fs.ReconAll(directive='autorecon3', openmp=omp_nthreads),
iterfield='hemi', n_procs=omp_nthreads, mem_gb=5,
name='autorecon3')
autorecon3.inputs.hemi = ['lh', 'rh']
# Only generate the report once; should be nothing to do
recon_report = pe.Node(
ReconAllRPT(directive='autorecon3', generate_report=True),
name='recon_report', mem_gb=5)
def _dedup(in_list):
vals = set(in_list)
if len(vals) > 1:
raise ValueError(
"Non-identical values can't be deduplicated:\n{!r}".format(in_list))
return vals.pop()
workflow.connect([
(inputnode, autorecon3, [('use_T2', 'use_T2'),
('use_FLAIR', 'use_FLAIR')]),
(inputnode, autorecon2_vol, [('subjects_dir', 'subjects_dir'),
('subject_id', 'subject_id')]),
(autorecon2_vol, autorecon_surfs, [('subjects_dir', 'subjects_dir'),
('subject_id', 'subject_id')]),
(autorecon_surfs, autorecon3, [(('subjects_dir', _dedup), 'subjects_dir'),