-
Notifications
You must be signed in to change notification settings - Fork 21
/
anatomical.py
1427 lines (1272 loc) · 43.5 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
# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*-
# vi: set ft=python sts=4 ts=4 sw=4 et:
"""Anatomical post-processing workflows."""
from nipype import Function, logging
from nipype.interfaces import utility as niu
from nipype.interfaces.ants import CompositeTransformUtil # MB
from nipype.interfaces.freesurfer import MRIsConvert
from nipype.pipeline import engine as pe
from niworkflows.engine.workflows import LiterateWorkflow as Workflow
from pkg_resources import resource_filename as pkgrf
from templateflow.api import get as get_template
from xcp_d.interfaces.ants import (
ApplyTransforms,
CompositeInvTransformUtil,
ConvertTransformFile,
)
from xcp_d.interfaces.bids import DerivativesDataSink
from xcp_d.interfaces.c3 import C3d # TM
from xcp_d.interfaces.nilearn import BinaryMath, Merge
from xcp_d.interfaces.workbench import ( # MB,TM
ApplyAffine,
ApplyWarpfield,
ChangeXfmType,
CiftiSurfaceResample,
ConvertAffine,
SurfaceAverage,
SurfaceGenerateInflated,
SurfaceSphereProjectUnproject,
)
from xcp_d.utils.bids import get_freesurfer_dir, get_freesurfer_sphere
from xcp_d.utils.doc import fill_doc
from xcp_d.workflows.execsummary import (
init_brainsprite_figures_wf,
init_execsummary_anatomical_plots_wf,
)
from xcp_d.workflows.outputs import init_copy_inputs_to_outputs_wf
LOGGER = logging.getLogger("nipype.workflow")
@fill_doc
def init_postprocess_anat_wf(
output_dir,
input_type,
t1w_available,
t2w_available,
target_space,
dcan_qc,
omp_nthreads,
mem_gb,
name="postprocess_anat_wf",
):
"""Copy T1w, segmentation, and, optionally, T2w to the derivative directory.
If necessary, this workflow will also warp the images to standard space.
Workflow Graph
.. workflow::
:graph2use: orig
:simple_form: yes
from xcp_d.workflows.anatomical import init_postprocess_anat_wf
wf = init_postprocess_anat_wf(
output_dir=".",
input_type="fmriprep",
t1w_available=True,
t2w_available=True,
target_space="MNI152NLin6Asym",
dcan_qc=True,
omp_nthreads=1,
mem_gb=0.1,
name="postprocess_anat_wf",
)
Parameters
----------
%(output_dir)s
%(input_type)s
t1w_available : bool
True if a preprocessed T1w is available, False if not.
t2w_available : bool
True if a preprocessed T2w is available, False if not.
target_space : :obj:`str`
Target NIFTI template for T1w.
%(dcan_qc)s
%(omp_nthreads)s
%(mem_gb)s
%(name)s
Default is "postprocess_anat_wf".
Inputs
------
t1w : :obj:`str`
Path to the preprocessed T1w file.
This file may be in standard space or native T1w space.
t2w : :obj:`str` or None
Path to the preprocessed T2w file.
This file may be in standard space or native T1w space.
anat_dseg : :obj:`str`
Path to the T1w segmentation file.
%(anat_to_template_xfm)s
We need to use MNI152NLin6Asym for the template.
template : :obj:`str`
The target template.
Outputs
-------
t1w : :obj:`str`
Path to the preprocessed T1w file in standard space.
t2w : :obj:`str` or None
Path to the preprocessed T2w file in standard space.
"""
workflow = Workflow(name=name)
inputnode = pe.Node(
niu.IdentityInterface(
fields=[
"t1w",
"t2w",
"anat_dseg",
"anat_to_template_xfm",
"template",
]
),
name="inputnode",
)
outputnode = pe.Node(
niu.IdentityInterface(fields=["t1w", "t2w"]),
name="outputnode",
)
# Split cohort out of the space for MNIInfant templates.
cohort = None
if "+" in target_space:
target_space, cohort = target_space.split("+")
template_file = str(
get_template(template=target_space, cohort=cohort, resolution=1, desc=None, suffix="T1w")
)
inputnode.inputs.template = template_file
ds_anat_dseg_std = pe.Node(
DerivativesDataSink(
base_directory=output_dir,
space=target_space,
cohort=cohort,
extension=".nii.gz",
),
name="ds_anat_dseg_std",
run_without_submitting=False,
)
# fmt:off
workflow.connect([(inputnode, ds_anat_dseg_std, [("anat_dseg", "source_file")])])
# fmt:on
if t1w_available:
ds_t1w_std = pe.Node(
DerivativesDataSink(
base_directory=output_dir,
space=target_space,
cohort=cohort,
extension=".nii.gz",
),
name="ds_t1w_std",
run_without_submitting=False,
)
# fmt:off
workflow.connect([
(inputnode, ds_t1w_std, [("t1w", "source_file")]),
(ds_t1w_std, outputnode, [("out_file", "t1w")]),
])
# fmt:on
if t2w_available:
ds_t2w_std = pe.Node(
DerivativesDataSink(
base_directory=output_dir,
space=target_space,
cohort=cohort,
extension=".nii.gz",
),
name="ds_t2w_std",
run_without_submitting=False,
)
# fmt:off
workflow.connect([
(inputnode, ds_t2w_std, [("t2w", "source_file")]),
(ds_t2w_std, outputnode, [("out_file", "t2w")]),
])
# fmt:on
if input_type in ("dcan", "hcp"):
# Assume that the T1w, T1w segmentation, and T2w files are in standard space,
# but don't have the "space" entity, for the "dcan" and "hcp" derivatives.
# This is a bug, and the converted filenames are inaccurate, so we have this
# workaround in place.
# fmt:off
workflow.connect([(inputnode, ds_anat_dseg_std, [("anat_dseg", "in_file")])])
# fmt:on
if t1w_available:
# fmt:off
workflow.connect([(inputnode, ds_t1w_std, [("t1w", "in_file")])])
# fmt:on
if t2w_available:
# fmt:off
workflow.connect([(inputnode, ds_t2w_std, [("t2w", "in_file")])])
# fmt:on
else:
warp_anat_dseg_to_template = pe.Node(
ApplyTransforms(
num_threads=2,
interpolation="GenericLabel",
input_image_type=3,
dimension=3,
),
name="warp_anat_dseg_to_template",
mem_gb=mem_gb,
n_procs=omp_nthreads,
)
# fmt:off
workflow.connect([
(inputnode, warp_anat_dseg_to_template, [
("anat_dseg", "input_image"),
("anat_to_template_xfm", "transforms"),
("template", "reference_image"),
]),
(warp_anat_dseg_to_template, ds_anat_dseg_std, [("output_image", "in_file")]),
])
# fmt:on
if t1w_available:
# Warp the native T1w-space T1w, T1w segmentation, and T2w files to standard space.
warp_t1w_to_template = pe.Node(
ApplyTransforms(
num_threads=2,
interpolation="LanczosWindowedSinc",
input_image_type=3,
dimension=3,
),
name="warp_t1w_to_template",
mem_gb=mem_gb,
n_procs=omp_nthreads,
)
# fmt:off
workflow.connect([
(inputnode, warp_t1w_to_template, [
("t1w", "input_image"),
("anat_to_template_xfm", "transforms"),
("template", "reference_image"),
]),
(warp_t1w_to_template, ds_t1w_std, [("output_image", "in_file")]),
])
# fmt:on
if t2w_available:
warp_t2w_to_template = pe.Node(
ApplyTransforms(
num_threads=2,
interpolation="LanczosWindowedSinc",
input_image_type=3,
dimension=3,
),
name="warp_t2w_to_template",
mem_gb=mem_gb,
n_procs=omp_nthreads,
)
# fmt:off
workflow.connect([
(inputnode, warp_t2w_to_template, [
("t2w", "input_image"),
("anat_to_template_xfm", "transforms"),
("template", "reference_image"),
]),
(warp_t2w_to_template, ds_t2w_std, [("output_image", "in_file")]),
])
# fmt:on
if dcan_qc:
execsummary_anatomical_plots_wf = init_execsummary_anatomical_plots_wf(
t1w_available=t1w_available,
t2w_available=t2w_available,
output_dir=output_dir,
name="execsummary_anatomical_plots_wf",
)
# fmt:off
workflow.connect([
(inputnode, execsummary_anatomical_plots_wf, [("template", "inputnode.template")]),
])
# fmt:on
if t1w_available:
# fmt:off
workflow.connect([
(ds_t1w_std, execsummary_anatomical_plots_wf, [("out_file", "inputnode.t1w")]),
])
# fmt:on
if t2w_available:
# fmt:off
workflow.connect([
(ds_t2w_std, execsummary_anatomical_plots_wf, [("out_file", "inputnode.t2w")]),
])
# fmt:on
return workflow
@fill_doc
def init_postprocess_surfaces_wf(
fmri_dir,
subject_id,
dcan_qc,
process_surfaces,
mesh_available,
standard_space_mesh,
shape_available,
output_dir,
t1w_available,
t2w_available,
mem_gb,
omp_nthreads,
name="postprocess_surfaces_wf",
):
"""Postprocess surfaces.
Workflow Graph
.. workflow::
:graph2use: orig
:simple_form: yes
from xcp_d.workflows.anatomical import init_postprocess_surfaces_wf
wf = init_postprocess_surfaces_wf(
fmri_dir=".",
subject_id="01",
dcan_qc=True,
process_surfaces=True,
mesh_available=True,
standard_space_mesh=False,
shape_available=True,
output_dir=".",
t1w_available=True,
t2w_available=True,
mem_gb=0.1,
omp_nthreads=1,
name="postprocess_surfaces_wf",
)
Parameters
----------
fmri_dir
subject_id
%(dcan_qc)s
process_surfaces : bool
mesh_available : bool
standard_space_mesh : bool
shape_available : bool
%(output_dir)s
t1w_available : bool
True if a T1w image is available.
t2w_available : bool
True if a T2w image is available.
%(mem_gb)s
%(omp_nthreads)s
%(name)s
Default is "postprocess_surfaces_wf".
Inputs
------
t1w
Preprocessed T1w file. May be in native or standard space.
t2w
Preprocessed T2w file. May be in native or standard space.
%(anat_to_template_xfm)s
%(template_to_anat_xfm)s
lh_pial_surf, rh_pial_surf
lh_wm_surf, rh_wm_surf
lh_sulcal_depth, rh_sulcal_depth
lh_sulcal_curv, rh_sulcal_curv
lh_cortical_thickness, rh_cortical_thickness
"""
workflow = Workflow(name=name)
inputnode = pe.Node(
niu.IdentityInterface(
fields=[
"t1w",
"t2w",
"anat_to_template_xfm",
"template_to_anat_xfm",
"lh_pial_surf",
"rh_pial_surf",
"lh_wm_surf",
"rh_wm_surf",
"lh_sulcal_depth",
"rh_sulcal_depth",
"lh_sulcal_curv",
"rh_sulcal_curv",
"lh_cortical_thickness",
"rh_cortical_thickness",
],
),
name="inputnode",
)
if dcan_qc and mesh_available:
# Plot the white and pial surfaces on the brain in a brainsprite figure.
brainsprite_wf = init_brainsprite_figures_wf(
output_dir=output_dir,
t1w_available=t1w_available,
t2w_available=t2w_available,
omp_nthreads=omp_nthreads,
mem_gb=mem_gb,
)
# fmt:off
workflow.connect([
(inputnode, brainsprite_wf, [
("t1w", "inputnode.t1w"),
("t2w", "inputnode.t2w"),
]),
])
# fmt:on
if (not process_surfaces) or (mesh_available and standard_space_mesh):
# Use original surfaces for brainsprite.
# For fMRIPrep derivatives, this will be the native-space surfaces.
# For DCAN/HCP derivatives, it will be standard-space surfaces.
# fmt:off
workflow.connect([
(inputnode, brainsprite_wf, [
("lh_pial_surf", "inputnode.lh_pial_surf"),
("rh_pial_surf", "inputnode.rh_pial_surf"),
("lh_wm_surf", "inputnode.lh_wm_surf"),
("rh_wm_surf", "inputnode.rh_wm_surf"),
]),
])
# fmt:on
if not process_surfaces:
# Return early, as all other steps require process_surfaces.
return workflow
if shape_available or (mesh_available and standard_space_mesh):
# At least some surfaces are already in fsLR space and must be copied,
# without modification, to the output directory.
copy_std_surfaces_to_datasink = init_copy_inputs_to_outputs_wf(
output_dir=output_dir,
name="copy_std_surfaces_to_datasink",
)
if shape_available:
# fmt:off
workflow.connect([
(inputnode, copy_std_surfaces_to_datasink, [
("lh_sulcal_depth", "inputnode.lh_sulcal_depth"),
("rh_sulcal_depth", "inputnode.rh_sulcal_depth"),
("lh_sulcal_curv", "inputnode.lh_sulcal_curv"),
("rh_sulcal_curv", "inputnode.rh_sulcal_curv"),
("lh_cortical_thickness", "inputnode.lh_cortical_thickness"),
("rh_cortical_thickness", "inputnode.rh_cortical_thickness"),
]),
])
# fmt:on
if mesh_available:
# Generate and output HCP-style surface files.
hcp_surface_wfs = {
hemi: init_generate_hcp_surfaces_wf(
output_dir=output_dir,
mem_gb=mem_gb,
omp_nthreads=omp_nthreads,
name=f"{hemi}_generate_hcp_surfaces_wf",
)
for hemi in ["lh", "rh"]
}
# fmt:off
workflow.connect([
(inputnode, hcp_surface_wfs["lh"], [
("lh_pial_surf", "inputnode.name_source"),
]),
(inputnode, hcp_surface_wfs["rh"], [
("rh_pial_surf", "inputnode.name_source"),
]),
])
# fmt:on
if mesh_available and standard_space_mesh:
# Mesh files are already in fsLR.
# fmt:off
workflow.connect([
(inputnode, copy_std_surfaces_to_datasink, [
("lh_pial_surf", "inputnode.lh_pial_surf"),
("rh_pial_surf", "inputnode.rh_pial_surf"),
("lh_wm_surf", "inputnode.lh_wm_surf"),
("rh_wm_surf", "inputnode.rh_wm_surf"),
]),
(inputnode, hcp_surface_wfs["lh"], [
("lh_pial_surf", "inputnode.pial_surf"),
("lh_wm_surf", "inputnode.wm_surf"),
]),
(inputnode, hcp_surface_wfs["rh"], [
("rh_pial_surf", "inputnode.pial_surf"),
("rh_wm_surf", "inputnode.wm_surf"),
]),
])
# fmt:on
elif mesh_available:
# Mesh files are in fsnative and must be warped to fsLR.
warp_surfaces_to_template_wf = init_warp_surfaces_to_template_wf(
fmri_dir=fmri_dir,
subject_id=subject_id,
output_dir=output_dir,
omp_nthreads=omp_nthreads,
mem_gb=mem_gb,
name="warp_surfaces_to_template_wf",
)
# fmt:off
workflow.connect([
(inputnode, warp_surfaces_to_template_wf, [
("lh_pial_surf", "inputnode.lh_pial_surf"),
("rh_pial_surf", "inputnode.rh_pial_surf"),
("lh_wm_surf", "inputnode.lh_wm_surf"),
("rh_wm_surf", "inputnode.rh_wm_surf"),
("anat_to_template_xfm", "inputnode.anat_to_template_xfm"),
("template_to_anat_xfm", "inputnode.template_to_anat_xfm"),
]),
(warp_surfaces_to_template_wf, hcp_surface_wfs["lh"], [
("outputnode.lh_pial_surf", "inputnode.pial_surf"),
("outputnode.lh_wm_surf", "inputnode.wm_surf"),
]),
(warp_surfaces_to_template_wf, hcp_surface_wfs["rh"], [
("outputnode.rh_pial_surf", "inputnode.pial_surf"),
("outputnode.rh_wm_surf", "inputnode.wm_surf"),
]),
])
# fmt:on
if dcan_qc:
# Use standard-space T1w and surfaces for brainsprite.
# fmt:off
workflow.connect([
(warp_surfaces_to_template_wf, brainsprite_wf, [
("outputnode.lh_pial_surf", "inputnode.lh_pial_surf"),
("outputnode.rh_pial_surf", "inputnode.rh_pial_surf"),
("outputnode.lh_wm_surf", "inputnode.lh_wm_surf"),
("outputnode.rh_wm_surf", "inputnode.rh_wm_surf"),
]),
])
# fmt:on
elif not shape_available:
raise ValueError(
"No surfaces found. "
"Surfaces are required if `--warp-surfaces-native2std` is enabled."
)
return workflow
@fill_doc
def init_warp_surfaces_to_template_wf(
fmri_dir,
subject_id,
output_dir,
omp_nthreads,
mem_gb,
name="warp_surfaces_to_template_wf",
):
"""Transform surfaces from native to standard fsLR-32k space.
Workflow Graph
.. workflow::
:graph2use: orig
:simple_form: yes
from xcp_d.workflows.anatomical import init_warp_surfaces_to_template_wf
wf = init_warp_surfaces_to_template_wf(
fmri_dir=".",
subject_id="01",
output_dir=".",
omp_nthreads=1,
mem_gb=0.1,
name="warp_surfaces_to_template_wf",
)
Parameters
----------
%(fmri_dir)s
%(subject_id)s
%(output_dir)s
%(omp_nthreads)s
%(mem_gb)s
%(name)s
Default is "warp_surfaces_to_template_wf".
Inputs
------
%(anat_to_template_xfm)s
The template in question should match the volumetric space of the BOLD CIFTI files
being processed by the main xcpd workflow.
For example, MNI152NLin6Asym for fsLR-space CIFTIs.
%(template_to_anat_xfm)s
The template in question should match the volumetric space of the BOLD CIFTI files
being processed by the main xcpd workflow.
For example, MNI152NLin6Asym for fsLR-space CIFTIs.
lh_pial_surf, rh_pial_surf : :obj:`str`
Left- and right-hemisphere pial surface files in fsnative space.
lh_wm_surf, rh_wm_surf : :obj:`str`
Left- and right-hemisphere smoothed white matter surface files in fsnative space.
Outputs
-------
lh_pial_surf, rh_pial_surf : :obj:`str`
Left- and right-hemisphere pial surface files, in standard space.
lh_wm_surf, rh_wm_surf : :obj:`str`
Left- and right-hemisphere smoothed white matter surface files, in standard space.
"""
workflow = Workflow(name=name)
inputnode = pe.Node(
niu.IdentityInterface(
fields=[
# transforms
"anat_to_template_xfm",
"template_to_anat_xfm",
# surfaces
"lh_pial_surf",
"rh_pial_surf",
"lh_wm_surf",
"rh_wm_surf",
],
),
name="inputnode",
)
# Feed the standard-space pial and white matter surfaces to the outputnode for the brainsprite
# and the HCP-surface generation workflow.
outputnode = pe.Node(
niu.IdentityInterface(
fields=[
"lh_pial_surf",
"rh_pial_surf",
"lh_wm_surf",
"rh_wm_surf",
],
),
name="outputnode",
)
# Warp the surfaces to space-fsLR, den-32k.
get_freesurfer_dir_node = pe.Node(
Function(
function=get_freesurfer_dir,
input_names=["fmri_dir"],
output_names=["freesurfer_path"],
),
name="get_freesurfer_dir_node",
)
get_freesurfer_dir_node.inputs.fmri_dir = fmri_dir
# First, we create the Connectome WorkBench-compatible transform files.
update_xfm_wf = init_ants_xfm_to_fsl_wf(
mem_gb=mem_gb,
omp_nthreads=omp_nthreads,
name="update_xfm_wf",
)
# fmt:off
workflow.connect([
(inputnode, update_xfm_wf, [
("anat_to_template_xfm", "inputnode.anat_to_template_xfm"),
("template_to_anat_xfm", "inputnode.template_to_anat_xfm"),
]),
])
# fmt:on
# TODO: It would be nice to replace this for loop with MapNodes or iterables some day.
for hemi in ["L", "R"]:
hemi_label = f"{hemi.lower()}h"
# Place the surfaces in a single node.
collect_surfaces = pe.Node(
niu.Merge(2),
name=f"collect_surfaces_{hemi_label}",
)
# fmt:off
# NOTE: Must match order of split_up_surfaces_fsLR_32k.
workflow.connect([
(inputnode, collect_surfaces, [
(f"{hemi_label}_pial_surf", "in1"),
(f"{hemi_label}_wm_surf", "in2"),
]),
])
# fmt:on
apply_transforms_wf = init_warp_one_hemisphere_wf(
participant_id=subject_id,
hemisphere=hemi,
mem_gb=mem_gb,
omp_nthreads=omp_nthreads,
name=f"{hemi_label}_apply_transforms_wf",
)
# fmt:off
workflow.connect([
(get_freesurfer_dir_node, apply_transforms_wf, [
("freesurfer_path", "inputnode.freesurfer_path"),
]),
(update_xfm_wf, apply_transforms_wf, [
("outputnode.merged_warpfield", "inputnode.merged_warpfield"),
("outputnode.merged_inv_warpfield", "inputnode.merged_inv_warpfield"),
("outputnode.world_xfm", "inputnode.world_xfm"),
]),
(collect_surfaces, apply_transforms_wf, [("out", "inputnode.hemi_files")]),
])
# fmt:on
# Split up the surfaces
# NOTE: Must match order of collect_surfaces
split_up_surfaces_fsLR_32k = pe.Node(
niu.Split(
splits=[
1, # pial
1, # wm
],
squeeze=True,
),
name=f"split_up_surfaces_fsLR_32k_{hemi_label}",
)
# fmt:off
workflow.connect([
(apply_transforms_wf, split_up_surfaces_fsLR_32k, [
("outputnode.warped_hemi_files", "inlist"),
]),
(split_up_surfaces_fsLR_32k, outputnode, [
("out1", f"{hemi_label}_pial_surf"),
("out2", f"{hemi_label}_wm_surf"),
]),
])
# fmt:on
ds_standard_space_surfaces = pe.MapNode(
DerivativesDataSink(
base_directory=output_dir,
space="fsLR",
den="32k",
extension=".surf.gii", # the extension is taken from the in_file by default
),
name=f"ds_standard_space_surfaces_{hemi_label}",
run_without_submitting=True,
mem_gb=1,
iterfield=["in_file", "source_file"],
)
# fmt:off
workflow.connect([
(collect_surfaces, ds_standard_space_surfaces, [("out", "source_file")]),
(apply_transforms_wf, ds_standard_space_surfaces, [
("outputnode.warped_hemi_files", "in_file"),
]),
])
# fmt:on
return workflow
@fill_doc
def init_generate_hcp_surfaces_wf(
output_dir,
mem_gb,
omp_nthreads,
name="generate_hcp_surfaces_wf",
):
"""Generate midthickness, inflated, and very-inflated HCP-style surfaces.
Workflow Graph
.. workflow::
:graph2use: orig
:simple_form: yes
from xcp_d.workflows.anatomical import init_generate_hcp_surfaces_wf
wf = init_generate_hcp_surfaces_wf(
output_dir=".",
mem_gb=0.1,
omp_nthreads=1,
name="generate_hcp_surfaces_wf",
)
Parameters
----------
%(output_dir)s
%(mem_gb)s
%(omp_nthreads)s
%(name)s
Default is "generate_hcp_surfaces_wf".
Inputs
------
name_source : :obj:`str`
Path to the file that will be used as the source_file for datasinks.
pial_surf : :obj:`str`
The surface file to inflate.
wm_surf : :obj:`str`
The surface file to inflate.
"""
workflow = Workflow(name=name)
inputnode = pe.Node(
niu.IdentityInterface(
fields=[
"name_source",
"pial_surf",
"wm_surf",
],
),
name="inputnode",
)
generate_midthickness = pe.Node(
SurfaceAverage(),
name="generate_midthickness",
mem_gb=mem_gb,
n_procs=omp_nthreads,
)
# fmt:off
workflow.connect([
(inputnode, generate_midthickness, [
("pial_surf", "surface_in1"),
("wm_surf", "surface_in2"),
]),
])
# fmt:on
ds_midthickness = pe.Node(
DerivativesDataSink(
base_directory=output_dir,
check_hdr=False,
space="fsLR",
den="32k",
desc="hcp",
suffix="midthickness",
extension=".surf.gii",
),
name="ds_midthickness",
run_without_submitting=False,
mem_gb=2,
)
# fmt:off
workflow.connect([
(inputnode, ds_midthickness, [("name_source", "source_file")]),
(generate_midthickness, ds_midthickness, [("out_file", "in_file")]),
])
# fmt:on
# Generate (very-)inflated surface from standard-space midthickness surface.
inflate_surface = pe.Node(
SurfaceGenerateInflated(iterations_scale_value=0.75),
mem_gb=mem_gb,
omp_nthreads=omp_nthreads,
name="inflate_surface",
)
# fmt:off
workflow.connect([
(generate_midthickness, inflate_surface, [("out_file", "anatomical_surface_in")]),
])
# fmt:on
ds_inflated = pe.Node(
DerivativesDataSink(
base_directory=output_dir,
check_hdr=False,
space="fsLR",
den="32k",
desc="hcp",
suffix="inflated",
extension=".surf.gii",
),
name="ds_inflated",
run_without_submitting=False,
mem_gb=2,
)
# fmt:off
workflow.connect([
(inputnode, ds_inflated, [("name_source", "source_file")]),
(inflate_surface, ds_inflated, [("inflated_out_file", "in_file")]),
])
# fmt:on
ds_vinflated = pe.Node(
DerivativesDataSink(
base_directory=output_dir,
check_hdr=False,
space="fsLR",
den="32k",
desc="hcp",
suffix="vinflated",
extension=".surf.gii",
),
name="ds_vinflated",
run_without_submitting=False,
mem_gb=2,
)
# fmt:off
workflow.connect([
(inputnode, ds_vinflated, [("name_source", "source_file")]),
(inflate_surface, ds_vinflated, [("very_inflated_out_file", "in_file")]),
])
# fmt:on
return workflow
@fill_doc
def init_ants_xfm_to_fsl_wf(mem_gb, omp_nthreads, name="ants_xfm_to_fsl_wf"):
"""Modify ANTS-style fMRIPrep transforms to work with Connectome Workbench/FSL FNIRT.
Workflow Graph
.. workflow::
:graph2use: orig
:simple_form: yes
from xcp_d.workflows.anatomical import init_ants_xfm_to_fsl_wf
wf = init_ants_xfm_to_fsl_wf(
mem_gb=0.1,
omp_nthreads=1,
name="ants_xfm_to_fsl_wf",
)
Parameters
----------
%(mem_gb)s
%(omp_nthreads)s
%(name)s
Default is "ants_xfm_to_fsl_wf".
Inputs
------
anat_to_template_xfm
ANTS/fMRIPrep-style H5 transform from T1w image to template.
template_to_anat_xfm
ANTS/fMRIPrep-style H5 transform from template to T1w image.
Outputs
-------
world_xfm
TODO: Add description.
merged_warpfield
TODO: Add description.
merged_inv_warpfield
TODO: Add description.
"""
workflow = Workflow(name=name)
inputnode = pe.Node(
niu.IdentityInterface(fields=["anat_to_template_xfm", "template_to_anat_xfm"]),
name="inputnode",
)
outputnode = pe.Node(
niu.IdentityInterface(fields=["world_xfm", "merged_warpfield", "merged_inv_warpfield"]),
name="outputnode",
)
# Now we can start the actual workflow.
# use ANTs CompositeTransformUtil to separate the .h5 into affine and warpfield xfms
disassemble_h5 = pe.Node(
CompositeTransformUtil(
process="disassemble",
output_prefix="T1w_to_MNI152NLin6Asym",
),
name="disassemble_h5",
mem_gb=mem_gb,
n_procs=omp_nthreads,
) # MB
# fmt:off