def new_window(): active_streamlines = Streamlines() for bundle in self.cla.keys(): if bundle.GetVisibility(): t = self.cla[bundle]['tractogram'] c = self.cla[bundle]['cluster'] indices = self.tractogram_clusters[t][c] active_streamlines.extend(Streamlines(indices)) # Using the header of the first of the tractograms active_sft = StatefulTractogram(active_streamlines, self.tractograms[0], Space.RASMM) hz2 = Horizon([active_sft], self.images, cluster=True, cluster_thr=self.cluster_thr / 2., random_colors=self.random_colors, length_lt=np.inf, length_gt=0, clusters_lt=np.inf, clusters_gt=0, world_coords=True, interactive=True) ren2 = hz2.build_scene() hz2.build_show(ren2)
def test_rb_no_neighb(): # what if no neighbors are found? No recognition b = Streamlines(fornix) b1 = b.copy() b2 = b1[:20].copy() b2._data += np.array([100, 0, 0]) b3 = b1[:20].copy() b3._data += np.array([300, 0, 0]) b.extend(b3) rb = RecoBundles(b, greater_than=0, clust_thr=10) rec_trans, rec_labels = rb.recognize(model_bundle=b2, model_clust_thr=5., reduction_thr=10) if len(rec_trans) > 0: refine_trans, refine_labels = rb.refine(model_bundle=b2, pruned_streamlines=rec_trans, model_clust_thr=5., reduction_thr=10) assert_equal(len(refine_labels), 0) assert_equal(len(refine_trans), 0) else: assert_equal(len(rec_labels), 0) assert_equal(len(rec_trans), 0)
def test_rb_no_neighb(): # what if no neighbors are found? No recognition b = Streamlines(fornix) b1 = b.copy() b2 = b1[:20].copy() b2._data += np.array([100, 0, 0]) b3 = b1[:20].copy() b3._data += np.array([300, 0, 0]) b.extend(b3) rb = RecoBundles(b, greater_than=0, clust_thr=10) rec_trans, rec_labels = rb.recognize(model_bundle=b2, model_clust_thr=5., reduction_thr=10) if len(rec_trans) > 0: refine_trans, refine_labels = rb.refine(model_bundle=b2, pruned_streamlines=rec_trans, model_clust_thr=5., reduction_thr=10) assert_equal(len(refine_labels), 0) assert_equal(len(refine_trans), 0) else: assert_equal(len(rec_labels), 0) assert_equal(len(rec_trans), 0)
def test_recobundles_flow(): with TemporaryDirectory() as out_dir: data_path = get_fnames('fornix') fornix = load_tractogram(data_path, 'same', bbox_valid_check=False).streamlines f = Streamlines(fornix) f1 = f.copy() f2 = f1[:15].copy() f2._data += np.array([40, 0, 0]) f.extend(f2) f2_path = pjoin(out_dir, "f2.trk") sft = StatefulTractogram(f2, data_path, Space.RASMM) save_tractogram(sft, f2_path, bbox_valid_check=False) f1_path = pjoin(out_dir, "f1.trk") sft = StatefulTractogram(f, data_path, Space.RASMM) save_tractogram(sft, f1_path, bbox_valid_check=False) rb_flow = RecoBundlesFlow(force=True) rb_flow.run(f1_path, f2_path, greater_than=0, clust_thr=10, model_clust_thr=5., reduction_thr=10, out_dir=out_dir) labels = rb_flow.last_generated_outputs['out_recognized_labels'] recog_trk = rb_flow.last_generated_outputs['out_recognized_transf'] rec_bundle = load_tractogram(recog_trk, 'same', bbox_valid_check=False).streamlines npt.assert_equal(len(rec_bundle) == len(f2), True) label_flow = LabelsBundlesFlow(force=True) label_flow.run(f1_path, labels) recog_bundle = label_flow.last_generated_outputs['out_bundle'] rec_bundle_org = load_tractogram(recog_bundle, 'same', bbox_valid_check=False).streamlines BMD = BundleMinDistanceMetric() nb_pts = 20 static = set_number_of_points(f2, nb_pts) moving = set_number_of_points(rec_bundle_org, nb_pts) BMD.setup(static, moving) x0 = np.array([0, 0, 0, 0, 0, 0, 1., 1., 1, 0, 0, 0]) # affine bmd_value = BMD.distance(x0.tolist()) npt.assert_equal(bmd_value < 1, True)
def test_afq_profile(): data = np.ones((10, 10, 10)) bundle = Streamlines() bundle.extend(np.array([[[0, 0., 0], [1, 0., 0.], [2, 0., 0.]]])) bundle.extend(np.array([[[0, 0., 0.], [1, 0., 0], [2, 0, 0.]]])) profile = afq_profile(data, bundle) npt.assert_equal(profile, np.ones(100)) profile = afq_profile(data, bundle, affine=None, n_points=10, weights=None) npt.assert_equal(profile, np.ones(10)) profile = afq_profile(data, bundle, affine=None, weights=gaussian_weights, stat=np.median) npt.assert_equal(profile, np.ones(100)) profile = afq_profile(data, bundle, affine=None, orient_by=bundle[0], weights=gaussian_weights, stat=np.median) npt.assert_equal(profile, np.ones(100)) profile = afq_profile(data, bundle, affine=None, n_points=10, weights=None) npt.assert_equal(profile, np.ones(10)) profile = afq_profile(data, bundle, affine=None, n_points=10, weights=np.ones((2, 10)) * 0.5) npt.assert_equal(profile, np.ones(10)) # Disallow setting weights that don't sum to 1 across fibers/nodes: npt.assert_raises(ValueError, afq_profile, data, bundle, affine=None, n_points=10, weights=np.ones((2, 10)) * 0.6) # Test using an affine: affine = np.eye(4) affine[:, 3] = [-1, 100, -20, 1] # Transform the streamlines: bundle._data = bundle._data + affine[:3, 3] profile = afq_profile(data, bundle, affine=affine, n_points=10, weights=None) npt.assert_equal(profile, np.ones(10)) # Test for error-handling: empty_bundle = Streamlines([]) npt.assert_raises(ValueError, afq_profile, data, empty_bundle)
def test_afq_profile(): data = np.ones((10, 10, 10)) bundle = Streamlines() bundle.extend(np.array([[[0, 0., 0], [1, 0., 0.], [2, 0., 0.]]])) bundle.extend(np.array([[[0, 0., 0.], [1, 0., 0], [2, 0, 0.]]])) profile = afq_profile(data, bundle) npt.assert_equal(profile, np.ones(100)) profile = afq_profile(data, bundle, affine=None, n_points=10, weights=None) npt.assert_equal(profile, np.ones(10)) profile = afq_profile(data, bundle, affine=None, weights=gaussian_weights, stat=np.median) npt.assert_equal(profile, np.ones(100)) profile = afq_profile(data, bundle, affine=None, orient_by=bundle[0], weights=gaussian_weights, stat=np.median) npt.assert_equal(profile, np.ones(100)) profile = afq_profile(data, bundle, affine=None, n_points=10, weights=None) npt.assert_equal(profile, np.ones(10)) profile = afq_profile(data, bundle, affine=None, n_points=10, weights=np.ones((2, 10)) * 0.5) npt.assert_equal(profile, np.ones(10)) # Disallow setting weights that don't sum to 1 across fibers/nodes: npt.assert_raises(ValueError, afq_profile, data, bundle, affine=None, n_points=10, weights=np.ones((2, 10)) * 0.6) # Test using an affine: affine = np.eye(4) affine[:, 3] = [-1, 100, -20, 1] # Transform the streamlines: bundle._data = bundle._data + affine[:3, 3] profile = afq_profile(data, bundle, affine=affine, n_points=10, weights=None) npt.assert_equal(profile, np.ones(10)) # Test for error-handling: empty_bundle = Streamlines([]) npt.assert_raises(ValueError, afq_profile, data, empty_bundle)
def test_recobundles_flow(): with TemporaryDirectory() as out_dir: data_path = get_fnames('fornix') streams, hdr = nib.trackvis.read(data_path) fornix = [s[0] for s in streams] f = Streamlines(fornix) f1 = f.copy() f2 = f1[:15].copy() f2._data += np.array([40, 0, 0]) f.extend(f2) f2_path = pjoin(out_dir, "f2.trk") save_trk(f2_path, f2, affine=np.eye(4)) f1_path = pjoin(out_dir, "f1.trk") save_trk(f1_path, f, affine=np.eye(4)) rb_flow = RecoBundlesFlow(force=True) rb_flow.run(f1_path, f2_path, greater_than=0, clust_thr=10, model_clust_thr=5., reduction_thr=10, out_dir=out_dir) labels = rb_flow.last_generated_outputs['out_recognized_labels'] recog_trk = rb_flow.last_generated_outputs['out_recognized_transf'] rec_bundle, _ = load_trk(recog_trk) npt.assert_equal(len(rec_bundle) == len(f2), True) label_flow = LabelsBundlesFlow(force=True) label_flow.run(f1_path, labels) recog_bundle = label_flow.last_generated_outputs['out_bundle'] rec_bundle_org, _ = load_trk(recog_bundle) BMD = BundleMinDistanceMetric() nb_pts = 20 static = set_number_of_points(f2, nb_pts) moving = set_number_of_points(rec_bundle_org, nb_pts) BMD.setup(static, moving) x0 = np.array([0, 0, 0, 0, 0, 0, 1., 1., 1, 0, 0, 0]) # affine bmd_value = BMD.distance(x0.tolist()) npt.assert_equal(bmd_value < 1, True)
def save(): saving_streamlines = Streamlines() for bundle in self.cla.keys(): if bundle.GetVisibility(): t = self.cla[bundle]['tractogram'] c = self.cla[bundle]['cluster'] indices = self.tractogram_clusters[t][c] saving_streamlines.extend(Streamlines(indices)) print('Saving result in tmp.trk') # Using the header of the first of the tractograms sft_new = StatefulTractogram(saving_streamlines, self.tractograms[0], Space.RASMM) save_tractogram(sft_new, 'tmp.trk', bbox_valid_check=False) print('Saved!')
def setup_module(): global f, f1, f2, f3, fornix fname = get_fnames('fornix') fornix = load_tractogram(fname, 'same', bbox_valid_check=False).streamlines f = Streamlines(fornix) f1 = f.copy() f2 = f1[:20].copy() f2._data += np.array([50, 0, 0]) f3 = f1[200:].copy() f3._data += np.array([100, 0, 0]) f.extend(f2) f.extend(f3)
def test_recobundles_flow(): with TemporaryDirectory() as out_dir: data_path = get_fnames('fornix') streams, hdr = nib.trackvis.read(data_path) fornix = [s[0] for s in streams] f = Streamlines(fornix) f1 = f.copy() f2 = f1[:15].copy() f2._data += np.array([40, 0, 0]) f.extend(f2) f2_path = pjoin(out_dir, "f2.trk") save_trk(f2_path, f2, affine=np.eye(4)) f1_path = pjoin(out_dir, "f1.trk") save_trk(f1_path, f, affine=np.eye(4)) rb_flow = RecoBundlesFlow(force=True) rb_flow.run(f1_path, f2_path, greater_than=0, clust_thr=10, model_clust_thr=5., reduction_thr=10, out_dir=out_dir) labels = rb_flow.last_generated_outputs['out_recognized_labels'] recog_trk = rb_flow.last_generated_outputs['out_recognized_transf'] rec_bundle, _ = load_trk(recog_trk) npt.assert_equal(len(rec_bundle) == len(f2), True) label_flow = LabelsBundlesFlow(force=True) label_flow.run(f1_path, labels) recog_bundle = label_flow.last_generated_outputs['out_bundle'] rec_bundle_org, _ = load_trk(recog_bundle) BMD = BundleMinDistanceMetric() nb_pts = 20 static = set_number_of_points(f2, nb_pts) moving = set_number_of_points(rec_bundle_org, nb_pts) BMD.setup(static, moving) x0 = np.array([0, 0, 0, 0, 0, 0, 1., 1., 1, 0, 0, 0]) # affine bmd_value = BMD.distance(x0.tolist()) npt.assert_equal(bmd_value < 1, True)
fetch_bundles_2_subjects() """ The following function outputs a dictionary with the required bundles e.g. ``af left`` (left arcuate fasciculus) and maps, e.g. FA for a specific subject. """ res = read_bundles_2_subjects('subj_1', ['t1', 'fa'], ['af.left', 'cst.right', 'cc_1']) """ We will use 3 bundles, FA and the affine transformation that brings the voxel coordinates to world coordinates (RAS 1mm). """ streamlines = Streamlines(res['af.left']) streamlines.extend(res['cst.right']) streamlines.extend(res['cc_1']) data = res['fa'] shape = data.shape affine = res['affine'] """ With our current design it is easy to decide in which space you want the streamlines and slices to appear. The default we have here is to appear in world coordinates (RAS 1mm). """ world_coords = True """ If we want to see the objects in native space we need to make sure that all objects which are currently in world coordinates are transformed back to
def key_press(obj, event): key = obj.GetKeySym() if self.cluster: # hide on/off unselected centroids if key == 'h' or key == 'H': if self.hide_centroids: for ca in self.cea: if (self.cea[ca]['length'] >= self.length_min or self.cea[ca]['size'] >= self.size_min): if self.cea[ca]['selected'] == 0: ca.VisibilityOff() else: for ca in self.cea: if (self.cea[ca]['length'] >= self.length_min and self.cea[ca]['size'] >= self.size_min): if self.cea[ca]['selected'] == 0: ca.VisibilityOn() self.hide_centroids = not self.hide_centroids show_m.render() # invert selection if key == 'i' or key == 'I': for ca in self.cea: if (self.cea[ca]['length'] >= self.self.length_min and self.cea[ca]['size'] >= self.size_min): self.cea[ca]['selected'] = \ not self.cea[ca]['selected'] cas = self.cea[ca]['cluster_actor'] self.cla[cas]['selected'] = \ self.cea[ca]['selected'] show_m.render() # save current result if key == 's' or key == 'S': saving_streamlines = Streamlines() for bundle in self.cla.keys(): if bundle.GetVisibility(): t = self.cla[bundle]['tractogram'] c = self.cla[bundle]['cluster'] indices = self.tractogram_clusters[t][c] saving_streamlines.extend(Streamlines(indices)) print('Saving result in tmp.trk') save_trk('tmp.trk', saving_streamlines, np.eye(4)) if key == 'y' or key == 'Y': active_streamlines = Streamlines() for bundle in self.cla.keys(): if bundle.GetVisibility(): t = self.cla[bundle]['tractogram'] c = self.cla[bundle]['cluster'] indices = self.tractogram_clusters[t][c] active_streamlines.extend(Streamlines(indices)) # self.tractograms = [active_streamlines] hz2 = horizon([active_streamlines], self.images, cluster=True, cluster_thr=5, random_colors=self.random_colors, length_lt=np.inf, length_gt=0, clusters_lt=np.inf, clusters_gt=0, world_coords=True, interactive=True) ren2 = hz2.build_scene() hz2.build_show(ren2) if key == 'a' or key == 'A': if self.select_all is False: for ca in self.cea: if (self.cea[ca]['length'] >= self.length_min and self.cea[ca]['size'] >= self.size_min): self.cea[ca]['selected'] = 1 cas = self.cea[ca]['cluster_actor'] self.cla[cas]['selected'] = \ self.cea[ca]['selected'] show_m.render() self.select_all = True else: for ca in self.cea: if (self.cea[ca]['length'] >= self.length_min and self.cea[ca]['size'] >= self.size_min): self.cea[ca]['selected'] = 0 cas = self.cea[ca]['cluster_actor'] self.cla[cas]['selected'] = \ self.cea[ca]['selected'] show_m.render() self.select_all = False if key == 'e' or key == 'E': for c in self.cea: if self.cea[c]['selected']: if not self.cea[c]['expanded']: len_ = self.cea[c]['length'] sz_ = self.cea[c]['size'] if (len_ >= self.length_min and sz_ >= self.size_min): self.cea[c]['cluster_actor']. \ VisibilityOn() c.VisibilityOff() self.cea[c]['expanded'] = 1 show_m.render() if key == 'r' or key == 'R': for c in self.cea: if (self.cea[c]['length'] >= self.length_min and self.cea[c]['size'] >= self.size_min): self.cea[c]['cluster_actor'].VisibilityOff() c.VisibilityOn() self.cea[c]['expanded'] = 0 show_m.render()
from dipy.segment.clustering import qbx_and_merge streams, hdr = nib.trackvis.read(get_fnames('fornix')) fornix = [s[0] for s in streams] f = Streamlines(fornix) f1 = f.copy() f2 = f1[:20].copy() f2._data += np.array([50, 0, 0]) f3 = f1[200:].copy() f3._data += np.array([100, 0, 0]) f.extend(f2) f.extend(f3) def test_rb_check_defaults(): rb = RecoBundles(f, greater_than=0, clust_thr=10) rec_trans, rec_labels = rb.recognize(model_bundle=f2, model_clust_thr=5., reduction_thr=10) D = bundles_distances_mam(f2, f[rec_labels]) # check if the bundle is recognized correctly if len(f2) == len(rec_labels):
from dipy.tracking.streamline import Streamlines from dipy.segment.clustering import qbx_and_merge streams, hdr = nib.trackvis.read(get_fnames('fornix')) fornix = [s[0] for s in streams] f = Streamlines(fornix) f1 = f.copy() f2 = f1[:20].copy() f2._data += np.array([50, 0, 0]) f3 = f1[200:].copy() f3._data += np.array([100, 0, 0]) f.extend(f2) f.extend(f3) def test_rb_check_defaults(): rb = RecoBundles(f, greater_than=0, clust_thr=10) rec_trans, rec_labels = rb.recognize(model_bundle=f2, model_clust_thr=5., reduction_thr=10) D = bundles_distances_mam(f2, f[rec_labels]) # check if the bundle is recognized correctly if len(f2) == len(rec_labels):
def key_press(obj, event): key = obj.GetKeySym() if self.cluster: # hide on/off unselected centroids if key == 'h' or key == 'H': if self.hide_centroids: for ca in self.cea: if (self.cea[ca]['length'] >= self.length_min or self.cea[ca]['size'] >= self.size_min): if self.cea[ca]['selected'] == 0: ca.VisibilityOff() else: for ca in self.cea: if (self.cea[ca]['length'] >= self.length_min and self.cea[ca]['size'] >= self.size_min): if self.cea[ca]['selected'] == 0: ca.VisibilityOn() self.hide_centroids = not self.hide_centroids show_m.render() # invert selection if key == 'i' or key == 'I': for ca in self.cea: if (self.cea[ca]['length'] >= self.self.length_min and self.cea[ca]['size'] >= self.size_min): self.cea[ca]['selected'] = \ not self.cea[ca]['selected'] cas = self.cea[ca]['cluster_actor'] self.cla[cas]['selected'] = \ self.cea[ca]['selected'] show_m.render() # save current result if key == 's' or key == 'S': saving_streamlines = Streamlines() for bundle in self.cla.keys(): if bundle.GetVisibility(): t = self.cla[bundle]['tractogram'] c = self.cla[bundle]['cluster'] indices = self.tractogram_clusters[t][c] saving_streamlines.extend(Streamlines(indices)) print('Saving result in tmp.trk') save_trk('tmp.trk', saving_streamlines, np.eye(4)) if key == 'y' or key == 'Y': active_streamlines = Streamlines() for bundle in self.cla.keys(): if bundle.GetVisibility(): t = self.cla[bundle]['tractogram'] c = self.cla[bundle]['cluster'] indices = self.tractogram_clusters[t][c] active_streamlines.extend(Streamlines(indices)) # self.tractograms = [active_streamlines] hz2 = horizon([active_streamlines], self.images, cluster=True, cluster_thr=5, random_colors=self.random_colors, length_lt=np.inf, length_gt=0, clusters_lt=np.inf, clusters_gt=0, world_coords=True, interactive=True) ren2 = hz2.build_scene() hz2.build_show(ren2) if key == 'a' or key == 'A': if self.select_all is False: for ca in self.cea: if (self.cea[ca]['length'] >= self.length_min and self.cea[ca]['size'] >= self.size_min): self.cea[ca]['selected'] = 1 cas = self.cea[ca]['cluster_actor'] self.cla[cas]['selected'] = \ self.cea[ca]['selected'] show_m.render() self.select_all = True else: for ca in self.cea: if (self.cea[ca]['length'] >= self.length_min and self.cea[ca]['size'] >= self.size_min): self.cea[ca]['selected'] = 0 cas = self.cea[ca]['cluster_actor'] self.cla[cas]['selected'] = \ self.cea[ca]['selected'] show_m.render() self.select_all = False if key == 'e' or key == 'E': for c in self.cea: if self.cea[c]['selected']: if not self.cea[c]['expanded']: len_ = self.cea[c]['length'] sz_ = self.cea[c]['size'] if (len_ >= self.length_min and sz_ >= self.size_min): self.cea[c]['cluster_actor']. \ VisibilityOn() c.VisibilityOff() self.cea[c]['expanded'] = 1 show_m.render() if key == 'r' or key == 'R': for c in self.cea: if (self.cea[c]['length'] >= self.length_min and self.cea[c]['size'] >= self.size_min): self.cea[c]['cluster_actor'].VisibilityOff() c.VisibilityOn() self.cea[c]['expanded'] = 0 show_m.render()