Exemplo n.º 1
0
def test_colormap():
    v = np.linspace(0., .5)
    map1 = fvtk.create_colormap(v, 'bone', auto=True)
    map2 = fvtk.create_colormap(v, 'bone', auto=False)
    npt.assert_(not np.allclose(map1, map2))

    npt.assert_raises(ValueError, fvtk.create_colormap, np.ones((2, 3)))
    npt.assert_raises(ValueError, fvtk.create_colormap, v, 'no such map')
Exemplo n.º 2
0
def test_colormap():
    v = np.linspace(0., .5)
    map1 = fvtk.create_colormap(v, 'bone', auto=True)
    map2 = fvtk.create_colormap(v, 'bone', auto=False)
    npt.assert_(not np.allclose(map1, map2))

    npt.assert_raises(ValueError, fvtk.create_colormap, np.ones((2, 3)))
    npt.assert_raises(ValueError, fvtk.create_colormap, v, 'no such map')
def show(imgtck, clusters, out_path):
    colormap = fvtk.create_colormap(np.ravel(clusters.centroids), name='jet')
    colormap_full = np.ones((len(imgtck.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(imgtck.streamlines, colormap_full))
    fvtk.record(ren, n_frames=1, out_path=out_path, size=(600, 600))
    fvtk.show(ren)
Exemplo n.º 4
0
def renderCentroids(streamlines, clusters):
    from dipy.viz import fvtk
    import numpy as np
    
    ren = fvtk.ren()
    ren.SetBackground(0, 0, 0)
    colormap = fvtk.create_colormap(np.arange(len(clusters)))

    colormap_full = np.ones((len(streamlines), 3))
    for cluster in clusters:
        colormap_full[cluster.indices] = np.random.rand(3)

    #fvtk.add(ren, fvtk.streamtube(streamlines, fvtk.colors.white, opacity=0.05))
    fvtk.add(ren, fvtk.line(clusters.centroids, linewidth=0.4, opacity=1))
    #fvtk.record(ren, n_frames=1, out_path='fornix_centroids.png', size=(600, 600))
    fvtk.show(ren)
    fvtk.clear(ren)
Exemplo n.º 5
0
def renderCentroids(clusters):
    from dipy.viz import fvtk
    import numpy as np

    ren = fvtk.ren()
    ren.SetBackground(0, 0, 0)
    colormap = fvtk.create_colormap(np.arange(len(clusters)))

    colormap_full = np.ones((len(streamlines), 3))
    for cluster in clusters:
        colormap_full[cluster.indices] = np.random.rand(3)

    #fvtk.add(ren, fvtk.streamtube(streamlines, fvtk.colors.white, opacity=0.05))
    fvtk.add(ren, fvtk.line(clusters.centroids, linewidth=0.4, opacity=1))
    #fvtk.record(ren, n_frames=1, out_path='fornix_centroids.png', size=(600, 600))
    fvtk.show(ren)
    fvtk.clear(ren)
Exemplo n.º 6
0
def renderBundles(streamlines, clusters):
    from dipy.viz import fvtk
    import numpy as np
    
    ren = fvtk.ren()
    ren.SetBackground(0, 0, 0)

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

    colormap_full = np.ones((len(streamlines), 3))
    for cluster in clusters:
        colormap_full[cluster.indices] = np.random.rand(3)

    fvtk.add(ren, fvtk.line(streamlines, colormap_full))
    #fvtk.record(ren, n_frames=1, out_path='fornix_clusters.png', size=(600, 600))
    fvtk.show(ren)
    fvtk.clear(ren)
Exemplo n.º 7
0
def renderBundles(clusters):
    from dipy.viz import fvtk
    import numpy as np

    ren = fvtk.ren()
    ren.SetBackground(0, 0, 0)

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

    colormap_full = np.ones((len(streamlines), 3))
    for cluster in clusters:
        colormap_full[cluster.indices] = np.random.rand(3)

    fvtk.add(ren, fvtk.line(streamlines, colormap_full))
    #fvtk.record(ren, n_frames=1, out_path='fornix_clusters.png', size=(600, 600))
    fvtk.show(ren)
    fvtk.clear(ren)
Exemplo n.º 8
0
def show_bundles(streamlines, clusters, show_b=True):
    # Color each streamline according to the cluster they belong to.
    colormap = fvtk.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

    ren = fvtk.ren()
    ren.SetBackground(1, 1, 1)
    fvtk.add(ren, fvtk.streamtube(streamlines, colormap_full))
    fvtk.record(ren,
                n_frames=1,
                out_path='fornix_clusters_cosine.png',
                size=(600, 600))

    if show_b:
        fvtk.show(ren)
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))
fvtk.record(ren, n_frames=1, out_path='fornix_clusters_arclength.png', size=(600, 600))

"""
.. figure:: fornix_clusters_arclength.png
   :align: center

   **Showing the different clusters obtained by using the arc length**.
Exemplo n.º 10
0
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))
fvtk.record(ren,
            n_frames=1,
            out_path='fornix_clusters_arclength.png',
            size=(600, 600))
"""
.. figure:: fornix_clusters_arclength.png
   :align: center
Exemplo n.º 11
0
"""

ren = fvtk.ren()
ren.SetBackground(1, 1, 1)
fvtk.add(ren, fvtk.streamtube(streamlines, fvtk.colors.white))
fvtk.record(ren, n_frames=1, out_path='fornix_initial.png', size=(600, 600))
"""
.. figure:: fornix_initial.png
   :align: center

   Initial Fornix dataset.

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

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

fvtk.clear(ren)
ren.SetBackground(1, 1, 1)
fvtk.add(ren, fvtk.streamtube(streamlines, fvtk.colors.white, opacity=0.05))
fvtk.add(ren, fvtk.streamtube(clusters.centroids, colormap, linewidth=0.4))
fvtk.record(ren, n_frames=1, out_path='fornix_centroids.png', size=(600, 600))
"""
.. figure:: fornix_centroids.png
   :align: center

   Showing the different QuickBundles centroids with random colors.

Show the labeled fornix (colors from centroids).
"""
Exemplo n.º 12
0
def interactive_viewer(streamlines, outlierness):
    import vtk
    from dipy.viz import fvtk, actor, window, widget
    from dipy.data.fetcher import read_viz_icons

    colormap_name = "jet"
    stream_actor = actor.line(streamlines,
                              colors=fvtk.create_colormap(outlierness,
                                                          name=colormap_name))
    stream_actor.SetPosition(-np.array(stream_actor.GetCenter()))

    global threshold
    threshold = 0.8

    streamlines_color = np.zeros(len(streamlines), dtype="float32")
    streamlines_color[outlierness < threshold] = 1
    streamlines_color[outlierness >= threshold] = 0

    lut = vtk.vtkLookupTable()
    lut.SetNumberOfTableValues(2)
    lut.Build()
    lut.SetTableValue(0, tuple(fvtk.colors.orange_red) + (1, ))
    lut.SetTableValue(1, tuple(fvtk.colors.green) + (1, ))
    lut.SetTableRange(0, 1)

    stream_split_actor = actor.line(streamlines,
                                    colors=streamlines_color,
                                    lookup_colormap=lut)
    stream_split_actor.SetPosition(-np.array(stream_split_actor.GetCenter()))
    hist_actor, hist_fig = create_hist_actor(outlierness,
                                             colormap_name=colormap_name)

    # Main renderder
    bg = (0, 0, 0)
    global screen_size
    screen_size = (0, 0)
    ren_main = window.Renderer()
    ren_main.background(bg)
    show_m = window.ShowManager(ren_main,
                                size=(1066, 600),
                                interactor_style="trackball")
    show_m.window.SetNumberOfLayers(2)
    ren_main.SetLayer(1)
    ren_main.InteractiveOff()

    # Outlierness renderer
    ren_outlierness = window.Renderer()
    show_m.window.AddRenderer(ren_outlierness)
    ren_outlierness.background(bg)
    ren_outlierness.SetViewport(0, 0.3, 0.5, 1)
    ren_outlierness.add(stream_actor)
    # ren_outlierness.reset_camera_tight()

    ren_split = window.Renderer()
    show_m.window.AddRenderer(ren_split)
    ren_split.background(bg)
    ren_split.SetViewport(0.5, 0.3, 1, 1)
    ren_split.add(stream_split_actor)
    ren_split.SetActiveCamera(ren_outlierness.GetActiveCamera())

    # Histogram renderer
    ren_hist = window.Renderer()
    show_m.window.AddRenderer(ren_hist)
    ren_hist.projection("parallel")
    ren_hist.background(bg)
    ren_hist.SetViewport(0, 0, 1, 0.3)
    ren_hist.add(hist_actor)
    ren_hist.SetInteractive(False)

    def apply_threshold(obj, evt):
        global threshold
        new_threshold = np.round(obj.GetSliderRepresentation().GetValue(),
                                 decimals=2)
        obj.GetSliderRepresentation().SetValue(new_threshold)
        if threshold != new_threshold:
            threshold = new_threshold

            streamlines_color = np.zeros(len(streamlines), dtype=np.float32)
            streamlines_color[outlierness < threshold] = 1
            streamlines_color[outlierness >= threshold] = 0

            colors = []
            for color, streamline in zip(streamlines_color, streamlines):
                colors += [color] * len(streamline)

            scalars = stream_split_actor.GetMapper().GetInput().GetPointData(
            ).GetScalars()
            for i, c in enumerate(colors):
                scalars.SetValue(i, c)

            scalars.Modified()

    threshold_slider_rep = vtk.vtkSliderRepresentation3D()
    threshold_slider_rep.SetMinimumValue(0.)
    threshold_slider_rep.SetMaximumValue(1.)
    threshold_slider_rep.SetValue(threshold)
    threshold_slider_rep.SetLabelFormat("%0.2lf")
    threshold_slider_rep.SetLabelHeight(0.02)
    threshold_slider_rep.GetPoint1Coordinate().SetCoordinateSystemToWorld()
    x1, x2, y1, y2, z1, z2 = hist_actor.GetBounds()
    threshold_slider_rep.GetPoint1Coordinate().SetValue(x1 * 1., y1 - 5, 0)
    threshold_slider_rep.GetPoint2Coordinate().SetCoordinateSystemToWorld()
    threshold_slider_rep.GetPoint2Coordinate().SetValue(x2 * 1., y1 - 5, 0)
    threshold_slider_rep.SetEndCapLength(0.)
    threshold_slider_rep.SetEndCapWidth(0.)

    threshold_slider = vtk.vtkSliderWidget()
    threshold_slider.SetInteractor(show_m.iren)
    threshold_slider.SetRepresentation(threshold_slider_rep)
    threshold_slider.SetCurrentRenderer(ren_hist)
    threshold_slider.SetAnimationModeToJump()
    threshold_slider.EnabledOn()

    threshold_slider.AddObserver("InteractionEvent", apply_threshold)

    #ren_main
    def _place_buttons():
        sz = 30.0
        width, _ = ren_main.GetSize()

        # bds = np.zeros(6)
        # bds[0] = width - sz - 5
        # bds[1] = bds[0] + sz
        # bds[2] = 5
        # bds[3] = bds[2] + sz
        # bds[4] = bds[5] = 0.0
        # save_button.GetRepresentation().PlaceWidget(bds)

    def _window_callback(obj, event):
        # ren_hist.reset_camera_tight(margin_factor=1.2)
        _place_buttons()

    show_m.add_window_callback(_window_callback)
    show_m.initialize()
    show_m.render()
    show_m.start()

    inliers = [
        s for s, keep in zip(streamlines, outlierness < threshold) if keep
    ]
    outliers = [
        s for s, keep in zip(streamlines, outlierness >= threshold) if keep
    ]
    return inliers, outliers
import numpy as np
from dipy.viz import fvtk
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 = fvtk.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 = fvtk.ren()
fvtk.clear(ren)
ren.SetBackground(0, 0, 0)
fvtk.add(ren, fvtk.streamtube(streamlines, colormap_full))
fvtk.record(ren, n_frames=1, out_path='cosine_metric.png', size=(600, 600))

"""
.. figure:: cosine_metric.png
   :align: center
Exemplo n.º 14
0
def interactive_viewer(streamlines, outlierness):
    import vtk
    from dipy.viz import fvtk, actor, window, widget
    from dipy.data.fetcher import read_viz_icons

    colormap_name = "jet"
    stream_actor = actor.line(streamlines, colors=fvtk.create_colormap(outlierness, name=colormap_name))
    stream_actor.SetPosition(-np.array(stream_actor.GetCenter()))

    global threshold
    threshold = 0.8

    streamlines_color = np.zeros(len(streamlines), dtype="float32")
    streamlines_color[outlierness < threshold] = 1
    streamlines_color[outlierness >= threshold] = 0

    lut = vtk.vtkLookupTable()
    lut.SetNumberOfTableValues(2)
    lut.Build()
    lut.SetTableValue(0, tuple(fvtk.colors.orange_red) + (1,))
    lut.SetTableValue(1, tuple(fvtk.colors.green) + (1,))
    lut.SetTableRange(0, 1)

    stream_split_actor = actor.line(streamlines, colors=streamlines_color, lookup_colormap=lut)
    stream_split_actor.SetPosition(-np.array(stream_split_actor.GetCenter()))
    hist_actor, hist_fig = create_hist_actor(outlierness, colormap_name=colormap_name)

    # Main renderder
    bg = (0, 0, 0)
    global screen_size
    screen_size = (0, 0)
    ren_main = window.Renderer()
    ren_main.background(bg)
    show_m = window.ShowManager(ren_main, size=(1066, 600), interactor_style="trackball")
    show_m.window.SetNumberOfLayers(2)
    ren_main.SetLayer(1)
    ren_main.InteractiveOff()

    # Outlierness renderer
    ren_outlierness = window.Renderer()
    show_m.window.AddRenderer(ren_outlierness)
    ren_outlierness.background(bg)
    ren_outlierness.SetViewport(0, 0.3, 0.5, 1)
    ren_outlierness.add(stream_actor)
    ren_outlierness.reset_camera_tight()

    ren_split = window.Renderer()
    show_m.window.AddRenderer(ren_split)
    ren_split.background(bg)
    ren_split.SetViewport(0.5, 0.3, 1, 1)
    ren_split.add(stream_split_actor)
    ren_split.SetActiveCamera(ren_outlierness.GetActiveCamera())

    # Histogram renderer
    ren_hist = window.Renderer()
    show_m.window.AddRenderer(ren_hist)
    ren_hist.projection("parallel")
    ren_hist.background(bg)
    ren_hist.SetViewport(0, 0, 1, 0.3)
    ren_hist.add(hist_actor)
    ren_hist.SetInteractive(False)

    def apply_threshold(obj, evt):
        global threshold
        new_threshold = np.round(obj.GetSliderRepresentation().GetValue(), decimals=2)
        obj.GetSliderRepresentation().SetValue(new_threshold)
        if threshold != new_threshold:
            threshold = new_threshold

            streamlines_color = np.zeros(len(streamlines), dtype=np.float32)
            streamlines_color[outlierness < threshold] = 1
            streamlines_color[outlierness >= threshold] = 0

            colors = []
            for color, streamline in zip(streamlines_color, streamlines):
                colors += [color] * len(streamline)

            scalars = stream_split_actor.GetMapper().GetInput().GetPointData().GetScalars()
            for i, c in enumerate(colors):
                scalars.SetValue(i, c)

            scalars.Modified()

    threshold_slider_rep = vtk.vtkSliderRepresentation3D()
    threshold_slider_rep.SetMinimumValue(0.)
    threshold_slider_rep.SetMaximumValue(1.)
    threshold_slider_rep.SetValue(threshold)
    threshold_slider_rep.SetLabelFormat("%0.2lf")
    threshold_slider_rep.SetLabelHeight(0.02)
    threshold_slider_rep.GetPoint1Coordinate().SetCoordinateSystemToWorld()
    x1, x2, y1, y2, z1, z2 = hist_actor.GetBounds()
    threshold_slider_rep.GetPoint1Coordinate().SetValue(x1*1., y1-5, 0)
    threshold_slider_rep.GetPoint2Coordinate().SetCoordinateSystemToWorld()
    threshold_slider_rep.GetPoint2Coordinate().SetValue(x2*1., y1-5, 0)
    threshold_slider_rep.SetEndCapLength(0.)
    threshold_slider_rep.SetEndCapWidth(0.)

    threshold_slider = vtk.vtkSliderWidget()
    threshold_slider.SetInteractor(show_m.iren)
    threshold_slider.SetRepresentation(threshold_slider_rep)
    threshold_slider.SetCurrentRenderer(ren_hist)
    threshold_slider.SetAnimationModeToJump()
    threshold_slider.EnabledOn()

    threshold_slider.AddObserver("InteractionEvent", apply_threshold)

    #ren_main
    def _place_buttons():
        sz = 30.0
        width, _ = ren_main.GetSize()

        # bds = np.zeros(6)
        # bds[0] = width - sz - 5
        # bds[1] = bds[0] + sz
        # bds[2] = 5
        # bds[3] = bds[2] + sz
        # bds[4] = bds[5] = 0.0
        # save_button.GetRepresentation().PlaceWidget(bds)

    def _window_callback(obj, event):
        ren_hist.reset_camera_tight(margin_factor=1.2)
        _place_buttons()

    show_m.add_window_callback(_window_callback)
    show_m.initialize()
    show_m.render()
    show_m.start()

    inliers = [s for s, keep in zip(streamlines, outlierness < threshold) if keep]
    outliers = [s for s, keep in zip(streamlines, outlierness >= threshold) if keep]
    return inliers, outliers
Exemplo n.º 15
0
# print "Centroid of the last cluster:\n", clusters[-1].centroid

# show rhe initial dataset
ren = fvtk.ren()
ren.SetBackground(1, 1, 1)
fvtk.add(ren, fvtk.streamtube(streamlines, fvtk.colors.white))
fvtk.record(
    ren,
    n_frames=1,
    out_path='/home/brain/workingdir/data/dwi/hcp/preprocessed/'
    'response_dhollander/100206/result/CC_fib_remove_non_cc_z-10_x-min_10_jet.png',
    size=(600, 600))
# fvtk.show(ren)

# show the centroids of the CC
colormap = fvtk.create_colormap(np.arange(len(clusters)), name='jet')
fvtk.clear(ren)
ren.SetBackground(1, 1, 1)
fvtk.add(ren, fvtk.streamtube(streamlines, fvtk.colors.white, opacity=0.05))
fvtk.add(ren, fvtk.streamtube(clusters.centroids, colormap, linewidth=0.4))
fvtk.record(
    ren,
    n_frames=1,
    out_path='/home/brain/workingdir/data/dwi/hcp/preprocessed/'
    'response_dhollander/100206/result/CC_fib1_remove_non_cc_z-10_x-min_10_jet.png',
    size=(600, 600))
# fvtk.show(ren)

# show the label CC (colors form centroids)
colormap_full = np.ones((len(streamlines)), np.float64(3))
for clusters, color in zip(clusters, colormap):