From f02bd21f0e000cfe12da30b5d39f3948817ca717 Mon Sep 17 00:00:00 2001
From: Michiel Cottaar <MichielCottaar@gmail.com>
Date: Thu, 26 Apr 2018 11:08:09 +0100
Subject: [PATCH] ENH: added command line support for output images (in FSL
 style)

---
 fsl/utils/parse_data.py  | 10 ++++++++++
 tests/test_parse_data.py | 33 ++++++++++++++++++++++++++++++++-
 2 files changed, 42 insertions(+), 1 deletion(-)

diff --git a/fsl/utils/parse_data.py b/fsl/utils/parse_data.py
index 613e33b57..90fa56761 100644
--- a/fsl/utils/parse_data.py
+++ b/fsl/utils/parse_data.py
@@ -40,6 +40,16 @@ def Image(filename):
     return image.Image(full_filename)
 
 
+def ImageOut(basename):
+    """
+    Uses the FSL convention to create a complete image output filename
+
+    :param basename: filename provided by the user
+    :return: filename with extension
+    """
+    return image.addExt(basename, mustExist=False)
+
+
 def Mesh(filename):
     """
     Reads in a mesh from either a GIFTI (.surf.gii) or a VTK (.vtk) file
diff --git a/tests/test_parse_data.py b/tests/test_parse_data.py
index eb1bc02d1..77a8dce24 100644
--- a/tests/test_parse_data.py
+++ b/tests/test_parse_data.py
@@ -6,7 +6,7 @@
 #
 
 import argparse
-from fsl.utils import parse_data, tempdir
+from fsl.utils import parse_data, tempdir, path
 import os.path as op
 from fsl.data.vtk import VTKMesh
 from fsl.data.gifti import GiftiMesh
@@ -14,6 +14,7 @@ from fsl.data.image import Image
 from fsl.data.atlases import Atlas
 from pytest import raises
 from .test_image import make_image
+import os
 
 
 datadir = op.join(op.dirname(__file__), 'testdata')
@@ -84,6 +85,36 @@ def test_image():
             image_parser.parse_args([double_filename])
 
 
+def test_image_out():
+    image_parser = argparse.ArgumentParser("Reads an image")
+    image_parser.add_argument("image_out", type=parse_data.ImageOut)
+    for fsl_output_type, extension in (
+            ('NIFTI', '.nii'),
+            ('NIFTI_PAIR', '.img'),
+            ('NIFTI_GZ', '.nii.gz')
+    ):
+        os.environ['FSLOUTPUTTYPE'] = fsl_output_type
+        args = image_parser.parse_args(['test'])
+        assert path.hasExt(args.image_out, extension)
+        assert args.image_out == 'test' + extension
+
+        args = image_parser.parse_args(['test.nii'])
+        assert path.hasExt(args.image_out, '.nii')
+        assert args.image_out == 'test.nii'
+
+        args = image_parser.parse_args(['test.nii.gz'])
+        assert path.hasExt(args.image_out, '.nii.gz')
+        assert args.image_out == 'test.nii.gz'
+
+        args = image_parser.parse_args(['test.img'])
+        assert path.hasExt(args.image_out, '.img')
+        assert args.image_out == 'test.img'
+
+        args = image_parser.parse_args(['test.surf.gii'])
+        assert path.hasExt(args.image_out, extension)
+        assert args.image_out == 'test.surf.gii' + extension
+
+
 def test_atlas():
     atlas_parser = argparse.ArgumentParser('reads an atlas')
     atlas_parser.add_argument('atlas', type=parse_data.Atlas)
-- 
GitLab