def test_crop(): shape = (4, 6, 2) data = np.ones(shape) padded = pad_img(data, 3, 2, 4, 4, 5, 7) padd_nii = Nifti1Image(padded, np.eye(4)) cropped = crop_img(padd_nii, pad=False) np.testing.assert_equal(cropped.get_data(), data)
def test_crop(): # Testing that padding of arrays and cropping of images work symmetrically shape = (4, 6, 2) data = np.ones(shape) padded = _pad_array(data, [3, 2, 4, 4, 5, 7]) padd_nii = Nifti1Image(padded, np.eye(4)) cropped = crop_img(padd_nii, pad=False) np.testing.assert_equal(cropped.get_data(), data)
def test_crop_threshold_tolerance(): """Check to see whether crop can skip values that are extremely close to zero in a relative sense and will crop them away""" data = np.zeros([10, 14, 12]) data[3:7, 3:7, 5:9] = 1. active_shape = (4 + 2, 4 + 2, 4 + 2) # add padding # add an infinitesimal outside this block data[3, 3, 3] = 1e-12 affine = np.eye(4) img = nibabel.Nifti1Image(data, affine=affine) cropped_img = image.crop_img(img) assert_true(cropped_img.shape == active_shape)
def test_crop_img(): data = np.zeros((5, 6, 7)) data[2:4, 1:5, 3:6] = 1 affine = np.diag((4, 3, 2, 1)) img = nibabel.Nifti1Image(data, affine=affine) cropped_img = image.crop_img(img) # correction for padding with "-1" new_origin = np.array((4, 3, 2)) * np.array((2 - 1, 1 - 1, 3 - 1)) # check that correct part was extracted: # This also corrects for padding assert_true((cropped_img.get_data()[1:-1, 1:-1, 1:-1] == 1).all()) assert_true(cropped_img.shape == (2 + 2, 4 + 2, 3 + 2))