Skip to content

Commit a61fcfe

Browse files
committed
enh: cli to apply transforms
1 parent ee5f8ef commit a61fcfe

File tree

2 files changed

+83
-0
lines changed

2 files changed

+83
-0
lines changed

nitransforms/cli.py

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
from argparse import ArgumentParser, RawDescriptionHelpFormatter
2+
from textwrap import dedent
3+
4+
import nibabel as nb
5+
6+
from .linear import load as linload
7+
from .nonlinear import load as nlinload
8+
9+
def cli_apply(**kwargs):
10+
"""
11+
Apply a transformation to an image, resampling on the reference
12+
13+
Usage:
14+
15+
$ nt apply moving.nii.gz xform.fsl --reference reference.nii.gz
16+
"""
17+
try:
18+
xfm = linload(xform)
19+
except:
20+
xfm = nlinload(xform)
21+
22+
xfm.apply(moving, **kwargs)
23+
24+
25+
def get_parser():
26+
desc = dedent(
27+
"""
28+
NiTransforms command-line utility.
29+
30+
Commands:
31+
32+
apply Apply a transformation to an image
33+
34+
For command specific information, use 'nt <command> -h'.
35+
"""
36+
)
37+
38+
parser = ArgumentParser(
39+
description=desc, formatter_class=RawDescriptionHelpFormatter
40+
)
41+
subparsers = parser.add_subparsers(dest='command')
42+
def _add_subparser(name, description):
43+
subp = subparsers.add_parser(
44+
name,
45+
description=dedent(description),
46+
formatter_class=RawDescriptionHelpFormatter,
47+
)
48+
return subp
49+
50+
applyp = _add_subparser('apply', cli_apply.__doc__)
51+
applyp.add_argument('transform', help='The transform file')
52+
applyp.add_argument(
53+
'moving', help='The image containing the data to be resampled'
54+
)
55+
applyp.add_argument('reference', help='The reference space to resample onto')
56+
applyp.add_argument('--order', help='The order of the spline transformation')
57+
applyp.add_argument(
58+
'--mode',
59+
help='Determines how the input image is extended when the resampling overflows a border'
60+
)
61+
applyp.add_argument('--cval', help='Constant used when using "constant" mode')
62+
applyp.add_argument(
63+
'--prefilter',
64+
action='store_true',
65+
help="Determines if the image's data array is prefiltered with a spline filter before "
66+
"interpolation"
67+
)
68+
return parser
69+
70+
71+
def main(pargs=None):
72+
parser = get_parser()
73+
pargs = parser.parse_args(pargs)
74+
if pargs.command is None:
75+
parser.print_help()
76+
return
77+
78+
if pargs.command == 'apply':
79+
cli_apply(**vars(pargs))

setup.cfg

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ tests =
2828
all =
2929
%(test)s
3030

31+
[options.entry_points]
32+
console_scripts =
33+
nt = nitransforms.cli:main
34+
3135
[flake8]
3236
max-line-length = 100
3337
ignore = D100,D101,D102,D103,D104,D105,D200,D201,D202,D204,D205,D208,D209,D210,D300,D301,D400,D401,D403,E24,E121,E123,E126,E226,E266,E402,E704,E731,F821,I100,I101,I201,N802,N803,N804,N806,W503,W504,W605

0 commit comments

Comments
 (0)