def test_diffeomorphic_map_simplification_2d(): r""" Create an invertible deformation field, and define a DiffeomorphicMap using different voxel-to-space transforms for domain, codomain, and reference discretizations, also use a non-identity pre-aligning matrix. Warp a circle using the diffeomorphic map to obtain the expected warped circle. Now simplify the DiffeomorphicMap and warp the same circle using this simplified map. Verify that the two warped circles are equal up to numerical precision. """ #create a simple affine transformation dom_shape = (64, 64) cod_shape = (80, 80) nr = dom_shape[0] nc = dom_shape[1] s = 1.1 t = 0.25 trans = np.array([[1, 0, -t*nr], [0, 1, -t*nc], [0, 0, 1]]) trans_inv = np.linalg.inv(trans) scale = np.array([[1*s, 0, 0], [0, 1*s, 0], [0, 0, 1]]) gt_affine = trans_inv.dot(scale.dot(trans)) # Create the invertible displacement fields and the circle radius = 16 circle = vfu.create_circle(cod_shape[0], cod_shape[1], radius) d, dinv = vfu.create_harmonic_fields_2d(dom_shape[0], dom_shape[1], 0.3, 6) #Define different voxel-to-space transforms for domain, codomain and #reference grid, also, use a non-identity pre-align transform D = gt_affine C = imwarp.mult_aff(gt_affine, gt_affine) R = np.eye(3) P = gt_affine #Create the original diffeomorphic map diff_map = imwarp.DiffeomorphicMap(2, dom_shape, R, dom_shape, D, cod_shape, C, P) diff_map.forward = np.array(d, dtype = floating) diff_map.backward = np.array(dinv, dtype = floating) #Warp the circle to obtain the expected image expected = diff_map.transform(circle, 'linear') #Simplify simplified = diff_map.get_simplified_transform() #warp the circle warped = simplified.transform(circle, 'linear') #verify that the simplified map is equivalent to the #original one assert_array_almost_equal(warped, expected) #And of course, it must be simpler... assert_equal(simplified.domain_affine, None) assert_equal(simplified.codomain_affine, None) assert_equal(simplified.disp_affine, None) assert_equal(simplified.domain_affine_inv, None) assert_equal(simplified.codomain_affine_inv, None) assert_equal(simplified.disp_affine_inv, None)
def test_mult_aff(): r""" Test matrix multiplication using None as identity """ A = np.array([[1.0, 2.0], [3.0, 4.0]]) B = np.array([[2.0, 0.0], [0.0, 2.0]]) C = imwarp.mult_aff(A, B) expected_mult = np.array([[2.0, 4.0], [6.0, 8.0]]) assert_array_almost_equal(C, expected_mult) C = imwarp.mult_aff(A, None) assert_array_almost_equal(C, A) C = imwarp.mult_aff(None, B) assert_array_almost_equal(C, B) C = imwarp.mult_aff(None, None) assert_equal(C, None)
def test_mult_aff(): r"""mult_aff from imwarp returns the matrix product A.dot(B) considering None as the identity """ A = np.array([[1.0, 2.0], [3.0, 4.0]]) B = np.array([[2.0, 0.0], [0.0, 2.0]]) C = imwarp.mult_aff(A, B) expected_mult = np.array([[2.0, 4.0], [6.0, 8.0]]) assert_array_almost_equal(C, expected_mult) C = imwarp.mult_aff(A, None) assert_array_almost_equal(C, A) C = imwarp.mult_aff(None, B) assert_array_almost_equal(C, B) C = imwarp.mult_aff(None, None) assert_equal(C, None)