Skip to content
Snippets Groups Projects
Commit a5b31bd4 authored by Paul McCarthy's avatar Paul McCarthy :mountain_bicyclist:
Browse files

TEST: Unit tests for _FileOrThing

parent 26b41155
No related branches found
No related tags found
No related merge requests found
...@@ -203,6 +203,11 @@ def cleardir(dir, pat=None): ...@@ -203,6 +203,11 @@ def cleardir(dir, pat=None):
elif op.isdir(f): shutil.rmtree(f) 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): def random_voxels(shape, nvoxels=1):
randVoxels = np.vstack( randVoxels = np.vstack(
[np.random.randint(0, s, nvoxels) for s in shape[:3]]).T [np.random.randint(0, s, nvoxels) for s in shape[:3]]).T
......
...@@ -15,14 +15,23 @@ import fsl.utils.run as run ...@@ -15,14 +15,23 @@ import fsl.utils.run as run
from . import mockFSLDIR 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, """We can't control the order in which command line args are generated,
so we need to test all possible orderings. so we need to test all possible orderings.
:arg cmd: Generated command :arg cmd: Generated command
:arg base: Beginning of expected command :arg base: Beginning of expected command
:arg args: Sequence of expected arguments :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)) permutations = it.permutations(args, len(args))
possible = [' '.join([base] + list(p)) for p in permutations] possible = [' '.join([base] + list(p)) for p in permutations]
...@@ -34,7 +43,7 @@ def test_bet(): ...@@ -34,7 +43,7 @@ def test_bet():
bet = op.join(fsldir, 'bin', 'bet') bet = op.join(fsldir, 'bin', 'bet')
result = fw.bet('input', 'output', mask=True, c=(10, 20, 30)) result = fw.bet('input', 'output', mask=True, c=(10, 20, 30))
expected = (bet + ' input output', ('-m', '-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(): def test_robustfov():
......
...@@ -25,7 +25,7 @@ import fsl.data.image as fslimage ...@@ -25,7 +25,7 @@ import fsl.data.image as fslimage
import fsl.wrappers.wrapperutils as wutils import fsl.wrappers.wrapperutils as wutils
from . import mockFSLDIR, cleardir from . import mockFSLDIR, cleardir, checkdir
from .test_run import mock_submit from .test_run import mock_submit
...@@ -311,10 +311,6 @@ def test_fileOrImage(): ...@@ -311,10 +311,6 @@ def test_fileOrImage():
def test_fileOrImage_outprefix(): def test_fileOrImage_outprefix():
import logging
logging.basicConfig()
logging.getLogger('fsl.wrappers').setLevel(logging.DEBUG)
@wutils.fileOrImage('img', outprefix='output_base') @wutils.fileOrImage('img', outprefix='output_base')
def basefunc(img, output_base): def basefunc(img, output_base):
img = nib.load(img).get_data() img = nib.load(img).get_data()
...@@ -325,6 +321,7 @@ def test_fileOrImage_outprefix(): ...@@ -325,6 +321,7 @@ def test_fileOrImage_outprefix():
nib.save(out1, '{}_times5.nii.gz' .format(output_base)) nib.save(out1, '{}_times5.nii.gz' .format(output_base))
nib.save(out2, '{}_times10.nii.gz'.format(output_base)) nib.save(out2, '{}_times10.nii.gz'.format(output_base))
with tempdir.tempdir() as td: with tempdir.tempdir() as td:
img = nib.nifti1.Nifti1Image(np.array([[1, 2], [3, 4]]), np.eye(4)) img = nib.nifti1.Nifti1Image(np.array([[1, 2], [3, 4]]), np.eye(4))
exp1 = img.get_data() * 5 exp1 = img.get_data() * 5
...@@ -395,12 +392,64 @@ def test_fileOrImage_outprefix_differentTypes(): ...@@ -395,12 +392,64 @@ def test_fileOrImage_outprefix_differentTypes():
cleardir(td, 'myout*') 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(): def test_chained_fileOrImageAndArray():
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment