1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
|
"""
=========================================
Tractography Clustering with QuickBundles
=========================================
This example explains how we can use QuickBundles
:footcite:p:`Garyfallidis2012a` to simplify/cluster streamlines.
First import the necessary modules.
"""
import numpy as np
from dipy.data import get_fnames
from dipy.io.pickles import save_pickle
from dipy.io.streamline import load_tractogram
from dipy.segment.clustering import QuickBundles
from dipy.viz import actor, colormap, window
###############################################################################
# For educational purposes we will try to cluster a small streamline bundle
# known from neuroanatomy as the fornix.
fname = get_fnames(name="fornix")
###############################################################################
# Load fornix streamlines.
fornix = load_tractogram(fname, "same", bbox_valid_check=False)
streamlines = fornix.streamlines
###############################################################################
# Perform QuickBundles clustering using the MDF metric and a 10mm distance
# threshold. Keep in mind that since the MDF metric requires streamlines to
# have the same number of points, the clustering algorithm will internally use
# a representation of streamlines that have been automatically
# downsampled/upsampled so they have only 12 points (To set manually the
# number of points, see :ref:`clustering-examples-ResampleFeature`).
qb = QuickBundles(threshold=10.0)
clusters = qb.cluster(streamlines)
###############################################################################
# `clusters` is a `ClusterMap` object which contains attributes that
# provide information about the clustering result.
print("Nb. clusters:", len(clusters))
print("Cluster sizes:", map(len, clusters))
print("Small clusters:", clusters < 10)
print("Streamlines indices of the first cluster:\n", clusters[0].indices)
print("Centroid of the last cluster:\n", clusters[-1].centroid)
###############################################################################
# `clusters` also has attributes such as `centroids` (cluster representatives),
# and methods like `add`, `remove`, and `clear` to modify the clustering
# result.
#
# Let's first show the initial dataset.
# Enables/disables interactive visualization
interactive = False
scene = window.Scene()
scene.SetBackground(1, 1, 1)
scene.add(actor.streamtube(streamlines, colors=window.colors.white))
window.record(scene=scene, out_path="fornix_initial.png", size=(600, 600))
if interactive:
window.show(scene)
###############################################################################
# .. rst-class:: centered small fst-italic fw-semibold
#
# Initial Fornix dataset.
#
#
# Show the centroids of the fornix after clustering (with random colors):
colormap = colormap.create_colormap(np.arange(len(clusters)))
scene.clear()
scene.SetBackground(1, 1, 1)
scene.add(actor.streamtube(streamlines, colors=window.colors.white, opacity=0.05))
scene.add(actor.streamtube(clusters.centroids, colors=colormap, linewidth=0.4))
window.record(scene=scene, out_path="fornix_centroids.png", size=(600, 600))
if interactive:
window.show(scene)
###############################################################################
# .. rst-class:: centered small fst-italic fw-semibold
#
# Showing the different QuickBundles centroids with random colors.
#
#
# Show the labeled fornix (colors from centroids).
colormap_full = np.ones((len(streamlines), 3))
for cluster, color in zip(clusters, colormap):
colormap_full[cluster.indices] = color
scene.clear()
scene.SetBackground(1, 1, 1)
scene.add(actor.streamtube(streamlines, colors=colormap_full))
window.record(scene=scene, out_path="fornix_clusters.png", size=(600, 600))
if interactive:
window.show(scene)
###############################################################################
# .. rst-class:: centered small fst-italic fw-semibold
#
# Showing the different clusters.
#
#
# It is also possible to save the complete `ClusterMap` object with pickling.
save_pickle("QB.pkl", clusters)
###############################################################################
# Finally, here is a video of QuickBundles applied on a larger dataset.
#
# .. raw:: html
#
# <iframe width="420" height="315" src="https://www.youtube.com/embed/kstL7KKqu94" frameborder="0" allowfullscreen></iframe> # noqa: E501
#
#
# References
# ----------
#
# .. footbibliography::
#
|