{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%gui qt" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Update Image and Isocurve Visuals\n\nShow use of SceneCanvas to display and update Image and Isocurve visuals using\nViewBox visual.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import sys\nimport numpy as np\nfrom itertools import cycle\n\nfrom vispy import app, scene\nfrom vispy.scene import STTransform\nfrom vispy.util.filter import gaussian_filter\nfrom vispy.color import get_colormaps, get_color_names\n\ncanvas = scene.SceneCanvas(keys='interactive',\n title='Show update capabilities of Isocurve Visual',\n show=True)\ncanvas.show()\n\n# Setting up four viewboxes\nvb1 = scene.widgets.ViewBox(border_color='yellow', parent=canvas.scene)\nvb2 = scene.widgets.ViewBox(border_color='blue', parent=canvas.scene)\nvb3 = scene.widgets.ViewBox(border_color='red', parent=canvas.scene)\nvb4 = scene.widgets.ViewBox(border_color='purple', parent=canvas.scene)\n\nvb = (vb1, vb2, vb3, vb4)\n\n# add grid as central widget, add viewboxes into grid\ngrid = canvas.central_widget.add_grid()\ngrid.padding = 0\ngrid.add_widget(vb1, 0, 0)\ngrid.add_widget(vb2, 0, 1)\ngrid.add_widget(vb3, 1, 0)\ngrid.add_widget(vb4, 1, 1)\n\n# panzoom cameras for every viewbox\nfor box in vb:\n box.camera = 'panzoom'\n box.camera.aspect = 1.0\n\n# Create random image\nimg_data1 = np.empty((200, 100, 3), dtype=np.ubyte)\nnoise = np.random.normal(size=(200, 100), loc=50, scale=150)\nnoise = gaussian_filter(noise, (4, 4, 0)).astype(np.float32)\nimg_data1[:] = noise[..., np.newaxis]\n\n# create 2d array with some function\nx, y = np.mgrid[0:2*np.pi:201j, 0:2*np.pi:101j]\nmyfunc = np.cos(2*x[:-1, :-1]) + np.sin(2*y[:-1, :-1])\nmyfunc = myfunc.astype(np.float32)\n\n# add image to viewbox1\nimage1 = scene.visuals.Image(noise, parent=vb1.scene, cmap='cubehelix')\n# move image behind curves\nimage1.transform = STTransform(translate=(0, 0, 0.5))\nvb1.camera.set_range()\n\n# add image to viewbox2\nimage2 = scene.visuals.Image(myfunc, parent=vb2.scene, cmap='cubehelix')\n# move image behind curves\nimage2.transform = STTransform(translate=(0, 0, 0.5))\nvb2.camera.set_range()\n\n# create some level for the isocurves\nlevels1 = np.linspace(noise.min(), noise.max(), num=52, endpoint=True)[1:-1]\nlevels2 = np.linspace(myfunc.min(), myfunc.max(), num=52, endpoint=True)[1:-1]\n\n# create curve 1a (image overlay, black) and 1b (plain, cubehelix colored)\n# to viewboxes 1 and 3\ncurve1a = scene.visuals.Isocurve(\n noise, levels=levels1[::4], color_lev='k', parent=vb1.scene)\ncurve1b = scene.visuals.Isocurve(\n noise, levels=levels1, color_lev='cubehelix', parent=vb3.scene)\n\n# create curve 2a (2darray overlay, black) and 2b (plain, cubehelix colored)\n# to viewboxes 2 and 4\ncurve2a = scene.visuals.Isocurve(\n myfunc, levels=levels2[::4], color_lev='k', parent=vb2.scene)\ncurve2b = scene.visuals.Isocurve(\n myfunc, levels=levels2, color_lev='cubehelix', parent=vb4.scene)\n\n# set viewport\nvb3.camera.set_range((-100, 200), (0, 200))\nvb4.camera.set_range((0, 100), (0, 200))\n\n\n# setup update parameters\nup = 1\nindex = 1\nclip = np.linspace(myfunc.min(), myfunc.max(), num=51)\ncmap = cycle(get_colormaps())\ncolor = cycle(get_color_names())\n\n\ndef update(ev):\n global myfunc, index, up, levels2, noise, cmap, color\n\n if index > 0 and index < 25:\n # update left panes rolling upwards\n noise = np.roll(noise, 1, axis=0)\n image1.set_data(noise)\n curve1a.set_data(noise)\n curve1b.set_data(noise)\n\n # update colors/colormap\n if (index % 5) == 0:\n curve1b.color = next(color)\n cm = next(cmap)\n image2.cmap = cm\n curve2b.color = cm\n\n # change isocurves by clipping data/or changing limits\n # update curve1b levels (clip)\n curve1b.levels = levels1[index:-index]\n\n # update curve2b data with clipped data\n im2 = np.clip(myfunc, clip[index], clip[-index])\n curve2b.set_data(im2)\n\n index += up\n else:\n # change index direction\n up = -up\n index += up\n\n canvas.update()\n\n# setup timer\ntimer = app.Timer()\ntimer.connect(update)\n# slow this down a bit to better see what happens\ntimer.start(0)\n\nif __name__ == '__main__' and sys.flags.interactive == 0:\n app.run()" ] } ], "metadata": { "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.9.19" } }, "nbformat": 4, "nbformat_minor": 0 }