1
1
from argparse import ArgumentParser , RawDescriptionHelpFormatter
2
2
from textwrap import dedent
3
-
4
- import nibabel as nb
3
+ import sys
5
4
6
5
from .linear import load as linload
7
6
from .nonlinear import load as nlinload
8
7
9
- def cli_apply (** kwargs ):
8
+
9
+ def cli_apply (pargs ):
10
10
"""
11
11
Apply a transformation to an image, resampling on the reference
12
12
13
- Usage:
13
+ Sample usage:
14
+
15
+ $ nt apply xform.fsl moving.nii.gz --ref reference.nii.gz
16
+
17
+ $ nt apply warp.nii.gz moving.nii.gz --fmt afni --nonlinear
14
18
15
- $ nt apply moving.nii.gz xform.fsl --reference reference.nii.gz
16
19
"""
17
- try :
18
- xfm = linload (xform )
19
- except :
20
- xfm = nlinload (xform )
20
+ fmt = pargs .fmt or pargs .transform .split ('.' )[- 1 ]
21
+ if fmt == 'tfm' :
22
+ fmt = 'itk'
23
+ elif fmt == 'lta' :
24
+ fmt = 'fs'
25
+
26
+ if fmt not in ('fs' , 'itk' , 'fsl' , 'afni' ):
27
+ raise RuntimeError (
28
+ "Cannot determine transformation format, manually set format with the `--fmt` flag"
29
+ )
21
30
22
- xfm .apply (moving , ** kwargs )
31
+ if pargs .nonlinear :
32
+ xfm = nlinload (pargs .transform , fmt = fmt , reference = pargs .ref )
33
+ else :
34
+ xfm = linload (pargs .transform , fmt = fmt )
35
+
36
+ xfm .apply (
37
+ pargs .moving ,
38
+ order = pargs .order ,
39
+ mode = pargs .mode ,
40
+ cval = pargs .cval ,
41
+ prefilter = pargs .prefilter
42
+ )
23
43
24
44
25
45
def get_parser ():
26
- desc = dedent (
27
- """
46
+ desc = dedent ("""
28
47
NiTransforms command-line utility.
29
48
30
49
Commands:
31
50
32
51
apply Apply a transformation to an image
33
52
34
53
For command specific information, use 'nt <command> -h'.
35
- """
36
- )
54
+ """ )
37
55
38
56
parser = ArgumentParser (
39
57
description = desc , formatter_class = RawDescriptionHelpFormatter
40
58
)
41
59
subparsers = parser .add_subparsers (dest = 'command' )
60
+
42
61
def _add_subparser (name , description ):
43
62
subp = subparsers .add_parser (
44
63
name ,
@@ -52,18 +71,42 @@ def _add_subparser(name, description):
52
71
applyp .add_argument (
53
72
'moving' , help = 'The image containing the data to be resampled'
54
73
)
55
- applyp .add_argument ('reference' , help = 'The reference space to resample onto' )
56
- applyp .add_argument ('--order' , help = 'The order of the spline transformation' )
74
+ applyp .add_argument ('--ref' , help = 'The reference space to resample onto' )
57
75
applyp .add_argument (
58
- '--mode' ,
59
- help = 'Determines how the input image is extended when the resampling overflows a border'
76
+ '--fmt' ,
77
+ choices = ('itk' , 'fsl' , 'afni' , 'fs' ),
78
+ help = 'Format of transformation. If no option is passed, nitransforms will '
79
+ 'estimate based on the transformation file extension.'
60
80
)
61
- applyp .add_argument ('--cval' , help = 'Constant used when using "constant" mode' )
62
81
applyp .add_argument (
63
- '--prefilter' ,
64
- action = 'store_true' ,
82
+ '--nonlinear' , action = 'store_true' , help = 'Transformation is nonlinear (default: False)'
83
+ )
84
+ applykwargs = applyp .add_argument_group ('Apply customization' )
85
+ applykwargs .add_argument (
86
+ '--order' ,
87
+ type = int ,
88
+ default = 3 ,
89
+ choices = range (6 ),
90
+ help = 'The order of the spline transformation (default: 3)'
91
+ )
92
+ applykwargs .add_argument (
93
+ '--mode' ,
94
+ choices = ('constant' , 'reflect' , 'nearest' , 'mirror' , 'wrap' ),
95
+ default = 'constant' ,
96
+ help = 'Determines how the input image is extended when the resampling overflows a border '
97
+ '(default: constant)'
98
+ )
99
+ applykwargs .add_argument (
100
+ '--cval' ,
101
+ type = float ,
102
+ default = 0.0 ,
103
+ help = 'Constant used when using "constant" mode (default: 0.0)'
104
+ )
105
+ applykwargs .add_argument (
106
+ '--prefilter' ,
107
+ action = 'store_false' ,
65
108
help = "Determines if the image's data array is prefiltered with a spline filter before "
66
- "interpolation"
109
+ "interpolation (default: True) "
67
110
)
68
111
return parser
69
112
@@ -73,7 +116,7 @@ def main(pargs=None):
73
116
pargs = parser .parse_args (pargs )
74
117
if pargs .command is None :
75
118
parser .print_help ()
76
- return
119
+ sys . exit ( 1 )
77
120
78
121
if pargs .command == 'apply' :
79
- cli_apply (** vars ( pargs ) )
122
+ cli_apply (pargs )
0 commit comments