示例#1
0
def test_text_widget():

    interactive = False

    renderer = window.Renderer()
    axes = fvtk.axes()
    window.add(renderer, axes)
    renderer.ResetCamera()

    show_manager = window.ShowManager(renderer, size=(900, 900))

    if interactive:
        show_manager.initialize()
        show_manager.render()

    fetch_viz_icons()
    button_png = read_viz_icons(fname='home3.png')

    def button_callback(obj, event):
        print('Button Pressed')

    button = widget.button(show_manager.iren, show_manager.ren,
                           button_callback, button_png, (.8, 1.2), (100, 100))

    global rulez
    rulez = True

    def text_callback(obj, event):

        global rulez
        print('Text selected')
        if rulez:
            obj.GetTextActor().SetInput("Diffusion Imaging Rulez!!")
            rulez = False
        else:
            obj.GetTextActor().SetInput("Diffusion Imaging in Python")
            rulez = True
        show_manager.render()

    text = widget.text(show_manager.iren,
                       show_manager.ren,
                       text_callback,
                       message="Diffusion Imaging in Python",
                       left_down_pos=(0., 0.),
                       right_top_pos=(0.4, 0.05),
                       opacity=1.,
                       border=False)

    if not interactive:
        button.Off()
        text.Off()
        pass

    if interactive:
        show_manager.render()
        show_manager.start()

    arr = window.snapshot(renderer, size=(900, 900))
    report = window.analyze_snapshot(arr)
    npt.assert_equal(report.objects, 3)
示例#2
0
def show_signal(r,S,gradients,offset=np.zeros(3)):
    PS = np.dot(np.diag(S),gradients)
#for (i,s) in enumerate(S):
    fvtk.add(r,fvtk.point(offset+PS/np.max(PS),fvtk.cyan,point_radius=0.05,theta=8,phi=8))
    fvtk.add(r,fvtk.point([offset],fvtk.green,point_radius=0.1,theta=8,phi=8))    
    fvtk.add(r,fvtk.axes((1,1,1)))
    lines = fvtk.line([1.5*np.row_stack((needles3d[0],-needles3d[0])), \
                   1.5*np.row_stack((needles3d[1],-needles3d[1]))], \
                   colors=np.row_stack((fvtk.golden,fvtk.aquamarine)),linewidth=10)
    fvtk.add(r,lines)
示例#3
0
def playing_with_diffusivities_and_simulations():

    data,bvals,bvecs=get_data(name='101_32')
    #S=data[:,:,42]
    #drange=np.linspace(0,40,5)
    
    S1,sticks1=simulations_dipy(bvals,bvecs,d=0.0015,S0=200,angles=[(0,0),(90,0)],fractions=[50,50],snr=None)
    S2,sticks2=simulations_dipy(bvals,bvecs,d=0.0015,S0=200,angles=[(0,0),(90,0),(90,90)],fractions=[33,33,33],snr=None)
    
    scale=10**4
    
    ob=-1/bvals[1:]       
    D1=ob*(np.log(S1[1:])-np.log(S1[0]))*scale
    D2=ob*(np.log(S2[1:])-np.log(S2[0]))*scale
    
    Bvecs=np.concatenate([bvecs[1:],-bvecs[1:]])
    SS1=np.concatenate([S1[1:],S1[1:]])
    SS2=np.concatenate([S2[1:],S2[1:]])
    
    DD1=np.concatenate([D1,D1])
    DD2=np.concatenate([D2,D2])
    
    X1=np.dot(np.diag(DD1),Bvecs)
    X2=np.dot(np.diag(DD2),Bvecs)
    
    U,s,V=np.linalg.svd(np.dot(X1.T,X2),full_matrices=True)
    R=np.dot(U,V.T)
    
    print np.round(R,2)
    print np.sum(np.dot(X1,R)-X2,axis=0)
    
    r=fvtk.ren()
    fvtk.add(r,fvtk.point(X1,fvtk.red,1,.5,16,16))
    fvtk.add(r,fvtk.point(X2,fvtk.green,1,.5,16,16))
    fvtk.add(r,fvtk.axes((10,10,10)))
    fvtk.show(r)
    
    U1,s1,V1=np.linalg.svd(np.dot(X1.T,X1),full_matrices=True)
    U2,s2,V2=np.linalg.svd(np.dot(X2.T,X2),full_matrices=True)
    
    u1=U1[:,2]
    u2=U2[:,2]
    
    R2=vec2vecrotmat(u1,u2)
    
    print np.round(R2,2)
    print np.rad2deg(np.arccos(np.dot(u1,u2)))
    
    uu1=np.zeros((2,3))
    uu1[1]=u1
    uu2=np.zeros((2,3))
    uu2[1]=u2
    
    kX1=online_kurtosis(X1)
    kX2=online_kurtosis(X2)
    
    print 's1',s1
    print 's2',s2
    print 'kX1',kX1
    print 'kX2',kX2
    print 'average diffusivity 1',np.mean(np.linalg.norm(X1))
    print 'average diffusivity 2',np.mean(np.linalg.norm(X2))
示例#4
0
def horizon(tractograms, data, affine, cluster=False, cluster_thr=15.,
            random_colors=False,
            length_lt=0, length_gt=np.inf, clusters_lt=0, clusters_gt=np.inf):

    rng = np.random.RandomState(42)
    slicer_opacity = .8

    ren = window.Renderer()
    global centroid_actors
    centroid_actors = []
    for streamlines in tractograms:

        print(' Number of streamlines loaded {} \n'.format(len(streamlines)))

        if not random_colors:
            ren.add(actor.line(streamlines, opacity=1., lod_points=10 ** 5))
        else:
            colors = rng.rand(3)
            ren.add(actor.line(streamlines, colors, opacity=1., lod_points=10 ** 5))

    class SimpleTrackBallNoBB(window.vtk.vtkInteractorStyleTrackballCamera):
        def HighlightProp(self, p):
            pass

    style = SimpleTrackBallNoBB()
    # very hackish way
    style.SetPickColor(0, 0, 0)
    # style.HighlightProp(None)
    show_m = window.ShowManager(ren, size=(1200, 900), interactor_style=style)
    show_m.initialize()

    if data is not None:
        #from dipy.core.geometry import rodrigues_axis_rotation
        #affine[:3, :3] = np.dot(affine[:3, :3], rodrigues_axis_rotation((0, 0, 1), 45))

        image_actor = actor.slicer(data, affine)
        image_actor.opacity(slicer_opacity)
        image_actor.SetInterpolate(False)
        ren.add(image_actor)

        ren.add(fvtk.axes((10, 10, 10)))

        last_value = [10]
        def change_slice(obj, event):
            new_value = int(np.round(obj.get_value()))
            if new_value == image_actor.shape[1] - 1 or new_value == 0:
                new_value = last_value[0] + np.sign(new_value - last_value[0])

            image_actor.display(None, new_value, None)
            obj.set_value(new_value)
            last_value[0] = new_value

        slider = widget.slider(show_m.iren, show_m.ren,
                               callback=change_slice,
                               min_value=0,
                               max_value=image_actor.shape[1] - 1,
                               value=image_actor.shape[1] / 2,
                               label="Move slice",
                               right_normalized_pos=(.98, 0.6),
                               size=(120, 0), label_format="%0.lf",
                               color=(1., 1., 1.),
                               selected_color=(0.86, 0.33, 1.))

        slider.SetAnimationModeToJump()

    global size
    size = ren.GetSize()
    # ren.background((1, 0.5, 0))
    ren.background((0, 0, 0))
    global picked_actors
    picked_actors = {}

    def pick_callback(obj, event):
        global centroid_actors
        global picked_actors

        prop = obj.GetProp3D()

        ac = np.array(centroid_actors)
        index = np.where(ac == prop)[0]

        if len(index) > 0:
            try:
                bundle = picked_actors[prop]
                ren.rm(bundle)
                del picked_actors[prop]
            except:
                bundle = actor.line(clusters[visible_cluster_id[index]],
                                    lod=False)
                picked_actors[prop] = bundle
                ren.add(bundle)

        if prop in picked_actors.values():
            ren.rm(prop)

    def win_callback(obj, event):
        global size
        if size != obj.GetSize():

            if data is not None:
                slider.place(ren)
            size = obj.GetSize()

    global centroid_visibility
    centroid_visibility = True

    def key_press(obj, event):
        global centroid_visibility
        key = obj.GetKeySym()
        if key == 'h' or key == 'H':
            if cluster:
                if centroid_visibility is True:
                    for ca in centroid_actors:
                        ca.VisibilityOff()
                    centroid_visibility = False
                else:
                    for ca in centroid_actors:
                        ca.VisibilityOn()
                    centroid_visibility = True
                show_m.render()

    show_m.initialize()
    show_m.iren.AddObserver('KeyPressEvent', key_press)
    show_m.add_window_callback(win_callback)
    #show_m.add_picker_callback(pick_callback)
    show_m.render()
    show_m.start()
示例#5
0
def test_text_widget():

    interactive = False

    renderer = window.Renderer()
    axes = fvtk.axes()
    window.add(renderer, axes)
    renderer.ResetCamera()

    show_manager = window.ShowManager(renderer, size=(900, 900))

    if interactive:
        show_manager.initialize()
        show_manager.render()

    fetch_viz_icons()
    button_png = read_viz_icons(fname='home3.png')

    def button_callback(obj, event):
        print('Button Pressed')

    button = widget.button(show_manager.iren,
                           show_manager.ren,
                           button_callback,
                           button_png, (.8, 1.2), (100, 100))

    global rulez
    rulez = True

    def text_callback(obj, event):

        global rulez
        print('Text selected')
        if rulez:
            obj.GetTextActor().SetInput("Diffusion Imaging Rulez!!")
            rulez = False
        else:
            obj.GetTextActor().SetInput("Diffusion Imaging in Python")
            rulez = True
        show_manager.render()

    text = widget.text(show_manager.iren,
                       show_manager.ren,
                       text_callback,
                       message="Diffusion Imaging in Python",
                       left_down_pos=(0., 0.),
                       right_top_pos=(0.4, 0.05),
                       opacity=1.,
                       border=False)

    if not interactive:
        button.Off()
        text.Off()
        pass

    if interactive:
        show_manager.render()
        show_manager.start()

    arr = window.snapshot(renderer, size=(900, 900))
    report = window.analyze_snapshot(arr)
    npt.assert_equal(report.objects, 3)
示例#6
0
def show_peak_directions(peaks, scale=0.3, x=5, y=None, z=None, show_axes=False):
    """ visualize peak directions

    Parameters
    ----------
    peaks : ndarray,
            (X, Y, Z, 15)
    scale : float
            voxel scaling (0 =< `scale` =< 1)
    x : int,
        x slice (0 <= x <= X-1)
    y : int,
        y slice (0 <= y <= Y-1)
    z : int,
        z slice (0 <= z <= Z-1)

    Notes
    -----
    If x, y, z are Nones then the full volume is shown.

    """
    # if x is None and y is None and z is None:
    #    raise ValueError('A slice should be provided')

    pshape = peaks.shape[:-1]
    if x is not None:
        x = np.clip(x, 0, pshape[0] - 1)
    if y is not None:
        y = np.clip(y, 0, pshape[0] - 1)
    if z is not None:
        z = np.clip(z, 0, pshape[0] - 1)

    print x, y, z

    peaks = np.asarray(peaks)
    if peaks.ndim > 4 or peaks.ndim < 4:
        raise ValueError("peaks has wrong shape")

    r = fvtk.ren()

    for index in ndindex(peaks.shape[:-1]):

        peak = peaks[index]
        directions = peak.reshape(peak.shape[0] / 3, 3)
        
        pos = np.array(index)
        if x is not None:
            pos[0] = x
        if y is not None:
            pos[1] = y
        if z is not None:
            pos[2] = z
            
        for i in xrange(directions.shape[0]):

            if np.linalg.norm(directions[i]) != 0:

                if (x, y, z) == (None, None, None):
                    line_actor = fvtk.line(index +
                                           scale * np.vstack((-directions[i], directions[i])),
                                           np.abs(directions[i] / np.linalg.norm(directions[i])))
                    fvtk.add(r, line_actor)

                if tuple(pos) == index:
                    line_actor = fvtk.line(pos +
                                           scale * np.vstack((-directions[i], directions[i])),
                                           np.abs(directions[i] / np.linalg.norm(directions[i])))
                    fvtk.add(r, line_actor)

    if show_axes:
        fvtk.add(r, fvtk.axes((2, 2, 2)))
    fvtk.show(r)
示例#7
0
文件: viz.py 项目: szho42/learn2track
def horizon(tractograms, data, affine):

    rng = np.random.RandomState(42)
    slicer_opacity = .8

    ren = window.Renderer()
    global centroid_actors
    centroid_actors = []
    for streamlines in tractograms:

        print(' Number of streamlines loaded {} \n'.format(len(streamlines)))
        colors = rng.rand(3)
        ren.add(actor.line(streamlines, colors, opacity=1., lod_points=10**5))

    class SimpleTrackBallNoBB(window.vtk.vtkInteractorStyleTrackballCamera):
        def HighlightProp(self, p):
            pass

    style = SimpleTrackBallNoBB()
    # very hackish way
    style.SetPickColor(0, 0, 0)
    # style.HighlightProp(None)
    show_m = window.ShowManager(ren, size=(1200, 900), interactor_style=style)
    show_m.initialize()

    if data is not None:
        #from dipy.core.geometry import rodrigues_axis_rotation
        #affine[:3, :3] = np.dot(affine[:3, :3], rodrigues_axis_rotation((0, 0, 1), 45))

        image_actor = actor.slicer(data, affine)
        image_actor.opacity(slicer_opacity)
        image_actor.SetInterpolate(False)
        ren.add(image_actor)

        ren.add(fvtk.axes((10, 10, 10)))

        last_value = [10]

        def change_slice(obj, event):
            new_value = int(np.round(obj.get_value()))
            if new_value == image_actor.shape[1] - 1 or new_value == 0:
                new_value = last_value[0] + np.sign(new_value - last_value[0])

            image_actor.display(None, new_value, None)
            obj.set_value(new_value)
            last_value[0] = new_value

        slider = widget.slider(show_m.iren,
                               show_m.ren,
                               callback=change_slice,
                               min_value=0,
                               max_value=image_actor.shape[1] - 1,
                               value=image_actor.shape[1] / 2,
                               label="Move slice",
                               right_normalized_pos=(.98, 0.6),
                               size=(120, 0),
                               label_format="%0.lf",
                               color=(1., 1., 1.),
                               selected_color=(0.86, 0.33, 1.))

        slider.SetAnimationModeToJump()

    global size
    size = ren.GetSize()
    # ren.background((1, 0.5, 0))
    ren.background((0, 0, 0))
    global picked_actors
    picked_actors = {}

    def win_callback(obj, event):
        global size
        if size != obj.GetSize():

            if data is not None:
                slider.place(ren)
            size = obj.GetSize()

    global centroid_visibility
    centroid_visibility = True

    show_m.initialize()
    show_m.add_window_callback(win_callback)
    show_m.render()
    show_m.start()
示例#8
0
print invariant_angles(S)

Sinit = S.copy()

Rot = rotation_matrix(np.array([0, 1, 0]), 30)

S = np.dot(Rot, S.T).T + np.array([1, 0, 0])
print "Dummy translated"
print invariant_angles(S)

from dipy.viz import fvtk

r = fvtk.ren()
fvtk.add(r, fvtk.line(Sinit, fvtk.red))
fvtk.add(r, fvtk.line(S, fvtk.green))
fvtk.add(r, fvtk.axes())
fvtk.show(r)

# helix
theta = 2 * np.pi * np.linspace(0, 2, 100)
x = np.cos(theta)
y = np.sin(theta)
z = theta / (2 * np.pi)
Shel = np.vstack((x, y, z)).T

from dipy.tracking.metrics import downsample

Shel = downsample(Shel, 12)

print "Helix standard"
print invariant_angles(Shel)