/
utils.py
1190 lines (1005 loc) · 34.7 KB
/
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
# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*-
# vi: set ft=python sts=4 ts=4 sw=4 et:
# -*- coding: utf-8 -*-
import os.path as op
from ...utils.filemanip import split_filename
from ..base import (
CommandLineInputSpec,
CommandLine,
traits,
TraitedSpec,
File,
InputMultiPath,
isdefined,
)
from .base import MRTrix3BaseInputSpec, MRTrix3Base
class BrainMaskInputSpec(MRTrix3BaseInputSpec):
in_file = File(
exists=True,
argstr="%s",
mandatory=True,
position=-2,
desc="input diffusion weighted images",
)
out_file = File(
"brainmask.mif",
argstr="%s",
mandatory=True,
position=-1,
usedefault=True,
desc="output brain mask",
)
class BrainMaskOutputSpec(TraitedSpec):
out_file = File(exists=True, desc="the output response file")
class BrainMask(CommandLine):
"""
Convert a mesh surface to a partial volume estimation image
Example
-------
>>> import nipype.interfaces.mrtrix3 as mrt
>>> bmsk = mrt.BrainMask()
>>> bmsk.inputs.in_file = 'dwi.mif'
>>> bmsk.cmdline # doctest: +ELLIPSIS
'dwi2mask dwi.mif brainmask.mif'
>>> bmsk.run() # doctest: +SKIP
"""
_cmd = "dwi2mask"
input_spec = BrainMaskInputSpec
output_spec = BrainMaskOutputSpec
def _list_outputs(self):
outputs = self.output_spec().get()
outputs["out_file"] = op.abspath(self.inputs.out_file)
return outputs
class MRCatInputSpec(MRTrix3BaseInputSpec):
in_files = traits.List(
File(exists=True),
argstr="%s",
position=-2,
mandatory=True,
desc="files to concatenate",
)
out_file = File(
"concatenated.mif",
argstr="%s",
mandatory=True,
position=-1,
usedefault=True,
desc="output concatenated image",
)
axis = traits.Int(
argstr="-axis %s",
desc="""specify axis along which concatenation should be performed. By default,
the program will use the last non-singleton, non-spatial axis of any of
the input images - in other words axis 3 or whichever axis (greater than
3) of the input images has size greater than one""",
)
datatype = traits.Enum(
"float32",
"float32le",
"float32be",
"float64",
"float64le",
"float64be",
"int64",
"uint64",
"int64le",
"uint64le",
"int64be",
"uint64be",
"int32",
"uint32",
"int32le",
"uint32le",
"int32be",
"uint32be",
"int16",
"uint16",
"int16le",
"uint16le",
"int16be",
"uint16be",
"cfloat32",
"cfloat32le",
"cfloat32be",
"cfloat64",
"cfloat64le",
"cfloat64be",
"int8",
"uint8",
"bit",
argstr="-datatype %s",
desc="specify output image data type",
)
class MRCatOutputSpec(TraitedSpec):
out_file = File(exists=True, desc="the output concatenated image")
class MRCat(CommandLine):
"""
Concatenate several images into one
Example
-------
>>> import nipype.interfaces.mrtrix3 as mrt
>>> mrcat = mrt.MRCat()
>>> mrcat.inputs.in_files = ['dwi.mif','mask.mif']
>>> mrcat.cmdline # doctest: +ELLIPSIS
'mrcat dwi.mif mask.mif concatenated.mif'
>>> mrcat.run() # doctest: +SKIP
"""
_cmd = "mrcat"
input_spec = MRCatInputSpec
output_spec = MRCatOutputSpec
def _list_outputs(self):
outputs = self.output_spec().get()
outputs["out_file"] = op.abspath(self.inputs.out_file)
return outputs
class Mesh2PVEInputSpec(CommandLineInputSpec):
in_file = File(
exists=True, argstr="%s", mandatory=True, position=-3, desc="input mesh"
)
reference = File(
exists=True,
argstr="%s",
mandatory=True,
position=-2,
desc="input reference image",
)
in_first = File(
exists=True,
argstr="-first %s",
desc="indicates that the mesh file is provided by FSL FIRST",
)
out_file = File(
"mesh2volume.nii.gz",
argstr="%s",
mandatory=True,
position=-1,
usedefault=True,
desc="output file containing SH coefficients",
)
class Mesh2PVEOutputSpec(TraitedSpec):
out_file = File(exists=True, desc="the output response file")
class Mesh2PVE(CommandLine):
"""
Convert a mesh surface to a partial volume estimation image
Example
-------
>>> import nipype.interfaces.mrtrix3 as mrt
>>> m2p = mrt.Mesh2PVE()
>>> m2p.inputs.in_file = 'surf1.vtk'
>>> m2p.inputs.reference = 'dwi.mif'
>>> m2p.inputs.in_first = 'T1.nii.gz'
>>> m2p.cmdline # doctest: +ELLIPSIS
'mesh2pve -first T1.nii.gz surf1.vtk dwi.mif mesh2volume.nii.gz'
>>> m2p.run() # doctest: +SKIP
"""
_cmd = "mesh2pve"
input_spec = Mesh2PVEInputSpec
output_spec = Mesh2PVEOutputSpec
def _list_outputs(self):
outputs = self.output_spec().get()
outputs["out_file"] = op.abspath(self.inputs.out_file)
return outputs
class Generate5ttInputSpec(MRTrix3BaseInputSpec):
algorithm = traits.Enum(
"fsl",
"gif",
"freesurfer",
argstr="%s",
position=-3,
mandatory=True,
desc="tissue segmentation algorithm",
)
in_file = File(
exists=True, argstr="%s", mandatory=True, position=-2, desc="input image"
)
out_file = File(argstr="%s", mandatory=True, position=-1, desc="output image")
class Generate5ttOutputSpec(TraitedSpec):
out_file = File(exists=True, desc="output image")
class Generate5tt(MRTrix3Base):
"""
Generate a 5TT image suitable for ACT using the selected algorithm
Example
-------
>>> import nipype.interfaces.mrtrix3 as mrt
>>> gen5tt = mrt.Generate5tt()
>>> gen5tt.inputs.in_file = 'T1.nii.gz'
>>> gen5tt.inputs.algorithm = 'fsl'
>>> gen5tt.inputs.out_file = '5tt.mif'
>>> gen5tt.cmdline # doctest: +ELLIPSIS
'5ttgen fsl T1.nii.gz 5tt.mif'
>>> gen5tt.run() # doctest: +SKIP
"""
_cmd = "5ttgen"
input_spec = Generate5ttInputSpec
output_spec = Generate5ttOutputSpec
def _list_outputs(self):
outputs = self.output_spec().get()
outputs["out_file"] = op.abspath(self.inputs.out_file)
return outputs
class TensorMetricsInputSpec(CommandLineInputSpec):
in_file = File(
exists=True,
argstr="%s",
mandatory=True,
position=-1,
desc="input DTI image",
)
out_fa = File(argstr="-fa %s", desc="output FA file")
out_adc = File(argstr="-adc %s", desc="output ADC file")
out_ad = File(argstr="-ad %s", desc="output AD file")
out_rd = File(argstr="-rd %s", desc="output RD file")
out_cl = File(argstr="-cl %s", desc="output CL file")
out_cp = File(argstr="-cp %s", desc="output CP file")
out_cs = File(argstr="-cs %s", desc="output CS file")
out_evec = File(argstr="-vector %s", desc="output selected eigenvector(s) file")
out_eval = File(argstr="-value %s", desc="output selected eigenvalue(s) file")
component = traits.List(
[1],
usedefault=True,
argstr="-num %s",
sep=",",
desc=(
"specify the desired eigenvalue/eigenvector(s). Note that "
"several eigenvalues can be specified as a number sequence"
),
)
in_mask = File(
exists=True,
argstr="-mask %s",
desc=(
"only perform computation within the specified binary" " brain mask image"
),
)
modulate = traits.Enum(
"FA",
"none",
"eval",
argstr="-modulate %s",
desc=("how to modulate the magnitude of the" " eigenvectors"),
)
class TensorMetricsOutputSpec(TraitedSpec):
out_fa = File(desc="output FA file")
out_adc = File(desc="output ADC file")
out_ad = File(desc="output AD file")
out_rd = File(desc="output RD file")
out_cl = File(desc="output CL file")
out_cp = File(desc="output CP file")
out_cs = File(desc="output CS file")
out_evec = File(desc="output selected eigenvector(s) file")
out_eval = File(desc="output selected eigenvalue(s) file")
class TensorMetrics(CommandLine):
"""
Compute metrics from tensors
Example
-------
>>> import nipype.interfaces.mrtrix3 as mrt
>>> comp = mrt.TensorMetrics()
>>> comp.inputs.in_file = 'dti.mif'
>>> comp.inputs.out_fa = 'fa.mif'
>>> comp.cmdline # doctest: +ELLIPSIS
'tensor2metric -num 1 -fa fa.mif dti.mif'
>>> comp.run() # doctest: +SKIP
"""
_cmd = "tensor2metric"
input_spec = TensorMetricsInputSpec
output_spec = TensorMetricsOutputSpec
def _list_outputs(self):
outputs = self.output_spec().get()
for k in list(outputs.keys()):
if isdefined(getattr(self.inputs, k)):
outputs[k] = op.abspath(getattr(self.inputs, k))
return outputs
class ComputeTDIInputSpec(CommandLineInputSpec):
in_file = File(
exists=True, argstr="%s", mandatory=True, position=-2, desc="input tractography"
)
out_file = File(
"tdi.mif", argstr="%s", usedefault=True, position=-1, desc="output TDI file"
)
reference = File(
exists=True,
argstr="-template %s",
desc="a reference" "image to be used as template",
)
vox_size = traits.List(
traits.Int, argstr="-vox %s", sep=",", desc="voxel dimensions"
)
data_type = traits.Enum(
"float",
"unsigned int",
argstr="-datatype %s",
desc="specify output image data type",
)
use_dec = traits.Bool(argstr="-dec", desc="perform mapping in DEC space")
dixel = File(
argstr="-dixel %s",
desc="map streamlines to"
"dixels within each voxel. Directions are stored as"
"azimuth elevation pairs.",
)
max_tod = traits.Int(
argstr="-tod %d",
desc="generate a Track Orientation " "Distribution (TOD) in each voxel.",
)
contrast = traits.Enum(
"tdi",
"length",
"invlength",
"scalar_map",
"scalar_map_conut",
"fod_amp",
"curvature",
argstr="-constrast %s",
desc="define the desired " "form of contrast for the output image",
)
in_map = File(
exists=True,
argstr="-image %s",
desc="provide the"
"scalar image map for generating images with "
"'scalar_map' contrasts, or the SHs image for fod_amp",
)
stat_vox = traits.Enum(
"sum",
"min",
"mean",
"max",
argstr="-stat_vox %s",
desc="define the statistic for choosing the final"
"voxel intesities for a given contrast",
)
stat_tck = traits.Enum(
"mean",
"sum",
"min",
"max",
"median",
"mean_nonzero",
"gaussian",
"ends_min",
"ends_mean",
"ends_max",
"ends_prod",
argstr="-stat_tck %s",
desc="define the statistic for choosing "
"the contribution to be made by each streamline as a function of"
" the samples taken along their lengths.",
)
fwhm_tck = traits.Float(
argstr="-fwhm_tck %f",
desc="define the statistic for choosing the"
" contribution to be made by each streamline as a function of the "
"samples taken along their lengths",
)
map_zero = traits.Bool(
argstr="-map_zero",
desc="if a streamline has zero contribution based "
"on the contrast & statistic, typically it is not mapped; use this "
"option to still contribute to the map even if this is the case "
"(these non-contributing voxels can then influence the mean value in "
"each voxel of the map)",
)
upsample = traits.Int(
argstr="-upsample %d",
desc="upsample the tracks by"
" some ratio using Hermite interpolation before "
"mapping",
)
precise = traits.Bool(
argstr="-precise",
desc="use a more precise streamline mapping "
"strategy, that accurately quantifies the length through each voxel "
"(these lengths are then taken into account during TWI calculation)",
)
ends_only = traits.Bool(
argstr="-ends_only", desc="only map the streamline" " endpoints to the image"
)
tck_weights = File(
exists=True,
argstr="-tck_weights_in %s",
desc="specify" " a text scalar file containing the streamline weights",
)
nthreads = traits.Int(
argstr="-nthreads %d",
desc="number of threads. if zero, the number" " of available cpus will be used",
nohash=True,
)
class ComputeTDIOutputSpec(TraitedSpec):
out_file = File(desc="output TDI file")
class ComputeTDI(MRTrix3Base):
"""
Use track data as a form of contrast for producing a high-resolution
image.
.. admonition:: References
* For TDI or DEC TDI: Calamante, F.; Tournier, J.-D.; Jackson, G. D. &
Connelly, A. Track-density imaging (TDI): Super-resolution white
matter imaging using whole-brain track-density mapping. NeuroImage,
2010, 53, 1233-1243
* If using -contrast length and -stat_vox mean: Pannek, K.; Mathias,
J. L.; Bigler, E. D.; Brown, G.; Taylor, J. D. & Rose, S. E. The
average pathlength map: A diffusion MRI tractography-derived index
for studying brain pathology. NeuroImage, 2011, 55, 133-141
* If using -dixel option with TDI contrast only: Smith, R.E., Tournier,
J-D., Calamante, F., Connelly, A. A novel paradigm for automated
segmentation of very large whole-brain probabilistic tractography
data sets. In proc. ISMRM, 2011, 19, 673
* If using -dixel option with any other contrast: Pannek, K., Raffelt,
D., Salvado, O., Rose, S. Incorporating directional information in
diffusion tractography derived maps: angular track imaging (ATI).
In Proc. ISMRM, 2012, 20, 1912
* If using -tod option: Dhollander, T., Emsell, L., Van Hecke, W., Maes,
F., Sunaert, S., Suetens, P. Track Orientation Density Imaging (TODI)
and Track Orientation Distribution (TOD) based tractography.
NeuroImage, 2014, 94, 312-336
* If using other contrasts / statistics: Calamante, F.; Tournier, J.-D.;
Smith, R. E. & Connelly, A. A generalised framework for
super-resolution track-weighted imaging. NeuroImage, 2012, 59,
2494-2503
* If using -precise mapping option: Smith, R. E.; Tournier, J.-D.;
Calamante, F. & Connelly, A. SIFT: Spherical-deconvolution informed
filtering of tractograms. NeuroImage, 2013, 67, 298-312 (Appendix 3)
Example
-------
>>> import nipype.interfaces.mrtrix3 as mrt
>>> tdi = mrt.ComputeTDI()
>>> tdi.inputs.in_file = 'dti.mif'
>>> tdi.cmdline # doctest: +ELLIPSIS
'tckmap dti.mif tdi.mif'
>>> tdi.run() # doctest: +SKIP
"""
_cmd = "tckmap"
input_spec = ComputeTDIInputSpec
output_spec = ComputeTDIOutputSpec
def _list_outputs(self):
outputs = self.output_spec().get()
outputs["out_file"] = op.abspath(self.inputs.out_file)
return outputs
class TCK2VTKInputSpec(CommandLineInputSpec):
in_file = File(
exists=True, argstr="%s", mandatory=True, position=-2, desc="input tractography"
)
out_file = File(
"tracks.vtk", argstr="%s", usedefault=True, position=-1, desc="output VTK file"
)
reference = File(
exists=True,
argstr="-image %s",
desc="if specified, the properties of"
" this image will be used to convert track point positions from real "
"(scanner) coordinates into image coordinates (in mm).",
)
voxel = File(
exists=True,
argstr="-image %s",
desc="if specified, the properties of"
" this image will be used to convert track point positions from real "
"(scanner) coordinates into image coordinates.",
)
nthreads = traits.Int(
argstr="-nthreads %d",
desc="number of threads. if zero, the number" " of available cpus will be used",
nohash=True,
)
class TCK2VTKOutputSpec(TraitedSpec):
out_file = File(desc="output VTK file")
class TCK2VTK(MRTrix3Base):
"""
Convert a track file to a vtk format, cave: coordinates are in XYZ
coordinates not reference
Example
-------
>>> import nipype.interfaces.mrtrix3 as mrt
>>> vtk = mrt.TCK2VTK()
>>> vtk.inputs.in_file = 'tracks.tck'
>>> vtk.inputs.reference = 'b0.nii'
>>> vtk.cmdline # doctest: +ELLIPSIS
'tck2vtk -image b0.nii tracks.tck tracks.vtk'
>>> vtk.run() # doctest: +SKIP
"""
_cmd = "tck2vtk"
input_spec = TCK2VTKInputSpec
output_spec = TCK2VTKOutputSpec
def _list_outputs(self):
outputs = self.output_spec().get()
outputs["out_file"] = op.abspath(self.inputs.out_file)
return outputs
class DWIExtractInputSpec(MRTrix3BaseInputSpec):
in_file = File(
exists=True, argstr="%s", mandatory=True, position=-2, desc="input image"
)
out_file = File(argstr="%s", mandatory=True, position=-1, desc="output image")
bzero = traits.Bool(argstr="-bzero", desc="extract b=0 volumes")
nobzero = traits.Bool(argstr="-no_bzero", desc="extract non b=0 volumes")
singleshell = traits.Bool(
argstr="-singleshell", desc="extract volumes with a specific shell"
)
shell = traits.List(
traits.Float,
sep=",",
argstr="-shell %s",
desc="specify one or more gradient shells",
)
class DWIExtractOutputSpec(TraitedSpec):
out_file = File(exists=True, desc="output image")
class DWIExtract(MRTrix3Base):
"""
Extract diffusion-weighted volumes, b=0 volumes, or certain shells from a
DWI dataset
Example
-------
>>> import nipype.interfaces.mrtrix3 as mrt
>>> dwiextract = mrt.DWIExtract()
>>> dwiextract.inputs.in_file = 'dwi.mif'
>>> dwiextract.inputs.bzero = True
>>> dwiextract.inputs.out_file = 'b0vols.mif'
>>> dwiextract.inputs.grad_fsl = ('bvecs', 'bvals')
>>> dwiextract.cmdline # doctest: +ELLIPSIS
'dwiextract -bzero -fslgrad bvecs bvals dwi.mif b0vols.mif'
>>> dwiextract.run() # doctest: +SKIP
"""
_cmd = "dwiextract"
input_spec = DWIExtractInputSpec
output_spec = DWIExtractOutputSpec
def _list_outputs(self):
outputs = self.output_spec().get()
outputs["out_file"] = op.abspath(self.inputs.out_file)
return outputs
class MRConvertInputSpec(MRTrix3BaseInputSpec):
in_file = File(
exists=True, argstr="%s", mandatory=True, position=-2, desc="input image"
)
out_file = File(
"dwi.mif",
argstr="%s",
mandatory=True,
position=-1,
usedefault=True,
desc="output image",
)
coord = traits.List(
traits.Int,
sep=" ",
argstr="-coord %s",
desc="extract data at the specified coordinates",
)
vox = traits.List(
traits.Float, sep=",", argstr="-vox %s", desc="change the voxel dimensions"
)
axes = traits.List(
traits.Int,
sep=",",
argstr="-axes %s",
desc="specify the axes that will be used",
)
scaling = traits.List(
traits.Float,
sep=",",
argstr="-scaling %s",
desc="specify the data scaling parameter",
)
json_import = File(
exists=True,
argstr="-json_import %s",
mandatory=False,
desc="import data from a JSON file into header key-value pairs",
)
json_export = File(
exists=False,
argstr="-json_export %s",
mandatory=False,
desc="export data from an image header key-value pairs into a JSON file",
)
class MRConvertOutputSpec(TraitedSpec):
out_file = File(exists=True, desc="output image")
json_export = File(
exists=True,
desc="exported data from an image header key-value pairs in a JSON file",
)
out_bvec = File(exists=True, desc="export bvec file in FSL format")
out_bval = File(exists=True, desc="export bvec file in FSL format")
class MRConvert(MRTrix3Base):
"""
Perform conversion between different file types and optionally extract a
subset of the input image
Example
-------
>>> import nipype.interfaces.mrtrix3 as mrt
>>> mrconvert = mrt.MRConvert()
>>> mrconvert.inputs.in_file = 'dwi.nii.gz'
>>> mrconvert.inputs.grad_fsl = ('bvecs', 'bvals')
>>> mrconvert.cmdline # doctest: +ELLIPSIS
'mrconvert -fslgrad bvecs bvals dwi.nii.gz dwi.mif'
>>> mrconvert.run() # doctest: +SKIP
"""
_cmd = "mrconvert"
input_spec = MRConvertInputSpec
output_spec = MRConvertOutputSpec
def _list_outputs(self):
outputs = self.output_spec().get()
outputs["out_file"] = op.abspath(self.inputs.out_file)
if self.inputs.json_export:
outputs["json_export"] = op.abspath(self.inputs.json_export)
if self.inputs.out_bvec:
outputs["out_bvec"] = op.abspath(self.inputs.out_bvec)
if self.inputs.out_bval:
outputs["out_bval"] = op.abspath(self.inputs.out_bval)
return outputs
class TransformFSLConvertInputSpec(MRTrix3BaseInputSpec):
in_file = File(
exists=True,
argstr="%s",
mandatory=True,
position=1,
desc="FLIRT input image",
)
reference = File(
exists=True,
argstr="%s",
mandatory=True,
position=2,
desc="FLIRT reference image",
)
in_transform = File(
exists=True,
argstr="%s",
mandatory=True,
position=0,
desc="FLIRT output transformation matrix",
)
out_transform = File(
"transform_mrtrix.txt",
argstr="%s",
mandatory=True,
position=-1,
usedefault=True,
desc="output transformed affine in mrtrix3's format",
)
flirt_import = traits.Bool(
True,
argstr="flirt_import",
mandatory=True,
usedefault=True,
position=-2,
desc="import transform from FSL's FLIRT.",
)
class TransformFSLConvertOutputSpec(TraitedSpec):
out_transform = File(
exists=True, desc="output transformed affine in mrtrix3's format"
)
class TransformFSLConvert(MRTrix3Base):
"""
Perform conversion between FSL's transformation matrix format to mrtrix3's.
"""
_cmd = "transformconvert"
input_spec = TransformFSLConvertInputSpec
output_spec = TransformFSLConvertOutputSpec
def _list_outputs(self):
outputs = self.output_spec().get()
outputs["out_transform"] = op.abspath(self.inputs.out_transform)
return outputs
class MRTransformInputSpec(MRTrix3BaseInputSpec):
in_files = InputMultiPath(
File(exists=True),
argstr="%s",
mandatory=True,
position=-2,
desc="Input images to be transformed",
)
out_file = File(
genfile=True,
argstr="%s",
position=-1,
desc="Output image",
)
invert = traits.Bool(
argstr="-inverse",
position=1,
desc="Invert the specified transform before using it",
)
linear_transform = File(
exists=True,
argstr="-linear %s",
position=1,
desc=(
"Specify a linear transform to apply, in the form of a 3x4 or 4x4 ascii file. "
"Note the standard reverse convention is used, "
"where the transform maps points in the template image to the moving image. "
"Note that the reverse convention is still assumed even if no -template image is supplied."
),
)
replace_transform = traits.Bool(
argstr="-replace",
position=1,
desc="replace the current transform by that specified, rather than applying it to the current transform",
)
transformation_file = File(
exists=True,
argstr="-transform %s",
position=1,
desc="The transform to apply, in the form of a 4x4 ascii file.",
)
template_image = File(
exists=True,
argstr="-template %s",
position=1,
desc="Reslice the input image to match the specified template image.",
)
reference_image = File(
exists=True,
argstr="-reference %s",
position=1,
desc="in case the transform supplied maps from the input image onto a reference image, use this option to specify the reference. Note that this implicitly sets the -replace option.",
)
flip_x = traits.Bool(
argstr="-flipx",
position=1,
desc="assume the transform is supplied assuming a coordinate system with the x-axis reversed relative to the MRtrix convention (i.e. x increases from right to left). This is required to handle transform matrices produced by FSL's FLIRT command. This is only used in conjunction with the -reference option.",
)
quiet = traits.Bool(
argstr="-quiet",
position=1,
desc="Do not display information messages or progress status.",
)
debug = traits.Bool(argstr="-debug", position=1, desc="Display debugging messages.")
class MRTransformOutputSpec(TraitedSpec):
out_file = File(exists=True, desc="the output image of the transformation")
class MRTransform(MRTrix3Base):
"""
Apply spatial transformations or reslice images
Example
-------
>>> MRxform = MRTransform()
>>> MRxform.inputs.in_files = 'anat_coreg.mif'
>>> MRxform.run() # doctest: +SKIP
"""
_cmd = "mrtransform"
input_spec = MRTransformInputSpec
output_spec = MRTransformOutputSpec
def _list_outputs(self):
outputs = self.output_spec().get()
outputs["out_file"] = self.inputs.out_file
if not isdefined(outputs["out_file"]):
outputs["out_file"] = op.abspath(self._gen_outfilename())
else:
outputs["out_file"] = op.abspath(outputs["out_file"])
return outputs
def _gen_filename(self, name):
if name == "out_file":
return self._gen_outfilename()
else:
return None
def _gen_outfilename(self):
_, name, _ = split_filename(self.inputs.in_files[0])
return name + "_MRTransform.mif"
class MRMathInputSpec(MRTrix3BaseInputSpec):
in_file = File(
exists=True, argstr="%s", mandatory=True, position=-3, desc="input image"
)
out_file = File(argstr="%s", mandatory=True, position=-1, desc="output image")
operation = traits.Enum(
"mean",
"median",
"sum",
"product",
"rms",
"norm",
"var",
"std",
"min",
"max",
"absmax",
"magmax",
argstr="%s",
position=-2,
mandatory=True,
desc="operation to computer along a specified axis",
)
axis = traits.Int(
0, argstr="-axis %d", desc="specified axis to perform the operation along"
)
class MRMathOutputSpec(TraitedSpec):
out_file = File(exists=True, desc="output image")
class MRMath(MRTrix3Base):
"""
Compute summary statistic on image intensities
along a specified axis of a single image
Example
-------
>>> import nipype.interfaces.mrtrix3 as mrt
>>> mrmath = mrt.MRMath()
>>> mrmath.inputs.in_file = 'dwi.mif'
>>> mrmath.inputs.operation = 'mean'
>>> mrmath.inputs.axis = 3
>>> mrmath.inputs.out_file = 'dwi_mean.mif'
>>> mrmath.inputs.grad_fsl = ('bvecs', 'bvals')
>>> mrmath.cmdline # doctest: +ELLIPSIS
'mrmath -axis 3 -fslgrad bvecs bvals dwi.mif mean dwi_mean.mif'
>>> mrmath.run() # doctest: +SKIP
"""
_cmd = "mrmath"
input_spec = MRMathInputSpec
output_spec = MRMathOutputSpec
def _list_outputs(self):
outputs = self.output_spec().get()
outputs["out_file"] = op.abspath(self.inputs.out_file)
return outputs
class MRResizeInputSpec(MRTrix3BaseInputSpec):
in_file = File(
exists=True, argstr="%s", position=-2, mandatory=True, desc="input DWI image"
)
image_size = traits.Tuple(
(traits.Int, traits.Int, traits.Int),
argstr="-size %d,%d,%d",
mandatory=True,
desc="Number of voxels in each dimension of output image",
xor=["voxel_size", "scale_factor"],
)
voxel_size = traits.Tuple(
(traits.Float, traits.Float, traits.Float),
argstr="-voxel %g,%g,%g",
mandatory=True,
desc="Desired voxel size in mm for the output image",
xor=["image_size", "scale_factor"],
)
scale_factor = traits.Tuple(
(traits.Float, traits.Float, traits.Float),
argstr="-scale %g,%g,%g",
mandatory=True,