|
| 1 | +#!python |
| 2 | +# emacs: -*- mode: python-mode; py-indent-offset: 4; indent-tabs-mode: nil -*- |
| 3 | +# vi: set ft=python sts=4 ts=4 sw=4 et: |
| 4 | +### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ## |
| 5 | +# |
| 6 | +# See COPYING file distributed along with the NiBabel package for the |
| 7 | +# copyright and license terms. |
| 8 | +# |
| 9 | +### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ## |
| 10 | +""" |
| 11 | +Compute image statistics |
| 12 | +""" |
| 13 | + |
| 14 | +import argparse |
| 15 | +from nibabel.loadsave import load |
| 16 | +from nibabel.imagestats import mask_volume, count_nonzero_voxels |
| 17 | + |
| 18 | + |
| 19 | +def _get_parser(): |
| 20 | + """Return command-line argument parser.""" |
| 21 | + p = argparse.ArgumentParser(description=__doc__) |
| 22 | + p.add_argument("infile", |
| 23 | + help="Neuroimaging volume to compute statistics on.") |
| 24 | + p.add_argument("-V", "--Volume", action="store_true", required=False, |
| 25 | + help="Compute mask volume of a given mask image.") |
| 26 | + p.add_argument("--units", default="mm3", required=False, |
| 27 | + choices=("mm3", "vox"), help="Preferred output units") |
| 28 | + return p |
| 29 | + |
| 30 | + |
| 31 | +def main(args=None): |
| 32 | + """Main program function.""" |
| 33 | + parser = _get_parser() |
| 34 | + opts = parser.parse_args(args) |
| 35 | + from_img = load(opts.infile) |
| 36 | + |
| 37 | + if opts.Volume: |
| 38 | + if opts.units == 'mm3': |
| 39 | + computed_volume = mask_volume(from_img) |
| 40 | + elif opts.units == 'vox': |
| 41 | + computed_volume = count_nonzero_voxels(from_img) |
| 42 | + else: |
| 43 | + raise ValueError(f'{opts.units} is not a valid unit. Choose "mm3" or "vox".') |
| 44 | + print(computed_volume) |
| 45 | + return 0 |
0 commit comments