Skip to content

Commit 6a06cdc

Browse files
committed
enh+rf: output moved image, improve parser
1 parent 8bedfa4 commit 6a06cdc

File tree

1 file changed

+26
-10
lines changed

1 file changed

+26
-10
lines changed

nitransforms/cli.py

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
from argparse import ArgumentParser, RawDescriptionHelpFormatter
2-
from textwrap import dedent
2+
import os
33
import sys
4+
from textwrap import dedent
5+
46

57
from .linear import load as linload
68
from .nonlinear import load as nlinload
@@ -12,7 +14,7 @@ def cli_apply(pargs):
1214
1315
Sample usage:
1416
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
1618
1719
$ nt apply warp.nii.gz moving.nii.gz --fmt afni --nonlinear
1820
@@ -24,22 +26,28 @@ def cli_apply(pargs):
2426
fmt = 'fs'
2527

2628
if fmt not in ('fs', 'itk', 'fsl', 'afni'):
27-
raise RuntimeError(
29+
raise ValueError(
2830
"Cannot determine transformation format, manually set format with the `--fmt` flag"
2931
)
3032

3133
if pargs.nonlinear:
32-
xfm = nlinload(pargs.transform, fmt=fmt, reference=pargs.ref)
34+
xfm = nlinload(pargs.transform, fmt=fmt)
3335
else:
3436
xfm = linload(pargs.transform, fmt=fmt)
3537

36-
xfm.apply(
38+
# ensure a reference is set
39+
xfm.reference = pargs.ref or pargs.moving
40+
41+
moved = xfm.apply(
3742
pargs.moving,
3843
order=pargs.order,
3944
mode=pargs.mode,
4045
cval=pargs.cval,
4146
prefilter=pargs.prefilter
4247
)
48+
moved.to_filename(
49+
pargs.out or "nt_{}".format(os.path.basename(pargs.moving))
50+
)
4351

4452

4553
def get_parser():
@@ -67,6 +75,7 @@ def _add_subparser(name, description):
6775
return subp
6876

6977
applyp = _add_subparser('apply', cli_apply.__doc__)
78+
applyp.set_defaults(func=cli_apply)
7079
applyp.add_argument('transform', help='The transform file')
7180
applyp.add_argument(
7281
'moving', help='The image containing the data to be resampled'
@@ -78,6 +87,9 @@ def _add_subparser(name, description):
7887
help='Format of transformation. If no option is passed, nitransforms will '
7988
'estimate based on the transformation file extension.'
8089
)
90+
applyp.add_argument(
91+
'--out', help="The transformed image. If not set, will be set to `nt_{moving}`"
92+
)
8193
applyp.add_argument(
8294
'--nonlinear', action='store_true', help='Transformation is nonlinear (default: False)'
8395
)
@@ -108,15 +120,19 @@ def _add_subparser(name, description):
108120
help="Determines if the image's data array is prefiltered with a spline filter before "
109121
"interpolation (default: True)"
110122
)
111-
return parser
123+
return parser, subparsers
112124

113125

114126
def main(pargs=None):
115-
parser = get_parser()
127+
parser, subparsers = get_parser()
116128
pargs = parser.parse_args(pargs)
117-
if pargs.command is None:
129+
if not pargs.command or 'func' not in pargs:
118130
parser.print_help()
119131
sys.exit(1)
120132

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

Comments
 (0)