def test_anisotropic_sphere_extraction(): data = np.random.RandomState(42).random_sample((3, 3, 3, 5)) affine = np.eye(4) affine[0, 0] = 2 affine[2, 2] = 2 img = nibabel.Nifti1Image(data, affine) masker = NiftiSpheresMasker([(2, 1, 2)], radius=1) # Test the fit masker.fit() # Test the transform s = masker.transform(img) mask = np.zeros((3, 3, 3), dtype=bool) mask[1, :, 1] = True assert_array_equal(s[:, 0], np.mean(data[mask], axis=0)) # Now with a mask mask_img = np.zeros((3, 2, 3)) mask_img[1, 0, 1] = 1 affine_2 = affine.copy() affine_2[0, 0] = 4 mask_img = nibabel.Nifti1Image(mask_img, affine=affine_2) masker = NiftiSpheresMasker([(2, 1, 2)], radius=1, mask_img=mask_img) masker.fit() s = masker.transform(img) assert_array_equal(s[:, 0], data[1, 0, 1])
def test_small_radius_inverse(): affine = np.eye(4) shape = (3, 3, 3) data = np.random.RandomState(42).random_sample(shape) mask = np.zeros(shape) mask[1, 1, 1] = 1 mask[2, 2, 2] = 1 affine = np.eye(4) * 1.2 seed = (1.4, 1.4, 1.4) masker = NiftiSpheresMasker([seed], radius=0.1, mask_img=nibabel.Nifti1Image(mask, affine)) spheres_data = masker.fit_transform(nibabel.Nifti1Image(data, affine)) masker.inverse_transform(spheres_data) # Test if masking is taken into account mask[1, 1, 1] = 0 mask[1, 1, 0] = 1 masker = NiftiSpheresMasker([seed], radius=0.1, mask_img=nibabel.Nifti1Image(mask, affine)) masker.fit(nibabel.Nifti1Image(data, affine)) with pytest.raises(ValueError, match='These spheres are empty'): masker.inverse_transform(spheres_data) masker = NiftiSpheresMasker([seed], radius=1.6, mask_img=nibabel.Nifti1Image(mask, affine)) masker.fit(nibabel.Nifti1Image(data, affine)) masker.inverse_transform(spheres_data)
def test_seed_extraction(): data = np.random.RandomState(42).random_sample((3, 3, 3, 5)) img = nibabel.Nifti1Image(data, np.eye(4)) masker = NiftiSpheresMasker([(1, 1, 1)]) # Test the fit masker.fit() # Test the transform s = masker.transform(img) assert_array_equal(s[:, 0], data[1, 1, 1])
def test_sphere_extraction(): data = np.random.RandomState(42).random_sample((3, 3, 3, 5)) img = nibabel.Nifti1Image(data, np.eye(4)) masker = NiftiSpheresMasker([(1, 1, 1)], radius=1) # Test the fit masker.fit() # Test the transform s = masker.transform(img) mask = np.zeros((3, 3, 3), dtype=bool) mask[:, 1, 1] = True mask[1, :, 1] = True mask[1, 1, :] = True assert_array_equal(s[:, 0], np.mean(data[mask], axis=0)) # Now with a mask mask_img = np.zeros((3, 3, 3)) mask_img[1, :, :] = 1 mask_img = nibabel.Nifti1Image(mask_img, np.eye(4)) masker = NiftiSpheresMasker([(1, 1, 1)], radius=1, mask_img=mask_img) masker.fit() s = masker.transform(img) assert_array_equal( s[:, 0], np.mean(data[np.logical_and(mask, get_data(mask_img))], axis=0))
def test_nifti_spheres_masker_inverse_transform(): # Applying the sphere_extraction example from above backwards data = np.random.RandomState(42).random_sample((3, 3, 3, 5)) img = nibabel.Nifti1Image(data, np.eye(4)) masker = NiftiSpheresMasker([(1, 1, 1)], radius=1) # Test the fit masker.fit() # Transform data with pytest.raises(ValueError, match='Please provide mask_img'): masker.inverse_transform(data[0, 0, 0, :]) # Mask describes the extend of the masker's sphere mask = np.zeros((3, 3, 3), dtype=bool) mask[:, 1, 1] = True mask[1, :, 1] = True mask[1, 1, :] = True # Now with a mask mask_img = np.zeros((3, 3, 3)) mask_img[1, :, :] = 1 mask_img = nibabel.Nifti1Image(mask_img, np.eye(4)) masker = NiftiSpheresMasker([(1, 1, 1)], radius=1, mask_img=mask_img) masker.fit() s = masker.transform(img) # Create an array mask array_mask = np.logical_and(mask, get_data(mask_img)) inverse_map = masker.inverse_transform(s) # Testing whether mask is applied to inverse transform assert_array_equal( np.mean(get_data(inverse_map), axis=-1) != 0, array_mask) # Test whether values are preserved assert_array_equal(get_data(inverse_map)[array_mask].mean(0), s[:, 0]) # Test whether the mask's shape is applied assert_array_equal(inverse_map.shape[:3], mask_img.shape)
def test_errors(): masker = NiftiSpheresMasker(([1, 2]), radius=.2) with pytest.raises(ValueError, match='Seeds must be a list .+'): masker.fit()