Skip to content
Snippets Groups Projects
Commit 9a99c71d authored by Paul McCarthy's avatar Paul McCarthy
Browse files

test_image module rewritten so it generates its own data, instead of relying

on an existing data set.
parent 5d063e35
No related branches found
No related tags found
No related merge requests found
...@@ -60,24 +60,14 @@ def cleardir(dir): ...@@ -60,24 +60,14 @@ def cleardir(dir):
elif op.isdir(f): shutil.rmtree(f) elif op.isdir(f): shutil.rmtree(f)
def make_random_image(filename, dims=(10, 10, 10), affine=None): def make_random_image(filename, dims=(10, 10, 10)):
"""Creates a NIFTI image with random data, returns the hash of said data. """Creates a NIFTI1 image with random data, saves and
returns it.
""" """
if affine is None: data = np.array(np.random.random(dims) * 100, dtype=np.float32)
affine = np.eye(4) img = nib.Nifti1Image(data, np.eye(4))
data = np.random.random(dims)
img = nib.Nifti1Image(data, affine)
nib.save(img, filename) nib.save(img, filename)
return hash(data.tobytes()) return img
def check_image_hash(filename, datahash):
"""Checks that the given NIFTI image matches the given hash.
"""
img = nib.load(filename)
assert hash(img.get_data().tobytes()) == datahash
This diff is collapsed.
...@@ -31,11 +31,22 @@ import fsl.data.image as fslimage ...@@ -31,11 +31,22 @@ import fsl.data.image as fslimage
from . import make_random_image from . import make_random_image
from . import make_dummy_file from . import make_dummy_file
from . import check_image_hash
from . import looks_like_image from . import looks_like_image
from . import cleardir from . import cleardir
def makeImage(filename):
return hash(make_random_image(filename).get_data().tobytes())
def checkImageHash(filename, datahash):
"""Checks that the given NIFTI image matches the given hash.
"""
img = nib.load(filename)
assert hash(img.get_data().tobytes()) == datahash
def checkFilesToExpect(files, outdir, outputType, datahashes): def checkFilesToExpect(files, outdir, outputType, datahashes):
exts = { exts = {
...@@ -91,7 +102,7 @@ def checkFilesToExpect(files, outdir, outputType, datahashes): ...@@ -91,7 +102,7 @@ def checkFilesToExpect(files, outdir, outputType, datahashes):
else: else:
h = datahashes[op.basename(f)] h = datahashes[op.basename(f)]
check_image_hash(f, h) checkImageHash(f, h)
def test_imcp_script_shouldPass(move=False): def test_imcp_script_shouldPass(move=False):
...@@ -308,7 +319,7 @@ def test_imcp_script_shouldPass(move=False): ...@@ -308,7 +319,7 @@ def test_imcp_script_shouldPass(move=False):
print('files_to_expect: ', files_to_expect) print('files_to_expect: ', files_to_expect)
for i, fname in enumerate(files_to_create.split()): for i, fname in enumerate(files_to_create.split()):
imageHashes.append(make_random_image(op.join(indir, fname))) imageHashes.append(makeImage(op.join(indir, fname)))
imcp_args = imcp_args.split() imcp_args = imcp_args.split()
...@@ -413,7 +424,7 @@ def test_imcp_script_shouldFail(move=False): ...@@ -413,7 +424,7 @@ def test_imcp_script_shouldFail(move=False):
imcp_args = imcp_args .split() imcp_args = imcp_args .split()
for fname in files_to_create: for fname in files_to_create:
make_random_image(op.join(indir, fname)) makeImage(op.join(indir, fname))
imcp_args[:-1] = [op.join(indir, a) for a in imcp_args[:-1]] imcp_args[:-1] = [op.join(indir, a) for a in imcp_args[:-1]]
imcp_args[ -1] = op.join(outdir, imcp_args[-1]) imcp_args[ -1] = op.join(outdir, imcp_args[-1])
...@@ -609,7 +620,7 @@ def test_imcp_shouldPass(move=False): ...@@ -609,7 +620,7 @@ def test_imcp_shouldPass(move=False):
hashes = {} hashes = {}
for fn in files_to_create: for fn in files_to_create:
if looks_like_image(fn): if looks_like_image(fn):
hashes[fn] = make_random_image(op.join(indir, fn)) hashes[fn] = makeImage(op.join(indir, fn))
else: else:
hashes[fn] = make_dummy_file(op.join(indir, fn)) hashes[fn] = make_dummy_file(op.join(indir, fn))
......
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