def test_whole_brain_slr(): streams, hdr = nib.trackvis.read(get_fnames('fornix')) fornix = [s[0] for s in streams] f = Streamlines(fornix) f1 = f.copy() f2 = f.copy() # check translation f2._data += np.array([50, 0, 0]) moved, transform, qb_centroids1, qb_centroids2 = whole_brain_slr( f1, f2, x0='affine', verbose=True, rm_small_clusters=2, greater_than=0, less_than=np.inf, qbx_thr=[5, 2, 1], progressive=False) # we can check the quality of registration by comparing the matrices # MAM streamline distances before and after SLR D12 = bundles_distances_mam(f1, f2) D1M = bundles_distances_mam(f1, moved) d12_minsum = np.sum(np.min(D12, axis=0)) d1m_minsum = np.sum(np.min(D1M, axis=0)) print("distances= ", d12_minsum, " ", d1m_minsum) assert_equal(d1m_minsum < d12_minsum, True) assert_array_almost_equal(transform[:3, 3], [-50, -0, -0], 2) # check rotation mat = compose_matrix44([0, 0, 0, 15, 0, 0]) f3 = f.copy() f3 = transform_streamlines(f3, mat) moved, transform, qb_centroids1, qb_centroids2 = slr_with_qbx( f1, f3, verbose=False, rm_small_clusters=1, greater_than=20, less_than=np.inf, qbx_thr=[2], progressive=True) # we can also check the quality by looking at the decomposed transform assert_array_almost_equal(decompose_matrix44(transform)[3], -15, 2) moved, transform, qb_centroids1, qb_centroids2 = slr_with_qbx( f1, f3, verbose=False, rm_small_clusters=1, select_random=400, greater_than=20, less_than=np.inf, qbx_thr=[2], progressive=True) # we can also check the quality by looking at the decomposed transform assert_array_almost_equal(decompose_matrix44(transform)[3], -15, 2)
def test_slr_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() f1_path = pjoin(out_dir, "f1.trk") sft = StatefulTractogram(f1, data_path, Space.RASMM) save_tractogram(sft, f1_path, bbox_valid_check=False) f2 = f1.copy() f2._data += np.array([50, 0, 0]) f2_path = pjoin(out_dir, "f2.trk") sft = StatefulTractogram(f2, data_path, Space.RASMM) save_tractogram(sft, f2_path, bbox_valid_check=False) slr_flow = SlrWithQbxFlow(force=True) slr_flow.run(f1_path, f2_path) out_path = slr_flow.last_generated_outputs['out_moved'] npt.assert_equal(os.path.isfile(out_path), True)
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_slr_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() f1_path = pjoin(out_dir, "f1.trk") save_trk(f1_path, Streamlines(f1), affine=np.eye(4)) f2 = f1.copy() f2._data += np.array([50, 0, 0]) f2_path = pjoin(out_dir, "f2.trk") save_trk(f2_path, Streamlines(f2), affine=np.eye(4)) slr_flow = SlrWithQbxFlow(force=True) slr_flow.run(f1_path, f2_path) out_path = slr_flow.last_generated_outputs['out_moved'] npt.assert_equal(os.path.isfile(out_path), True)
def test_whole_brain_slr(): streams, hdr = nib.trackvis.read(get_data('fornix')) fornix = [s[0] for s in streams] f = Streamlines(fornix) f1 = f.copy() f2 = f.copy() # check translation f2._data += np.array([50, 0, 0]) moved, transform, qb_centroids1, qb_centroids2 = whole_brain_slr( f1, f2, verbose=True, rm_small_clusters=2, greater_than=0, less_than=np.inf, qb_thr=5, progressive=False) # we can check the quality of registration by comparing the matrices # MAM streamline distances before and after SLR D12 = bundles_distances_mam(f1, f2) D1M = bundles_distances_mam(f1, moved) d12_minsum = np.sum(np.min(D12, axis=0)) d1m_minsum = np.sum(np.min(D1M, axis=0)) assert_equal(d1m_minsum < d12_minsum, True) assert_array_almost_equal(transform[:3, 3], [-50, -0, -0], 3) # check rotation mat = compose_matrix44([0, 0, 0, 15, 0, 0]) f3 = f.copy() f3 = transform_streamlines(f3, mat) moved, transform, qb_centroids1, qb_centroids2 = slr_with_qb( f1, f3, verbose=False, rm_small_clusters=1, greater_than=20, less_than=np.inf, qb_thr=2, progressive=True) # we can also check the quality by looking at the decomposed transform assert_array_almost_equal(decompose_matrix44(transform)[3], -15, 2) moved, transform, qb_centroids1, qb_centroids2 = slr_with_qb( f1, f3, verbose=False, rm_small_clusters=1, select_random=400, greater_than=20, less_than=np.inf, qb_thr=2, progressive=True) # we can also check the quality by looking at the decomposed transform assert_array_almost_equal(decompose_matrix44(transform)[3], -15, 2)
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_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 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)
import nibabel as nib from numpy.testing import (assert_equal, assert_almost_equal, run_module_suite) from dipy.data import get_fnames from dipy.segment.bundles import RecoBundles from dipy.tracking.distances import bundles_distances_mam 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)
def test_whole_brain_slr(): fname = get_fnames('fornix') fornix = load_tractogram(fname, 'same', bbox_valid_check=False).streamlines f = Streamlines(fornix) f1 = f.copy() f2 = f.copy() # check translation f2._data += np.array([50, 0, 0]) moved, transform, qb_centroids1, qb_centroids2 = whole_brain_slr( f1, f2, x0='affine', verbose=True, rm_small_clusters=2, greater_than=0, less_than=np.inf, qbx_thr=[5, 2, 1], progressive=False) # we can check the quality of registration by comparing the matrices # MAM streamline distances before and after SLR D12 = bundles_distances_mam(f1, f2) D1M = bundles_distances_mam(f1, moved) d12_minsum = np.sum(np.min(D12, axis=0)) d1m_minsum = np.sum(np.min(D1M, axis=0)) print("distances= ", d12_minsum, " ", d1m_minsum) assert_equal(d1m_minsum < d12_minsum, True) assert_array_almost_equal(transform[:3, 3], [-50, -0, -0], 2) # check rotation mat = compose_matrix44([0, 0, 0, 15, 0, 0]) f3 = f.copy() f3 = transform_streamlines(f3, mat) moved, transform, qb_centroids1, qb_centroids2 = slr_with_qbx( f1, f3, verbose=False, rm_small_clusters=1, greater_than=20, less_than=np.inf, qbx_thr=[2], progressive=True) # we can also check the quality by looking at the decomposed transform assert_array_almost_equal(decompose_matrix44(transform)[3], -15, 2) moved, transform, qb_centroids1, qb_centroids2 = slr_with_qbx( f1, f3, verbose=False, rm_small_clusters=1, select_random=400, greater_than=20, less_than=np.inf, qbx_thr=[2], progressive=True) # we can also check the quality by looking at the decomposed transform assert_array_almost_equal(decompose_matrix44(transform)[3], -15, 2)
import numpy as np import nibabel as nib from numpy.testing import (assert_equal, assert_almost_equal, run_module_suite) from dipy.data import get_fnames from dipy.segment.bundles import RecoBundles from dipy.tracking.distances import bundles_distances_mam 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.,