Skip to content

Commit 8bedfa4

Browse files
committed
enh: improve argument documentation, some basic validation
1 parent ce59572 commit 8bedfa4

File tree

1 file changed

+67
-24
lines changed

1 file changed

+67
-24
lines changed

nitransforms/cli.py

Lines changed: 67 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,63 @@
11
from argparse import ArgumentParser, RawDescriptionHelpFormatter
22
from textwrap import dedent
3-
4-
import nibabel as nb
3+
import sys
54

65
from .linear import load as linload
76
from .nonlinear import load as nlinload
87

9-
def cli_apply(**kwargs):
8+
9+
def cli_apply(pargs):
1010
"""
1111
Apply a transformation to an image, resampling on the reference
1212
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
1418
15-
$ nt apply moving.nii.gz xform.fsl --reference reference.nii.gz
1619
"""
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+
)
2130

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+
)
2343

2444

2545
def get_parser():
26-
desc = dedent(
27-
"""
46+
desc = dedent("""
2847
NiTransforms command-line utility.
2948
3049
Commands:
3150
3251
apply Apply a transformation to an image
3352
3453
For command specific information, use 'nt <command> -h'.
35-
"""
36-
)
54+
""")
3755

3856
parser = ArgumentParser(
3957
description=desc, formatter_class=RawDescriptionHelpFormatter
4058
)
4159
subparsers = parser.add_subparsers(dest='command')
60+
4261
def _add_subparser(name, description):
4362
subp = subparsers.add_parser(
4463
name,
@@ -52,18 +71,42 @@ def _add_subparser(name, description):
5271
applyp.add_argument(
5372
'moving', help='The image containing the data to be resampled'
5473
)
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')
5775
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.'
6080
)
61-
applyp.add_argument('--cval', help='Constant used when using "constant" mode')
6281
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',
65108
help="Determines if the image's data array is prefiltered with a spline filter before "
66-
"interpolation"
109+
"interpolation (default: True)"
67110
)
68111
return parser
69112

@@ -73,7 +116,7 @@ def main(pargs=None):
73116
pargs = parser.parse_args(pargs)
74117
if pargs.command is None:
75118
parser.print_help()
76-
return
119+
sys.exit(1)
77120

78121
if pargs.command == 'apply':
79-
cli_apply(**vars(pargs))
122+
cli_apply(pargs)

0 commit comments

Comments
 (0)