def viewclusters(clusters,streamlines, outpath=None, interactive=False):
    #Linked to viewing clusters. If outpath given, will save info to right location, if interactive, will show window
    colormap = actor.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

    scene = window.Scene()
    scene.SetBackground(1, 1, 1)
    scene.add(actor.streamtube(streamlines, colormap_full))
    window.record(scene, out_path=outpath, size=(600, 600))

    # Enables/disables interactive visualization
    if interactive:
        window.show(scene)
# pts (number of points in each streamline) which will be used for downsampling before clustering
# Default values : dist_thr = 4 & pts = 12
qb = QuickBundles(streamlines, dist_thr=20, pts=20)
clusters = qb.clusters()
print('Number of clusters %i' % qb.total_clusters)
print('Cluster size', qb.clusters_sizes())

# Display streamlines
ren = window.Renderer()
ren.add(actor.streamtube(streamlines, window.colors.white))
window.show(ren)
window.record(ren, out_path=filename + '_stream_lines_eu.png', size=(600, 600))

# Display centroids
window.clear(ren)
colormap = actor.create_colormap(np.arange(qb.total_clusters))
ren.add(actor.streamtube(streamlines, window.colors.white, opacity=0.1))
ren.add(actor.streamtube(qb.centroids, colormap, linewidth=0.5))
window.show(ren)
window.record(ren, out_path=filename + '_centroids_eu.png', size=(600, 600))

# Display tracks
window.clear(ren)
colormap_full = np.ones((len(streamlines), 3))
for cluster, color in zip(clusters.items(), colormap):
    colormap_full[cluster[1]['indices']] = color
ren.add(actor.streamtube(streamlines, colormap_full))
window.show(ren)
window.record(ren,
              out_path=filename + '_stream_line_cluster_eu.png',
              size=(600, 600))
Beispiel #3
0
from dipy.segment.metric import VectorOfEndpointsFeature
from dipy.segment.metric import CosineMetric

# Enables/disables interactive visualization
interactive = False

# 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 = actor.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))
window.record(scene, out_path='cosine_metric.png', size=(600, 600))
if interactive:
    window.show(scene)

"""
.. figure:: cosine_metric.png
ren.SetBackground(1, 1, 1)
ren.add(actor.streamtube(streamlines, window.colors.white))
window.record(ren, out_path='fornix_initial.png', size=(600, 600))
if interactive:
    window.show(ren)

"""
.. figure:: fornix_initial.png
   :align: center

   Initial Fornix dataset.

Show the centroids of the fornix after clustering (with random colors):
"""

colormap = actor.create_colormap(np.arange(len(clusters)))

window.clear(ren)
ren.SetBackground(1, 1, 1)
ren.add(actor.streamtube(streamlines, window.colors.white, opacity=0.05))
ren.add(actor.streamtube(clusters.centroids, colormap, linewidth=0.4))
window.record(ren, out_path='fornix_centroids.png', size=(600, 600))
if interactive:
    window.show(ren)

"""
.. figure:: fornix_centroids.png
   :align: center

   Showing the different QuickBundles centroids with random colors.
Beispiel #5
0
interactive = False

# Get some streamlines.
streamlines = get_streamlines()  # Previously defined.

feature = CenterOfMassFeature()
metric = EuclideanMetric(feature)

qb = QuickBundles(threshold=5., metric=metric)
clusters = qb.cluster(streamlines)

# Extract feature of every streamline.
centers = np.asarray(list(map(feature.extract, streamlines)))

# Color each center of mass according to the cluster they belong to.
colormap = actor.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
ren = window.Renderer()
window.clear(ren)
ren.SetBackground(0, 0, 0)
ren.add(actor.streamtube(streamlines, window.colors.white, opacity=0.05))
ren.add(actor.point(centers[:, 0, :], colormap_full, point_radius=0.2))
window.record(ren,
              n_frames=1,
              out_path='center_of_mass_feature.png',
              size=(600, 600))
if interactive:
``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 = actor.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 = window.Renderer()
ren.SetBackground(1, 1, 1)
ren.add(actor.streamtube(streamlines, colormap_full))
window.record(ren, out_path='fornix_clusters_arclength.png', size=(600, 600))

# Enables/disables interactive visualization
interactive = False
if interactive:
    window.show(ren)

"""
Beispiel #7
0
def launch_quickbundles(streamlines,
                        outpath,
                        ROIname="all",
                        threshold=10.,
                        labelmask=None,
                        affine=np.eye(4),
                        interactive=False):

    #qb = QuickBundles(threshold=10.)
    qb = QuickBundles(threshold=threshold)
    clusters = qb.cluster(streamlines)

    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)

    # Cluster sizes: [64, 191, 47, 1]

    # Small clusters: array([False, False, False, True], dtype=bool)

    scene = window.Scene()
    scene.SetBackground(1, 1, 1)
    scene.add(actor.streamtube(streamlines, window.colors.misty_rose))
    if labelmask is not None:
        shape = labelmask.shape
        image_actor_z = actor.slicer(labelmask, affine)
        slicer_opacity = 0.6
        image_actor_z.opacity(slicer_opacity)

        image_actor_x = image_actor_z.copy()
        x_midpoint = int(np.round(shape[0] / 2))
        image_actor_x.display_extent(x_midpoint, x_midpoint, 0, shape[1] - 1,
                                     0, shape[2] - 1)

        image_actor_y = image_actor_z.copy()
        y_midpoint = int(np.round(shape[1] / 2))
        image_actor_y.display_extent(0, shape[0] - 1, y_midpoint, y_midpoint,
                                     0, shape[2] - 1)
        scene.add(image_actor_z)
        scene.add(image_actor_x)
        scene.add(image_actor_y)

    window.record(scene,
                  out_path=outpath + ROIname + '_initial.png',
                  size=(600, 600))
    if interactive:
        window.show(scene)

    colormap = actor.create_colormap(np.arange(len(clusters)))

    scene.clear()
    scene.SetBackground(1, 1, 1)
    scene.add(actor.streamtube(streamlines, window.colors.white, opacity=0.05))
    scene.add(actor.streamtube(clusters.centroids, colormap, linewidth=0.4))
    if labelmask is not None:
        image_actor_z = actor.slicer(labelmask, affine)
    window.record(scene,
                  out_path=outpath + ROIname + '_centroids.png',
                  size=(600, 600))
    if interactive:
        window.show(scene)

    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, colormap_full))
    window.record(scene,
                  out_path=outpath + ROIname + '_clusters.png',
                  size=(600, 600))
    if interactive:
        window.show(scene)