Permalink
Browse files

RF: refactor scripts to extend arguments and help

Add more help and command line arguments. Make tsdiffana work better
without a screen.
  • Loading branch information...
1 parent cfd81d9 commit ae7d234058ba5dec3e4be17c02066021e6852357 @matthew-brett matthew-brett committed Dec 19, 2012
Showing with 39 additions and 23 deletions.
  1. +14 −9 scripts/nipy_3dto4d
  2. +20 −13 scripts/nipy_4dto3d
  3. +5 −1 scripts/nipy_tsdiffana
View
@@ -1,13 +1,20 @@
#!/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 '''
+DESCRIP = 'Read 3D image files and write a 4D file'
+EPILOG = \
+'''nipy_3dto4d will take a series of 3D nifti images in any format readable by
+nibabel and concatenate them into a 4D image, and write the image with format
+guessed from the output image filename. You can set the filename with the
+``--out-4d`` parameter, or we make a filename from the input names.
+'''
import os
from os.path import join as pjoin
-import nipy.externals.argparse as argparse
+from nipy.externals.argparse import (ArgumentParser,
+ RawDescriptionHelpFormatter)
+
import nibabel as nib
@@ -20,11 +27,11 @@ def do_3d_to_4d(filenames, check_affines=True):
def main():
- # create the parser
- parser = argparse.ArgumentParser()
- # add the arguments
+ parser = ArgumentParser(description=DESCRIP,
+ epilog=EPILOG,
+ formatter_class=RawDescriptionHelpFormatter)
parser.add_argument('in_filenames', type=str,
- nargs='+',
+ nargs='+',
help='3D image filenames')
parser.add_argument('--out-4d', type=str,
help='4D output image name')
@@ -57,5 +64,3 @@ def main():
if __name__ == '__main__':
main()
-
-
View
@@ -1,34 +1,41 @@
#!/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 files in any format that we read (nifti,
-analyze, MINC, at the moment, as nifti 3D files '''
+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 os
-
-import nipy.externals.argparse as argparse
import nibabel as nib
+from nipy.externals.argparse import (ArgumentParser,
+ RawDescriptionHelpFormatter)
+
def main():
- # create the parser
- parser = argparse.ArgumentParser()
- # add the arguments
+ parser = ArgumentParser(description=DESCRIP,
+ epilog=EPILOG,
+ formatter_class=RawDescriptionHelpFormatter)
parser.add_argument('filename', type=str,
help='4D image filename')
- # parse the command line
+ 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 = os.path.splitext(args.filename)
+ froot, ext = splitext(args.filename)
if ext in ('.gz', '.bz2'):
- froot, ext = os.path.splitext(froot)
+ froot, ext = splitext(froot)
+ if not out_path is 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()
-
-
View
@@ -4,7 +4,6 @@
''' Analyze, plot time series difference metrics'''
import nipy.externals.argparse as argparse
-import nipy.algorithms.diagnostics as nad
def main():
@@ -19,6 +18,11 @@ def main():
# parse the command line
args = parser.parse_args()
show = args.out_file is None
+ if not show:
+ import matplotlib
+ matplotlib.use('Agg')
+ # Import late to allow setting Agg backend
+ import nipy.algorithms.diagnostics as nad
axes = nad.plot_tsdiffs_image(args.filename, show=show)
if args.out_file:
axes[0].figure.savefig(args.out_file)

0 comments on commit ae7d234

Please sign in to comment.