def test_compose_transformations(): A = np.eye(4) A[0, -1] = 10 B = np.eye(4) B[0, -1] = -20 C = np.eye(4) C[0, -1] = 10 CBA = compose_transformations(A, B, C) assert_array_equal(CBA, np.eye(4)) assert_raises(ValueError, compose_transformations, A)
def optimize(self, static, moving, mat=None): """ Find the minimum of the provided metric. Parameters ---------- static : streamlines Reference or fixed set of streamlines. moving : streamlines Moving set of streamlines. mat : array Transformation (4, 4) matrix to start the registration. ``mat`` is applied to moving. Default value None which means that initial transformation will be generated by shifting the centers of moving and static sets of streamlines to the origin. Returns ------- map : StreamlineRegistrationMap """ msg = 'need to have the same number of points. Use ' msg += 'set_number_of_points from dipy.tracking.streamline' if not np.all(np.array(list(map(len, static))) == static[0].shape[0]): raise ValueError('Static streamlines ' + msg) if not np.all(np.array(list(map(len, moving))) == moving[0].shape[0]): raise ValueError('Moving streamlines ' + msg) if not np.all(np.array(list(map(len, moving))) == static[0].shape[0]): raise ValueError('Static and moving streamlines ' + msg) if mat is None: static_centered, static_shift = center_streamlines(static) moving_centered, moving_shift = center_streamlines(moving) static_mat = compose_matrix44([static_shift[0], static_shift[1], static_shift[2], 0, 0, 0]) moving_mat = compose_matrix44([-moving_shift[0], -moving_shift[1], -moving_shift[2], 0, 0, 0]) else: static_centered = static moving_centered = transform_streamlines(moving, mat) static_mat = np.eye(4) moving_mat = mat self.metric.setup(static_centered, moving_centered) distance = self.metric.distance if self.method == 'Powell': if self.options is None: self.options = {'xtol': 1e-6, 'ftol': 1e-6, 'maxiter': 1e6} opt = Optimizer(distance, self.x0.tolist(), method=self.method, options=self.options, evolution=self.evolution) if self.method == 'L-BFGS-B': if self.options is None: self.options = {'maxcor': 10, 'ftol': 1e-7, 'gtol': 1e-5, 'eps': 1e-8, 'maxiter': 100} opt = Optimizer(distance, self.x0.tolist(), method=self.method, bounds=self.bounds, options=self.options, evolution=self.evolution) if self.verbose: opt.print_summary() opt_mat = compose_matrix44(opt.xopt) mat = compose_transformations(moving_mat, opt_mat, static_mat) mat_history = [] if opt.evolution is not None: for vecs in opt.evolution: mat_history.append( compose_transformations(moving_mat, compose_matrix44(vecs), static_mat)) srm = StreamlineRegistrationMap(mat, opt.xopt, opt.fopt, mat_history, opt.nfev, opt.nit) del opt return srm
def optimize(self, static, moving, mat=None): """ Find the minimum of the provided metric. Parameters ---------- static : streamlines Reference or fixed set of streamlines. moving : streamlines Moving set of streamlines. mat : array Transformation (4, 4) matrix to start the registration. ``mat`` is applied to moving. Default value None which means that initial transformation will be generated by shifting the centers of moving and static sets of streamlines to the origin. Returns ------- map : StreamlineRegistrationMap """ msg = 'need to have the same number of points. Use ' msg += 'set_number_of_points from dipy.tracking.streamline' if not np.all(np.array(list(map(len, static))) == static[0].shape[0]): raise ValueError('Static streamlines ' + msg) if not np.all(np.array(list(map(len, moving))) == moving[0].shape[0]): raise ValueError('Moving streamlines ' + msg) if not np.all(np.array(list(map(len, moving))) == static[0].shape[0]): raise ValueError('Static and moving streamlines ' + msg) if mat is None: static_centered, static_shift = center_streamlines(static) moving_centered, moving_shift = center_streamlines(moving) static_mat = compose_matrix44( [static_shift[0], static_shift[1], static_shift[2], 0, 0, 0]) moving_mat = compose_matrix44([ -moving_shift[0], -moving_shift[1], -moving_shift[2], 0, 0, 0 ]) else: static_centered = static moving_centered = transform_streamlines(moving, mat) static_mat = np.eye(4) moving_mat = mat self.metric.setup(static_centered, moving_centered) distance = self.metric.distance if self.method == 'Powell': if self.options is None: self.options = {'xtol': 1e-6, 'ftol': 1e-6, 'maxiter': 1e6} opt = Optimizer(distance, self.x0.tolist(), method=self.method, options=self.options, evolution=self.evolution) if self.method == 'L-BFGS-B': if self.options is None: self.options = { 'maxcor': 10, 'ftol': 1e-7, 'gtol': 1e-5, 'eps': 1e-8, 'maxiter': 100 } opt = Optimizer(distance, self.x0.tolist(), method=self.method, bounds=self.bounds, options=self.options, evolution=self.evolution) if self.verbose: opt.print_summary() opt_mat = compose_matrix44(opt.xopt) mat = compose_transformations(moving_mat, opt_mat, static_mat) mat_history = [] if opt.evolution is not None: for vecs in opt.evolution: mat_history.append( compose_transformations(moving_mat, compose_matrix44(vecs), static_mat)) srm = StreamlineRegistrationMap(mat, opt.xopt, opt.fopt, mat_history, opt.nfev, opt.nit) del opt return srm