def qb_metrics_features(streamlines, threshold=10.0, metric=None, max_nb_clusters=np.iinfo('i4').max): """ Enhancing QuickBundles with different metrics and features metric: 'IF', 'RF', 'CoMF', 'MF', 'AF', 'VBEF', None """ if metric == 'IF': feature = IdentityFeature() metric = AveragePointwiseEuclideanMetric(feature=feature) elif metric == 'RF': feature = ResampleFeature(nb_point=24) metric = AveragePointwiseEuclideanMetric(feature=feature) elif metric == 'CoMF': feature = CenterOfMassFeature() metric = EuclideanMetric(feature) elif metric == 'MF': feature = MidpointFeature() metric = EuclideanMetric(feature) elif metric == 'AF': feature = ArcLengthFeature() metric = EuclideanMetric(feature) elif metric == 'VBEF': feature = VectorOfEndpointsFeature() metric = CosineMetric(feature) else: metric = "MDF_12points" qb = QuickBundles(threshold=threshold, metric=metric, max_nb_clusters=max_nb_clusters) clusters = qb.cluster(streamlines) labels = np.array(len(streamlines) * [None]) N_list = [] for i in range(len(clusters)): N_list.append(clusters[i]['N']) data_clusters = [] for i in range(len(clusters)): labels[clusters[i]['indices']] = i + 1 data_clusters.append(streamlines[clusters[i]['indices']]) return labels, data_clusters, N_list
**Note:** Since streamlines endpoints are ambiguous (e.g. the first point could be either the beginning or the end of the streamline), one must be careful when using this feature. """ import numpy as np from dipy.viz import window, colormap from dipy.segment.clustering import QuickBundles from dipy.segment.metric import VectorOfEndpointsFeature from dipy.segment.metric import CosineMetric # Get some streamlines. streamlines = get_streamlines() # Previously defined. feature = VectorOfEndpointsFeature() metric = CosineMetric(feature) qb = QuickBundles(threshold=0.1, metric=metric) clusters = qb.cluster(streamlines) # Color each streamline according to the cluster they belong to. colormap = colormap.create_colormap(np.arange(len(clusters))) colormap_full = np.ones((len(streamlines), 3)) for cluster, color in zip(clusters, colormap): colormap_full[cluster.indices] = color # Visualization scene = window.Scene() scene.clear() scene.SetBackground(0, 0, 0) scene.add(actor.streamtube(streamlines, colormap_full))
def __init__(self): # For simplicity, features will be the vector between endpoints of a streamline. super(CosineMetric, self).__init__(feature=VectorOfEndpointsFeature())