{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import ipyvolume.pylab as p3\n", "import ipyvolume as ipv\n", "import IPython.display\n", "import ipyvolume\n", "import numpy as np\n", "import os" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import ipywidgets" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%%javascript\n", "require.onResourceLoad = function(context, map)\n", "{\n", " console.log(\"loading \" +map.name)\n", " //require.undef(map.name);\n", "};\n", "require.undef(\"nbextensions/ipyvolume/index\")\n", "require([\"nbextensions/ipyvolume/index\"])" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "!mkdir -p test" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import shutil\n", "def checkfig(name):\n", " !mkdir -p /tmp/ipyvolume/\n", " imagename = name + \".png\"\n", " output = os.path.join(\"/tmp/ipyvolume/\" + imagename)\n", " compare = os.path.join(\"test/\" + imagename)\n", " ipv.savefig(output)\n", " print(compare, output)\n", " if os.path.exists(compare):\n", " with open(output, \"rb\") as f:\n", " output_data = f.read()\n", " with open(compare, \"rb\") as f:\n", " compare_data = f.read()\n", " print(output, compare, output_data == compare_data)\n", " if output_data != compare_data:\n", " print(\"output didn't match expected output\")\n", " img = IPython.display.Image(filename=compare)\n", " IPython.display.display(img)\n", " else:\n", " print(\"comparison image didn't exists, copying\")\n", " shutil.copy(output, compare)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "shell = get_ipython()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "shell.kernel." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "ball = ipyvolume.examples.ball(shape=32, draw=False)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "fig = p3.figure(screen_capture_enabled=True)\n", "vol = p3.volshow(ball)\n", "p3.show()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "with " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "checkfig(\"ball\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "vol.ray_steps =20" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": false }, "outputs": [], "source": [ "x_org = x = np.linspace(0, 2, 10)\n", "y = x**0.5\n", "z = x**2\n", "fig = p3.figure(screen_capture_enabled=True)\n", "s = p3.scatter(x, y, z)\n", "p3.xyzlim(0, 4)\n", "p3.show()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "checkfig(\"test_scatter1a\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "s.x = s.x + 1\n", "s.y = s.x**0.5\n", "s.z = s.x**2" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "checkfig(\"test_scatter1b\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "x = np.array([x_org, x_org+1])\n", "y = x**0.5\n", "z = x**2\n", "fig = p3.figure(screen_capture_enabled=True)\n", "fig.animation = 200\n", "s = p3.scatter(x, y, z)\n", "p3.xyzlim(0, 4)\n", "p3.show()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "checkfig(\"test_scatter1a\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "s.sequence_index = 1" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "checkfig(\"test_scatter1b\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "u = np.linspace(-10, 10, 100)\n", "x, y = np.meshgrid(u, u)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "u = np.linspace(-10, 10, 50)\n", "x2d, y2d = x, y = np.meshgrid(u, u)\n", "x = x.flatten()\n", "y = y.flatten()\n", "r = np.sqrt(x**2+y**2)\n", "time = np.linspace(0, np.pi*2, 15)\n", "z = np.array([(np.cos(r + t) * np.exp(-r/5)) for t in time])\n", "color = np.array([[np.cos(r + t), 1-np.abs(z[i]), 0.1+z[i]*0] for i, t in enumerate(time)])#.tolist()\n", "size = (z+1)\n", "color = np.transpose(color, axes=(0,2,1))\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "color.shape" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": false }, "outputs": [], "source": [ "p3.figure(screen_capture_enabled=True)\n", "s = p3.scatter(x, z, y, color=color, size=size*2, marker=\"sphere\")\n", "p3.animation_control(s, interval=200)\n", "p3.ylim(-3,3)\n", "p3.show()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "checkfig(\"test_wave1a\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "s.sequence_index = 1" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "checkfig(\"test_wave1b\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "z2d = z.reshape((-1,) + x2d.shape)\n", "color2d = color.reshape((-1,) + x2d.shape + (3,))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "p3.figure(screen_capture_enabled=True)\n", "m = p3.plot_surface(x2d, z2d, y2d, color=color2d)\n", "p3.animation_control(m, interval=200)\n", "p3.ylim(-3,3)\n", "p3.show()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "checkfig(\"test_wave1surfacea\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "m.sequence_index = 1" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "checkfig(\"test_wave1surfaceb\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "p3.figure(screen_capture_enabled=True)\n", "m = p3.plot_wireframe(x2d, z2d, y2d, color=color2d)\n", "p3.animation_control(m, interval=200)\n", "p3.ylim(-3,3)\n", "p3.show()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "checkfig(\"test_wave1wirea\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "m.sequence_index = 1" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "checkfig(\"test_wave1wireb\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "p3.figure(screen_capture_enabled=True)\n", "p3.plot_trisurf([0, 0, 3., 3.], [0, 4., 0, 4.], 2,\\\n", " triangles=[[0, 2, 3], [0, 3, 1]])\n", "p3.plot_trisurf([0, 0, 3., 3.], 2, [0, 4., 0, 4.],\\\n", " triangles=[[0, 2, 3], [0, 3, 1]], color=\"blue\")\n", "p3.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " # Test mesh visibility" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from scipy.spatial import ConvexHull\n", "s = 1/2**0.5\n", "poly = np.array([[1,0,-s],[-1,0,-s],[0,1,s],[0,-1,s]],dtype=float)\n", "triangles = ConvexHull(poly).simplices\n", "lines = []\n", "for i,j,k in triangles:\n", " lines += [[i,j],[i,k],[j,k]]\n", "x, y, z = poly.T\n", "fig1 = p3.figure(width=200,height=200)\n", "mesh = p3.plot_trisurf(x, y, z, triangles=triangles, lines=lines, color='orange')\n", "fig1" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "checkfig(\"test_mesh_visible_all\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "mesh.line_material.visible = False" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "checkfig(\"test_mesh_visible_faces\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "mesh.material.visible = False" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "checkfig(\"test_mesh_visible_none\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "mesh.line_material.visible = True" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "checkfig(\"test_mesh_visible_lines\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "mesh.visible = False" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "checkfig(\"test_mesh_visible_none\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Test scatter/line visibility" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": false }, "outputs": [], "source": [ "x_org = x = np.linspace(0, 2, 10)\n", "y = x**0.5\n", "z = x**2\n", "fig = p3.figure()\n", "s = p3.plot(x, y, z)\n", "s.size = 2\n", "p3.xyzlim(0, 4)\n", "p3.show()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "checkfig(\"test_scatter1a_just_lines\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "s.material.visible = True" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "checkfig(\"test_scatter1a_with_lines\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "s.line_material.visible = False" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "s.line_material.visible = not s.line_material.visible" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "checkfig(\"test_scatter1a\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "s.material.visible = False" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "checkfig(\"test_scatter1a_empty\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "s.material.visible = True\n", "s.line_material.visible = True" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "checkfig(\"test_scatter1a_with_lines\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "s.visible = False" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "checkfig(\"test_scatter1a_empty\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "s.visible = True" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "checkfig(\"test_scatter1a_with_lines\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Test embedding" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "fig = ipv.figure()\n", "ipv.volshow(ball)\n", "ipv.scatter(x, y, z)\n", "ipv.plot_trisurf(mesh.x, mesh.y, mesh.z, mesh.triangles, color='orange')\n", "ipv.show()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "ipv.save(\"test_embed/index.html\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "!open test_embed/index.html" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "!rm -rf test_embed" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "ipv.save(\"test_embed/index.html\", offline=True)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "!open test_embed/index.html" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Performance test\n", "Don't execute this for testing, but only if you want to see how the performance is doing" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import time" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "N = int(1e2)\n", "M = 3\n", "x = np.random.random((M,N)).astype(np.float32)\n", "y = np.random.random((M,N)).astype(np.float32)\n", "z = np.random.random((M,N)).astype(np.float32)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "fig = p3.figure()\n", "#t0 = time.time()\n", "#text = ipywidgets.Text()\n", "#def timeit(change):\n", "# text.value = \"spend {} seconds\".format(time.time() - t0)\n", "#fig.observe(timeit, \"screen_capture_data\")\n", "s = p3.scatter(x, y, z, size=1, marker=\"box\")\n", "p3.animate_glyphs(s, interval=500)\n", "s\n", "#print(time.time() - t0)\n", "#p3.show(extra_widgets=[text])\n", "p3.show()\n", "# print(\"show finished in\", time.time() - t0, \"seconds\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "p3.save(\"../tmp/plot.html\", template_options={\"embed_url\":\"embed.js\"})" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "ipywidgets.widget._remove_buffers" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "s.sequence_index = 2" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "s.color = \"blue\"\n", "s.color_selected = \"blue\"" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "s.size_selected = 4" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#s.color = (np.random.random((N,3)) * 255).astype(np.uint8)\n", "s.color = (np.random.random((N,3)))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "s.color_selected = (np.random.random((N,3)))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "s.selected = np.random.randint(0, N, 100)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "s.selected = [0,1] # np.random.randint(0, N, 100)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "s.color_selected = \"yellow\"" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "s.color_selected = \"blue\"" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "s.color_selected = \"brown\"" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "anaconda-cloud": {}, "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.4" } }, "nbformat": 4, "nbformat_minor": 2 }