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_resampling_result_axis_permutation(): # Transform real data using easily checkable transformations # For now: axis permutations # create a cuboid full of deterministic data, padded with one # voxel thickness of zeros core_shape = (3, 5, 4) core_data = np.arange(np.prod(core_shape)).reshape(core_shape) full_data_shape = np.array(core_shape) + 2 full_data = np.zeros(full_data_shape) full_data[[slice(1, 1 + s) for s in core_shape]] = core_data source_img = Nifti1Image(full_data, np.eye(4)) axis_permutations = [[0, 1, 2], [1, 0, 2], [2, 1, 0], [0, 2, 1]] # check 3x3 transformation matrix for ap in axis_permutations: target_affine = np.eye(3)[ap] resampled_img = resample_img(source_img, target_affine=target_affine) resampled_data = get_data(resampled_img) what_resampled_data_should_be = full_data.transpose(ap) assert_array_almost_equal(resampled_data, what_resampled_data_should_be) # check 4x4 transformation matrix offset = np.array([-2, 1, -3]) for ap in axis_permutations: target_affine = np.eye(4) target_affine[:3, :3] = np.eye(3)[ap] target_affine[:3, 3] = offset resampled_img = resample_img(source_img, target_affine=target_affine) resampled_data = get_data(resampled_img) offset_cropping = np.vstack([-offset[ap][np.newaxis, :], np.zeros([1, 3])] ).T.ravel().astype(int) what_resampled_data_should_be = _pad_array(full_data.transpose(ap), list(offset_cropping)) assert_array_almost_equal(resampled_data, what_resampled_data_should_be)