SciBLOG

2015-04-29 Moving from Mayavi to VisPy

2015-04-29 09:56

A long standing dependency of MotionClouds is MayaVi. While powerful, it is tedious to compile and may discourage new users. We are trying here to show some attempts to do the same with the vispy library.

In another post, we tried to use matplotlib, but this had some limitations. Let's now try vispy, a scientific visualisation library in python using opengl.

VisPy is a quite young library, with a mature mid-level object-otriented wrapper to openGL (that they called gloo, I guess it is GL object-oriented) and the early stages of a high-level wrapper quite similar to MayaVi in the functioanlity. As I do not need to mess with the GPU, I will use the latter. As for the time being, we need to install the latest version:

Let's render this scene offline as it makes it easier to see in the notebook:

In [6]:

importnumpyasnpnp.set_printoptions(precision=3,suppress=True)fromvispyimportapp,scenefromvispy.util.transformsimportperspective,translate,rotateapp.use_app('pyglet')canvas=scene.SceneCanvas(keys='interactive',size=(1280,1024),position=(0,0),bgcolor='white',dpi=450)view=canvas.central_widget.add_view()surface=scene.visuals.Isosurface(z,level=z.max()/4.,color=(0.5,0.6,1,.3),shading='smooth',parent=view.scene)surface.transform=scene.transforms.STTransform(translate=(-mc.N_X/2,-mc.N_Y/2,-mc.N_frame/2))axis=scene.visuals.XYZAxis(parent=view.scene)cam=scene.TurntableCamera(elevation=30,azimuth=30)cam.set_range((-mc.N_X/2,mc.N_X/2),(-mc.N_Y/2,mc.N_Y/2),(-mc.N_frame/2,mc.N_frame/2))view.camera=cam# The rendering is done, we get the rendering output (4D NumPy array)im=canvas.render()

fromvispyimportapp,scenefromvispy.util.transformsimportperspective,translate,rotatecanvas=scene.SceneCanvas(keys='interactive',size=(1280,1024),position=(0,0),bgcolor='white',dpi=450)view=canvas.central_widget.add_view()N_X,N_Y,N_frame=mc.N_X,mc.N_Y,mc.N_frameaxis=scene.visuals.XYZAxis(parent=view.scene)forpin([1,1,1,-1,1,1],[1,1,-1,-1,1,-1],[1,-1,1,-1,-1,1],[1,-1,-1,-1,-1,-1],[1,1,1,1,-1,1],[-1,1,1,-1,-1,1],[1,1,-1,1,-1,-1],[-1,1,-1,-1,-1,-1],[1,1,1,1,1,-1],[-1,1,1,-1,1,-1],[1,-1,1,1,-1,-1],[-1,-1,1,-1,-1,-1]):line=scene.visuals.Line(pos=np.array([[p[0]*N_X/2,p[1]*N_Y/2,p[2]*N_frame/2],[p[3]*N_X/2,p[4]*N_Y/2,p[5]*N_frame/2]]),color='black',parent=view.scene)axisX=scene.visuals.Line(pos=np.array([[0,-N_Y/2,0],[0,N_Y/2,0]]),color='red',parent=view.scene)axisY=scene.visuals.Line(pos=np.array([[-N_X/2,0,0],[N_X/2,0,0]]),color='green',parent=view.scene)axisZ=scene.visuals.Line(pos=np.array([[0,0,-N_frame/2],[0,0,N_frame/2]]),color='blue',parent=view.scene)cam=scene.TurntableCamera(elevation=20,azimuth=30)cam.fov=45cam.scale_factor=mc.N_X*2.cam.set_range((-mc.N_X/2,mc.N_X/2),(-mc.N_Y/2,mc.N_Y/2),(-mc.N_frame/2,mc.N_frame/2))view.camera=cam#view.set_camera('turntable', mode='perspective', up='z', distance=2,# azimuth=30., elevation=30.)# The rendering is done, we get the rendering output (4D NumPy array)# Use render to generate an image objectim=canvas.render()im2img(im)

Out[9]:

Now a more complete scene:

In [10]:

fromvispyimportapp,scenefromvispy.util.transformsimportperspective,translate,rotatecanvas=scene.SceneCanvas(keys='interactive',size=(1280,1024),position=(0,0),bgcolor='white',dpi=450)view=canvas.central_widget.add_view()volume=scene.visuals.Volume(z/z.max(),parent=view.scene,emulate_texture=True)center=scene.transforms.STTransform(translate=(-mc.N_X/2,-mc.N_Y/2,-mc.N_frame/2))volume.transform=centervolume.cmap='blues'surface=scene.visuals.Isosurface(z,level=z.max()/4.,color=(0.5,0.6,1,.9),shading='smooth',parent=view.scene)surface.transform=centert={}fortextin['f_x','f_y','f_t']:t[text]=scene.visuals.Text(text,parent=canvas.scene,color='black')t[text].font_size=8t['f_x'].pos=canvas.size[0]//3,canvas.size[1]-canvas.size[1]//8t['f_y'].pos=canvas.size[0]-canvas.size[0]//4,canvas.size[1]-canvas.size[1]//8t['f_t'].pos=canvas.size[0]//6,canvas.size[1]//2N_X,N_Y,N_frame=mc.N_X,mc.N_Y,mc.N_frameforpin([1,1,1,-1,1,1],[1,1,-1,-1,1,-1],[1,-1,1,-1,-1,1],[1,-1,-1,-1,-1,-1],[1,1,1,1,-1,1],[-1,1,1,-1,-1,1],[1,1,-1,1,-1,-1],[-1,1,-1,-1,-1,-1],[1,1,1,1,1,-1],[-1,1,1,-1,1,-1],[1,-1,1,1,-1,-1],[-1,-1,1,-1,-1,-1]):line=scene.visuals.Line(pos=np.array([[p[0]*N_X/2,p[1]*N_Y/2,p[2]*N_frame/2],[p[3]*N_X/2,p[4]*N_Y/2,p[5]*N_frame/2]]),color='black',parent=view.scene)axisX=scene.visuals.Line(pos=np.array([[0,-N_Y/2,0],[0,N_Y/2,0]]),color='red',parent=view.scene)axisY=scene.visuals.Line(pos=np.array([[-N_X/2,0,0],[N_X/2,0,0]]),color='green',parent=view.scene)axisZ=scene.visuals.Line(pos=np.array([[0,0,-N_frame/2],[0,0,N_frame/2]]),color='blue',parent=view.scene)axis=scene.visuals.XYZAxis(parent=view.scene)cam=scene.TurntableCamera(elevation=20,azimuth=30)cam.fov=45cam.scale_factor=mc.N_X*2.cam.set_range((-mc.N_X/2,mc.N_X/2),(-mc.N_Y/2,mc.N_Y/2),(-mc.N_frame/2,mc.N_frame/2))view.camera=cam#view.set_camera('turntable', mode='perspective', up='z', distance=2,# azimuth=30., elevation=30.)# The rendering is done, we get the rendering output (4D NumPy array)# Use render to generate an image objectim=canvas.render()im2img(im)

Out[10]:

Looks promising! Let's first test the axis are right:

In [11]:

importMotionCloudsasmcfigsize=(900,900)mc.N_X,mc.N_Y,mc.N_frame=2**5,2**6,2**7N_X,N_Y,N_frame=mc.N_X,mc.N_Y,mc.N_framefx,fy,ft=mc.get_grids(mc.N_X,mc.N_Y,mc.N_frame)z=mc.envelope_gabor(fx,fy,ft,V_X=1.,V_Y=1.,theta=np.pi/4)importnumpyasnpfromvispyimportapp,scenefromvispy.util.transformsimportperspective,translate,rotatecanvas=scene.SceneCanvas(keys='interactive',size=(1280,1024),position=(0,0),bgcolor='white',dpi=450)view=canvas.central_widget.add_view()vol_data=np.rollaxis(np.rollaxis(z,1),2)volume=scene.visuals.Volume(vol_data,parent=view.scene)center=scene.transforms.STTransform(translate=(-N_X/2,-N_Y/2,-N_frame/2))volume.transform=centervolume.cmap='blues'surface=scene.visuals.Isosurface(z,level=z.max()/4.,color=(0.5,0.6,1,.9),shading='smooth',parent=view.scene)surface.transform=centert={}fortextin['f_x','f_y','f_t']:t[text]=scene.visuals.Text(text,parent=canvas.scene,color='black')t[text].font_size=8t['f_x'].pos=canvas.size[0]//3,canvas.size[1]-canvas.size[1]//18t['f_y'].pos=canvas.size[0]-canvas.size[0]//3,canvas.size[1]-canvas.size[1]//8t['f_t'].pos=canvas.size[0]//6,canvas.size[1]//2N_X,N_Y,N_frame=mc.N_X,mc.N_Y,mc.N_frameforpin([1,1,1,-1,1,1],[1,1,-1,-1,1,-1],[1,-1,1,-1,-1,1],[1,-1,-1,-1,-1,-1],[1,1,1,1,-1,1],[-1,1,1,-1,-1,1],[1,1,-1,1,-1,-1],[-1,1,-1,-1,-1,-1],[1,1,1,1,1,-1],[-1,1,1,-1,1,-1],[1,-1,1,1,-1,-1],[-1,-1,1,-1,-1,-1]):line=scene.visuals.Line(pos=np.array([[p[0]*N_X/2,p[1]*N_Y/2,p[2]*N_frame/2],[p[3]*N_X/2,p[4]*N_Y/2,p[5]*N_frame/2]]),color='black',parent=view.scene)axisX=scene.visuals.Line(pos=np.array([[0,-N_Y/2,0],[0,N_Y/2,0]]),color='red',parent=view.scene)axisY=scene.visuals.Line(pos=np.array([[-N_X/2,0,0],[N_X/2,0,0]]),color='green',parent=view.scene)axisZ=scene.visuals.Line(pos=np.array([[0,0,-N_frame/2],[0,0,N_frame/2]]),color='blue',parent=view.scene)axis=scene.visuals.XYZAxis(parent=view.scene)cam=scene.TurntableCamera(elevation=20,azimuth=30)cam.fov=45cam.scale_factor=mc.N_X*5.cam.set_range((-mc.N_X/2,mc.N_X/2),(-mc.N_Y/2,mc.N_Y/2),(-mc.N_frame/2,mc.N_frame/2))view.camera=cam#view.set_camera('turntable', mode='perspective', up='z', distance=2,# azimuth=30., elevation=30.)# The rendering is done, we get the rendering output (4D NumPy array)# Use render to generate an image objectim=canvas.render()im2img(im)