Commit 16c69632 authored by Paul McCarthy's avatar Paul McCarthy 🚵
Browse files

TEST: Windows compatibility

parent b50751a2
......@@ -41,7 +41,7 @@ def seed(request):
seed = request.config.getoption('--seed')
if seed is None:
seed = np.random.randint(2 ** 32)
seed = np.random.randint(2 ** 30)
np.random.seed(seed)
random .seed(seed)
......
......@@ -21,19 +21,19 @@ def test_getDTIFitDataPrefix_and_isDTIFitPath():
for s in suffixes:
path = op.join(dir, '{}{}'.format(prefix, s))
with open(path, 'wt') as f:
f.write(path)
f.write(path)
prefixes = ['dti', 'blob', 'random-prefix', '01234']
suffixes = ['_V1.nii', '_V2.nii', '_V3.nii',
'_L1.nii', '_L2.nii', '_L3.nii']
badSuffixes = ['_V1.txt', '_V2.nii', '_V3.nii',
'_L1.txt', '_L2.tar', '_L3.nii']
'_L1.txt', '_L2.tar', '_L3.nii']
# Valid dtifit directories
with tests.testdir() as testdir:
for p in prefixes:
tests.cleardir(testdir)
make_dtifit_dir(testdir, p, suffixes)
assert dtifit.getDTIFitDataPrefix(testdir) == p
......@@ -82,6 +82,7 @@ def test_looksLikeTensorImage():
img = fslimage.Image(fname)
assert dtifit.looksLikeTensorImage(img) == expected
img = None
def test_decomposeTensorMatrix():
......@@ -102,7 +103,7 @@ def test_decomposeTensorMatrix():
[[ 0.701921939849854, -0.711941838264465, 0.021080270409584],
[-0.700381875038147, -0.695301055908203, -0.16131255030632 ],
[-0.129502296447754, -0.098464585840702, 0.986678183078766]],
[[-0.993700802326202, -0.104962401092052, -0.039262764155865],
[[-0.993700802326202, -0.104962401092052, -0.039262764155865],
[-0.081384353339672, 0.916762292385101, -0.391054302453995],
[-0.077040620148182, 0.385395616292953, 0.919529736042023]],
[[ 0.068294189870358, -0.666985750198364, 0.741933941841125],
......@@ -112,7 +113,7 @@ def test_decomposeTensorMatrix():
tensorMatrices = tensorMatrices.reshape(1, 1, 3, 6)
expEigVals = expEigVals .reshape(1, 1, 3, 3)
expEigVecs = expEigVecs .reshape(1, 1, 3, 3, 3)
v1, v2, v3, l1, l2, l3 = dtifit.decomposeTensorMatrix(tensorMatrices)
expV1 = expEigVecs[:, :, :, 0]
......@@ -129,7 +130,7 @@ def test_decomposeTensorMatrix():
# Vector signs are arbitrary
for vox in range(3):
for resvec, expvec in zip([v1, v2, v3], [expV1, expV2, expV3]):
resvec = resvec[:, :, vox]
expvec = expvec[:, :, vox]
......@@ -172,3 +173,7 @@ def test_DTIFitTensor():
assert np.all(np.isclose(dtiobj.voxToWorldMat, v1.voxToWorldMat))
assert np.all(np.isclose(dtiobj.shape[:3], v1.shape[:3]))
assert np.all(np.isclose(dtiobj.pixdim[:3], v1.pixdim[:3]))
del v1
del dtiobj
v1 = None
......@@ -28,3 +28,7 @@ def test_ensureIsImage():
for l in loaded:
assert isinstance(l, nib.nifti1.Nifti1Image)
assert np.all(img.get_data() == l.get_data())
l = None
loaded = None
img = None
......@@ -29,6 +29,7 @@ featdirs = ['1stlevel_1.feat', '1stlevel_2.feat', '1stlevel_2.feat',
'2ndlevel_2.gfeat/cope1.feat', '2ndlevel_2.gfeat/cope2.feat',
'2ndlevel_realdata.gfeat/cope1.feat',
'2ndlevel_realdata.gfeat/cope2.feat']
featdirs = [op.join(*d.split('/')) for d in featdirs]
shapes = [(64, 64, 5, 45),
(64, 64, 5, 45),
(64, 64, 5, 45),
......
......@@ -167,10 +167,9 @@ def test_loadVertexData_mgh():
mesh = fslfs.FreesurferMesh('lh.pial')
assert np.all(np.isclose(mesh.loadVertexData('lh.vdata.mgh'), data.reshape(-1, 1)))
del img
del mesh
img = None
def test_loadVertexData_annot():
......
......@@ -75,6 +75,10 @@ def test_deepest():
]
for path, suffixes, output in tests:
path = op.join(*path.split('/'))
if output is not None:
output = op.join(*output.split('/'))
assert fslpath.deepest(path, suffixes) == output
......@@ -107,6 +111,11 @@ def test_shallowest():
]
for path, suffixes, output in tests:
path = op.join(*path.split('/'))
if output is not None:
output = op.join(*output.split('/'))
assert fslpath.shallowest(path, suffixes) == output
......@@ -120,6 +129,8 @@ def test_allFiles():
'a/b/d/1',
]
create = [op.join(*c.split('/')) for c in create]
with testdir(create) as td:
assert (sorted(fslpath.allFiles('.')) ==
sorted([op.join('.', c) for c in create]))
......
......@@ -622,5 +622,5 @@ def test_mutex():
# Either t1 has to start and
# finish before t2 or vice versa
assert (t[0].method2start > t[0].method1end or
t[0].method1start > t[0].method2end)
assert (t[0].method2start >= t[0].method1end or
t[0].method1start >= t[0].method2end)
......@@ -314,6 +314,7 @@ def _test_Image_atts(imgtype):
allowedExts=allowedExts,
mustExist=True,
fileGroups=fileGroups)
i = None
def test_Image_atts2_analyze(): _test_Image_atts2(0)
......@@ -530,7 +531,7 @@ def _test_Image_orientation(imgtype, voxorient):
make_image(imagefile, imgtype, (10, 10, 10), pixdims, np.float32)
image = fslimage.Image(imagefile)
image = fslimage.Image(imagefile, mmap=False)
# analyze images are always assumed to be
# stored in radiological (LAS) orientation
......@@ -566,6 +567,7 @@ def _test_Image_orientation(imgtype, voxorient):
assert image.getOrientation(0, affine) == expectvox0Orientation
assert image.getOrientation(1, affine) == expectvox1Orientation
assert image.getOrientation(2, affine) == expectvox2Orientation
image = None
def test_Image_sqforms_nifti1_normal(): _test_Image_sqforms(1, 1, 1)
......@@ -693,6 +695,8 @@ def _test_Image_changeXform(imgtype, sformcode=None, qformcode=None):
# ANALYZE affine is not editable
with pytest.raises(Exception):
img.voxToWorldMat = newXform
del img
del image
return
img.voxToWorldMat = newXform
......@@ -709,6 +713,9 @@ def _test_Image_changeXform(imgtype, sformcode=None, qformcode=None):
assert np.all(np.isclose(img.worldToVoxMat, invx))
assert img.getXFormCode('sform') == expSformCode
assert img.getXFormCode('qform') == expQformCode
del img
del image
image = None
def test_Image_changeData_analyze(seed): _test_Image_changeData(0)
......@@ -724,14 +731,15 @@ def _test_Image_changeData(imgtype):
make_image(imagefile, imgtype)
img = fslimage.Image(imagefile)
img = fslimage.Image(imagefile, mmap=False)
shape = img.shape
notified = {}
def randvox():
return (np.random.randint(0, img.shape[0]),
np.random.randint(0, img.shape[1]),
np.random.randint(0, img.shape[2]))
return (np.random.randint(0, shape[0]),
np.random.randint(0, shape[1]),
np.random.randint(0, shape[2]))
def onData(*a):
notified['data'] = True
......@@ -806,6 +814,10 @@ def _test_Image_changeData(imgtype):
assert notified.get('dataRange', False)
assert np.isclose(img[maxx, maxy, maxz], newdmax)
assert np.all(np.isclose(img.dataRange, (newdmin, newdmax)))
img.deregister('name1', 'data')
img.deregister('name2', 'data')
img.deregister('name3', 'data')
img = None
def test_Image_2D_analyze(): _test_Image_2D(0)
......@@ -851,6 +863,7 @@ def _test_Image_2D(imgtype):
assert tuple(map(float, shape)) == tuple(map(float, image .shape))
assert tuple(map(float, shape)) == tuple(map(float, image[:].shape))
assert tuple(map(float, pixdim)) == tuple(map(float, image .pixdim))
image = None
def test_Image_5D_analyze(): _test_Image_5D(0)
......@@ -880,6 +893,8 @@ def _test_Image_5D(imgtype):
assert img.shape == dims
assert img.ndim == 5
assert img.data.shape == dims
del img
img = None
def test_Image_voxToScaledVox_analyze(): _test_Image_voxToScaledVox(0)
......@@ -917,6 +932,7 @@ def _test_Image_voxToScaledVox(imgtype):
assert np.all(np.isclose(expected, img.voxToScaledVoxMat))
assert np.all(np.isclose(invexpected, img.scaledVoxToVoxMat))
img = None
def test_Image_sameSpace():
......@@ -1022,6 +1038,7 @@ def _test_Image_save(imgtype):
for (x, y, z), v in zip(randvoxes, randvals):
assert np.isclose(img[x, y, z], v)
img2 = None
def test_image_resample(seed):
......@@ -1035,7 +1052,7 @@ def test_image_resample(seed):
shape = np.random.randint(5, 100, 3)
make_random_image(fname, shape)
img = fslimage.Image(fname)
img = fslimage.Image(fname, mmap=False)
# resampling to the same shape should be a no-op
samei, samex = img.resample(shape)
......@@ -1049,7 +1066,8 @@ def test_image_resample(seed):
resampled, xf = img.resample(rshape, order=0)
img.save('base.nii.gz')
fslimage.Image(resampled, xform=xf).save('res.nii.gz')
fslimage.Image(resampled, xform=xf,
mmap=False).save('res.nii.gz')
assert tuple(resampled.shape) == tuple(rshape)
......@@ -1102,6 +1120,8 @@ def test_image_resample(seed):
resampled = img.resample((15, 15, 15), slc)[0]
assert tuple(resampled.shape) == (15, 15, 15)
del img
img = None
def test_Image_init_xform_nifti1(): _test_Image_init_xform(1)
......@@ -1142,7 +1162,6 @@ def _test_Image_init_xform(imgtype):
assert fsform_code == sform_code
assert fqform_code == qform_code
# an image created off
# an xform only should
# get its sform set
......@@ -1183,3 +1202,7 @@ def _test_Image_init_xform(imgtype):
assert np.all(np.isclose(xform, rxform))
assert fsform_code == sform_code
assert fqform_code == qform_code
del fimg
del img
img = None
......@@ -347,8 +347,10 @@ def test_imcp_script_shouldPass(move=False):
' '.join(infiles)
for inf in infiles:
img = nib.load(op.join(tindir, inf))
img = nib.load(op.join(tindir, inf),
mmap=False)
imghash = hash(img.get_data().tobytes())
img = None
imageHashes.append(imghash)
print('adj files_to_expect: ', files_to_expect)
......@@ -375,8 +377,6 @@ def test_imcp_script_shouldPass(move=False):
# too hard if indir == outdir
if move and tindir != toutdir:
real_print('indir: ', tindir)
real_print('outdir: ', toutdir)
infiles = os.listdir(tindir)
infiles = [f for f in infiles if op.isfile(f)]
infiles = [f for f in infiles if op.isfile(f)]
......
......@@ -141,6 +141,9 @@ def test_getDataFile():
]
for paths, meldir, expected in testcases:
paths = [op.join(*p.split('/')) for p in paths]
if expected is not None:
expected = op.join(*expected.split('/'))
with tests.testdir(paths) as testdir:
assert mela.getDataFile(meldir) == expected
......@@ -151,9 +154,11 @@ def test_getMeanFile():
'analysis.ica/melodic_FTmix',
'analysis.ica/mean.nii.gz']
paths = [op.join(*p.split('/')) for p in paths]
with tests.testdir(paths) as testdir:
meldir = op.join(testdir, 'analysis.ica')
expected = op.join(testdir, 'analysis.ica/mean.nii.gz')
expected = op.join(testdir, 'analysis.ica', 'mean.nii.gz')
assert mela.getMeanFile(meldir) == expected
......@@ -162,6 +167,8 @@ def test_getMeanFile():
'analysis.ica/melodic_FTmix',
'analysis.ica/mean.txt']
paths = [op.join(*p.split('/')) for p in paths]
with tests.testdir(paths) as testdir:
meldir = op.join(testdir, 'analysis.ica')
with pytest.raises(fslpath.PathError):
......@@ -173,15 +180,19 @@ def test_getICFile():
'analysis.ica/melodic_mix',
'analysis.ica/melodic_FTmix']
paths = [op.join(*p.split('/')) for p in paths]
with tests.testdir(paths) as testdir:
meldir = op.join(testdir, 'analysis.ica')
expected = op.join(testdir, 'analysis.ica/melodic_IC.nii.gz')
expected = op.join(testdir, 'analysis.ica', 'melodic_IC.nii.gz')
assert mela.getICFile(meldir) == expected
paths = ['analysis.ica/melodic_IC.txt',
'analysis.ica/melodic_mix',
'analysis.ica/melodic_FTmix']
paths = [op.join(*p.split('/')) for p in paths]
with tests.testdir(paths) as testdir:
meldir = op.join(testdir, 'analysis.ica')
with pytest.raises(fslpath.PathError):
......@@ -193,13 +204,17 @@ def test_getMixFile():
'analysis.ica/melodic_mix',
'analysis.ica/melodic_FTmix']
paths = [op.join(*p.split('/')) for p in paths]
with tests.testdir(paths) as testdir:
meldir = op.join(testdir, 'analysis.ica')
expected = op.join(testdir, 'analysis.ica/melodic_mix')
expected = op.join(testdir, 'analysis.ica', 'melodic_mix')
assert mela.getMixFile(meldir) == expected
paths = ['analysis.ica/melodic_IC.ni.gz',
'analysis.ica/melodic_FTmix']
paths = [op.join(*p.split('/')) for p in paths]
with tests.testdir(paths) as testdir:
meldir = op.join(testdir, 'analysis.ica')
assert mela.getMixFile(meldir) is None
......@@ -208,14 +223,17 @@ def test_getFTMixFile():
paths = ['analysis.ica/melodic_IC.nii.gz',
'analysis.ica/melodic_mix',
'analysis.ica/melodic_FTmix']
paths = [op.join(*p.split('/')) for p in paths]
with tests.testdir(paths) as testdir:
meldir = op.join(testdir, 'analysis.ica')
expected = op.join(testdir, 'analysis.ica/melodic_FTmix')
expected = op.join(testdir, 'analysis.ica', 'melodic_FTmix')
assert mela.getFTMixFile(meldir) == expected
paths = ['analysis.ica/melodic_IC.ni.gz',
'analysis.ica/melodic_mix']
paths = [op.join(*p.split('/')) for p in paths]
with tests.testdir(paths) as testdir:
meldir = op.join(testdir, 'analysis.ica')
assert mela.getFTMixFile(meldir) is None
......@@ -225,15 +243,18 @@ def test_getReportFile():
'analysis.ica/filtfunc.ica/melodic_mix',
'analysis.ica/filtfunc.ica/melodic_FTmix',
'analysis.ica/report.html']
paths = [op.join(*p.split('/')) for p in paths]
with tests.testdir(paths) as testdir:
meldir = op.join(testdir, 'analysis.ica/filtfunc.ica')
expected = op.join(testdir, 'analysis.ica/report.html')
meldir = op.join(testdir, 'analysis.ica', 'filtfunc.ica')
expected = op.join(testdir, 'analysis.ica', 'report.html')
assert op.abspath(mela.getReportFile(meldir)) == expected
paths = ['analysis.ica/filtfunc.ica/melodic_IC.ni.gz',
'analysis.ica/filtfunc.ica/melodic_mix',
'analysis.ica/filtfunc.ica/melodic_FTmix']
paths = [op.join(*p.split('/')) for p in paths]
with tests.testdir(paths) as testdir:
meldir = op.join(testdir, 'analysis.ica')
assert mela.getReportFile(meldir) is None
......
......@@ -98,12 +98,12 @@ def test_MelodicImage_create():
paths = ['analysis.ica/melodic_IC.nii.gz',
'analysis.ica/melodic_mix',
'analysis.ica/melodic_FTmix']
paths = [op.join(*p.split('/')) for p in paths]
with tests.testdir(paths) as testdir:
path = op.join(testdir, 'analysis.ica/melodic_IC.nii.gz')
path = op.join(testdir, 'analysis.ica', 'melodic_IC.nii.gz')
with pytest.raises(Exception):
meli.MelodicImage(path)
for ic_prefix in ['melodic_IC', 'melodic_oIC']:
with tests.testdir() as testdir:
......@@ -114,9 +114,10 @@ def test_MelodicImage_create():
# Should be able to specify the
# melodic dir, or the IC image
meli.MelodicImage(meldir)
meli.MelodicImage(icfile)
meli.MelodicImage(icfilenosuf)
i = meli.MelodicImage(meldir)
i = meli.MelodicImage(icfile)
i = meli.MelodicImage(icfilenosuf)
i = None
def test_MelodicImage_atts():
......@@ -136,6 +137,8 @@ def test_MelodicImage_atts():
assert img.getDataFile() == mela.getDataFile(meldir)
assert img.getMeanFile() == mela.getMeanFile(meldir)
img = None
def test_MelodicImage_componentData():
with tests.testdir() as testdir:
......@@ -150,6 +153,8 @@ def test_MelodicImage_componentData():
assert np.all(img.getComponentTimeSeries( ic) == expectTS[:, ic])
assert np.all(img.getComponentPowerSpectrum(ic) == expectPS[:, ic])
img = None
def test_MelodicImage_tr():
......@@ -160,12 +165,16 @@ def test_MelodicImage_tr():
assert img.tr == 1
img = None
# Otherwise, it should be set to the datafile tr
with tests.testdir() as testdir:
meldir = _create_dummy_melodic_analysis(testdir, tr=5)
img = meli.MelodicImage(meldir)
assert img.tr == 5
img = None
# The TR can be updated
with tests.testdir() as testdir:
......@@ -182,3 +191,5 @@ def test_MelodicImage_tr():
assert cbCalled[0]
assert img.tr == 8
img = None
......@@ -79,6 +79,7 @@ def test_image():
image_parser.parse_args([filename + '.hdr'])
with raises(SystemExit):
image_parser.parse_args([filename + '.nii.gz'])
args = None
double_filename = op.join(testdir, 'image1')
make_image(double_filename, 0)
......
......@@ -27,6 +27,9 @@ import fsl.utils.fslsub as fslsub
from . import make_random_image, mockFSLDIR, CaptureStdout
pytestmark = pytest.mark.unixtest
def mkexec(path, contents):
with open(path, 'wt') as f:
f.write(contents)
......
......@@ -326,6 +326,9 @@ def test_listFiles():
'namespace2/setting2.txt',
'namespace2/setting3.txt']
ns1files = [op.join(*f.split('/')) for f in ns1files]
ns2files = [op.join(*f.split('/')) for f in ns2files]
with tests.testdir() as testdir:
s = settings.Settings(cfgid='test', cfgdir=testdir, writeOnExit=False)
......@@ -338,12 +341,11 @@ def test_listFiles():
assert list(sorted(s.listFiles())) == list(sorted(ns1files + ns2files))
assert list(sorted(s.listFiles('namespace1/*'))) == list(sorted(ns1files))
assert list(sorted(s.listFiles('namespace2/*'))) == list(sorted(ns2files))
assert list(sorted(s.listFiles('namespace?/*'))) == list(sorted(ns1files + ns2files))
assert list(sorted(s.listFiles(op.join('namespace1', '*')))) == list(sorted(ns1files))
assert list(sorted(s.listFiles(op.join('namespace2', '*')))) == list(sorted(ns2files))
assert list(sorted(s.listFiles(op.join('namespace?', '*')))) == list(sorted(ns1files + ns2files))
assert list(sorted(s.listFiles('*.txt'))) == list(sorted(ns1files + ns2files))
assert list(sorted(s.listFiles('*/setting1.txt'))) == list(sorted([ns1files[0]] + [ns2files[0]]))
assert list(sorted(s.listFiles(op.join('*', 'setting1.txt')))) == list(sorted([ns1files[0]] + [ns2files[0]]))
def test_filePath():
......@@ -351,6 +353,7 @@ def test_filePath():
testfiles = ['file1.txt',
'dir1/file2.txt',
'dir1/dir2/file3.txt']
testfiles = [op.join(*f.split('/')) for f in testfiles]
with tests.testdir() as testdir:
......
......@@ -26,7 +26,7 @@ import fsl.data.image as fslimage
import fsl.wrappers.wrapperutils as wutils
from . import mockFSLDIR, cleardir, checkdir
from . import mockFSLDIR, cleardir, checkdir, testdir
from .test_run import mock_submit
......@@ -469,20 +469,20 @@ def test_fileOrThing_outprefix_directory():
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)
assert np.all(res[op.join('myout_imgs', 'img2')].get_data() == exp2)
assert np.all(res[op.join('myout_imgs', 'img4')].get_data() == exp4)
res = func(img, 'myout', **{'myout_imgs/img2' : wutils.LOAD})
res = func(img, 'myout', **{op.join('myout_imgs', 'img2') : wutils.LOAD})
assert len(res) == 1
assert np.all(res['myout_imgs/img2'].get_data() == exp2)
assert np.all(res[op.join('myout_imgs', 'img2')].get_data() == exp2)
res = func(img, 'myout', **{'myout_imgs/img' : wutils.LOAD})
res = func(img, 'myout', **{op.join('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)
assert np.all(res[op.join('myout_imgs', 'img2')].get_data() == exp2)
assert np.all(res[op.join('myout_imgs', 'img4')].get_data() == exp4)
os.mkdir('foo')
res = func(img, 'foo/myout')
res = func(img, op.join('foo', 'myout'))
assert len(res) == 0
checkdir(td,
op.join('foo', 'myout_imgs', 'img2.nii.gz'),
......@@ -490,10 +490,10 @@ def test_fileOrThing_outprefix_directory():
cleardir(td, 'foo')
os.mkdir('foo')
res = func(img, 'foo/myout', **{'foo/myout' : wutils.LOAD})
res = func(img, op.join('foo', 'myout'), **{op.join('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)
assert np.all(res[op.join('foo', 'myout_imgs', 'img2')].get_data() == exp2)
assert np.all(res[op.join('foo', 'myout_imgs', 'img4')].get_data() == exp4)
def test_chained_fileOrImageAndArray():
......@@ -614,6 +614,7 @@ def _test_script_func(a, b):
return ['test_script', str(a), str(b)]
@pytest.mark.unixtest
def test_cmdwrapper_submit():
test_func = wutils.cmdwrapper(_test_script_func)
......@@ -637,6 +638,7 @@ def test_cmdwrapper_submit():