def test_hessian_matrix(): square = cp.zeros((5, 5)) square[2, 2] = 4 Hrr, Hrc, Hcc = hessian_matrix(square, sigma=0.1, order="rc") # fmt: off assert_array_almost_equal( Hrr, cp.asarray([ [0, 0, 0, 0, 0], # noqa [0, 0, 0, 0, 0], # noqa [2, 0, -2, 0, 2], # noqa [0, 0, 0, 0, 0], # noqa [0, 0, 0, 0, 0], # noqa ])) assert_array_almost_equal( Hrc, cp.asarray([ [0, 0, 0, 0, 0], # noqa [0, 1, 0, -1, 0], # noqa [0, 0, 0, 0, 0], # noqa [0, -1, 0, 1, 0], # noqa [0, 0, 0, 0, 0], # noqa ])) assert_array_almost_equal( Hcc, cp.asarray([ [0, 0, 2, 0, 0], # noqa [0, 0, 0, 0, 0], # noqa [0, 0, -2, 0, 0], # noqa [0, 0, 0, 0, 0], # noqa [0, 0, 2, 0, 0], # noqa ]))
def test_hessian_matrix_3d(): cube = cp.zeros((5, 5, 5)) cube[2, 2, 2] = 4 Hs = hessian_matrix(cube, sigma=0.1, order="rc") assert len( Hs) == 6, "incorrect number of Hessian images (%i) for 3D" % len(Hs) # fmt: off assert_array_almost_equal( Hs[2][:, 2, :], cp.asarray([ [0, 0, 0, 0, 0], # noqa [0, 1, 0, -1, 0], # noqa [0, 0, 0, 0, 0], # noqa [0, -1, 0, 1, 0], # noqa [0, 0, 0, 0, 0], # noqa ]))
def test_hessian_matrix_eigvals_3d(im3d): H = hessian_matrix(im3d) E = hessian_matrix_eigvals(H) E = cp.asnumpy(E) # test descending order: e0, e1, e2 = E assert np.all(e0 >= e1) and np.all(e1 >= e2) E0, E1, E2 = E[:, E.shape[1] // 2] # cross section row_center, col_center = np.asarray(E0.shape) // 2 circles = [ draw.circle_perimeter(row_center, col_center, radius, shape=E0.shape) for radius in range(1, E0.shape[1] // 2 - 1) ] response0 = np.array([np.mean(E0[c]) for c in circles]) response2 = np.array([np.mean(E2[c]) for c in circles]) # eigenvalues are negative just inside the sphere, positive just outside assert np.argmin(response2) < np.argmax(response0) assert np.min(response2) < 0 assert np.max(response0) > 0
def test_hessian_matrix_eigvals(): square = cp.zeros((5, 5)) square[2, 2] = 4 H = hessian_matrix(square, sigma=0.1, order="rc") l1, l2 = hessian_matrix_eigvals(H) # fmt: off assert_array_almost_equal( l1, cp.asarray([ [0, 0, 2, 0, 0], # noqa [0, 1, 0, 1, 0], # noqa [2, 0, -2, 0, 2], # noqa [0, 1, 0, 1, 0], # noqa [0, 0, 2, 0, 0], # noqa ])) assert_array_almost_equal( l2, cp.asarray([ [0, 0, 0, 0, 0], # noqa [0, -1, 0, -1, 0], # noqa [0, 0, -2, 0, 0], # noqa [0, -1, 0, -1, 0], # noqa [0, 0, 0, 0, 0], # noqa ]))