File: nipy_4dto3d

package info (click to toggle)
nipy 0.4.2-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 13,128 kB
  • sloc: python: 45,886; ansic: 30,932; makefile: 253; sh: 53
file content (41 lines) | stat: -rwxr-xr-x 1,419 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#!python
# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*-
# vi: set ft=python sts=4 ts=4 sw=4 et:
DESCRIP = 'Read 4D image file and write 3D nifti file for each volume'
EPILOG = \
'''nipy_4dto3d will generate a series of 3D nifti images for each volume a 4D
image series in any format readable by `nibabel`.
'''
from os.path import splitext, join as pjoin, split as psplit

import nibabel as nib

from nipy.externals.argparse import (ArgumentParser,
                                     RawDescriptionHelpFormatter)


def main():
    parser = ArgumentParser(description=DESCRIP,
                            epilog=EPILOG,
                            formatter_class=RawDescriptionHelpFormatter)
    parser.add_argument('filename', type=str,
                        help='4D image filename')
    parser.add_argument('--out-path', type=str,
                        help='path for output image files')
    args = parser.parse_args()
    out_path = args.out_path
    img = nib.load(args.filename)
    imgs = nib.four_to_three(img)
    froot, ext = splitext(args.filename)
    if ext in ('.gz', '.bz2'):
        froot, ext = splitext(froot)
    if out_path is not None:
        pth, fname = psplit(froot)
        froot = pjoin(out_path, fname)
    for i, img3d in enumerate(imgs):
        fname3d = '%s_%04d.nii' % (froot, i)
        nib.save(img3d, fname3d)


if __name__ == '__main__':
    main()