In [ ]:
import ipyvolume.pylab as p3
import ipyvolume as ipv
import IPython.display
import ipyvolume
import numpy as np
import os

In [ ]:
import ipywidgets

In [ ]:
%%javascript
require.onResourceLoad = function(context, map)
{
    console.log("loading " +map.name)
    //require.undef(map.name);
};
require.undef("nbextensions/ipyvolume/index")
require(["nbextensions/ipyvolume/index"])

In [ ]:
!mkdir -p test

In [ ]:
import shutil
def checkfig(name):
    !mkdir -p /tmp/ipyvolume/
    imagename = name  + ".png"
    output = os.path.join("/tmp/ipyvolume/" + imagename)
    compare = os.path.join("test/" + imagename)
    ipv.savefig(output)
    print(compare, output)
    if os.path.exists(compare):
        with open(output, "rb") as f:
            output_data = f.read()
        with open(compare, "rb") as f:
            compare_data = f.read()
        print(output, compare, output_data == compare_data)
        if output_data != compare_data:
            print("output didn't match expected output")
            img = IPython.display.Image(filename=compare)
            IPython.display.display(img)
    else:
        print("comparison image didn't exists, copying")
        shutil.copy(output, compare)

In [ ]:
shell = get_ipython()

In [ ]:
shell.kernel.

In [ ]:
ball = ipyvolume.examples.ball(shape=32, draw=False)

In [ ]:
fig = p3.figure(screen_capture_enabled=True)
vol = p3.volshow(ball)
p3.show()

In [ ]:
with

In [ ]:
checkfig("ball")

In [ ]:
vol.ray_steps =20

In [ ]:
x_org = x = np.linspace(0, 2, 10)
y = x**0.5
z = x**2
fig = p3.figure(screen_capture_enabled=True)
s = p3.scatter(x, y, z)
p3.xyzlim(0, 4)
p3.show()

In [ ]:
checkfig("test_scatter1a")

In [ ]:
s.x = s.x + 1
s.y = s.x**0.5
s.z = s.x**2

In [ ]:
checkfig("test_scatter1b")

In [ ]:
x = np.array([x_org, x_org+1])
y = x**0.5
z = x**2
fig = p3.figure(screen_capture_enabled=True)
fig.animation = 200
s = p3.scatter(x, y, z)
p3.xyzlim(0, 4)
p3.show()

In [ ]:
checkfig("test_scatter1a")

In [ ]:
s.sequence_index = 1

In [ ]:
checkfig("test_scatter1b")

In [ ]:
u = np.linspace(-10, 10, 100)
x, y = np.meshgrid(u, u)

In [ ]:
u = np.linspace(-10, 10, 50)
x2d, y2d = x, y = np.meshgrid(u, u)
x = x.flatten()
y = y.flatten()
r = np.sqrt(x**2+y**2)
time = np.linspace(0, np.pi*2, 15)
z = np.array([(np.cos(r + t) * np.exp(-r/5)) for t in time])
color = np.array([[np.cos(r + t), 1-np.abs(z[i]), 0.1+z[i]*0] for i, t in enumerate(time)])#.tolist()
size = (z+1)
color = np.transpose(color, axes=(0,2,1))

In [ ]:
color.shape

In [ ]:
p3.figure(screen_capture_enabled=True)
s = p3.scatter(x, z, y, color=color, size=size*2, marker="sphere")
p3.animation_control(s, interval=200)
p3.ylim(-3,3)
p3.show()

In [ ]:
checkfig("test_wave1a")

In [ ]:
s.sequence_index = 1

In [ ]:
checkfig("test_wave1b")

In [ ]:
z2d = z.reshape((-1,) + x2d.shape)
color2d = color.reshape((-1,) + x2d.shape + (3,))

In [ ]:
p3.figure(screen_capture_enabled=True)
m = p3.plot_surface(x2d, z2d, y2d, color=color2d)
p3.animation_control(m, interval=200)
p3.ylim(-3,3)
p3.show()

In [ ]:
checkfig("test_wave1surfacea")

In [ ]:
m.sequence_index = 1

In [ ]:
checkfig("test_wave1surfaceb")

In [ ]:
p3.figure(screen_capture_enabled=True)
m = p3.plot_wireframe(x2d, z2d, y2d, color=color2d)
p3.animation_control(m, interval=200)
p3.ylim(-3,3)
p3.show()

In [ ]:
checkfig("test_wave1wirea")

In [ ]:
m.sequence_index = 1

In [ ]:
checkfig("test_wave1wireb")

In [ ]:
p3.figure(screen_capture_enabled=True)
p3.plot_trisurf([0, 0, 3., 3.], [0, 4., 0, 4.], 2,\
    triangles=[[0, 2, 3], [0, 3, 1]])
p3.plot_trisurf([0, 0, 3., 3.], 2, [0, 4., 0, 4.],\
    triangles=[[0, 2, 3], [0, 3, 1]], color="blue")
p3.show()

Test mesh visibility


In [ ]:
from scipy.spatial import ConvexHull
s = 1/2**0.5
poly = np.array([[1,0,-s],[-1,0,-s],[0,1,s],[0,-1,s]],dtype=float)
triangles = ConvexHull(poly).simplices
lines = []
for i,j,k in triangles:
    lines += [[i,j],[i,k],[j,k]]
x, y, z = poly.T
fig1 = p3.figure(width=200,height=200)
mesh = p3.plot_trisurf(x, y, z, triangles=triangles, lines=lines, color='orange')
fig1

In [ ]:
checkfig("test_mesh_visible_all")

In [ ]:
mesh.line_material.visible = False

In [ ]:
checkfig("test_mesh_visible_faces")

In [ ]:
mesh.material.visible = False

In [ ]:
checkfig("test_mesh_visible_none")

In [ ]:
mesh.line_material.visible = True

In [ ]:
checkfig("test_mesh_visible_lines")

In [ ]:
mesh.visible = False

In [ ]:
checkfig("test_mesh_visible_none")

Test scatter/line visibility


In [ ]:
x_org = x = np.linspace(0, 2, 10)
y = x**0.5
z = x**2
fig = p3.figure()
s = p3.plot(x, y, z)
s.size = 2
p3.xyzlim(0, 4)
p3.show()

In [ ]:
checkfig("test_scatter1a_just_lines")

In [ ]:
s.material.visible = True

In [ ]:
checkfig("test_scatter1a_with_lines")

In [ ]:
s.line_material.visible = False

In [ ]:
s.line_material.visible = not s.line_material.visible

In [ ]:
checkfig("test_scatter1a")

In [ ]:
s.material.visible = False

In [ ]:
checkfig("test_scatter1a_empty")

In [ ]:
s.material.visible = True
s.line_material.visible = True

In [ ]:
checkfig("test_scatter1a_with_lines")

In [ ]:
s.visible = False

In [ ]:
checkfig("test_scatter1a_empty")

In [ ]:
s.visible = True

In [ ]:
checkfig("test_scatter1a_with_lines")

Test embedding


In [ ]:
fig = ipv.figure()
ipv.volshow(ball)
ipv.scatter(x, y, z)
ipv.plot_trisurf(mesh.x, mesh.y, mesh.z, mesh.triangles, color='orange')
ipv.show()

In [ ]:
ipv.save("test_embed/index.html")

In [ ]:
!open test_embed/index.html

In [ ]:
!rm -rf test_embed

In [ ]:
ipv.save("test_embed/index.html", offline=True)

In [ ]:
!open test_embed/index.html

Performance test

Don't execute this for testing, but only if you want to see how the performance is doing


In [ ]:
import time

In [ ]:
N = int(1e2)
M = 3
x = np.random.random((M,N)).astype(np.float32)
y = np.random.random((M,N)).astype(np.float32)
z = np.random.random((M,N)).astype(np.float32)

In [ ]:
fig = p3.figure()
#t0 = time.time()
#text = ipywidgets.Text()
#def timeit(change):
#    text.value = "spend {} seconds".format(time.time() - t0)
#fig.observe(timeit, "screen_capture_data")
s = p3.scatter(x, y, z, size=1, marker="box")
p3.animate_glyphs(s, interval=500)
s
#print(time.time() - t0)
#p3.show(extra_widgets=[text])
p3.show()
# print("show finished in", time.time() - t0, "seconds")

In [ ]:
p3.save("../tmp/plot.html", template_options={"embed_url":"embed.js"})

In [ ]:
ipywidgets.widget._remove_buffers

In [ ]:
s.sequence_index = 2

In [ ]:
s.color = "blue"
s.color_selected = "blue"

In [ ]:
s.size_selected = 4

In [ ]:
#s.color = (np.random.random((N,3)) * 255).astype(np.uint8)
s.color = (np.random.random((N,3)))

In [ ]:
s.color_selected = (np.random.random((N,3)))

In [ ]:
s.selected = np.random.randint(0, N, 100)

In [ ]:
s.selected = [0,1] # np.random.randint(0, N, 100)

In [ ]:
s.color_selected = "yellow"

In [ ]:
s.color_selected = "blue"

In [ ]:
s.color_selected = "brown"

In [ ]: