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