-
Notifications
You must be signed in to change notification settings - Fork 37
/
surfaces.py
1112 lines (966 loc) · 39 KB
/
surfaces.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
# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*-
# vi: set ft=python sts=4 ts=4 sw=4 et:
#
# Copyright 2021 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/
#
"""
Surface preprocessing workflows.
**sMRIPrep** uses FreeSurfer to reconstruct surfaces from T1w/T2w
structural images.
"""
import typing as ty
from nipype.pipeline import engine as pe
from nipype.interfaces.base import Undefined
from nipype.interfaces import (
fsl,
io as nio,
utility as niu,
freesurfer as fs,
workbench as wb,
)
from ..data import load_resource
from ..interfaces.freesurfer import ReconAll, MakeMidthickness
from niworkflows.engine.workflows import LiterateWorkflow as Workflow
from niworkflows.interfaces.freesurfer import (
FSDetectInputs,
FSInjectBrainExtracted,
PatchedLTAConvert as LTAConvert,
PatchedRobustRegister as RobustRegister,
RefineBrainMask,
)
from ..interfaces.workbench import CreateSignedDistanceVolume
def init_surface_recon_wf(*, omp_nthreads, hires, name="surface_recon_wf"):
r"""
Reconstruct 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 -noT2pial -noFLAIRpial
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:`~smriprep.workflows.surfaces.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 Graph
.. workflow::
:graph2use: orig
:simple_form: yes
from smriprep.workflows.surfaces 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
t1w2fsnative_xfm
LTA-style affine matrix translating from T1w to FreeSurfer-conformed subject space
fsnative2t1w_xfm
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
morphometrics
GIFTIs of cortical thickness, curvature, and sulcal depth
See also
--------
* :py:func:`~smriprep.workflows.surfaces.init_autorecon_resume_wf`
* :py:func:`~smriprep.workflows.surfaces.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",
"t1w2fsnative_xfm",
"fsnative2t1w_xfm",
"surfaces",
"out_brainmask",
"out_aseg",
"out_aparc",
"morphometrics",
]
),
name="outputnode",
)
recon_config = pe.Node(FSDetectInputs(hires_enabled=hires), name="recon_config")
fov_check = pe.Node(niu.Function(function=_check_cw256), name="fov_check")
fov_check.inputs.default_flags = ['-noskullstrip', '-noT2pial', '-noFLAIRpial']
autorecon1 = pe.Node(
ReconAll(directive="autorecon1", openmp=omp_nthreads),
name="autorecon1",
n_procs=omp_nthreads,
mem_gb=5,
)
autorecon1.interface._can_resume = False
autorecon1.interface._always_run = True
skull_strip_extern = pe.Node(FSInjectBrainExtracted(), name="skull_strip_extern")
fsnative2t1w_xfm = pe.Node(
RobustRegister(auto_sens=True, est_int_scale=True), name="fsnative2t1w_xfm"
)
t1w2fsnative_xfm = pe.Node(
LTAConvert(out_lta=True, invert=True), name="t1w2fsnative_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")
# fmt:off
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')]),
(inputnode, fov_check, [('t1w', 'in_files')]),
(fov_check, autorecon1, [('out', 'flags')]),
(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 sMRIPrep
# reoriented image
(inputnode, fsnative2t1w_xfm, [('t1w', 'target_file')]),
(autorecon1, fsnative2t1w_xfm, [('T1', 'source_file')]),
(fsnative2t1w_xfm, t1w2fsnative_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')]),
(fsnative2t1w_xfm, aseg_to_native_wf, [('out_reg_file', 'inputnode.fsnative2t1w_xfm')]),
(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')]),
(fsnative2t1w_xfm, aparc_to_native_wf, [('out_reg_file', 'inputnode.fsnative2t1w_xfm')]),
(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')]),
(gifti_surface_wf, outputnode, [('outputnode.surfaces', 'surfaces'),
('outputnode.morphometrics', 'morphometrics')]),
(t1w2fsnative_xfm, outputnode, [('out_lta', 't1w2fsnative_xfm')]),
(fsnative2t1w_xfm, outputnode, [('out_reg_file', 'fsnative2t1w_xfm')]),
(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')]),
])
# fmt:on
return workflow
def init_autorecon_resume_wf(*, omp_nthreads, name="autorecon_resume_wf"):
r"""
Resume recon-all execution, assuming the `-autorecon1` stage has been completed.
In order to utilize resources efficiently, this is broken down into seven
sub-stages; after the first stage, the second and third stages may be run
simultaneously, and the fifth and sixth stages may be run simultaneously,
if resources permit; the fourth stage must be run prior to the fifth and
sixth, and the seventh must be run after::
$ 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 -T2pial \
-noparcstats -noparcstats2 -noparcstats3 -nohyporelabel -nobalabels
$ recon-all -sd <output dir>/freesurfer -subjid sub-<subject_label> \
-autorecon-hemi rh -T2pial \
-noparcstats -noparcstats2 -noparcstats3 -nohyporelabel -nobalabels
$ recon-all -sd <output dir>/freesurfer -subjid sub-<subject_label> \
-cortribbon
$ recon-all -sd <output dir>/freesurfer -subjid sub-<subject_label> \
-autorecon-hemi lh -nohyporelabel
$ recon-all -sd <output dir>/freesurfer -subjid sub-<subject_label> \
-autorecon-hemi rh -nohyporelabel
$ recon-all -sd <output dir>/freesurfer -subjid sub-<subject_label> \
-autorecon3
The parcellation statistics steps are excluded from the second and third
stages, because they require calculation of the cortical ribbon volume
(the fourth stage).
Hypointensity relabeling is excluded from hemisphere-specific steps to avoid
race conditions, as it is a volumetric operation.
Workflow Graph
.. workflow::
:graph2use: orig
:simple_form: yes
from smriprep.workflows.surfaces 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
"""
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"]), name="outputnode"
)
# FreeSurfer 7.3 removed gcareg from autorecon2-volonly
# Adding it directly in would force it to run every time
gcareg = pe.Node(
ReconAll(directive=Undefined, steps=["gcareg"], openmp=omp_nthreads),
n_procs=omp_nthreads,
mem_gb=5,
name="gcareg",
)
gcareg.interface._always_run = True
autorecon2_vol = pe.Node(
ReconAll(directive="autorecon2-volonly", openmp=omp_nthreads),
n_procs=omp_nthreads,
mem_gb=5,
name="autorecon2_vol",
)
autorecon2_vol.interface._always_run = True
autorecon_surfs = pe.MapNode(
ReconAll(
directive="autorecon-hemi",
flags=[
"-noparcstats",
"-noparcstats2",
"-noparcstats3",
"-nohyporelabel",
"-nobalabels",
],
openmp=omp_nthreads,
),
iterfield="hemi",
n_procs=omp_nthreads,
mem_gb=5,
name="autorecon_surfs",
)
autorecon_surfs.inputs.hemi = ["lh", "rh"]
autorecon_surfs.interface._always_run = True
# -cortribbon is a prerequisite for -parcstats, -parcstats2, -parcstats3
# Claiming two threads because pial refinement can be split by hemisphere
# if -T2pial or -FLAIRpial is enabled.
# Parallelizing by hemisphere saves ~30 minutes over simply enabling
# OpenMP on an 8 core machine.
cortribbon = pe.Node(
ReconAll(directive=Undefined, steps=["cortribbon"], parallel=True),
n_procs=2,
name="cortribbon",
)
cortribbon.interface._always_run = True
# -parcstats* can be run per-hemisphere
# -hyporelabel is volumetric, even though it's part of -autorecon-hemi
parcstats = pe.MapNode(
ReconAll(
directive="autorecon-hemi", flags=["-nohyporelabel"], openmp=omp_nthreads
),
iterfield="hemi",
n_procs=omp_nthreads,
mem_gb=5,
name="parcstats",
)
parcstats.inputs.hemi = ["lh", "rh"]
parcstats.interface._always_run = True
# Runs: -hyporelabel -aparc2aseg -apas2aseg -segstats -wmparc
# All volumetric, so don't
autorecon3 = pe.Node(
ReconAll(directive="autorecon3", openmp=omp_nthreads),
n_procs=omp_nthreads,
mem_gb=5,
name="autorecon3",
)
autorecon3.interface._always_run = True
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()
# fmt:off
workflow.connect([
(inputnode, cortribbon, [('use_T2', 'use_T2'),
('use_FLAIR', 'use_FLAIR')]),
(inputnode, gcareg, [('subjects_dir', 'subjects_dir'),
('subject_id', 'subject_id')]),
(gcareg, autorecon2_vol, [('subjects_dir', 'subjects_dir'),
('subject_id', 'subject_id')]),
(autorecon2_vol, autorecon_surfs, [('subjects_dir', 'subjects_dir'),
('subject_id', 'subject_id')]),
(autorecon_surfs, cortribbon, [(('subjects_dir', _dedup), 'subjects_dir'),
(('subject_id', _dedup), 'subject_id')]),
(cortribbon, parcstats, [('subjects_dir', 'subjects_dir'),
('subject_id', 'subject_id')]),
(parcstats, autorecon3, [(('subjects_dir', _dedup), 'subjects_dir'),
(('subject_id', _dedup), 'subject_id')]),
(autorecon3, outputnode, [('subjects_dir', 'subjects_dir'),
('subject_id', 'subject_id')]),
])
# fmt:on
return workflow
def init_sphere_reg_wf(*, name="sphere_reg_wf"):
"""Generate GIFTI registration files to fsLR space"""
from ..interfaces.surf import FixGiftiMetadata
from ..interfaces.workbench import SurfaceSphereProjectUnproject
workflow = Workflow(name=name)
inputnode = pe.Node(
niu.IdentityInterface(["subjects_dir", "subject_id"]),
name="inputnode",
)
outputnode = pe.Node(
niu.IdentityInterface(["sphere_reg", "sphere_reg_fsLR"]), name="outputnode"
)
get_surfaces = pe.Node(nio.FreeSurferSource(), name="get_surfaces")
# Via FreeSurfer2CaretConvertAndRegisterNonlinear.sh#L270-L273
#
# See https://github.com/DCAN-Labs/DCAN-HCP/tree/9291324
sphere_gii = pe.MapNode(
fs.MRIsConvert(out_datatype="gii"), iterfield="in_file", name="sphere_gii"
)
fix_meta = pe.MapNode(FixGiftiMetadata(), iterfield="in_file", name="fix_meta")
# Via
# ${CARET7DIR}/wb_command -surface-sphere-project-unproject
# "$AtlasSpaceFolder"/"$NativeFolder"/"$Subject"."$Hemisphere".sphere.reg.native.surf.gii
# "$AtlasSpaceFolder"/fsaverage/"$Subject"."$Hemisphere".sphere."$HighResMesh"k_fs_"$Hemisphere".surf.gii
# "$AtlasSpaceFolder"/fsaverage/"$Subject"."$Hemisphere".def_sphere."$HighResMesh"k_fs_"$Hemisphere".surf.gii
# "$AtlasSpaceFolder"/"$NativeFolder"/"$Subject"."$Hemisphere".sphere.reg.reg_LR.native.surf.gii
project_unproject = pe.MapNode(
SurfaceSphereProjectUnproject(),
iterfield=["sphere_in", "sphere_project_to", "sphere_unproject_from"],
name="project_unproject",
)
atlases = load_resource('atlases')
project_unproject.inputs.sphere_project_to = [
atlases / 'fs_L' / 'fsaverage.L.sphere.164k_fs_L.surf.gii',
atlases / 'fs_R' / 'fsaverage.R.sphere.164k_fs_R.surf.gii',
]
project_unproject.inputs.sphere_unproject_from = [
atlases / 'fs_L' / 'fs_L-to-fs_LR_fsaverage.L_LR.spherical_std.164k_fs_L.surf.gii',
atlases / 'fs_R' / 'fs_R-to-fs_LR_fsaverage.R_LR.spherical_std.164k_fs_R.surf.gii',
]
# fmt:off
workflow.connect([
(inputnode, get_surfaces, [
('subjects_dir', 'subjects_dir'),
('subject_id', 'subject_id'),
]),
(get_surfaces, sphere_gii, [(('sphere_reg', _sorted_by_basename), 'in_file')]),
(sphere_gii, fix_meta, [('converted', 'in_file')]),
(fix_meta, project_unproject, [('out_file', 'sphere_in')]),
(sphere_gii, outputnode, [('converted', 'sphere_reg')]),
(project_unproject, outputnode, [('sphere_out', 'sphere_reg_fsLR')]),
])
# fmt:on
return workflow
def init_gifti_surface_wf(*, name="gifti_surface_wf"):
r"""
Prepare GIFTI surfaces from a FreeSurfer subjects directory.
If midthickness (or graymid) surfaces do not exist, they are generated and
saved to the subject directory as ``lh/rh.midthickness``.
These, along with the gray/white matter boundary (``lh/rh.white``), pial
sufaces (``lh/rh.pial``) and inflated surfaces (``lh/rh.inflated``) are
converted to GIFTI files.
Additionally, the vertex coordinates are :py:class:`recentered
<smriprep.interfaces.NormalizeSurf>` to align with native T1w space.
Workflow Graph
.. workflow::
:graph2use: orig
:simple_form: yes
from smriprep.workflows.surfaces import init_gifti_surface_wf
wf = init_gifti_surface_wf()
Inputs
------
subjects_dir
FreeSurfer SUBJECTS_DIR
subject_id
FreeSurfer subject ID
fsnative2t1w_xfm
LTA formatted affine transform file (inverse)
Outputs
-------
surfaces
GIFTI surfaces for gray/white matter boundary, pial surface,
midthickness (or graymid) surface, and inflated surfaces
morphometrics
GIFTIs of cortical thickness, curvature, and sulcal depth
"""
from ..interfaces.freesurfer import MRIsConvertData
from ..interfaces.surf import NormalizeSurf
workflow = Workflow(name=name)
inputnode = pe.Node(
niu.IdentityInterface(["subjects_dir", "subject_id", "fsnative2t1w_xfm"]),
name="inputnode",
)
outputnode = pe.Node(niu.IdentityInterface(["surfaces", "morphometrics"]), name="outputnode")
get_surfaces = pe.Node(nio.FreeSurferSource(), name="get_surfaces")
midthickness = pe.MapNode(
MakeMidthickness(thickness=True, distance=0.5, out_name="midthickness"),
iterfield="in_file",
name="midthickness",
)
save_midthickness = pe.Node(
nio.DataSink(parameterization=False), name="save_midthickness"
)
surface_list = pe.Node(
niu.Merge(4, ravel_inputs=True),
name="surface_list",
run_without_submitting=True,
)
fs2gii = pe.MapNode(
fs.MRIsConvert(out_datatype="gii", to_scanner=True), iterfield="in_file", name="fs2gii",
)
fix_surfs = pe.MapNode(NormalizeSurf(), iterfield="in_file", name="fix_surfs")
surfmorph_list = pe.Node(
niu.Merge(3, ravel_inputs=True),
name="surfmorph_list",
run_without_submitting=True,
)
morphs2gii = pe.MapNode(
MRIsConvertData(out_datatype="gii"),
iterfield="scalarcurv_file", name="morphs2gii",
)
# fmt:off
workflow.connect([
(inputnode, get_surfaces, [('subjects_dir', 'subjects_dir'),
('subject_id', 'subject_id')]),
(inputnode, save_midthickness, [('subjects_dir', 'base_directory'),
('subject_id', 'container')]),
# Generate midthickness surfaces and save to FreeSurfer derivatives
(get_surfaces, midthickness, [('white', 'in_file'),
('graymid', 'graymid')]),
(midthickness, save_midthickness, [('out_file', 'surf.@graymid')]),
# Produce valid GIFTI surface files (dense mesh)
(get_surfaces, surface_list, [('white', 'in1'),
('pial', 'in2'),
('inflated', 'in3')]),
(save_midthickness, surface_list, [('out_file', 'in4')]),
(surface_list, fs2gii, [('out', 'in_file')]),
(fs2gii, fix_surfs, [('converted', 'in_file')]),
(inputnode, fix_surfs, [('fsnative2t1w_xfm', 'transform_file')]),
(fix_surfs, outputnode, [('out_file', 'surfaces')]),
(get_surfaces, surfmorph_list, [('thickness', 'in1'),
('sulc', 'in2'),
('curv', 'in3')]),
(surfmorph_list, morphs2gii, [('out', 'scalarcurv_file')]),
(morphs2gii, outputnode, [('converted', 'morphometrics')]),
])
# fmt:on
return workflow
def init_segs_to_native_wf(*, name="segs_to_native", segmentation="aseg"):
"""
Get a segmentation from FreeSurfer conformed space into native T1w space.
Workflow Graph
.. workflow::
:graph2use: orig
:simple_form: yes
from smriprep.workflows.surfaces import init_segs_to_native_wf
wf = init_segs_to_native_wf()
Parameters
----------
segmentation
The name of a segmentation ('aseg' or 'aparc_aseg' or 'wmparc')
Inputs
------
in_file
Anatomical, merged T1w image after INU correction
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
-------
out_file
The selected segmentation, after resampling in native space
"""
workflow = Workflow(name="%s_%s" % (name, segmentation))
inputnode = pe.Node(
niu.IdentityInterface(
["in_file", "subjects_dir", "subject_id", "fsnative2t1w_xfm"]
),
name="inputnode",
)
outputnode = pe.Node(niu.IdentityInterface(["out_file"]), name="outputnode")
# Extract the aseg and aparc+aseg outputs
fssource = pe.Node(nio.FreeSurferSource(), name="fs_datasource")
# Resample from T1.mgz to T1w.nii.gz, applying any offset in fsnative2t1w_xfm,
# and convert to NIfTI while we're at it
resample = pe.Node(
fs.ApplyVolTransform(transformed_file="seg.nii.gz", interp="nearest"),
name="resample",
)
if segmentation.startswith("aparc"):
if segmentation == "aparc_aseg":
def _sel(x):
return [parc for parc in x if "aparc+" in parc][0] # noqa
elif segmentation == "aparc_a2009s":
def _sel(x):
return [parc for parc in x if "a2009s+" in parc][0] # noqa
elif segmentation == "aparc_dkt":
def _sel(x):
return [parc for parc in x if "DKTatlas+" in parc][0] # noqa
segmentation = (segmentation, _sel)
# fmt:off
workflow.connect([
(inputnode, fssource, [
('subjects_dir', 'subjects_dir'),
('subject_id', 'subject_id')]),
(inputnode, resample, [('in_file', 'target_file'),
('fsnative2t1w_xfm', 'lta_file')]),
(fssource, resample, [(segmentation, 'source_file')]),
(resample, outputnode, [('transformed_file', 'out_file')]),
])
# fmt:on
return workflow
def init_anat_ribbon_wf(name="anat_ribbon_wf"):
DEFAULT_MEMORY_MIN_GB = 0.01
workflow = pe.Workflow(name=name)
inputnode = pe.Node(
niu.IdentityInterface(
fields=[
"surfaces",
"t1w_mask",
]
),
name="inputnode",
)
outputnode = pe.Node(
niu.IdentityInterface(
fields=[
"anat_ribbon",
]
),
name="outputnode",
)
# 0, 1 = wm; 2, 3 = pial; 6, 7 = mid
# note that order of lh / rh within each surf type is not guaranteed due to use
# of unsorted glob by FreeSurferSource prior, but we can do a sort
# to ensure consistent ordering
select_wm = pe.Node(
niu.Select(index=[0, 1]),
name="select_wm",
mem_gb=DEFAULT_MEMORY_MIN_GB,
run_without_submitting=True,
)
select_pial = pe.Node(
niu.Select(index=[2, 3]),
name="select_pial",
mem_gb=DEFAULT_MEMORY_MIN_GB,
run_without_submitting=True,
)
select_midthick = pe.Node(
niu.Select(index=[6, 7]),
name="select_midthick",
mem_gb=DEFAULT_MEMORY_MIN_GB,
run_without_submitting=True,
)
create_wm_distvol = pe.MapNode(
CreateSignedDistanceVolume(),
iterfield=["surf_file"],
name="create_wm_distvol",
)
create_pial_distvol = pe.MapNode(
CreateSignedDistanceVolume(),
iterfield=["surf_file"],
name="create_pial_distvol",
)
thresh_wm_distvol = pe.MapNode(
fsl.maths.MathsCommand(args="-thr 0 -bin -mul 255"),
iterfield=["in_file"],
name="thresh_wm_distvol",
mem_gb=DEFAULT_MEMORY_MIN_GB,
)
uthresh_pial_distvol = pe.MapNode(
fsl.maths.MathsCommand(args="-uthr 0 -abs -bin -mul 255"),
iterfield=["in_file"],
name="uthresh_pial_distvol",
mem_gb=DEFAULT_MEMORY_MIN_GB,
)
bin_wm_distvol = pe.MapNode(
fsl.maths.UnaryMaths(operation="bin"),
iterfield=["in_file"],
name="bin_wm_distvol",
mem_gb=DEFAULT_MEMORY_MIN_GB,
)
bin_pial_distvol = pe.MapNode(
fsl.maths.UnaryMaths(operation="bin"),
iterfield=["in_file"],
name="bin_pial_distvol",
mem_gb=DEFAULT_MEMORY_MIN_GB,
)
split_wm_distvol = pe.Node(
niu.Split(splits=[1, 1]),
name="split_wm_distvol",
mem_gb=DEFAULT_MEMORY_MIN_GB,
run_without_submitting=True,
)
merge_wm_distvol_no_flatten = pe.Node(
niu.Merge(2),
no_flatten=True,
name="merge_wm_distvol_no_flatten",
mem_gb=DEFAULT_MEMORY_MIN_GB,
run_without_submitting=True,
)
make_ribbon_vol = pe.MapNode(
fsl.maths.MultiImageMaths(op_string="-mas %s -mul 255"),
iterfield=["in_file", "operand_files"],
name="make_ribbon_vol",
mem_gb=DEFAULT_MEMORY_MIN_GB,
)
bin_ribbon_vol = pe.MapNode(
fsl.maths.UnaryMaths(operation="bin"),
iterfield=["in_file"],
name="bin_ribbon_vol",
mem_gb=DEFAULT_MEMORY_MIN_GB,
)
split_squeeze_ribbon_vol = pe.Node(
niu.Split(splits=[1, 1], squeeze=True),
name="split_squeeze_ribbon",
mem_gb=DEFAULT_MEMORY_MIN_GB,
run_without_submitting=True,
)
combine_ribbon_vol_hemis = pe.Node(
fsl.maths.BinaryMaths(operation="add"),
name="combine_ribbon_vol_hemis",
mem_gb=DEFAULT_MEMORY_MIN_GB,
)
# make HCP-style ribbon volume in T1w space
workflow.connect(
[
(inputnode, select_wm, [("surfaces", "inlist")]),
(inputnode, select_pial, [("surfaces", "inlist")]),
(inputnode, select_midthick, [("surfaces", "inlist")]),
(select_wm, create_wm_distvol, [(("out", _sorted_by_basename), "surf_file")]),
(inputnode, create_wm_distvol, [("t1w_mask", "ref_file")]),
(select_pial, create_pial_distvol, [(("out", _sorted_by_basename), "surf_file")]),
(inputnode, create_pial_distvol, [("t1w_mask", "ref_file")]),
(create_wm_distvol, thresh_wm_distvol, [("out_file", "in_file")]),
(create_pial_distvol, uthresh_pial_distvol, [("out_file", "in_file")]),
(thresh_wm_distvol, bin_wm_distvol, [("out_file", "in_file")]),
(uthresh_pial_distvol, bin_pial_distvol, [("out_file", "in_file")]),
(bin_wm_distvol, split_wm_distvol, [("out_file", "inlist")]),
(split_wm_distvol, merge_wm_distvol_no_flatten, [("out1", "in1")]),
(split_wm_distvol, merge_wm_distvol_no_flatten, [("out2", "in2")]),
(bin_pial_distvol, make_ribbon_vol, [("out_file", "in_file")]),
(merge_wm_distvol_no_flatten, make_ribbon_vol, [("out", "operand_files")]),
(make_ribbon_vol, bin_ribbon_vol, [("out_file", "in_file")]),
(bin_ribbon_vol, split_squeeze_ribbon_vol, [("out_file", "inlist")]),
(split_squeeze_ribbon_vol, combine_ribbon_vol_hemis, [("out1", "in_file")]),
(split_squeeze_ribbon_vol, combine_ribbon_vol_hemis, [("out2", "operand_file")]),
(combine_ribbon_vol_hemis, outputnode, [("out_file", "anat_ribbon")]),
]
)
return workflow
def init_morph_grayords_wf(
grayord_density: ty.Literal['91k', '170k'],
name: str = "morph_grayords_wf",
):
"""
Sample Grayordinates files onto the fsLR atlas.
Outputs are in CIFTI2 format.
Workflow Graph
.. workflow::
:graph2use: colored
:simple_form: yes
from smriprep.workflows.surfaces import init_morph_grayords_wf
wf = init_morph_grayords_wf(grayord_density="91k")
Parameters
----------
grayord_density : :obj:`str`
Either `91k` or `170k`, representing the total of vertices or *grayordinates*.
name : :obj:`str`
Unique name for the subworkflow (default: ``"morph_grayords_wf"``)
Inputs
------
subject_id : :obj:`str`
FreeSurfer subject ID
subjects_dir : :obj:`str`
FreeSurfer SUBJECTS_DIR
Outputs
-------
cifti_morph : :obj:`list` of :obj:`str`
Paths of CIFTI dscalar files
cifti_metadata : :obj:`list` of :obj:`str`
Paths to JSON files containing metadata corresponding to ``cifti_morph``
"""
import templateflow.api as tf
from niworkflows.engine.workflows import LiterateWorkflow as Workflow
from smriprep.interfaces.cifti import GenerateDScalar
workflow = Workflow(name=name)
workflow.__desc__ = f"""\
*Grayordinate* "dscalar" files [@hcppipelines] containing {grayord_density} samples were
also generated using the highest-resolution ``fsaverage`` as an intermediate standardized
surface space.
"""
fslr_density = "32k" if grayord_density == "91k" else "59k"
inputnode = pe.Node(
niu.IdentityInterface(fields=["subject_id", "subjects_dir"]),
name="inputnode",
)
outputnode = pe.Node(
niu.IdentityInterface(fields=["cifti_morph", "cifti_metadata"]),
name="outputnode",
)
get_surfaces = pe.Node(nio.FreeSurferSource(), name="get_surfaces")
surfmorph_list = pe.Node(
niu.Merge(3, ravel_inputs=True),
name="surfmorph_list",
run_without_submitting=True,
)
surf2surf = pe.MapNode(
fs.SurfaceTransform(target_subject="fsaverage", target_type="gii"),
iterfield=["source_file", "hemi"],
name="surf2surf",
mem_gb=0.01,
)
surf2surf.inputs.hemi = ["lh", "rh"] * 3
# Setup Workbench command. LR ordering for hemi can be assumed, as it is imposed
# by the iterfield of the MapNode in the surface sampling workflow above.
resample = pe.MapNode(
wb.MetricResample(method="ADAP_BARY_AREA", area_metrics=True),
name="resample",
iterfield=[
"in_file",
"out_file",
"new_sphere",
"new_area",
"current_sphere",
"current_area",
],
)
resample.inputs.current_sphere = [
str(
tf.get(
"fsaverage",
hemi=hemi,