Commit da6d3389 authored by Paul McCarthy's avatar Paul McCarthy 🚵
Browse files

TEST: Basic unit test for outprefix option

parent 35aa3ba7
......@@ -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)
......
......@@ -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')
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment