def test_rigid_real_bundles(): bundle_initial = fornix_streamlines()[:20] bundle, shift = center_streamlines(bundle_initial) mat = compose_matrix44([0, 0, 20, 45., 0, 0]) bundle2 = transform_streamlines(bundle, mat) bundle_sum_distance = BundleSumDistanceMatrixMetric() srr = StreamlineLinearRegistration(bundle_sum_distance, x0=np.zeros(6), method='Powell') new_bundle2 = srr.optimize(bundle, bundle2).transform(bundle2) evaluate_convergence(bundle, new_bundle2) bundle_min_distance = BundleMinDistanceMatrixMetric() srr = StreamlineLinearRegistration(bundle_min_distance, x0=np.zeros(6), method='Powell') new_bundle2 = srr.optimize(bundle, bundle2).transform(bundle2) evaluate_convergence(bundle, new_bundle2) assert_raises(ValueError, StreamlineLinearRegistration, method='Whatever')
def test_rigid_partial_real_bundles(): static = fornix_streamlines()[:20] moving = fornix_streamlines()[20:40] static_center, shift = center_streamlines(static) moving_center, shift2 = center_streamlines(moving) print(shift2) mat = compose_matrix(translate=np.array([0, 0, 0.]), angles=np.deg2rad([40, 0, 0.])) moved = transform_streamlines(moving_center, mat) srr = StreamlineLinearRegistration() srm = srr.optimize(static_center, moved) print(srm.fopt) print(srm.iterations) print(srm.funcs) moving_back = srm.transform(moved) print(srm.matrix) static_center = set_number_of_points(static_center, 100) moving_center = set_number_of_points(moving_back, 100) vol = np.zeros((100, 100, 100)) spts = np.concatenate(static_center, axis=0) spts = np.round(spts).astype(np.int) + np.array([50, 50, 50]) mpts = np.concatenate(moving_center, axis=0) mpts = np.round(mpts).astype(np.int) + np.array([50, 50, 50]) for index in spts: i, j, k = index vol[i, j, k] = 1 vol2 = np.zeros((100, 100, 100)) for index in mpts: i, j, k = index vol2[i, j, k] = 1 overlap = np.sum(np.logical_and(vol, vol2)) / float(np.sum(vol2)) assert_equal(overlap * 100 > 40, True)
def test_stream_rigid(): static = fornix_streamlines()[:20] moving = fornix_streamlines()[20:40] center_streamlines(static) mat = compose_matrix44([0, 0, 0, 0, 40, 0]) moving = transform_streamlines(moving, mat) srr = StreamlineLinearRegistration() sr_params = srr.optimize(static, moving) moved = transform_streamlines(moving, sr_params.matrix) srr = StreamlineLinearRegistration(verbose=True) srm = srr.optimize(static, moving) moved2 = transform_streamlines(moving, srm.matrix) moved3 = srm.transform(moving) assert_array_almost_equal(moved[0], moved2[0], decimal=3) assert_array_almost_equal(moved2[0], moved3[0], decimal=3)
def test_center_and_transform(): A = np.array([[1, 2, 3], [1, 2, 3.]]) streamlines = [A for _ in range(10)] streamlines2, center = center_streamlines(streamlines) B = np.zeros((2, 3)) assert_array_equal(streamlines2[0], B) assert_array_equal(center, A[0]) affine = np.eye(4) affine[0, 0] = 2 affine[:3, -1] = -np.array([2, 1, 1]) * center streamlines3 = transform_streamlines(streamlines, affine) assert_array_equal(streamlines3[0], B)
def test_center_and_transform(): A = np.array([[1, 2, 3], [1, 2, 3.]]) streamlines = [A for i in range(10)] streamlines2, center = center_streamlines(streamlines) B = np.zeros((2, 3)) assert_array_equal(streamlines2[0], B) assert_array_equal(center, A[0]) affine = np.eye(4) affine[0, 0] = 2 affine[:3, -1] = - np.array([2, 1, 1]) * center streamlines3 = transform_streamlines(streamlines, affine) assert_array_equal(streamlines3[0], B)
def test_rigid_parallel_lines(): bundle_initial = simulated_bundle() bundle, shift = center_streamlines(bundle_initial) mat = compose_matrix44([20, 0, 10, 0, 40, 0]) bundle2 = transform_streamlines(bundle, mat) bundle_sum_distance = BundleSumDistanceMatrixMetric() options = {'maxcor': 100, 'ftol': 1e-9, 'gtol': 1e-16, 'eps': 1e-3} srr = StreamlineLinearRegistration(metric=bundle_sum_distance, x0=np.zeros(6), method='L-BFGS-B', bounds=None, options=options) new_bundle2 = srr.optimize(bundle, bundle2).transform(bundle2) evaluate_convergence(bundle, new_bundle2)
def test_affine_real_bundles(): bundle_initial = fornix_streamlines() bundle_initial, shift = center_streamlines(bundle_initial) bundle = bundle_initial[:20] xgold = [0, 4, 2, 0, 10, 10, 1.2, 1.1, 1., 0., 0.2, 0.] mat = compose_matrix44(xgold) bundle2 = transform_streamlines(bundle_initial[:20], mat) x0 = np.array([0, 0, 0, 0, 0, 0, 1., 1., 1., 0, 0, 0]) x = 25 bounds = [(-x, x), (-x, x), (-x, x), (-x, x), (-x, x), (-x, x), (0.1, 1.5), (0.1, 1.5), (0.1, 1.5), (-1, 1), (-1, 1), (-1, 1)] options = {'maxcor': 10, 'ftol': 1e-7, 'gtol': 1e-5, 'eps': 1e-8} metric = BundleMinDistanceMatrixMetric() slr = StreamlineLinearRegistration(metric=metric, x0=x0, method='L-BFGS-B', bounds=bounds, verbose=True, options=options) slm = slr.optimize(bundle, bundle2) new_bundle2 = slm.transform(bundle2) slr2 = StreamlineLinearRegistration(metric=metric, x0=x0, method='Powell', bounds=None, verbose=True, options=None) slm2 = slr2.optimize(bundle, new_bundle2) new_bundle2 = slm2.transform(new_bundle2) evaluate_convergence(bundle, new_bundle2)
def test_similarity_real_bundles(): bundle_initial = fornix_streamlines() bundle_initial, shift = center_streamlines(bundle_initial) bundle = bundle_initial[:20] xgold = [0, 0, 10, 0, 0, 0, 1.5] mat = compose_matrix44(xgold) bundle2 = transform_streamlines(bundle_initial[:20], mat) metric = BundleMinDistanceMatrixMetric() x0 = np.array([0, 0, 0, 0, 0, 0, 1], 'f8') slr = StreamlineLinearRegistration(metric=metric, x0=x0, method='Powell', bounds=None, verbose=False) slm = slr.optimize(bundle, bundle2) new_bundle2 = slm.transform(bundle2) evaluate_convergence(bundle, new_bundle2)
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
def test_bundle_maps(): renderer = window.renderer() bundle = fornix_streamlines() bundle, shift = center_streamlines(bundle) mat = np.array([[1, 0, 0, 100], [0, 1, 0, 100], [0, 0, 1, 100], [0, 0, 0, 1.]]) bundle = transform_streamlines(bundle, mat) # metric = np.random.rand(*(200, 200, 200)) metric = 100 * np.ones((200, 200, 200)) # add lower values metric[100, :, :] = 100 * 0.5 # create a nice orange-red colormap lut = actor.colormap_lookup_table(scale_range=(0., 100.), hue_range=(0., 0.1), saturation_range=(1, 1), value_range=(1., 1)) line = actor.line(bundle, metric, linewidth=0.1, lookup_colormap=lut) window.add(renderer, line) window.add(renderer, actor.scalar_bar(lut, ' ')) report = window.analyze_renderer(renderer) npt.assert_almost_equal(report.actors, 1) # window.show(renderer) renderer.clear() nb_points = np.sum([len(b) for b in bundle]) values = 100 * np.random.rand(nb_points) # values[:nb_points/2] = 0 line = actor.streamtube(bundle, values, linewidth=0.1, lookup_colormap=lut) renderer.add(line) # window.show(renderer) report = window.analyze_renderer(renderer) npt.assert_equal(report.actors_classnames[0], 'vtkLODActor') renderer.clear() colors = np.random.rand(nb_points, 3) # values[:nb_points/2] = 0 line = actor.line(bundle, colors, linewidth=2) renderer.add(line) # window.show(renderer) report = window.analyze_renderer(renderer) npt.assert_equal(report.actors_classnames[0], 'vtkLODActor') # window.show(renderer) arr = window.snapshot(renderer) report2 = window.analyze_snapshot(arr) npt.assert_equal(report2.objects, 1) # try other input options for colors renderer.clear() actor.line(bundle, (1., 0.5, 0)) actor.line(bundle, np.arange(len(bundle))) actor.line(bundle) colors = [np.random.rand(*b.shape) for b in bundle] actor.line(bundle, colors=colors)