1
1
from argparse import ArgumentParser , RawDescriptionHelpFormatter
2
- from textwrap import dedent
2
+ import os
3
3
import sys
4
+ from textwrap import dedent
5
+
4
6
5
7
from .linear import load as linload
6
8
from .nonlinear import load as nlinload
@@ -12,7 +14,7 @@ def cli_apply(pargs):
12
14
13
15
Sample usage:
14
16
15
- $ nt apply xform.fsl moving.nii.gz --ref reference.nii.gz
17
+ $ nt apply xform.fsl moving.nii.gz --ref reference.nii.gz --out moved.nii.gz
16
18
17
19
$ nt apply warp.nii.gz moving.nii.gz --fmt afni --nonlinear
18
20
@@ -24,22 +26,28 @@ def cli_apply(pargs):
24
26
fmt = 'fs'
25
27
26
28
if fmt not in ('fs' , 'itk' , 'fsl' , 'afni' ):
27
- raise RuntimeError (
29
+ raise ValueError (
28
30
"Cannot determine transformation format, manually set format with the `--fmt` flag"
29
31
)
30
32
31
33
if pargs .nonlinear :
32
- xfm = nlinload (pargs .transform , fmt = fmt , reference = pargs . ref )
34
+ xfm = nlinload (pargs .transform , fmt = fmt )
33
35
else :
34
36
xfm = linload (pargs .transform , fmt = fmt )
35
37
36
- xfm .apply (
38
+ # ensure a reference is set
39
+ xfm .reference = pargs .ref or pargs .moving
40
+
41
+ moved = xfm .apply (
37
42
pargs .moving ,
38
43
order = pargs .order ,
39
44
mode = pargs .mode ,
40
45
cval = pargs .cval ,
41
46
prefilter = pargs .prefilter
42
47
)
48
+ moved .to_filename (
49
+ pargs .out or "nt_{}" .format (os .path .basename (pargs .moving ))
50
+ )
43
51
44
52
45
53
def get_parser ():
@@ -67,6 +75,7 @@ def _add_subparser(name, description):
67
75
return subp
68
76
69
77
applyp = _add_subparser ('apply' , cli_apply .__doc__ )
78
+ applyp .set_defaults (func = cli_apply )
70
79
applyp .add_argument ('transform' , help = 'The transform file' )
71
80
applyp .add_argument (
72
81
'moving' , help = 'The image containing the data to be resampled'
@@ -78,6 +87,9 @@ def _add_subparser(name, description):
78
87
help = 'Format of transformation. If no option is passed, nitransforms will '
79
88
'estimate based on the transformation file extension.'
80
89
)
90
+ applyp .add_argument (
91
+ '--out' , help = "The transformed image. If not set, will be set to `nt_{moving}`"
92
+ )
81
93
applyp .add_argument (
82
94
'--nonlinear' , action = 'store_true' , help = 'Transformation is nonlinear (default: False)'
83
95
)
@@ -108,15 +120,19 @@ def _add_subparser(name, description):
108
120
help = "Determines if the image's data array is prefiltered with a spline filter before "
109
121
"interpolation (default: True)"
110
122
)
111
- return parser
123
+ return parser , subparsers
112
124
113
125
114
126
def main (pargs = None ):
115
- parser = get_parser ()
127
+ parser , subparsers = get_parser ()
116
128
pargs = parser .parse_args (pargs )
117
- if pargs .command is None :
129
+ if not pargs .command or 'func' not in pargs :
118
130
parser .print_help ()
119
131
sys .exit (1 )
120
132
121
- if pargs .command == 'apply' :
122
- cli_apply (pargs )
133
+ try :
134
+ pargs .func (pargs )
135
+ except Exception as e :
136
+ subparser = subparsers .choices [pargs .command ]
137
+ subparser .print_help ()
138
+ raise (e )
0 commit comments