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

Changes to tests.testdir and make_random_image utility functions

parent 33a9763a
No related branches found
No related tags found
No related merge requests found
...@@ -94,7 +94,7 @@ class CaptureStdout(object): ...@@ -94,7 +94,7 @@ class CaptureStdout(object):
def testdir(contents=None): def testdir(contents=None, suffix=None):
"""Returnsa context manager which creates, changes to, and returns a """Returnsa context manager which creates, changes to, and returns a
temporary directory, and then deletes it on exit. temporary directory, and then deletes it on exit.
""" """
...@@ -109,7 +109,7 @@ def testdir(contents=None): ...@@ -109,7 +109,7 @@ def testdir(contents=None):
def __enter__(self): def __enter__(self):
self.testdir = tempfile.mkdtemp() self.testdir = tempfile.mkdtemp(suffix=suffix)
self.prevdir = os.getcwd() self.prevdir = os.getcwd()
os.chdir(self.testdir) os.chdir(self.testdir)
...@@ -194,21 +194,56 @@ def random_voxels(shape, nvoxels=1): ...@@ -194,21 +194,56 @@ def random_voxels(shape, nvoxels=1):
return randVoxels return randVoxels
def make_random_image(filename, dims=(10, 10, 10), xform=None): def make_random_image(filename=None,
"""Creates a NIFTI1 image with random data, saves and dims=(10, 10, 10),
returns it. xform=None,
imgtype=1,
pixdims=None,
dtype=np.float32):
"""Convenience function which makes an image containing random data.
Saves and returns the nibabel object.
imgtype == 0: ANALYZE
imgtype == 1: NIFTI1
imgtype == 2: NIFTI2
""" """
if imgtype == 0: hdr = nib.AnalyzeHeader()
elif imgtype == 1: hdr = nib.Nifti1Header()
elif imgtype == 2: hdr = nib.Nifti2Header()
if pixdims is None:
pixdims = [1] * len(dims)
pixdims = pixdims[:len(dims)]
zooms = [abs(p) for p in pixdims]
hdr.set_data_dtype(dtype)
hdr.set_data_shape(dims)
hdr.set_zooms(zooms)
if xform is None: if xform is None:
xform = np.eye(4) xform = np.eye(4)
for i, p in enumerate(pixdims[:3]):
xform[i, i] = p
data = np.array(np.random.random(dims) * 100, dtype=np.float32) data = np.array(np.random.random(dims) * 100, dtype=dtype)
img = nib.Nifti1Image(data, xform)
nib.save(img, filename) if imgtype == 0: img = nib.AnalyzeImage(data, xform, hdr)
elif imgtype == 1: img = nib.Nifti1Image( data, xform, hdr)
elif imgtype == 2: img = nib.Nifti2Image( data, xform, hdr)
if filename is not None:
if op.splitext(filename)[1] == '':
if imgtype == 0: filename = '{}.img'.format(filename)
else: filename = '{}.nii'.format(filename)
nib.save(img, filename)
return img return img
def make_mock_feat_analysis(featdir, def make_mock_feat_analysis(featdir,
testdir, testdir,
shape4D, shape4D,
......
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