diff --git a/tests/test_image.py b/tests/test_image.py index 8f1459c8e5aeb2b9e2c8634770012fb379f59a1e..dfab39763b349f463fbda73fc54882c1f3ebe6d0 100644 --- a/tests/test_image.py +++ b/tests/test_image.py @@ -10,8 +10,9 @@ import os.path as op import pytest import glob -import numpy as np -import nibabel as nib +import numpy as np +import numpy.linalg as npla +import nibabel as nib from nibabel.spatialimages import ImageFileError @@ -201,6 +202,118 @@ def test_Image_sqforms(testdir): assert np.all(np.isclose(noqform.worldToVoxMat, benchmark.worldToVoxMat)) assert np.all(np.isclose(nosqform.voxToWorldMat, scalemat)) assert np.all(np.isclose(nosqform.worldToVoxMat, invScalemat)) + + assert benchmark.getXFormCode() == constants.NIFTI_XFORM_MNI_152 + assert benchmark.getXFormCode('sform') == constants.NIFTI_XFORM_MNI_152 + assert benchmark.getXFormCode('qform') == constants.NIFTI_XFORM_MNI_152 + assert nosform .getXFormCode() == constants.NIFTI_XFORM_MNI_152 + assert nosform .getXFormCode('sform') == constants.NIFTI_XFORM_UNKNOWN + assert nosform .getXFormCode('qform') == constants.NIFTI_XFORM_MNI_152 + assert noqform .getXFormCode() == constants.NIFTI_XFORM_MNI_152 + assert noqform .getXFormCode('sform') == constants.NIFTI_XFORM_MNI_152 + assert noqform .getXFormCode('qform') == constants.NIFTI_XFORM_UNKNOWN + assert nosqform .getXFormCode() == constants.NIFTI_XFORM_UNKNOWN + assert nosqform .getXFormCode('sform') == constants.NIFTI_XFORM_UNKNOWN + assert nosqform .getXFormCode('qform') == constants.NIFTI_XFORM_UNKNOWN + + +def test_Image_changeXform(testdir): + + img = fslimage.Image(op.join(testdir, 'MNI152_T1_2mm.nii.gz')) + + notified = {} + + def onXform(*a): + notified['xform'] = True + + def onSave(*a): + notified['save'] = True + + img.register('name1', onXform, 'transform') + img.register('name2', onSave, 'saveState') + + newXform = np.array([[5, 0, 0, 10], [0, 2, 0, 23], [0, 0, 14, 5], [0, 0, 0, 1]]) + + assert img.saveState + + img.voxToWorldMat = newXform + + invx = npla.inv(newXform) + + assert notified.get('xform', False) + assert notified.get('save', False) + assert not img.saveState + + assert np.all(np.isclose(img.voxToWorldMat, newXform)) + assert np.all(np.isclose(img.worldToVoxMat, invx)) + + +def test_Image_changeData(testdir): + + img = fslimage.Image(op.join(testdir, 'dtypes', 'MNI152_T1_1mm_float.nii.gz')) + + 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])) + + + def onData(*a): + notified['data'] = True + + def onSaveState(*a): + notified['save'] = True + + def onDataRange(*a): + notified['dataRange'] = True + + img.register('name1', onData, 'data') + img.register('name2', onSaveState, 'saveState') + img.register('name3', onDataRange, 'dataRange') + + data = img.nibImage.get_data() + dmin = data.min() + dmax = data.max() + drange = dmax - dmin + + assert img.saveState + assert np.all(np.isclose(img.dataRange, (dmin, dmax))) + + randval = dmin + np.random.random() * drange + rx, ry, rz = randvox() + + img[rx, ry, rz] = randval + + assert np.isclose(img[rx, ry, rz], randval) + assert notified.get('data', False) + assert notified.get('save', False) + assert not img.saveState + + notified.pop('data') + + newdmin = dmin - 100 + newdmax = dmax + 100 + + rx, ry, rz = randvox() + img[rx, ry, rz] = newdmin + + assert notified.get('data', False) + assert notified.get('dataRange', False) + assert np.isclose(img[rx, ry, rz], newdmin) + assert np.all(np.isclose(img.dataRange, (newdmin, dmax))) + + notified.pop('data') + notified.pop('dataRange') + + rx, ry, rz = randvox() + img[rx, ry, rz] = newdmax + + assert notified.get('data', False) + assert notified.get('dataRange', False) + assert np.isclose(img[rx, ry, rz], newdmax) + assert np.all(np.isclose(img.dataRange, (newdmin, newdmax))) def test_2D_images(testdir): diff --git a/tests/test_imagewrapper.py b/tests/test_imagewrapper.py index c655373c1ea5ee00f6fb32bc236d4f5469ddd4ad..2e7471ac37d9ed6da3811e0a096811103f8bec08 100644 --- a/tests/test_imagewrapper.py +++ b/tests/test_imagewrapper.py @@ -244,7 +244,7 @@ def coverageDataRange(data, coverage, slices=None): sliceobj = [] for d in range(ndims): - sliceobj.append(slice(cov[0, d], cov[1, d], 1)) + sliceobj.append(slice(int(cov[0, d]), int(cov[1, d]), 1)) sliceobj.append(vol) voldata = data[tuple(sliceobj)] diff --git a/tests/test_transform.py b/tests/test_transform.py new file mode 100644 index 0000000000000000000000000000000000000000..709337990997080288c8df10797b33c06936280c --- /dev/null +++ b/tests/test_transform.py @@ -0,0 +1,47 @@ +#!/usr/bin/env python +# +# test_transform.py - +# +# Author: Paul McCarthy <pauldmccarthy@gmail.com> +# + +import numpy as np +import numpy.linalg as npla + +import fsl.utils.transform as transform + +def test_scaleOffsetXform(): + + scales = [1, 2, 3] + offsets = [4, 5, 6] + + expected = np.eye(4) + expected[0, 0] = scales[ 0] + expected[1, 1] = scales[ 1] + expected[2, 2] = scales[ 2] + expected[0, 3] = offsets[0] + expected[1, 3] = offsets[1] + expected[2, 3] = offsets[2] + + generated = transform.scaleOffsetXform(scales, offsets) + + assert np.all(np.isclose(expected, generated)) + + + scale = 5 + offset = 3 + + expected = np.eye(4) + expected[0, 0] = scale + expected[1, 1] = 1 + expected[2, 2] = 1 + expected[2, 2] = 1 + expected[0, 3] = offset + + generated = transform.scaleOffsetXform(scale, offset) + assert np.all(np.isclose(expected, generated)) + + +def test_invert(): + + pass