**Note:** Inputs must be sequences of same length. """ from dipy.segment.clustering import QuickBundles from dipy.segment.metric import SumPointwiseEuclideanMetric # Get some streamlines. streamlines = get_streamlines() # Previously defined. # Make sure our streamlines have the same number of points. from dipy.tracking.streamline import set_number_of_points nb_points = 12 streamlines = set_number_of_points(streamlines, nb_points=nb_points) # Create the instance of `SumPointwiseEuclideanMetric` to use. metric = SumPointwiseEuclideanMetric() qb = QuickBundles(threshold=10.*nb_points, metric=metric) clusters = qb.cluster(streamlines) print("Nb. clusters:", len(clusters)) print("Cluster sizes:", map(len, clusters)) """ :: Nb. clusters: 4 Cluster sizes: [64, 191, 44, 1]
from nibabel import trackvis as tv from dipy.data import get_data from dipy.viz import fvtk fname = get_data('fornix') streams, hdr = tv.read(fname) streamlines = [i[0] for i in streams] """ Perform QuickBundles clustering using the metric `SumPointwiseEuclideanMetric` and our `ArcLengthFeature`. """ from dipy.segment.clustering import QuickBundles from dipy.segment.metric import SumPointwiseEuclideanMetric metric = SumPointwiseEuclideanMetric(feature=ArcLengthFeature()) qb = QuickBundles(threshold=2., metric=metric) clusters = qb.cluster(streamlines) """ We will now visualize the clustering result. """ # Color each streamline according to the cluster they belong to. colormap = fvtk.create_colormap(np.ravel(clusters.centroids)) colormap_full = np.ones((len(streamlines), 3)) for cluster, color in zip(clusters, colormap): colormap_full[cluster.indices] = color ren = fvtk.ren() ren.SetBackground(1, 1, 1) fvtk.add(ren, fvtk.streamtube(streamlines, colormap_full))