File: nipy_3dto4d

package info (click to toggle)
nipy 0.1.2%2B20100526-2
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 11,992 kB
  • ctags: 13,434
  • sloc: python: 47,720; ansic: 41,334; makefile: 197
file content (61 lines) | stat: -rwxr-xr-x 1,972 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/env python
# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*-
# vi: set ft=python sts=4 ts=4 sw=4 et:
''' Tiny script to write 4D file in any format that we write (nifti,
analyze, at the moment, from input 3d files '''

import os
from os.path import join as pjoin

import nipy.externals.argparse as argparse
import nipy.io.imageformats as nii


def do_3d_to_4d(filenames, check_affines=True):
    imgs = []
    for fname in filenames:
        img = nii.load(fname)
        imgs.append(img)
    return nii.concat_images(imgs, check_affines=check_affines)


def main():
    # create the parser
    parser = argparse.ArgumentParser()
    # add the arguments
    parser.add_argument('in_filenames', type=str,
                        nargs='+', 
                        help='3D image filenames')
    parser.add_argument('--out-4d', type=str,
                        help='4D output image name')
    parser.add_argument('--check-affines', type=bool,
                        default=True,
                        help='False if you want to ignore differences '
                        'in affines between the 3D images, True if you '
                        'want to raise an error for significant '
                        'differences (default is True)')
    # parse the command line
    args = parser.parse_args()
    # get input 3ds
    filenames = args.in_filenames
    # affine check
    check_affines = args.check_affines
    # get output name
    out_fname = args.out_4d
    if out_fname is None:
        pth, fname = os.path.split(filenames[0])
        froot, ext = os.path.splitext(fname)
        if ext in ('.gz', '.bz2'):
            gz = ext
            froot, ext = os.path.splitext(froot)
        else:
            gz = ''
        out_fname = pjoin(pth, froot + '_4d' + ext + gz)
    img4d = do_3d_to_4d(filenames, check_affines=check_affines)
    nii.save(img4d, out_fname)
    

if __name__ == '__main__':
    main()