diff --git a/tests/__init__.py b/tests/__init__.py index d8647089277169add9aead66a38bd911615448e3..1106e3059ababa34b839dbea6b106856d596f4bd 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 b5c393e78d92c1360576a1dbe309cb5587898ce7..6ed89c2c47f1f8fd6e27b57d938085738c5e72d0 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')