From da6d338902c6ba0d2f9cbc6784b6208dc56ec681 Mon Sep 17 00:00:00 2001
From: Paul McCarthy <pauldmccarthy@gmail.com>
Date: Thu, 5 Jul 2018 15:14:24 +0100
Subject: [PATCH] TEST: Basic unit test for outprefix option

---
 tests/__init__.py          | 10 ++++++--
 tests/test_wrapperutils.py | 49 +++++++++++++++++++++++++++++++++++++-
 2 files changed, 56 insertions(+), 3 deletions(-)

diff --git a/tests/__init__.py b/tests/__init__.py
index d86470892..1106e3059 100644
--- a/tests/__init__.py
+++ b/tests/__init__.py
@@ -11,8 +11,9 @@ import              os
 import              sys
 import              glob
 import              shutil
-import              tempfile
+import              fnmatch
 import              logging
+import              tempfile
 import              contextlib
 import itertools as it
 import os.path   as op
@@ -187,12 +188,17 @@ def make_dummy_image_file(path):
         make_dummy_file(path)
 
 
-def cleardir(dir):
+def cleardir(dir, pat=None):
     """Deletes everything in the given directory, but not the directory
     itself.
     """
     for f in os.listdir(dir):
+
+        if pat is not None and not fnmatch.fnmatch(f, pat):
+            continue
+
         f = op.join(dir, f)
+
         if   op.isfile(f): os.remove(f)
         elif op.isdir(f):  shutil.rmtree(f)
 
diff --git a/tests/test_wrapperutils.py b/tests/test_wrapperutils.py
index b5c393e78..6ed89c2c4 100644
--- a/tests/test_wrapperutils.py
+++ b/tests/test_wrapperutils.py
@@ -25,7 +25,7 @@ import fsl.data.image            as fslimage
 import fsl.wrappers.wrapperutils as wutils
 
 
-from . import mockFSLDIR
+from . import mockFSLDIR, cleardir
 from .test_run import mock_submit
 
 
@@ -309,6 +309,53 @@ def test_fileOrImage():
         assert np.all(result.get_data()[:] == expected)
 
 
+def test_fileOrImage_outprefix():
+
+    import logging
+    logging.basicConfig()
+    logging.getLogger('fsl.wrappers').setLevel(logging.DEBUG)
+
+    @wutils.fileOrImage('img', outprefix='output_base')
+    def basefunc(img, output_base):
+        img = nib.load(img).get_data()
+
+        out1 = nib.nifti1.Nifti1Image(img * 5,  np.eye(4))
+        out2 = nib.nifti1.Nifti1Image(img * 10, np.eye(4))
+
+        nib.save(out1, '{}_times5.nii.gz' .format(output_base))
+        nib.save(out2, '{}_times10.nii.gz'.format(output_base))
+
+    with tempdir.tempdir() as td:
+        img  = nib.nifti1.Nifti1Image(np.array([[1, 2], [3, 4]]), np.eye(4))
+        exp1 = img.get_data() * 5
+        exp2 = img.get_data() * 10
+        nib.save(img, 'img.nii')
+
+        basefunc('img.nii', 'myout')
+        assert np.all(nib.load('myout_times5.nii.gz') .get_data() == exp1)
+        assert np.all(nib.load('myout_times10.nii.gz').get_data() == exp2)
+        cleardir(td, 'myout*')
+
+        basefunc(img, 'myout')
+        assert np.all(nib.load('myout_times5.nii.gz') .get_data() == exp1)
+        assert np.all(nib.load('myout_times10.nii.gz').get_data() == exp2)
+        cleardir(td, 'myout*')
+
+        res = basefunc(img, 'myout', myout_times5=wutils.LOAD)
+        assert np.all(res['myout_times5'].get_data() == exp1)
+        cleardir(td, 'myout*')
+
+        res = basefunc(img, 'myout', myout_times10=wutils.LOAD)
+        assert np.all(res['myout_times10'].get_data() == exp2)
+        cleardir(td, 'myout*')
+
+        res = basefunc(img, 'myout', myout=wutils.LOAD)
+        assert np.all(res['myout_times5'] .get_data() == exp1)
+        assert np.all(res['myout_times10'].get_data() == exp2)
+        cleardir(td, 'myout*')
+
+
+
 def test_chained_fileOrImageAndArray():
     @wutils.fileOrImage('image')
     @wutils.fileOrArray('array')
-- 
GitLab