From a5b31bd45a54c317957910d0679b694dfa432de6 Mon Sep 17 00:00:00 2001 From: Paul McCarthy <pauldmccarthy@gmail.com> Date: Sat, 7 Jul 2018 12:54:18 +0100 Subject: [PATCH] TEST: Unit tests for _FileOrThing --- tests/__init__.py | 5 ++++ tests/test_wrappers.py | 19 ++++++++---- tests/test_wrapperutils.py | 61 ++++++++++++++++++++++++++++++++++---- 3 files changed, 74 insertions(+), 11 deletions(-) diff --git a/tests/__init__.py b/tests/__init__.py index 1106e3059..eb16fa95a 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -203,6 +203,11 @@ def cleardir(dir, pat=None): elif op.isdir(f): shutil.rmtree(f) +def checkdir(dir, *expfiles): + for f in expfiles: + assert op.exists(op.join(dir, f)) + + def random_voxels(shape, nvoxels=1): randVoxels = np.vstack( [np.random.randint(0, s, nvoxels) for s in shape[:3]]).T diff --git a/tests/test_wrappers.py b/tests/test_wrappers.py index 1e366a166..5dcca79c1 100644 --- a/tests/test_wrappers.py +++ b/tests/test_wrappers.py @@ -15,14 +15,23 @@ import fsl.utils.run as run from . import mockFSLDIR -def checkResult(cmd, base, args): +def checkResult(cmd, base, args, stripdir=None): """We can't control the order in which command line args are generated, so we need to test all possible orderings. - :arg cmd: Generated command - :arg base: Beginning of expected command - :arg args: Sequence of expected arguments + :arg cmd: Generated command + :arg base: Beginning of expected command + :arg args: Sequence of expected arguments + :arg stripdir: Sequence of indices indicating arguments + for whihc any leading directory should be ignored. """ + + if stripdir is not None: + cmd = list(cmd.split()) + for si in stripdir: + cmd[si] = op.basename(cmd[si]) + cmd = ' '.join(cmd) + permutations = it.permutations(args, len(args)) possible = [' '.join([base] + list(p)) for p in permutations] @@ -34,7 +43,7 @@ def test_bet(): bet = op.join(fsldir, 'bin', 'bet') result = fw.bet('input', 'output', mask=True, c=(10, 20, 30)) expected = (bet + ' input output', ('-m', '-c 10 20 30')) - assert checkResult(result.output[0], *expected) + assert checkResult(result.output[0], *expected, stripdir=[2]) def test_robustfov(): diff --git a/tests/test_wrapperutils.py b/tests/test_wrapperutils.py index 43341f91b..90cfc7ff9 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, cleardir +from . import mockFSLDIR, cleardir, checkdir from .test_run import mock_submit @@ -311,10 +311,6 @@ def test_fileOrImage(): 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() @@ -325,6 +321,7 @@ def test_fileOrImage_outprefix(): 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 @@ -395,12 +392,64 @@ def test_fileOrImage_outprefix_differentTypes(): cleardir(td, 'myout*') +def test_fileOrImage_outprefix_directory(): + + import logging + logging.basicConfig() + logging.getLogger('fsl.wrappers').setLevel(logging.DEBUG) + + @wutils.fileOrImage('img', outprefix='outpref') + def func(img, outpref): + img = nib.load(img) + img2 = nib.nifti1.Nifti1Image(img.get_data() * 2, np.eye(4)) + img4 = nib.nifti1.Nifti1Image(img.get_data() * 4, np.eye(4)) + outdir = op.abspath('{}_imgs'.format(outpref)) -# test directory + os.mkdir(outdir) + nib.save(img2, op.join(outdir, 'img2.nii.gz')) + nib.save(img4, op.join(outdir, 'img4.nii.gz')) + with tempdir.tempdir() as td: + img = nib.nifti1.Nifti1Image(np.array([[1, 2], [3, 4]]), np.eye(4)) + exp2 = img.get_data() * 2 + exp4 = img.get_data() * 4 + + res = func(img, 'myout') + assert len(res) == 0 + checkdir(td, + op.join('myout_imgs', 'img2.nii.gz'), + op.join('myout_imgs', 'img4.nii.gz')) + cleardir(td, 'myout*') + res = func(img, 'myout', myout_imgs=wutils.LOAD) + assert len(res) == 2 + assert np.all(res['myout_imgs/img2'].get_data() == exp2) + assert np.all(res['myout_imgs/img4'].get_data() == exp4) + + res = func(img, 'myout', **{'myout_imgs/img2' : wutils.LOAD}) + assert len(res) == 1 + assert np.all(res['myout_imgs/img2'].get_data() == exp2) + + res = func(img, 'myout', **{'myout_imgs/img' : wutils.LOAD}) + assert len(res) == 2 + assert np.all(res['myout_imgs/img2'].get_data() == exp2) + assert np.all(res['myout_imgs/img4'].get_data() == exp4) + + os.mkdir('foo') + res = func(img, 'foo/myout') + assert len(res) == 0 + checkdir(td, + op.join('foo', 'myout_imgs', 'img2.nii.gz'), + op.join('foo', 'myout_imgs', 'img4.nii.gz')) + cleardir(td, 'foo') + + os.mkdir('foo') + res = func(img, 'foo/myout', **{'foo/myout' : wutils.LOAD}) + assert len(res) == 2 + assert np.all(res['foo/myout_imgs/img2'].get_data() == exp2) + assert np.all(res['foo/myout_imgs/img4'].get_data() == exp4) def test_chained_fileOrImageAndArray(): -- GitLab