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

Bugfix in _test_Image_changeData. All uses of mkdtemp replaced with fsl.utils.tempdir

parent ebd4a8b5
Pipeline #1695 passed with stages
in 37 minutes and 45 seconds
......@@ -10,8 +10,6 @@
import os
import os.path as op
import itertools as it
import tempfile
import shutil
import pytest
......@@ -27,10 +25,10 @@ import fsl.data.imagewrapper as imagewrapper
import fsl.utils.path as fslpath
import fsl.utils.transform as transform
from fsl.utils.tempdir import tempdir
from . import make_random_image
from . import make_dummy_file
from . import testdir
from . import tempdir
def make_image(filename=None,
......@@ -121,7 +119,8 @@ def test_load():
('notnifti', ImageFileError),
('notnifti.nii.gz', ImageFileError)]
testdir = tempfile.mkdtemp()
with tempdir() as testdir:
for f in toCreate:
......@@ -131,7 +130,6 @@ def test_load():
make_random_image(op.join(testdir, f))
# Not raising an error means the test passes
try:
for fname in shouldPass:
fslimage.Image(op.join(testdir, fname))
......@@ -139,8 +137,6 @@ def test_load():
for fname, exc in shouldRaise:
with pytest.raises(exc):
fslimage.Image(op.join(testdir, fname))
finally:
shutil.rmtree(testdir)
def test_create():
......@@ -180,8 +176,7 @@ def test_create():
assert np.all(np.isclose(img.pixdim, (2, 3, 4)))
for imgtype in [0, 1, 2]:
testdir = tempfile.mkdtemp()
try:
with tempdir() as testdir:
fname = op.join(testdir, 'myimage')
nimg = make_image(fname, imgtype, pixdims=(2, 3, 4))
......@@ -193,9 +188,6 @@ def test_create():
assert img.niftiVersion == imgtype
assert np.all(np.isclose(img.pixdim, (2, 3, 4)))
finally:
shutil.rmtree(testdir)
def test_bad_create():
......@@ -236,7 +228,6 @@ def test_Image_atts_nifti2(): _test_Image_atts(2)
def _test_Image_atts(imgtype):
"""Test that basic Nifti/Image attributes are correct. """
testdir = tempfile.mkdtemp()
allowedExts = fslimage.ALLOWED_EXTENSIONS
fileGroups = fslimage.FILE_GROUPS
......@@ -268,6 +259,8 @@ def _test_Image_atts(imgtype):
tests = list(tests)
paths = ['test{:03d}'.format(i) for i in range(len(tests))]
with tempdir() as testdir:
for path, atts in zip(paths, tests):
dims, pixdims, dtype = atts
......@@ -278,8 +271,6 @@ def _test_Image_atts(imgtype):
path = op.abspath(op.join(testdir, path))
make_image(path, imgtype, dims, pixdims, dtype)
try:
for path, atts in zip(paths, tests):
dims, pixdims, dtype = atts
......@@ -305,8 +296,7 @@ def _test_Image_atts(imgtype):
allowedExts=allowedExts,
mustExist=True,
fileGroups=fileGroups)
finally:
shutil.rmtree(testdir)
def test_Image_atts2_analyze(): _test_Image_atts2(0)
def test_Image_atts2_nifti1(): _test_Image_atts2(1)
......@@ -390,7 +380,6 @@ def test_addExt():
"""Test the addExt function. """
default = fslimage.defaultExt()
testdir = tempfile.mkdtemp()
toCreate = [
'compressed.nii.gz',
......@@ -429,11 +418,13 @@ def test_addExt():
('ambiguous.img.gz', True, 'ambiguous.img.gz'),
('ambiguous.hdr.gz', True, 'ambiguous.hdr.gz')]
with tempdir() as testdir:
for path in toCreate:
path = op.abspath(op.join(testdir, path))
make_random_image(path)
try:
for path, mustExist, expected in tests:
path = op.abspath(op.join(testdir, path))
......@@ -445,8 +436,6 @@ def test_addExt():
with pytest.raises(fslimage.PathError):
path = op.join(testdir, 'ambiguous')
fslimage.addExt(path, mustExist=True)
finally:
shutil.rmtree(testdir)
def test_removeExt():
......@@ -506,7 +495,7 @@ def test_Image_orientation_nifti2_radio(): _test_Image_orientation(2, 'radio')
def _test_Image_orientation(imgtype, voxorient):
"""Test the Nifti.isNeurological and Nifti.getOrientation methods. """
testdir = tempfile.mkdtemp()
with tempdir() as testdir:
imagefile = op.join(testdir, 'image')
# an image with RAS voxel storage order
......@@ -544,8 +533,6 @@ def _test_Image_orientation(imgtype, voxorient):
expectvox1Orientation = constants.ORIENT_P2A
expectvox2Orientation = constants.ORIENT_I2S
try:
assert image.isNeurological() == expectNeuroTest
# All images should have the
......@@ -562,9 +549,6 @@ def _test_Image_orientation(imgtype, voxorient):
assert image.getOrientation(1, affine) == expectvox1Orientation
assert image.getOrientation(2, affine) == expectvox2Orientation
finally:
shutil.rmtree(testdir)
def test_Image_sqforms_nifti1_normal(): _test_Image_sqforms(1, 1, 1)
def test_Image_sqforms_nifti1_nosform(): _test_Image_sqforms(1, 0, 1)
......@@ -579,7 +563,7 @@ def _test_Image_sqforms(imgtype, sformcode, qformcode):
attributes for NIFTI images with the given sform/qform code combination.
"""
testdir = tempfile.mkdtemp()
with tempdir() as testdir:
imagefile = op.abspath(op.join(testdir, 'image.nii.gz'))
......@@ -627,7 +611,6 @@ def _test_Image_sqforms(imgtype, sformcode, qformcode):
with pytest.raises(ValueError):
image.getXFormCode('badcode')
try:
assert np.all(np.isclose(image.voxToWorldMat, expAffine))
assert np.all(np.isclose(image.worldToVoxMat, invExpAffine))
......@@ -636,8 +619,6 @@ def _test_Image_sqforms(imgtype, sformcode, qformcode):
assert image.getXFormCode('qform') == qformcode
assert image.getOrientation(0, image.voxToWorldMat) == expOrient
finally:
shutil.rmtree(testdir)
def test_Image_changeXform_analyze(): _test_Image_changeXform(0)
......@@ -647,7 +628,7 @@ def test_Image_changeXform_nifti2(): _test_Image_changeXform(2)
def _test_Image_changeXform(imgtype, sformcode=None, qformcode=None):
"""Test changing the Nifti.voxToWorldMat attribute. """
testdir = tempfile.mkdtemp()
with tempdir() as testdir:
imagefile = op.join(testdir, 'image')
image = make_image(imagefile, imgtype)
......@@ -687,8 +668,6 @@ def _test_Image_changeXform(imgtype, sformcode=None, qformcode=None):
expSformCode = constants.NIFTI_XFORM_ANALYZE
expQformCode = constants.NIFTI_XFORM_ANALYZE
try:
# Image state should initially be saved
assert img.saveState
......@@ -712,8 +691,6 @@ 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
finally:
shutil.rmtree(testdir)
def test_Image_changeData_analyze(seed): _test_Image_changeData(0)
......@@ -724,7 +701,7 @@ def _test_Image_changeData(imgtype):
the dataRange attribute to be updated.
"""
testdir = tempfile.mkdtemp()
with tempdir() as testdir:
imagefile = op.join(testdir, 'image')
make_image(imagefile, imgtype)
......@@ -757,15 +734,19 @@ def _test_Image_changeData(imgtype):
dmax = data.max()
drange = dmax - dmin
try:
assert img.saveState
assert np.all(np.isclose(img.dataRange, (dmin, dmax)))
# random value within the existing data range
# random value within the existing data range,
# making sure not to overwite the min or max
randval = dmin + np.random.random() * drange
while True:
rx, ry, rz = randvox()
if not (np.isclose(img[rx, ry, rz], dmin) or
np.isclose(img[rx, ry, rz], dmax)):
img[rx, ry, rz] = randval
break
assert np.isclose(img[rx, ry, rz], randval)
assert notified.get('data', False)
......@@ -808,17 +789,12 @@ def _test_Image_changeData(imgtype):
assert np.isclose(img[maxx, maxy, maxz], newdmax)
assert np.all(np.isclose(img.dataRange, (newdmin, newdmax)))
finally:
shutil.rmtree(testdir)
def test_Image_2D_analyze(): _test_Image_2D(0)
def test_Image_2D_nifti1(): _test_Image_2D(1)
def test_Image_2D_nifti2(): _test_Image_2D(2)
def _test_Image_2D(imgtype):
testdir = tempfile.mkdtemp()
# The first shape tests when the
# nifti dim0 field is set to 2,
# which happens when you create
......@@ -833,7 +809,7 @@ def _test_Image_2D(imgtype):
(10, 1, 20, 5),
(1, 10, 20, 5)]
try:
with tempdir() as testdir:
for shape in testdims:
......@@ -858,9 +834,6 @@ def _test_Image_2D(imgtype):
assert tuple(map(float, shape)) == tuple(map(float, image[:].shape))
assert tuple(map(float, pixdim)) == tuple(map(float, image .pixdim))
finally:
shutil.rmtree(testdir)
def test_Image_5D_analyze(): _test_Image_5D(0)
def test_Image_5D_nifti1(): _test_Image_5D(1)
......@@ -878,7 +851,7 @@ def _test_Image_5D(imgtype):
for dims in testdims:
with testdir() as td:
with tempdir() as td:
path = op.join(td, 'test.nii')
......@@ -970,7 +943,7 @@ def _test_Image_save(imgtype):
return rvoxes
testdir = tempfile.mkdtemp()
with tempdir() as testdir:
if imgtype == 0:
filename = op.join(testdir, 'blob.img')
filename2 = op.join(testdir, 'blob_copy.img')
......@@ -984,7 +957,6 @@ def _test_Image_save(imgtype):
xform[ 1, 1] = 55
xform[ 2, 2] = 38
try:
make_image(filename, imgtype)
# Using mmap can cause a "Bus error"
......@@ -1033,13 +1005,9 @@ def _test_Image_save(imgtype):
assert np.isclose(img[x, y, z], v)
finally:
shutil.rmtree(testdir)
def test_image_resample(seed):
with testdir() as td:
with tempdir() as td:
fname = op.join(td, 'test.nii')
......
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