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 run(self, static_files, moving_files, x0='affine', rm_small_clusters=50, qbx_thr=[40, 30, 20, 15], num_threads=None, greater_than=50, less_than=250, nb_pts=20, progressive=True, out_dir='', out_moved='moved.trk', out_affine='affine.txt', out_stat_centroids='static_centroids.trk', out_moving_centroids='moving_centroids.trk', out_moved_centroids='moved_centroids.trk'): """ Streamline-based linear registration. For efficiency we apply the registration on cluster centroids and remove small clusters. Parameters ---------- static_files : string moving_files : string x0 : string, optional rigid, similarity or affine transformation model (default affine) rm_small_clusters : int, optional Remove clusters that have less than `rm_small_clusters` (default 50) qbx_thr : variable int, optional Thresholds for QuickBundlesX (default [40, 30, 20, 15]) num_threads : int, optional Number of threads. If None (default) then all available threads will be used. Only metrics using OpenMP will use this variable. greater_than : int, optional Keep streamlines that have length greater than this value (default 50) less_than : int, optional Keep streamlines have length less than this value (default 250) np_pts : int, optional Number of points for discretizing each streamline (default 20) progressive : boolean, optional (default True) out_dir : string, optional Output directory (default input file directory) out_moved : string, optional Filename of moved tractogram (default 'moved.trk') out_affine : string, optional Filename of affine for SLR transformation (default 'affine.txt') out_stat_centroids : string, optional Filename of static centroids (default 'static_centroids.trk') out_moving_centroids : string, optional Filename of moving centroids (default 'moving_centroids.trk') out_moved_centroids : string, optional Filename of moved centroids (default 'moved_centroids.trk') Notes ----- The order of operations is the following. First short or long streamlines are removed. Second the tractogram or a random selection of the tractogram is clustered with QuickBundlesX. Then SLR [Garyfallidis15]_ is applied. References ---------- .. [Garyfallidis15] Garyfallidis et al. "Robust and efficient linear registration of white-matter fascicles in the space of streamlines", NeuroImage, 117, 124--140, 2015 .. [Garyfallidis14] Garyfallidis et al., "Direct native-space fiber bundle alignment for group comparisons", ISMRM, 2014. .. [Garyfallidis17] Garyfallidis et al. Recognition of white matter bundles using local and global streamline-based registration and clustering, NeuroImage, 2017. """ io_it = self.get_io_iterator() logging.info("QuickBundlesX clustering is in use") logging.info('QBX thresholds {0}'.format(qbx_thr)) for static_file, moving_file, out_moved_file, out_affine_file, \ static_centroids_file, moving_centroids_file, \ moved_centroids_file in io_it: logging.info('Loading static file {0}'.format(static_file)) logging.info('Loading moving file {0}'.format(moving_file)) static_obj = nib.streamlines.load(static_file) moving_obj = nib.streamlines.load(moving_file) static, static_header = static_obj.streamlines, static_obj.header moving, moving_header = moving_obj.streamlines, moving_obj.header moved, affine, centroids_static, centroids_moving = \ slr_with_qbx( static, moving, x0, rm_small_clusters=rm_small_clusters, greater_than=greater_than, less_than=less_than, qbx_thr=qbx_thr) logging.info('Saving output file {0}'.format(out_moved_file)) new_tractogram = nib.streamlines.Tractogram( moved, affine_to_rasmm=np.eye(4)) nib.streamlines.save(new_tractogram, out_moved_file, header=moving_header) logging.info('Saving output file {0}'.format(out_affine_file)) np.savetxt(out_affine_file, affine) logging.info( 'Saving output file {0}'.format(static_centroids_file)) new_tractogram = nib.streamlines.Tractogram( centroids_static, affine_to_rasmm=np.eye(4)) nib.streamlines.save(new_tractogram, static_centroids_file, header=static_header) logging.info( 'Saving output file {0}'.format(moving_centroids_file)) new_tractogram = nib.streamlines.Tractogram( centroids_moving, affine_to_rasmm=np.eye(4)) nib.streamlines.save(new_tractogram, moving_centroids_file, header=moving_header) centroids_moved = transform_streamlines(centroids_moving, affine) logging.info('Saving output file {0}'.format(moved_centroids_file)) new_tractogram = nib.streamlines.Tractogram( centroids_moved, affine_to_rasmm=np.eye(4)) nib.streamlines.save(new_tractogram, moved_centroids_file, header=moving_header)
def run(self, static_files, moving_files, x0='affine', rm_small_clusters=50, qbx_thr=[40, 30, 20, 15], num_threads=None, greater_than=50, less_than=250, nb_pts=20, progressive=True, out_dir='', out_moved='moved.trk', out_affine='affine.txt', out_stat_centroids='static_centroids.trk', out_moving_centroids='moving_centroids.trk', out_moved_centroids='moved_centroids.trk'): """ Streamline-based linear registration. For efficiency we apply the registration on cluster centroids and remove small clusters. Parameters ---------- static_files : string moving_files : string x0 : string, optional rigid, similarity or affine transformation model (default affine) rm_small_clusters : int, optional Remove clusters that have less than `rm_small_clusters` (default 50) qbx_thr : variable int, optional Thresholds for QuickBundlesX (default [40, 30, 20, 15]) num_threads : int, optional Number of threads. If None (default) then all available threads will be used. Only metrics using OpenMP will use this variable. greater_than : int, optional Keep streamlines that have length greater than this value (default 50) less_than : int, optional Keep streamlines have length less than this value (default 250) np_pts : int, optional Number of points for discretizing each streamline (default 20) progressive : boolean, optional (default True) out_dir : string, optional Output directory (default input file directory) out_moved : string, optional Filename of moved tractogram (default 'moved.trk') out_affine : string, optional Filename of affine for SLR transformation (default 'affine.txt') out_stat_centroids : string, optional Filename of static centroids (default 'static_centroids.trk') out_moving_centroids : string, optional Filename of moving centroids (default 'moving_centroids.trk') out_moved_centroids : string, optional Filename of moved centroids (default 'moved_centroids.trk') Notes ----- The order of operations is the following. First short or long streamlines are removed. Second the tractogram or a random selection of the tractogram is clustered with QuickBundlesX. Then SLR [Garyfallidis15]_ is applied. References ---------- .. [Garyfallidis15] Garyfallidis et al. "Robust and efficient linear registration of white-matter fascicles in the space of streamlines", NeuroImage, 117, 124--140, 2015 .. [Garyfallidis14] Garyfallidis et al., "Direct native-space fiber bundle alignment for group comparisons", ISMRM, 2014. .. [Garyfallidis17] Garyfallidis et al. Recognition of white matter bundles using local and global streamline-based registration and clustering, Neuroimage, 2017. """ io_it = self.get_io_iterator() logging.info("QuickBundlesX clustering is in use") logging.info('QBX thresholds {0}'.format(qbx_thr)) for static_file, moving_file, out_moved_file, out_affine_file, \ static_centroids_file, moving_centroids_file, \ moved_centroids_file in io_it: logging.info('Loading static file {0}'.format(static_file)) logging.info('Loading moving file {0}'.format(moving_file)) static, static_header = load_trk(static_file) moving, moving_header = load_trk(moving_file) moved, affine, centroids_static, centroids_moving = \ slr_with_qbx( static, moving, x0, rm_small_clusters=rm_small_clusters, greater_than=greater_than, less_than=less_than, qbx_thr=qbx_thr) logging.info('Saving output file {0}'.format(out_moved_file)) save_trk(out_moved_file, moved, affine=np.eye(4), header=static_header) logging.info('Saving output file {0}'.format(out_affine_file)) np.savetxt(out_affine_file, affine) logging.info('Saving output file {0}' .format(static_centroids_file)) save_trk(static_centroids_file, centroids_static, affine=np.eye(4), header=static_header) logging.info('Saving output file {0}' .format(moving_centroids_file)) save_trk(moving_centroids_file, centroids_moving, affine=np.eye(4), header=static_header) centroids_moved = transform_streamlines(centroids_moving, affine) logging.info('Saving output file {0}' .format(moved_centroids_file)) save_trk(moved_centroids_file, centroids_moved, affine=np.eye(4), header=static_header)