def jacobian_matrix(grid): """ Jacobian matrix at all points Parameters ---------- grid : ndarray Input grid. eg. if 3 dimensional ((dimension, len(x), len(y), len(z))) Returns ------- J : ndarray Jacobian matrix. eg. 3d case (dimension, dimension, len(x), len(y), len(z)) """ dimension = grid.ndim - 1 if dimension == 2: return np.array([gradient(grid[0]), gradient(grid[1])]) elif dimension == 3: return np.array( [gradient(grid[0]), gradient(grid[1]), gradient(grid[2])])
def get_derivative_template(reg, epsilon, params_der, window, T, n_jobs): # get I composed with phi # print moving_imgs.data[-1].shape, template_img.shape moving = copy.deepcopy(reg.moving) warp_fixed = np.copy( moving.apply_transform( Deformation(grid=reg.deformation.forward_mappings[-1]), order=3).data) # plt.imshow(warp_fixed.data) # plt.title('in dJ') # plt.show() dl_dv = -2. / reg.similarity.variance * gradient( warp_fixed) * reg.deformation.forward_dets[-1] # (t=1, ndim, img_shape)-for v and (img_shape,)- for template img J dl_dJ_dv = double_dev_J_v(dl_dv) params_grad = { 'reg': copy.deepcopy(reg), 'epsilon': epsilon, 'deformation': copy.deepcopy(reg.old_deformation) } loss = loss_func(reg=copy.deepcopy(reg), deformation=copy.deepcopy(reg.deformation)) dv_dJ = sparse_dot_product_forward(vector=np.copy( reg.resulting_vector_fields[-1].vector_fields), ndim=len(reg.fixed.shape), loss=loss, T=T, mat_shape=reg.fixed.shape, window=window, params_grad=params_grad, param_der=params_der, n_jobs=n_jobs).dot(dl_dJ_dv) # del dl_dv, dl_dJ_dv # gc.collect() return [-dv_dJ]
def derivative(self, J, I): Im = gaussian_filter(I, self.sigma, mode="constant") Jm = gaussian_filter(J, self.sigma, mode="constant") Ibar = I - Im Jbar = J - Jm II = gaussian_filter(I * I, self.sigma, mode="constant") - Im * Im JJ = gaussian_filter(J * J, self.sigma, mode="constant") - Jm * Jm IJ = gaussian_filter(I * J, self.sigma, mode="constant") - Im * Jm denom = II * JJ IJoverIIJJ = IJ / denom IJoverII = IJ / II IJoverIIJJ[np.where(denom < 1e-3)] = 0 IJoverII[np.where(II < 1e-3)] = 0 return (2 * gradient(Ibar) * IJoverIIJJ * (Jbar - Ibar * IJoverII) / self.variance)
def derivative(self, J, I): """ derivative of cost function of mahalanobis normalized cross correlation Parameters ---------- J : ndarray Input deformed fixed image. eg. 3 dimensional case (len(x), len(y), len(z)) I : ndarray Input deformed moving image. matrix : ndarray metric matrix Returns ------- momentum : ndarray Unsmoothed vector field """ assert (I.dtype == np.float) assert (J.dtype == np.float) Ai = sliding_matmul(I, self.matrix) Aj = sliding_matmul(J, self.matrix) Ibar = np.copy(Ai[..., self.index]).astype(np.float) Jbar = np.copy(Aj[..., self.index]).astype(np.float) II = np.einsum('...i,...i->...', Ai, Ai) JJ = np.einsum('...i,...i->...', Aj, Aj) IJ = np.einsum('...i,...i->...', Ai, Aj) IIJJ = II * JJ IJoverIIJJ = IJ / IIJJ IJoverII = IJ / II IJoverIIJJ[np.where(IIJJ < 1e-3)] = 0 IJoverII[np.where(II < 1e-3)] = 0 return (2 * gradient(Ibar) * IJoverIIJJ * (Jbar - Ibar * IJoverII) / self.variance)
def derivative(self, J, I): """ derivative of cost function of mahalanobis normalized cross correlation Parameters ---------- J : ndarray Input deformed fixed image. eg. 3 dimensional case (len(x), len(y), len(z)) I : ndarray Input deformed moving image. matrix : ndarray metric matrix Returns ------- momentum : ndarray Unsmoothed vector field """ assert I.dtype == np.float assert J.dtype == np.float Ai = sliding_matmul(I, self.matrix) Aj = sliding_matmul(J, self.matrix) Ibar = np.copy(Ai[..., self.index]).astype(np.float) Jbar = np.copy(Aj[..., self.index]).astype(np.float) II = np.einsum("...i,...i->...", Ai, Ai) JJ = np.einsum("...i,...i->...", Aj, Aj) IJ = np.einsum("...i,...i->...", Ai, Aj) IIJJ = II * JJ IJoverIIJJ = IJ / IIJJ IJoverII = IJ / II IJoverIIJJ[np.where(IIJJ < 1e-3)] = 0 IJoverII[np.where(II < 1e-3)] = 0 return 2 * gradient(Ibar) * IJoverIIJJ * (Jbar - Ibar * IJoverII) / self.variance
def derivative(self, J, I): """ derivative of cost function of zero means normalized cross correlation Parameters ---------- J : ndarray Input deformed fixed images. eg. 3 dimensional case (len(x), len(y), len(z)) I : ndarray Input deformed moving images. Returns ------- momentum : ndarray momentum field. eg. 3d case (dimension, len(x), len(y), len(z)) """ Im = uniform_filter(I, self.window_length) Jm = uniform_filter(J, self.window_length) Ibar = I - Im Jbar = J - Jm II = uniform_filter(I * I, self.window_length) - Im * Im JJ = uniform_filter(J * J, self.window_length) - Jm * Jm IJ = uniform_filter(I * J, self.window_length) - Im * Jm denom = II * JJ IJoverIIJJ = IJ / denom IJoverII = IJ / II IJoverIIJJ[np.where(denom < 1e-3)] = 0 IJoverII[np.where(II < 1e-3)] = 0 return (2 * gradient(Ibar) * IJoverIIJJ * (Jbar - Ibar * IJoverII) / self.variance)
def derivative(self, fixed, moving): return 2 * gradient(moving) * (fixed - moving) / self.variance