/
dwi_preprocessing_phase_difference_fieldmap3_utils.py
1670 lines (1402 loc) · 72.5 KB
/
dwi_preprocessing_phase_difference_fieldmap3_utils.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
#!/usr/bin/python
import nipype.interfaces.ants as ants
import nipype.interfaces.fsl as fsl
import nipype.interfaces.utility as niu
import nipype.pipeline.engine as pe
from nipype.workflows.dmri.fsl.utils import add_empty_vol
from nipype.workflows.dmri.fsl.utils import cleanup_edge_pipeline
from nipype.workflows.dmri.fsl.utils import demean_image
from nipype.workflows.dmri.fsl.utils import insert_mat
from nipype.workflows.dmri.fsl.utils import rads2radsec
from nipype.workflows.dmri.fsl.utils import rotate_bvecs
from nipype.workflows.dmri.fsl.utils import siemens2rads
from nipype.workflows.dmri.fsl.utils import vsm2warp
from nipype.workflows.dmri.fsl.utils import enhance
def diffusion_preprocessing_phasediff_fieldmap(in_dwi, in_bvals, in_bvecs, in_fmap_magnitude, in_fmap_phasediff,
delta_te, echospacing, enc_dir, caps_dir,
register_fmap_on_b0, register_b0_on_b0, work_dir, n_procs,
name='Diffusion_preprocessing_phasediff_fmb'):
import dwi_preprocessing_phase_difference_fieldmap3_utils as utils
import nipype.interfaces.utility as nutil
import nipype.pipeline.engine as npe
from nipype.workflows.dmri.fsl.utils import apply_all_corrections
import nipype.interfaces.io as nio
from os.path import join
pre = utils.prepare_data(register_b0_on_b0=register_b0_on_b0)
pre.inputs.inputnode.in_file = in_dwi
pre.inputs.inputnode.in_bvals = in_bvals
pre.inputs.inputnode.in_bvecs = in_bvecs
hmc = utils.hmc_pipeline(name='HeadMotionCorrection')
hmc.inputs.inputnode.ref_num = 0
sdc = utils.sdc_fmb(name='EPICorrectionWithPhaseDiffFmap',
fugue_params=dict(smooth3d=2.0),
register_fmap_on_b0=register_fmap_on_b0)
sdc.inputs.inputnode.in_fmap_magnitude = in_fmap_magnitude
sdc.inputs.inputnode.in_fmap_phasediff = in_fmap_phasediff
sdc.inputs.inputnode.delta_te = delta_te
sdc.inputs.inputnode.echospacing = echospacing
sdc.inputs.inputnode.enc_dir = enc_dir
ecc = utils.ecc_pipeline(name='EddyCurrentCorrection')
unwarp = apply_all_corrections(name='ApplyAllCorrections')
bias = utils.remove_bias(name='RemoveBias')
outputnode = npe.Node(nutil.IdentityInterface(
fields=['out_preprocessed_dwi', 'out_bvecs', 'out_bvals', 'out_b0_mask']),
name='outputnode')
datasink = npe.Node(nio.DataSink(), name='datasink')
# get the participant_id and session_id from the input in_dwi
participant_id = (in_dwi.split('/')[-1]).split('_')[0]
session_id = (in_dwi.split('/')[-1]).split('_')[1]
caps_identifier = participant_id + '_' + session_id
datasink.inputs.base_directory = join(caps_dir, 'subjects', participant_id, session_id, 'dwi')
datasink.inputs.substitutions = [
('vol0000_warp_maths_thresh_merged_roi_brain_mask.nii.gz', caps_identifier + '_b0Mask.nii.gz'),
('vol0000_maths_thresh_merged.nii.gz', caps_identifier + '_dwi.nii.gz'),
('bvecs_rotated.bvec', caps_identifier + '_dwi.bvec'),
('bvals', caps_identifier + '_dwi.bval'),
('merged_files.nii.gz', caps_identifier + '_dwi.nii.gz'),
('merged_files.nii.gz', caps_identifier + '_dwi.nii.gz'),
('merged_files.nii.gz', caps_identifier + '_dwi.nii.gz'),
('vol0000_unwarped_thresh_merged.nii.gz', caps_identifier + '_unwarped_thresh_merged.nii.gz'),
('vol0000_unwarped_thresh_merged_concatwarp.nii.gz',
caps_identifier + '_unwarped_thresh_merged_concatwarp.nii.gz'),
('vol0', caps_identifier + '_hmc-dwi-0'),
('_flirt.mat', '.mat')
]
wf = npe.Workflow(name=name)
wf.base_dir = work_dir
wf.connect([
# Head-motion correction:
(pre, hmc, [('outputnode.dwi_b0_merge', 'inputnode.in_file'),
('outputnode.out_bvals', 'inputnode.in_bval'),
('outputnode.out_bvecs', 'inputnode.in_bvec')]),
(pre, hmc, [('outputnode.mask_b0', 'inputnode.in_mask')]),
# Susceptibility distortion correction:
(hmc, sdc, [('outputnode.out_file', 'inputnode.in_file')]),
(hmc, sdc, [('outputnode.mask_B0', 'inputnode.in_mask')]),
# Eddy-currents correction:
(hmc, ecc, [('outputnode.out_xfms', 'inputnode.in_xfms')]),
(pre, ecc, [('outputnode.out_bvals', 'inputnode.in_bval')]),
(pre, ecc, [('outputnode.dwi_b0_merge', 'inputnode.in_file')]),
(pre, ecc, [('outputnode.mask_b0', 'inputnode.in_mask')]),
# Apply all corrections:
(pre, unwarp, [('outputnode.dwi_b0_merge', 'inputnode.in_dwi')]),
(hmc, unwarp, [('outputnode.out_xfms', 'inputnode.in_hmc')]),
(ecc, unwarp, [('outputnode.out_xfms', 'inputnode.in_ecc')]),
(sdc, unwarp, [('outputnode.out_warp', 'inputnode.in_sdc')]),
# Remove bias:
(unwarp, bias, [('outputnode.out_file', 'inputnode.in_file')]),
# (mask_b0, bias, [('mask_file', 'inputnode.in_mask')]),
# # Outputnode:
(hmc, outputnode, [('outputnode.out_bvec', 'out_bvecs')]),
(pre, outputnode, [('outputnode.out_bvals', 'out_bvals')]),
(bias, outputnode, [('outputnode.out_file', 'out_preprocessed_dwi')]),
(bias, outputnode, [('outputnode.b0_mask', 'out_b0_mask')]),
# Datasink:
(hmc, datasink, [('outputnode.out_xfms', 'preprocessing.head-motion-correction.@out_matrices')]),
(pre, datasink, [('outputnode.out_bvals', 'preprocessing.head-motion-correction.@out_bval')]),
(hmc, datasink, [('outputnode.out_bvec', 'preprocessing.head-motion-correction.@out_bvec')]),
(hmc, datasink, [('outputnode.out_file', 'preprocessing.head-motion-correction.@out_file')]),
(ecc, datasink, [('outputnode.out_file', 'preprocessing.eddy-currents-correction.@out_file')]),
(sdc, datasink,
[('outputnode.out_file', 'preprocessing.susceptibility-distortion-correction.@out_file')]),
(
sdc, datasink, [('outputnode.out_vsm', 'preprocessing.susceptibility-distortion-correction.@out_vsm')]),
(sdc, datasink,
[('outputnode.out_warp', 'preprocessing.susceptibility-distortion-correction.@out_warp')]),
(sdc, datasink, [('outputnode.out_registered_fmap',
'preprocessing.susceptibility-distortion-correction.@out_registered_fmap')]),
(pre, datasink, [('outputnode.out_bvals', 'preprocessing.@out_bvals')]),
(hmc, datasink, [('outputnode.out_bvec', 'preprocessing.@out_bvecs')]),
(bias, datasink, [('outputnode.b0_mask', 'preprocessing.@out_b0_mask')]),
(bias, datasink, [('outputnode.out_file', 'preprocessing.@out_preprocessed_dwi')])
])
return wf.run(plugin='MultiProc', plugin_args={'n_procs': n_procs})
# return wf
def prepare_data(register_b0_on_b0, name='prepare_data', low_bval=5.0):
"""
Create a pipelines that prepare the data for further corrections. This pipelines coregister the B0 images and then average it in order
to obtain only one average B0 images. The bvectors and bvales are update according to the modifications.
Parameters
----------
num_b0s : INT
Mandatory input. Number of the B0 volumes in the dataset.
Inputnode
---------
in_file : FILE
Mandatory input. Input dwi file.
in_bvecs : FILE
Mandatory input. Vector file of the diffusion directions of the dwi dataset.
in_bvals : FILE
Mandatory input. B values file.
Outputnode
----------
outputnode.dwi_b0_merge - average of B0 images merged to the DWIs
outputnode.b0_reference - average of the B0 images or the only B0 image
outputnode.out_bvec - updated gradient vectors table
outputnode.out_bvals - updated gradient values table
outputnode.mask_b0 - Binary mask obtained from the average of the B0 images
"""
import clinica.pipelines.dwi.dwi_preprocessing_utils as dwi_utils
inputnode = pe.Node(interface=niu.IdentityInterface(fields=["in_file", "in_bvecs", "in_bvals"]), name="inputnode")
b0_dwi_split = pe.Node(niu.Function(input_names=['in_file', 'in_bvals', 'in_bvecs'], output_names=['out_b0', 'out_dwi', 'out_bvals', 'out_bvecs'], function=dwi_utils.b0_dwi_split), name='b0_dwi_split')
b0_flirt = dwi_utils.b0_flirt_pipeline(name='b0_co_registration')
b0_avg = pe.Node(niu.Function(input_names=['in_file'], output_names=['out_file'], function=dwi_utils.b0_average), name='b0_average')
mask_b0 = pe.Node(fsl.BET(frac=0.3, mask=True, robust=True), name='mask_b0')
insert_b0_into_dwi = pe.Node(niu.Function(input_names=['in_b0', 'in_dwi', 'in_bvals', 'in_bvecs'], output_names=['out_dwi', 'out_bvals', 'out_bvecs'],
function=dwi_utils.insert_b0_into_dwi), name='insert_b0avg_into_dwi')
outputnode = pe.Node(niu.IdentityInterface(fields=['mask_b0', 'b0_reference','out_bvecs', 'dwi_b0_merge', 'out_bvals' ]), name='outputnode')
wf = pe.Workflow(name=name)
if register_b0_on_b0:
wf.connect([
(inputnode, b0_dwi_split, [('in_bvals', 'in_bvals'),
('in_bvecs', 'in_bvecs'),
('in_file', 'in_file')]),
(b0_dwi_split, b0_flirt, [('out_b0', 'inputnode.in_file')]),
(b0_flirt, b0_avg, [('outputnode.out_file', 'in_file')]),
(b0_avg, insert_b0_into_dwi, [('out_file', 'in_b0')]),
(b0_avg, mask_b0, [('out_file', 'in_file')]),
(b0_dwi_split, insert_b0_into_dwi, [('out_dwi','in_dwi'),
('out_bvals','in_bvals'),
('out_bvecs','in_bvecs')]),
(insert_b0_into_dwi, outputnode, [('out_dwi','dwi_b0_merge'),
('out_bvals','out_bvals'),
('out_bvecs','out_bvecs')]),
(mask_b0, outputnode, [('mask_file','mask_b0')]),
(b0_avg, outputnode, [('out_file','b0_reference')])
])
elif register_b0_on_b0 == False:
wf.connect([
(inputnode, b0_dwi_split, [('in_bvals', 'in_bvals'),
('in_bvecs', 'in_bvecs'),
('in_file', 'in_file')]),
(b0_dwi_split, insert_b0_into_dwi, [('out_b0', 'in_b0'),
('out_dwi','in_dwi'),
('out_bvals','in_bvals'),
('out_bvecs','in_bvecs')]),
(b0_dwi_split, mask_b0, [('out_b0', 'in_file')]),
(insert_b0_into_dwi, outputnode, [('out_dwi','dwi_b0_merge'),
('out_bvals','out_bvals'),
('out_bvecs','out_bvecs')]),
(mask_b0, outputnode, [('mask_file','mask_b0')]),
(insert_b0_into_dwi, outputnode, [('out_dwi','b0_reference')])
])
else:
raise()
return wf
def hmc_pipeline(name='motion_correct'):
"""
HMC stands for head-motion correction.
Creates a pipelines that corrects for head motion artifacts in dMRI
sequences. It takes a series of diffusion weighted images and
rigidly co-registers them to one reference image (FLIRT normalised
mutual information). Finally, the `b`-matrix is rotated
accordingly [Leemans09]_ making use of the rotation matrix
obtained by FLIRT.
A list of rigid transformation matrices is provided, so that transforms
can be chained.
This is useful to correct for artifacts with only one interpolation process
and also to compute nuisance regressors as proposed by [Yendiki13]_.
.. warning:: This workflow rotates the `b`-vectors, so please be advised
that not all the dicom converters ensure the consistency between the
resulting nifti orientation and the gradients table (e.g. dcm2nii
checks it).
.. admonition:: References
.. [Leemans09] Leemans A, and Jones DK, `The B-matrix must be rotated
when correcting for subject motion in DTI data
<http://dx.doi.org/10.1002/mrm.21890>`_,
Magn Reson Med. 61(6):1336-49. 2009. doi: 10.1002/mrm.21890.
.. [Yendiki13] Yendiki A et al., `Spurious group differences due to head
motion in a diffusion MRI study
<http://dx.doi.org/10.1016/j.neuroimage.2013.11.027>`_.
Neuroimage. 21(88C):79-90. 2013. doi: 10.1016/j.neuroimage.2013.11.027
Inputnode
---------
in_file : FILE
Mandatory input. Input dwi file.
in_bvec : FILE
Mandatory input. Vector file of the diffusion directions of the dwi dataset.
in_bval : FILE
Mandatory input. B values file.
in_mask : FILE
Mandatory input. Weights mask of reference image (a file with data
range in [0.0, 1.0], indicating the weight of each voxel when computing the metric
ref_num : INT
Optional input. Default=0. Index of the b0 volume that should be taken as reference.
Outputnode
----------
outputnode.out_file - corrected dwi file
outputnode.out_bvec - rotated gradient vectors table
outputnode.out_xfms - list of transformation matrices
"""
from nipype.workflows.data import get_flirt_schedule
from clinica.pipelines.dwi.dwi_preprocessing_utils import merge_volumes_tdim
from clinica.pipelines.dwi.dwi_preprocessing_utils import hmc_split
from clinica.pipelines.dwi.dwi_preprocessing_workflows import dwi_flirt
# params = dict(dof=6, interp='spline', cost='normmi', cost_func='normmi', bins=50, save_log=True, padding_size=10,
# schedule=get_flirt_schedule('hmc'),
# searchr_x=[-4, 4], searchr_y=[-4, 4], searchr_z=[-4, 4], fine_search=1, coarse_search=10 )
# params = dict(dof=6, interp='spline', cost='normmi', cost_func='normmi', save_log=True,
# no_search=True, bgvalue=0, padding_size=10,
# schedule=get_flirt_schedule('hmc'),
# searchr_x=[-5, 5], searchr_y=[-5, 5], searchr_z=[-25, 25])
params = dict(dof=6, bgvalue=0, save_log=True, no_search=True,
# cost='mutualinfo', cost_func='mutualinfo', bins=64,
schedule=get_flirt_schedule('hmc'))
inputnode = pe.Node(niu.IdentityInterface(
fields=['in_file', 'in_bvec', 'in_bval', 'in_mask', 'ref_num']),
name='inputnode')
split = pe.Node(niu.Function(function=hmc_split,
input_names=['in_file', 'in_bval', 'ref_num'],
output_names=['out_ref', 'out_mov', 'out_bval', 'volid']),
name='split_ref_moving')
flirt = dwi_flirt(flirt_param=params)
insmat = pe.Node(niu.Function(input_names=['inlist', 'volid'],
output_names=['out'], function=insert_mat), name='insert_ref_matrix')
rot_bvec = pe.Node(niu.Function(input_names=['in_bvec', 'in_matrix'],
output_names=['out_file'], function=rotate_bvecs),
name='Rotate_Bvec')
merged_volumes = pe.Node(niu.Function(input_names=['in_file1', 'in_file2'], output_names=['out_file'], function=merge_volumes_tdim), name='merge_reference_moving')
outputnode = pe.Node(niu.IdentityInterface(fields=['out_file',
'out_bvec', 'out_xfms', 'mask_B0']), name='outputnode')
wf = pe.Workflow(name=name)
wf.connect([
(inputnode, split, [('in_file', 'in_file'),
('in_bval', 'in_bval'),
('ref_num', 'ref_num')]),
(inputnode, flirt, [('in_mask', 'inputnode.ref_mask')]),
(split, flirt, [('out_ref', 'inputnode.reference'),
('out_mov', 'inputnode.in_file'),
('out_bval', 'inputnode.in_bval')]),
(flirt, insmat, [('outputnode.out_xfms', 'inlist')]),
(split, insmat, [('volid', 'volid')]),
(inputnode, rot_bvec, [('in_bvec', 'in_bvec')]),
(insmat, rot_bvec, [('out', 'in_matrix')]),
(rot_bvec, outputnode, [('out_file', 'out_bvec')]),
(flirt, merged_volumes, [('outputnode.out_ref', 'in_file1'),
('outputnode.out_file', 'in_file2')]),
(merged_volumes, outputnode, [('out_file', 'out_file')]),
(insmat, outputnode, [('out', 'out_xfms')]),
(flirt, outputnode, [('outputnode.out_ref', 'mask_B0')])
])
return wf
def ecc_pipeline(name='eddy_correct'):
"""
ECC stands for Eddy currents correction.
Creates a pipelines that corrects for artifacts induced by Eddy currents in
dMRI sequences.
It takes a series of diffusion weighted images and linearly co-registers
them to one reference image (the average of all b0s in the dataset).
DWIs are also modulated by the determinant of the Jacobian as indicated by
[Jones10]_ and [Rohde04]_.
A list of rigid transformation matrices can be provided, sourcing from a
:func:`.hmc_pipeline` workflow, to initialize registrations in a *motion
free* framework.
A list of affine transformation matrices is available as output, so that
transforms can be chained (discussion
`here <https://github.com/nipy/nipype/pull/530#issuecomment-14505042>`_).
.. admonition:: References
.. [Jones10] Jones DK, `The signal intensity must be modulated by the
determinant of the Jacobian when correcting for eddy currents in
diffusion MRI
<http://cds.ismrm.org/protected/10MProceedings/files/1644_129.pdf>`_,
Proc. ISMRM 18th Annual Meeting, (2010).
.. [Rohde04] Rohde et al., `Comprehensive Approach for Correction of
Motion and Distortion in Diffusion-Weighted MRI
<http://stbb.nichd.nih.gov/pdf/com_app_cor_mri04.pdf>`_, MRM
51:103-114 (2004).
Example
-------
from nipype.workflows.dmri.fsl.artifacts import ecc_pipeline
ecc = ecc_pipeline()
ecc.inputs.inputnode.in_file = 'diffusion.nii'
ecc.inputs.inputnode.in_bval = 'diffusion.bval'
ecc.inputs.inputnode.in_mask = 'mask.nii'
ecc.run() # doctest: +SKIP
Inputs::
inputnode.in_file - input dwi file
inputnode.in_mask - weights mask of reference image (a file with data \
range sin [0.0, 1.0], indicating the weight of each voxel when computing the \
metric.
inputnode.in_bval - b-values table
inputnode.in_xfms - list of matrices to initialize registration (from \
head-motion correction)
Outputs::
outputnode.out_file - corrected dwi file
outputnode.out_xfms - list of transformation matrices
"""
from clinica.pipelines.dwi.dwi_preprocessing_workflows import dwi_flirt
from nipype.workflows.data import get_flirt_schedule
from nipype.workflows.dmri.fsl.utils import extract_bval
from nipype.workflows.dmri.fsl.utils import recompose_xfm
from nipype.workflows.dmri.fsl.utils import recompose_dwi
from nipype.workflows.dmri.fsl.artifacts import _xfm_jacobian
from clinica.pipelines.dwi.dwi_preprocessing_utils import merge_volumes_tdim
# params = dict(dof=12, no_search=True, interp='spline', bgvalue=0,
# schedule=get_flirt_schedule('ecc'))
# params = dict(dof=12, interp='spline', cost='normmi', cost_func='normmi', save_log=True,
# no_search=True, bgvalue=0, padding_size=10,
# schedule=get_flirt_schedule('ecc'),
# searchr_x=[-5, 5], searchr_y=[-5, 5], searchr_z=[-25, 25])
params = dict(dof=12, no_search=True, interp='spline', bgvalue=0,
schedule=get_flirt_schedule('ecc'))
inputnode = pe.Node(niu.IdentityInterface(
fields=['in_file', 'in_bval', 'in_mask', 'in_xfms']), name='inputnode')
getb0 = pe.Node(fsl.ExtractROI(t_min=0, t_size=1), name='get_b0')
pick_dws = pe.Node(niu.Function(
input_names=['in_dwi', 'in_bval', 'b'], output_names=['out_file'],
function=extract_bval), name='extract_dwi')
pick_dws.inputs.b = 'diff'
flirt = dwi_flirt(flirt_param=params, excl_nodiff=True)
mult = pe.MapNode(fsl.BinaryMaths(operation='mul'), name='ModulateDWIs',
iterfield=['in_file', 'operand_value'])
thres = pe.MapNode(fsl.Threshold(thresh=0.0), iterfield=['in_file'],
name='RemoveNegative')
split = pe.Node(fsl.Split(dimension='t'), name='SplitDWIs')
get_mat = pe.Node(niu.Function(
input_names=['in_bval', 'in_xfms'], output_names=['out_files'],
function=recompose_xfm), name='GatherMatrices')
merge = pe.Node(niu.Function(
input_names=['in_dwi', 'in_bval', 'in_corrected'],
output_names=['out_file'], function=recompose_dwi), name='MergeDWIs')
merged_volumes = pe.Node(niu.Function(input_names=['in_file1', 'in_file2'], output_names=['out_file'], function=merge_volumes_tdim), name='merge_enhanced_ref_dwis')
outputnode = pe.Node(niu.IdentityInterface(
fields=['out_file', 'out_xfms']), name='outputnode')
wf = pe.Workflow(name=name)
wf.connect([
(inputnode, getb0, [('in_file', 'in_file')]),
(inputnode, pick_dws, [('in_file', 'in_dwi'),
('in_bval', 'in_bval')]),
(flirt, merged_volumes, [('outputnode.out_ref', 'in_file1'),
('outputnode.out_file', 'in_file2')]),
(merged_volumes, merge, [('out_file', 'in_dwi')]),
(inputnode, merge, [('in_bval', 'in_bval')]),
(inputnode, flirt, [('in_mask', 'inputnode.ref_mask'),
('in_xfms', 'inputnode.in_xfms'),
('in_bval', 'inputnode.in_bval')]),
(inputnode, get_mat, [('in_bval', 'in_bval')]),
(getb0, flirt, [('roi_file', 'inputnode.reference')]),
(pick_dws, flirt, [('out_file', 'inputnode.in_file')]),
(flirt, get_mat, [('outputnode.out_xfms', 'in_xfms')]),
(flirt, mult, [(('outputnode.out_xfms', _xfm_jacobian),
'operand_value')]),
(flirt, split, [('outputnode.out_file', 'in_file')]),
(split, mult, [('out_files', 'in_file')]),
(mult, thres, [('out_file', 'in_file')]),
(thres, merge, [('out_file', 'in_corrected')]),
(get_mat, outputnode, [('out_files', 'out_xfms')]),
(merge, outputnode, [('out_file', 'out_file')])
])
return wf
def sdc_fmb(fugue_params=dict(smooth3d=2.0),
register_fmap_on_b0=True,
name='fmb_correction'):
"""
SDC stands for susceptibility distortion correction. FMB stands for
fieldmap-based.
The fieldmap based method (FMB) implements SDC by using a mapping of the
B0 field as proposed by [Jezzard95]_. This workflow uses the implementation
of FSL (`FUGUE <http://fsl.fmrib.ox.ac.uk/fsl/fslwiki/FUGUE>`_). Phase
unwrapping is performed using `PRELUDE
<http://fsl.fmrib.ox.ac.uk/fsl/fsl-4.1.9/fugue/prelude.html>`_
[Jenkinson03]_. Preparation of the fieldmap is performed reproducing the
script in FSL `fsl_prepare_fieldmap
<http://fsl.fmrib.ox.ac.uk/fsl/fslwiki/FUGUE/Guide#SIEMENS_data>`_.
Parameters
----------
in_file : FILE
Mandatory input. Dwi dataset.
in_bval : FILE
Mandatory input. Bval file.
in_mask : FILE
Mandatory input. Mask file.
bmap_mag : FILE
Mandatory input. Grefield map. Magnitude.
bmap_pha : FILE
Mandatory input. Grefield map. Phase.
Outputs
------
out_file : FILE
Output.
out_vsm : FILE
Output. The set of dwi volumes.
out_warp : FILE
Output. The bvalues corresponding to the out_dwi.
.. warning:: Only SIEMENS format fieldmaps are supported.
.. admonition:: References
<
.. [Jezzard95] Jezzard P, and Balaban RS, `Correction for geometric
distortion in echo planar images from B0 field variations
<http://dx.doi.org/10.1002/mrm.1910340111>`_,
MRM 34(1):65-73. (1995). doi: 10.1002/mrm.1910340111.
.. [Jenkinson03] Jenkinson M., `Fast, automated, N-dimensional
phase-unwrapping algorithm <http://dx.doi.org/10.1002/mrm.10354>`_,
MRM 49(1):193-197, 2003, doi: 10.1002/mrm.10354.
echo_spacing = 1/(BandwidthPerPixelPhaseEncode x (AcquisitionMatrixText component #1))
"""
inputnode = pe.Node(niu.IdentityInterface(fields=['in_file',
'in_mask', 'in_fmap_phasediff', 'in_fmap_magnitude', 'delta_te',
'echospacing', 'enc_dir']),
name='inputnode')
outputnode = pe.Node(niu.IdentityInterface(fields=['out_file', 'out_vsm',
'out_warp', 'out_registered_fmap']),
name='outputnode')
getb0 = pe.Node(fsl.ExtractROI(t_min=0, t_size=1), name='get_b0')
firstmag = pe.Node(fsl.ExtractROI(t_min=0, t_size=1), name='GetFirst')
n4 = pe.Node(ants.N4BiasFieldCorrection(dimension=3), name='n4_magnitude')
bet = pe.Node(fsl.BET(frac=0.4, mask=True), name='bet_n4_magnitude')
dilate = pe.Node(fsl.maths.MathsCommand(nan2zeros=True,
args='-kernel sphere 5 -dilM'), name='dilate_bet')
pha2rads = pe.Node(niu.Function(input_names=['in_file'], output_names=['out_file'],
function=siemens2rads), name='PreparePhase')
prelude = pe.Node(fsl.PRELUDE(process3d=True), name='PhaseUnwrap')
rad2rsec = pe.Node(niu.Function(input_names=['in_file', 'delta_te'],
output_names=['out_file'], function=rads2radsec), name='ToRadSec')
############ Solution 3: use ants
fmm2b0 = pe.Node(ants.Registration(output_warped_image=True),
name="FMm_to_B0")
fmm2b0.inputs.transforms = ['Rigid'] * 2
fmm2b0.inputs.transform_parameters = [(1.0,)] * 2
fmm2b0.inputs.number_of_iterations = [[50], [20]]
fmm2b0.inputs.dimension = 3
fmm2b0.inputs.metric = ['Mattes', 'Mattes']
fmm2b0.inputs.metric_weight = [1.0] * 2
fmm2b0.inputs.radius_or_number_of_bins = [64, 64]
fmm2b0.inputs.sampling_strategy = ['Regular', 'Random']
fmm2b0.inputs.sampling_percentage = [None, 0.2]
fmm2b0.inputs.convergence_threshold = [1.e-5, 1.e-8]
fmm2b0.inputs.convergence_window_size = [20, 10]
fmm2b0.inputs.smoothing_sigmas = [[6.0], [2.0]]
fmm2b0.inputs.sigma_units = ['vox'] * 2
fmm2b0.inputs.shrink_factors = [[6], [1]] # ,[1] ]
fmm2b0.inputs.use_estimate_learning_rate_once = [True] * 2
fmm2b0.inputs.use_histogram_matching = [True] * 2
fmm2b0.inputs.initial_moving_transform_com = 0
fmm2b0.inputs.collapse_output_transforms = True
fmm2b0.inputs.winsorize_upper_quantile = 0.995
applyxfm = pe.Node(ants.ApplyTransforms(
dimension=3, interpolation='BSpline'), name='FMp_to_B0')
pre_fugue = pe.Node(fsl.FUGUE(save_fmap=True), name='PreliminaryFugue')
demean = pe.Node(niu.Function(input_names=['in_file', 'in_mask'],
output_names=['out_file'], function=demean_image),
name='DemeanFmap')
cleanup = cleanup_edge_pipeline()
addvol = pe.Node(niu.Function(input_names=['in_file'], output_names=['out_file'],
function=add_empty_vol), name='AddEmptyVol')
vsm = pe.Node(fsl.FUGUE(save_shift=True, **fugue_params),
name="ComputeVSM")
split = pe.Node(fsl.Split(dimension='t'), name='SplitDWIs')
merge = pe.Node(fsl.Merge(dimension='t'), name='MergeDWIs')
unwarp = pe.MapNode(fsl.FUGUE(icorr=True, forward_warping=False),
iterfield=['in_file'], name='UnwarpDWIs')
thres = pe.MapNode(fsl.Threshold(thresh=0.0), iterfield=['in_file'],
name='RemoveNegative')
vsm2dfm = vsm2warp()
vsm2dfm.inputs.inputnode.scaling = 1.0
wf = pe.Workflow(name=name)
wf.connect([
(inputnode, pha2rads, [('in_fmap_phasediff', 'in_file')]),
(inputnode, getb0, [('in_file', 'in_file')]),
(inputnode, firstmag, [('in_fmap_magnitude', 'in_file')]),
(firstmag, n4, [('roi_file', 'input_image')]),
(n4, bet, [('output_image', 'in_file')]),
(bet, dilate, [('mask_file', 'in_file')]),
(pha2rads, prelude, [('out_file', 'phase_file')]),
(n4, prelude, [('output_image', 'magnitude_file')]),
(dilate, prelude, [('out_file', 'mask_file')]),
(prelude, rad2rsec, [('unwrapped_phase_file', 'in_file')]),
(inputnode, rad2rsec, [('delta_te', 'delta_te')]),
])
if register_fmap_on_b0:
wf.connect([
(getb0, applyxfm, [('roi_file', 'reference_image')]),
(rad2rsec, applyxfm, [('out_file', 'input_image')]),
(fmm2b0, applyxfm, [
('forward_transforms', 'transforms'),
('forward_invert_flags', 'invert_transform_flags')]),
(applyxfm, pre_fugue, [('output_image', 'fmap_in_file')]),
###### Solution using ants
(getb0, fmm2b0, [('roi_file', 'fixed_image')]),
(n4, fmm2b0, [('output_image', 'moving_image')]),
(inputnode, fmm2b0, [('in_mask', 'fixed_image_mask')]),
(dilate, fmm2b0, [('out_file', 'moving_image_mask')]),
(inputnode, pre_fugue, [('in_mask', 'mask_file')]),
(pre_fugue, demean, [('fmap_out_file', 'in_file')]),
(inputnode, demean, [('in_mask', 'in_mask')]),
(demean, cleanup, [('out_file', 'inputnode.in_file')]),
(inputnode, cleanup, [('in_mask', 'inputnode.in_mask')]),
(cleanup, addvol, [('outputnode.out_file', 'in_file')]),
(inputnode, vsm, [('in_mask', 'mask_file')]),
(inputnode, vsm, [('echospacing', 'dwell_time')]),
(inputnode, vsm, [('delta_te', 'asym_se_time')]),
(addvol, vsm, [('out_file', 'fmap_in_file')]),
(inputnode, split, [('in_file', 'in_file')]),
(split, unwarp, [('out_files', 'in_file')]),
(vsm, unwarp, [('shift_out_file', 'shift_in_file')]),
(inputnode, unwarp, [('enc_dir', 'unwarp_direction')]),
(unwarp, thres, [('unwarped_file', 'in_file')]),
(thres, merge, [('out_file', 'in_files')]),
(inputnode, vsm2dfm, [('enc_dir', 'inputnode.enc_dir')]),
(merge, vsm2dfm, [('merged_file', 'inputnode.in_ref')]),
(vsm, vsm2dfm, [('shift_out_file', 'inputnode.in_vsm')]),
(applyxfm, outputnode, [('output_image', 'out_registered_fmap')]),
(rad2rsec, outputnode, [('out_file', 'out_native_fmap')]),
(merge, outputnode, [('merged_file', 'out_file')]),
(vsm, outputnode, [('shift_out_file', 'out_vsm')]),
(vsm2dfm, outputnode, [('outputnode.out_warp', 'out_warp')])
])
else:
wf.connect([
(rad2rsec, pre_fugue, [('out_file', 'fmap_in_file')]),
(inputnode, pre_fugue, [('in_mask', 'mask_file')]),
(pre_fugue, demean, [('fmap_out_file', 'in_file')]),
(inputnode, demean, [('in_mask', 'in_mask')]),
(demean, cleanup, [('out_file', 'inputnode.in_file')]),
(inputnode, cleanup, [('in_mask', 'inputnode.in_mask')]),
(cleanup, addvol, [('outputnode.out_file', 'in_file')]),
(inputnode, vsm, [('in_mask', 'mask_file')]),
(inputnode, vsm, [('echospacing', 'echospacing')]),
(inputnode, vsm, [('delta_te', 'delta_te')]),
(addvol, vsm, [('out_file', 'fmap_in_file')]),
(inputnode, split, [('in_file', 'in_file')]),
(split, unwarp, [('out_files', 'in_file')]),
(vsm, unwarp, [('shift_out_file', 'shift_in_file')]),
(inputnode, unwarp, [('enc_dir', 'enc_dir')]),
(unwarp, thres, [('unwarped_file', 'in_file')]),
(thres, merge, [('out_file', 'in_files')]),
(merge, vsm2dfm, [('merged_file', 'inputnode.in_ref')]),
(vsm, vsm2dfm, [('shift_out_file', 'inputnode.in_vsm')]),
(inputnode, vsm2dfm, [('enc_dir', 'inputnode.enc_dir')]),
(rad2rsec, outputnode, [('out_file', 'out_native_fmap')]),
(merge, outputnode, [('merged_file', 'out_file')]),
(vsm, outputnode, [('shift_out_file', 'out_vsm')]),
(vsm2dfm, outputnode, [('outputnode.out_warp', 'out_warp')])
])
return wf
def sdc_fmb_twophase(name='fmb_correction',
fugue_params=dict(smooth3d=2.0),
fmap_params=dict(delta_te=2.46e-3),
epi_params=dict(echospacing=0.39e-3,
enc_dir='y')):
"""
SDC stands for susceptibility distortion correction. FMB stands for
fieldmap-based.
The fieldmap based method (FMB) implements SDC by using a mapping of the
B0 field as proposed by [Jezzard95]_. This workflow uses the implementation
of FSL (`FUGUE <http://fsl.fmrib.ox.ac.uk/fsl/fslwiki/FUGUE>`_). Phase
unwrapping is performed using `PRELUDE
<http://fsl.fmrib.ox.ac.uk/fsl/fsl-4.1.9/fugue/prelude.html>`_
[Jenkinson03]_. Preparation of the fieldmap is performed reproducing the
script in FSL `fsl_prepare_fieldmap
<http://fsl.fmrib.ox.ac.uk/fsl/fslwiki/FUGUE/Guide#SIEMENS_data>`_.
Parameters
----------
in_file : FILE
Mandatory input. Dwi dataset.
in_bval : FILE
Mandatory input. Bval file.
in_mask : FILE
Mandatory input. Mask file.
bmap_mag : FILE
Mandatory input. Grefield map. Magnitude.
bmap_pha : FILE
Mandatory input. Grefield map. Phase.
Outputs
------
out_file : FILE
Output.
out_vsm : FILE
Output. The set of dwi volumes.
out_warp : FILE
Output. The bvalues corresponding to the out_dwi.
.. warning:: Only SIEMENS format fieldmaps are supported.
.. admonition:: References
<
.. [Jezzard95] Jezzard P, and Balaban RS, `Correction for geometric
distortion in echo planar images from B0 field variations
<http://dx.doi.org/10.1002/mrm.1910340111>`_,
MRM 34(1):65-73. (1995). doi: 10.1002/mrm.1910340111.
.. [Jenkinson03] Jenkinson M., `Fast, automated, N-dimensional
phase-unwrapping algorithm <http://dx.doi.org/10.1002/mrm.10354>`_,
MRM 49(1):193-197, 2003, doi: 10.1002/mrm.10354.
echo_spacing = 1/(BandwidthPerPixelPhaseEncode x (AcquisitionMatrixText component #1))
"""
from clinica.pipelines.dwi.dwi_preprocessing_utils import convert_phase_in_radians
from clinica.pipelines.dwi.dwi_preprocessing_utils import create_phase_in_radsec
inputnode = pe.Node(niu.IdentityInterface(
fields=['in_file', 'in_mask', 'in_fmap_phase1', 'in_fmap_phase2', 'in_fmap_magnitude1', 'in_fmap_magnitude2']),
name='inputnode')
outputnode = pe.Node(niu.IdentityInterface(
fields=['out_file', 'out_vsm', 'out_warp', 'out_native_fmap']),
name='outputnode')
getb0 = pe.Node(fsl.ExtractROI(t_min=0, t_size=1), name='get_b0')
# n4 = pe.Node(ants.N4BiasFieldCorrection(dimension=3), name='n4_magnitude')
n4_1 = pe.Node(ants.N4BiasFieldCorrection(dimension=3), name='n4_magnitude1')
n4_2 = pe.Node(ants.N4BiasFieldCorrection(dimension=3), name='n4_magnitude2')
# bet = pe.Node(fsl.BET(frac=0.4, mask=True), name='bet_n4_magnitude')
bet_1 = pe.Node(fsl.BET(frac=0.4, mask=True), name='bet_n4_magnitude1')
bet_2 = pe.Node(fsl.BET(frac=0.4, mask=True), name='bet_n4_magnitude2')
# dilate = pe.Node(fsl.maths.MathsCommand(nan2zeros=True,
# args='-kernel sphere 5 -dilM'), name='dilate_bet')
dilate_1 = pe.Node(fsl.maths.MathsCommand(nan2zeros=True,
args='-kernel sphere 5 -dilM'), name='dilate_bet_1')
dilate_2 = pe.Node(fsl.maths.MathsCommand(nan2zeros=True,
args='-kernel sphere 5 -dilM'), name='dilate_bet_2')
# phase1_in_rad = pe.Node(niu.Function(input_names=['in_file', 'name_output_file'], output_names=['out_file'],
# function=convert_phase_in_radians), name='Phase1InRad')
# phase2_in_rad = pe.Node(niu.Function(input_names=['in_file', 'name_output_file'], output_names=['out_file'],
# function=convert_phase_in_radians), name='Phase2InRad')
phase1_in_rad = pe.Node(niu.Function(input_names=['in_file'], output_names=['out_file'],
function=siemens2rads), name='Phase1InRad')
phase2_in_rad = pe.Node(niu.Function(input_names=['in_file'], output_names=['out_file'],
function=siemens2rads), name='Phase2InRad')
# phase1_unwarp = pe.Node(fsl.PRELUDE(process3d=True), name='Phase1Unwarp')
# phase2_unwarp = pe.Node(fsl.PRELUDE(process3d=True), name='Phase2Unwarp')
phase_in_rsec =pe.Node(niu.Function(input_names=['in_phase1', 'in_phase2', 'delta_te', 'out_file'], output_names=['out_file'],
function=create_phase_in_radsec), name='PhaseInRadSec')
phase_in_rsec.inputs.delta_te = fmap_params['delta_te']
# pha2rads = pe.Node(niu.Function(input_names=['in_file'], output_names=['out_file'],
# function=siemens2rads), name='PreparePhase')
# prelude = pe.Node(fsl.PRELUDE(process3d=True), name='PhaseUnwrap')
# rad2rsec = pe.Node(niu.Function(input_names=['in_file', 'delta_te'],
# output_names=['out_file'], function=rads2radsec), name='ToRadSec')
# rad2rsec.inputs.delta_te = fmap_params['delta_te']
flirt = pe.Node(fsl.FLIRT(interp='spline', cost='normmi', cost_func='normmi',
dof=6, bins=64, save_log=True, padding_size=10,
searchr_x=[-4, 4], searchr_y=[-4, 4], searchr_z=[-4, 4],
fine_search=1, coarse_search=10),
name='BmapMag2B0')
applyxfm = pe.Node(fsl.ApplyXfm(interp='spline', padding_size=10, apply_xfm=True),
name='BmapPha2B0')
pre_fugue = pe.Node(fsl.FUGUE(save_fmap=True), name='PreliminaryFugue')
demean = pe.Node(niu.Function(input_names=['in_file', 'in_mask'],
output_names=['out_file'], function=demean_image),
name='DemeanFmap')
cleanup = cleanup_edge_pipeline()
addvol = pe.Node(niu.Function(input_names=['in_file'], output_names=['out_file'],
function=add_empty_vol), name='AddEmptyVol')
vsm = pe.Node(fsl.FUGUE(save_shift=True, **fugue_params),
name="ComputeVSM")
vsm.inputs.asym_se_time = fmap_params['delta_te']
vsm.inputs.dwell_time = epi_params['echospacing']
split = pe.Node(fsl.Split(dimension='t'), name='SplitDWIs')
merge = pe.Node(fsl.Merge(dimension='t'), name='MergeDWIs')
unwarp = pe.MapNode(fsl.FUGUE(icorr=True, forward_warping=False),
iterfield=['in_file'], name='UnwarpDWIs')
unwarp.inputs.unwarp_direction = epi_params['enc_dir']
thres = pe.MapNode(fsl.Threshold(thresh=0.0), iterfield=['in_file'],
name='RemoveNegative')
vsm2dfm = vsm2warp()
vsm2dfm.inputs.inputnode.scaling = 1.0
vsm2dfm.inputs.inputnode.enc_dir = epi_params['enc_dir']
wf = pe.Workflow(name=name)
wf.connect([
# (inputnode, pha2rads, [('bmap_pha', 'in_file')]),
(inputnode, phase1_in_rad, [('in_fmap_phase1', 'in_file')]),
(inputnode, phase2_in_rad, [('in_fmap_phase2', 'in_file')]),
(inputnode, getb0, [('in_file', 'in_file')]),
# (inputnode, n4, [('bmap_mag', 'input_image')]),
(inputnode, n4_1, [('in_fmap_magnitude1', 'input_image')]),
(inputnode, n4_2, [('in_fmap_magnitude2', 'input_image')]),
# (n4, bet, [('output_image', 'in_file')]),
(n4_1, bet_1, [('output_image', 'in_file')]),
(n4_2, bet_2, [('output_image', 'in_file')]),
# (bet, dilate, [('mask_file', 'in_file')]),
(bet_1, dilate_1, [('mask_file', 'in_file')]),
(bet_2, dilate_2, [('mask_file', 'in_file')]),
# (pha2rads, prelude, [('out_file', 'phase_file')]),
# (n4, prelude, [('output_image', 'magnitude_file')]),
# (dilate, prelude, [('out_file', 'mask_file')]),
# (phase1_in_rad, phase1_unwarp, [('out_file', 'phase_file')]),
# (n4_1, phase1_unwarp, [('output_image', 'magnitude_file')]),
# (dilate_1, phase1_unwarp, [('out_file', 'mask_file')]),
# (phase2_in_rad, phase2_unwarp, [('out_file', 'phase_file')]),
# (n4_2, phase2_unwarp, [('output_image', 'magnitude_file')]),
# (dilate_2, phase2_unwarp, [('out_file', 'mask_file')]),
# (prelude, rad2rsec, [('unwrapped_phase_file', 'in_file')]),
(phase1_in_rad, phase_in_rsec, [('out_file', 'in_phase1')]),
(phase2_in_rad, phase_in_rsec, [('out_file', 'in_phase2')]),
# (phase1_unwarp, phase_in_rsec, [('unwrapped_phase_file', 'in_phase1')]),
# (phase2_unwarp, phase_in_rsec, [('unwrapped_phase_file', 'in_phase2')]),
(getb0, flirt, [('roi_file', 'reference')]),
(inputnode, flirt, [('in_mask', 'ref_weight')]),
# (n4, flirt, [('output_image', 'in_file')]),
# (dilate, flirt, [('out_file', 'in_weight')]),
(n4_1, flirt, [('output_image', 'in_file')]),
(dilate_1, flirt, [('out_file', 'in_weight')]),
(getb0, applyxfm, [('roi_file', 'reference')]),
# (rad2rsec, applyxfm, [('out_file', 'in_file')]),
(phase_in_rsec, applyxfm, [('out_file', 'in_file')]),
(flirt, applyxfm, [('out_matrix_file', 'in_matrix_file')]),
(applyxfm, pre_fugue, [('out_file', 'fmap_in_file')]),
(inputnode, pre_fugue, [('in_mask', 'mask_file')]),
(pre_fugue, demean, [('fmap_out_file', 'in_file')]),
(inputnode, demean, [('in_mask', 'in_mask')]),
(demean, cleanup, [('out_file', 'inputnode.in_file')]),
(inputnode, cleanup, [('in_mask', 'inputnode.in_mask')]),
(cleanup, addvol, [('outputnode.out_file', 'in_file')]),
(inputnode, vsm, [('in_mask', 'mask_file')]),
(addvol, vsm, [('out_file', 'fmap_in_file')]),
(inputnode, split, [('in_file', 'in_file')]),
(split, unwarp, [('out_files', 'in_file')]),
(vsm, unwarp, [('shift_out_file', 'shift_in_file')]),
(unwarp, thres, [('unwarped_file', 'in_file')]),
(thres, merge, [('out_file', 'in_files')]),
(merge, vsm2dfm, [('merged_file', 'inputnode.in_ref')]),
(vsm, vsm2dfm, [('shift_out_file', 'inputnode.in_vsm')]),
(applyxfm, outputnode, [('out_file', 'out_registered_fmap')]),
(merge, outputnode, [('merged_file', 'out_file')]),
(vsm, outputnode, [('shift_out_file', 'out_vsm')]),
(vsm2dfm, outputnode, [('outputnode.out_warp', 'out_warp')])
])
return wf
def sdc_syb_pipeline(name='sdc_syb_correct'):
"""
SDC stands for susceptibility distortion correction and SYB stand for SyN based. This workflow
allows to correct for echo-planare induced susceptibility artifacts without fieldmap
(e.g. ADNI Database) by elastically register DWIs to their respective baseline T1-weighted
structural scans using an inverse consistent registration algorithm with a mutual information cost
function (SyN algorithm).
.. References
.. Nir et al. (Neurobiology of Aging 2015)- Connectivity network measures predict volumetric atrophy in mild cognitive impairment
Leow et al. (IEEE Trans Med Imaging 2007)- Statistical Properties of Jacobian Maps and the Realization of Unbiased Large Deformation Nonlinear Image Registration
Inputnode
---------
DWI : FILE
Mandatory input. Input dwi file.
T1 : FILE
Mandatory input. Input T1 file.
Outputnode
----------
outputnode.out_dwi - corrected dwi file
outputnode.out_bvec - rotated gradient vectors table
outputnode.out_b0_to_t1_rigid_body_matrix - B0 to T1 image FLIRT rigid body fsl coregistration matrix
outputnode.out_t1_coregistered_to_b0 - T1 image rigid body coregistered to the B0 image
outputnode.out_b0_to_t1_affine_matrix - B0 to T1 image ANTs affine itk coregistration matrix
outputnode.out_b0_to_t1_syn_defomation_field - B0 to T1 image ANTs SyN itk warp
outputnode.out_warp - Out warp allowing DWI to T1 registration and susceptibilty induced artifacts correction
Example
-------
>>> epi = epi_pipeline()
>>> epi.inputs.inputnode.in_dwi = 'DWI.nii'
>>> epi.inputs.inputnode.in_t1 = 'T1.nii'
>>> epi.run() # doctest: +SKIP
"""
import nipype.pipeline.engine as pe
import nipype.interfaces.utility as niu
import nipype.interfaces.fsl as fsl
from clinica.pipelines.dwi.dwi_registration import ants_registration_syn_quick, antscombintransform
inputnode = pe.Node(niu.IdentityInterface(fields=['in_t1', 'in_dwi']), name='inputnode')
split = pe.Node(fsl.Split(dimension='t'), name='SplitDWIs')
pick_ref = pe.Node(niu.Select(), name='Pick_b0')
pick_ref.inputs.index = [0]
flirt_b0_to_t1 = pe.Node(interface=fsl.FLIRT(dof=6), name = 'flirt_b0_to_t1')
flirt_b0_to_t1.inputs.interp = "spline"
flirt_b0_to_t1.inputs.cost = 'normmi'
flirt_b0_to_t1.inputs.cost_func = 'normmi'
invert_xfm = pe.Node(interface=fsl.ConvertXFM(), name='invert_xfm')
invert_xfm.inputs.invert_xfm = True
apply_xfm = pe.Node(interface=fsl.ApplyXfm(), name='apply_xfm')
apply_xfm.inputs.apply_xfm = True
apply_xfm.inputs.interp = "spline"
apply_xfm.inputs.cost = 'normmi'
apply_xfm.inputs.cost_func = 'normmi'
ants_registration_syn_quick = pe.Node(interface=niu.Function(
input_names=['fixe_image', 'moving_image'],
output_names=['image_warped', 'affine_matrix', 'warp', 'inverse_warped', 'inverse_warp'],
function=ants_registration_syn_quick), name='ants_registration_syn_quick')
merge_transform = pe.Node(niu.Merge(2), name='MergeTransforms')
combin_warp = pe.Node(interface=niu.Function(
input_names=['in_file', 'transforms_list', 'reference'],
output_names=['out_warp'],
function=antscombintransform), name='combin_warp')
coeffs = pe.Node(fsl.WarpUtils(out_format='spline'), name='CoeffComp')
fsl_transf = pe.Node(fsl.WarpUtils(out_format='field'), name='fsl_transf')
apply_warp = pe.MapNode(interface=fsl.ApplyWarp(), iterfield=['in_file'],name='apply_warp')
apply_warp.inputs.interp = 'spline'
thres = pe.MapNode(fsl.Threshold(thresh=0.0), iterfield=['in_file'],
name='RemoveNegative')
merge = pe.Node(fsl.Merge(dimension='t'), name='MergeDWIs')
outputnode = pe.Node(niu.IdentityInterface(
fields=['out_b0_to_t1_rigid_body_matrix', 'out_t1_to_b0_rigid_body_matrix', 'out_t1_coregistered_to_b0',
'out_b0_to_t1_syn_defomation_field', 'out_b0_to_t1_affine_matrix', 'out_dwi', 'out_warp']),
name='outputnode')
wf = pe.Workflow(name='sdc_syb_pipeline')
wf.connect([
(inputnode, split, [('in_dwi', 'in_file')]),
(split, pick_ref, [('out_files', 'inlist')]),
(pick_ref, flirt_b0_to_t1, [('out', 'in_file')]),
(inputnode, flirt_b0_to_t1, [('in_t1', 'reference')]),
(flirt_b0_to_t1, invert_xfm, [('out_matrix_file', 'in_file')]),
(invert_xfm, apply_xfm, [('out_file', 'in_matrix_file')]),
(inputnode, apply_xfm, [('in_t1', 'in_file')]),
(pick_ref, apply_xfm, [('out', 'reference')]),
(apply_xfm, ants_registration_syn_quick, [('out_file', 'fixe_image')]),
(pick_ref, ants_registration_syn_quick, [('out', 'moving_image')]),
(ants_registration_syn_quick, merge_transform, [('affine_matrix', 'in2'),
('warp', 'in1')]),
(pick_ref, combin_warp, [('out', 'in_file')]),
(merge_transform, combin_warp, [('out', 'transforms_list')]),
(apply_xfm, combin_warp, [('out_file', 'reference')]),
(apply_xfm, coeffs, [('out_file', 'reference')]),
(combin_warp, coeffs, [('out_warp', 'in_file')]),
(coeffs, fsl_transf, [('out_file', 'in_file')]),
(apply_xfm, fsl_transf, [('out_file', 'reference')]),
(fsl_transf, apply_warp, [('out_file', 'field_file')]),
(split, apply_warp, [('out_files', 'in_file')]),
(apply_xfm, apply_warp, [('out_file', 'ref_file')]),
(apply_warp, thres, [('out_file', 'in_file')]),
(thres, merge, [('out_file', 'in_files')]),
(merge, outputnode, [('merged_file', 'out_dwi')]),
(flirt_b0_to_t1, outputnode, [('out_matrix_file', 'out_b0_to_t1_rigid_body_matrix')]),
(invert_xfm, outputnode, [('out_file', 'out_t1_to_b0_rigid_body_matrix')]),
(apply_xfm, outputnode, [('out_file', 'out_t1_coregistered_to_b0')]),
(ants_registration_syn_quick, outputnode, [('warp', 'out_b0_to_t1_syn_defomation_field'),
('affine_matrix', 'out_b0_to_t1_affine_matrix')]),
(fsl_transf, outputnode, [('out_file', 'out_warp')])
])
return wf