def test_apply(): # most tests are in ``same_transform`` above, via the # test_io_orientations. a = np.arange(24).reshape((2,3,4)) # Test 4D t_arr = apply_orientation(a[:,:,:,None], ornt) yield assert_equal(t_arr.ndim, 4) # Orientation errors yield assert_raises(OrientationError, apply_orientation, a[:,:,1], ornt) yield assert_raises(OrientationError, apply_orientation, a, [[0,1],[np.nan,np.nan],[2,1]])
def same_transform(taff, ornt, shape): # Applying transformations implied by `ornt` to a made-up array # ``arr`` of shape `shape`, results in ``t_arr``. When the point # indices from ``arr`` are transformed by (the inverse of) `taff`, # and we index into ``t_arr`` with these transformed points, then we # should get the same values as we would from indexing into arr with # the untransformed points. shape = np.array(shape) size = np.prod(shape) arr = np.arange(size).reshape(shape) # apply ornt transformations t_arr = apply_orientation(arr, ornt) # get all point indices in arr i,j,k = shape arr_pts = np.mgrid[:i,:j,:k].reshape((3,-1)) # inverse of taff takes us from point index in arr to point index in # t_arr itaff = np.linalg.inv(taff) # apply itaff so that points indexed in t_arr should correspond o2t_pts = np.dot(itaff[:3,:3], arr_pts) + itaff[:3,3][:,None] assert np.allclose(np.round(o2t_pts), o2t_pts) # fancy index out the t_arr values vals = t_arr[list(o2t_pts.astype('i'))] return np.all(vals == arr.ravel())