Professional Documents
Culture Documents
Mayavi Tutorial Scipy08
Mayavi Tutorial Scipy08
Mayavi Tutorial Scipy08
Mayavi
Outline
1
Outline
1
Introduction
Mayavi
Features
Mayavi provides
An application for 3D visualization An easy interface: mlab Ability to embed mayavi in your objects/views Envisage plugins A more general purpose OO library A numpy/Python friendly API
Mayavi
Mayavi
mlab
Example code
dims = [ 6 4 , 64 , 6 4 ] xmin , xmax , ymin , ymax , zmin , zmax = \ [ 5 ,5 , 5 ,5 , 5 ,5] x , y , z = numpy . o g r i d [ xmin : xmax : dims [ 0 ] 1 j , ymin : ymax : dims [ 1 ] 1 j , zmin : zmax : dims [ 2 ] 1 j ] x , y , z = [ t . astype ( f ) f o r t i n ( x , y , z ) ] s c a l a r s = xx 0.5 + yy + zz 2.0 # Contour t h e data . from enthought . mayavi import mlab mlab . contour3d ( s c a l a r s , c o n t o u r s =4 , t r a n s p a r e n t =True )
Mayavi
Mayavi
Mayavi
Outline
1
Installation
Requirements:
numpy wxPython-2.8.x or PyQt 4.x VTK-5.x IPython Ideally ETS-3.0.0 if not ETS-2.8.0 will work
easy_install Mayavi[app]
Get help on enthought-dev@mail.enthought.com
Mayavi
Introduction mlab VTK and TVTK Advanced features Introduction Animating data
Outline
1
Introduction mlab VTK and TVTK Advanced features Introduction Animating data
Outline
Exposure to major mlab functionality Introduction to the visualization pipeline Advanced features
Datasets in mlab Filtering data Animating data
Mayavi
Introduction mlab VTK and TVTK Advanced features Introduction Animating data
Outline
1
Introduction mlab VTK and TVTK Advanced features Introduction Animating data
Animating data
Animate data without recreating the pipeline Use the mlab_source attribute to modify data Example code
x , y = numpy . mgrid [ 0 : 3 : 1 , 0 : 3 : 1 ] s = mlab . s u r f ( x , y , numpy . a s a r r a y ( x 0 . 1 , d ) ) # Animate t h e data . ms = s . mlab_source # Get t h e source f o r i i n range ( 1 0 ) : # Modify t h e s c a l a r s ms . s c a l a r s = numpy . a s a r r a y ( x 0 . 1 ( i + 1 ) , d )
Mayavi
Introduction mlab VTK and TVTK Advanced features Introduction Animating data
Animating data
Typical mlab_source traits x, y, z: x, y, z positions points: generated from x, y, z scalars: scalar data u, v, w: components of vector data vectors: vector data Important methods: set: set multiple traits efciently reset: use when the arrays change shape; slow update: call when you change points/scalars/vectors in-place Check out the examples: mlab.test_*_anim
Mayavi
Introduction mlab VTK and TVTK Advanced features Introduction Animating data
Exercise
Show iso-contours of the function sin(xyz )/xyz x , y , z in region [5, 5] with 32 points along each axis
Mayavi
Introduction mlab VTK and TVTK Advanced features Introduction to VTK and TVTK TVTK datasets from numpy arrays
Outline
1
Introduction mlab VTK and TVTK Advanced features Introduction to VTK and TVTK TVTK datasets from numpy arrays
Introduction
Open source, BSD style license High level library 3D graphics, imaging and visualization Core implemented in C++ for speed Uses OpenGL for rendering Wrappers for Python, Tcl and Java Cross platform: *nix, Windows, and Mac OSX Around 40 developers worldwide Very powerful with lots of features/functionality: > 900 classes Pipeline architecture Not trivial to learn (VTK book helps) Reasonable learning curve
Prabhu Ramachandran, Gal Varoquaux Mayavi
Introduction mlab VTK and TVTK Advanced features Introduction to VTK and TVTK TVTK datasets from numpy arrays
Filter
Processes data
Writer
Mapper
Graphics primitives
Sinks
Actor
Display
Mayavi
Introduction mlab VTK and TVTK Advanced features Introduction to VTK and TVTK TVTK datasets from numpy arrays
API is not Pythonic for complex scripts Native array interface Using NumPy arrays with VTK: non-trivial and inelegant Native iterator interface Cant be pickled GUI editors need to be hand-made (> 800 classes!)
Mayavi
Introduction mlab VTK and TVTK Advanced features Introduction to VTK and TVTK TVTK datasets from numpy arrays
TVTK
Mayavi
Introduction mlab VTK and TVTK Advanced features Introduction to VTK and TVTK TVTK datasets from numpy arrays
The differences
TVTK from enthought.tvtk.api import tvtk tvtk.ConeSource traits set on creation cone.height cone.representation=w
vtk3DWidget ThreeDWidget
Method names: consistent with ETS (lower_case_with_underscores) VTK class properties (Set/Get pairs or Getters): traits
Mayavi
Introduction mlab VTK and TVTK Advanced features Introduction to VTK and TVTK TVTK datasets from numpy arrays
Array example
Any method accepting DataArray, Points, IdList or CellArray instances can be passed a numpy array or a Python list!
>>> from enthought . t v t k . a p i import t v t k >>> from numpy import a r r a y >>> p o i n t s = a r r a y ( [ [ 0 , 0 , 0 ] , [ 1 , 0 , 0 ] , [ 0 , 1 , 0 ] , [ 0 , 0 , 1 ] ] , f ) >>> t r i a n g l e s = a r r a y ( [ [ 0 , 1 , 3 ] , [ 0 , 3 , 2 ] , [ 1 , 2 , 3 ] , [ 0 , 2 , 1 ] ] ) >>> mesh = t v t k . PolyData ( ) >>> mesh . p o i n t s = p o i n t s >>> mesh . p o l y s = t r i a n g l e s >>> t e mp e r at u r e = a r r a y ( [ 1 0 , 20 , 2 0 , 3 0 ] , f ) >>> mesh . p o i n t _ d a t a . s c a l a r s = t e m pe r a tu r e >>> import o p e r a t o r # A r r a y s are P y t h o n i c . >>> reduce ( o p e r a t o r . add , mesh . p o i n t _ d a t a . s c a l a r s , 0 . 0 ) 80.0 >>> p t s = t v t k . P o i n t s ( ) # Demo o f f r o m _ a r r a y / t o _ a r r a y >>> p t s . f r o m _ a r r a y ( p o i n t s ) >>> p r i n t p t s . t o _ a r r a y ( )
Prabhu Ramachandran, Gal Varoquaux Mayavi
Introduction mlab VTK and TVTK Advanced features Introduction to VTK and TVTK TVTK datasets from numpy arrays
More details
projects/mayavi/documentation.php
Mayavi
Introduction mlab VTK and TVTK Advanced features Introduction to VTK and TVTK TVTK datasets from numpy arrays
Outline
1
Introduction mlab VTK and TVTK Advanced features Introduction to VTK and TVTK TVTK datasets from numpy arrays
2D line plots are easy Visualizing 3D data: requires a little more information Need to specify a topology (i.e. how are the points connected)
Mayavi
Introduction mlab VTK and TVTK Advanced features Introduction to VTK and TVTK TVTK datasets from numpy arrays
Introduction mlab VTK and TVTK Advanced features Introduction to VTK and TVTK TVTK datasets from numpy arrays
Specify the points of the space Specify the connectivity between the points (topology) Connectivity specify cells partitioning the space Specify attribute data at the points or cells
Points
Rectangular cell
Triangular cells
Point data 40 30 10 20
Mayavi
Cell data 20 10
Introduction mlab VTK and TVTK Advanced features Introduction to VTK and TVTK TVTK datasets from numpy arrays
Types of datasets
Mayavi
Introduction mlab VTK and TVTK Advanced features Introduction to VTK and TVTK TVTK datasets from numpy arrays
Easiest example: a rectangular mesh Non-rectangular mesh certainly possible (structured grid)
Mayavi
Introduction mlab VTK and TVTK Advanced features Introduction to VTK and TVTK TVTK datasets from numpy arrays
Mayavi
Introduction mlab VTK and TVTK Advanced features Introduction to VTK and TVTK TVTK datasets from numpy arrays
Mayavi
Introduction mlab VTK and TVTK Advanced features Introduction to VTK and TVTK TVTK datasets from numpy arrays
Dataset attributes
Mayavi
Introduction mlab VTK and TVTK Advanced features Introduction to VTK and TVTK TVTK datasets from numpy arrays
Overview
Creating datasets with TVTK and Numpy: by example Very handy when working with Numpy No need to create VTK data les Can visualize them easily with mlab/mayavi See examples/mayavi/datasets.py
Mayavi
Introduction mlab VTK and TVTK Advanced features Introduction to VTK and TVTK TVTK datasets from numpy arrays
PolyData
Create the dataset: tvtk.PolyData() Set the points: points Set the connectivity: polys Set the point/cell attributes (in same order)
Mayavi
Introduction mlab VTK and TVTK Advanced features Introduction to VTK and TVTK TVTK datasets from numpy arrays
PolyData
from enthought . t v t k . a p i import t v t k # The p o i n t s i n 3D . points = array ( [ [ 0 , 0 , 0 ] , [1 ,0 ,0] , [0 ,1 ,0] , [ 0 , 0 , 1 ] ] , f ) # C o n n e c t i v i t y via i n d i c e s to the p o i n t s . t r i a n g l e s = array ( [ [ 0 , 1 , 3 ] , [0 ,3 ,2] , [1 ,2 ,3] , [ 0 , 2 , 1 ] ] ) # C r e a t i n g t h e data o b j e c t . mesh = t v t k . PolyData ( ) mesh . p o i n t s = p o i n t s # t h e p o i n t s mesh . p o l y s = t r i a n g l e s # t r i a n g l e s f o r c o n n e c t i v i t y . # For l i n e s / v e r t s use : mesh . l i n e s = l i n e s ; mesh . v e r t s = v e r t i c e s # Now c r e a t e some p o i n t data . t em p e r at u r e = a r r a y ( [ 1 0 , 20 , 2 0 , 3 0 ] , f ) mesh . p o i n t _ d a t a . s c a l a r s = t em p e ra t u r e mesh . p o i n t _ d a t a . s c a l a r s . name = t em p e ra t u r e # Some v e c t o r s . v e l o c i t y = array ( [ [ 0 , 0 , 0 ] , [1 ,0 ,0] , [0 ,1 ,0] , [ 0 , 0 , 1 ] ] , f ) mesh . p o i n t _ d a t a . v e c t o r s = v e l o c i t y mesh . p o i n t _ d a t a . v e c t o r s . name = v e l o c i t y # Thats i t !
Prabhu Ramachandran, Gal Varoquaux Mayavi
Introduction mlab VTK and TVTK Advanced features Introduction to VTK and TVTK TVTK datasets from numpy arrays
PolyData
Mayavi
Introduction mlab VTK and TVTK Advanced features Introduction to VTK and TVTK TVTK datasets from numpy arrays
ImageData
Orthogonal cube of data: uniform spacing Create the dataset: tvtk.ImageData() Implicit points and topology: origin: origin of region spacing: spacing of points dimensions: think size of array Set the point/cell attributes (in same order) Implicit topology implies that the data must be ordered The X co-ordinate increases rst, Y next and Z last
Mayavi
Introduction mlab VTK and TVTK Advanced features Introduction to VTK and TVTK TVTK datasets from numpy arrays
Introduction mlab VTK and TVTK Advanced features Introduction to VTK and TVTK TVTK datasets from numpy arrays
ImageData 2D
Mayavi
Introduction mlab VTK and TVTK Advanced features Introduction to VTK and TVTK TVTK datasets from numpy arrays
Image data: 3D
from numpy import a r r a y , o g r i d , s i n , r a v e l dims = a r r a y ( ( 1 2 8 , 128 , 1 2 8 ) ) v o l = a r r a y ( ( 5 . , 5 , 5, 5 , 5, 5 ) ) origin = vol [ : : 2 ] spacing = ( v o l [ 1 : : 2 ] o r i g i n ) / ( dims 1) xmin , xmax , ymin , ymax , zmin , zmax = v o l x , y , z = o g r i d [ xmin : xmax : dims [ 0 ] 1 j , ymin : ymax : dims [ 1 ] 1 j , zmin : zmax : dims [ 2 ] 1 j ] x , y , z = [ t . astype ( f ) f o r t i n ( x , y , z ) ] s c a l a r s = s i n ( xyz ) / ( xyz ) # img = t v t k . ImageData ( o r i g i n = o r i g i n , spacing =spacing , dimensions=dims ) # The copy makes t h e data c o n t i g u o u s and t h e t r a n s p o s e # makes i t s u i t a b l e f o r d i s p l a y v i a t v t k . s = s c a l a r s . t r a n s po s e ( ) . copy ( ) img . p o i n t _ d a t a . s c a l a r s = r a v e l ( s ) img . p o i n t _ d a t a . s c a l a r s . name = s c a l a r s
Prabhu Ramachandran, Gal Varoquaux Mayavi
Introduction mlab VTK and TVTK Advanced features Introduction to VTK and TVTK TVTK datasets from numpy arrays
ImageData 3D
Mayavi
Introduction mlab VTK and TVTK Advanced features Introduction to VTK and TVTK TVTK datasets from numpy arrays
RectilinearGrid
Orthogonal cube of data: non-uniform spacing Create the dataset: tvtk.RectilinearGrid() Explicit points: x_coordinates, y_coordinates,
z_coordinates
Implicit topology: X rst, Y next and Z last Set the point/cell attributes (in same order)
Mayavi
Introduction mlab VTK and TVTK Advanced features Introduction to VTK and TVTK TVTK datasets from numpy arrays
StructuredGrid
Create the dataset: tvtk.StructuredGrid() Explicit points: points Implicit topology: X rst, Y next and Z last Set the point/cell attributes (in same order)
Mayavi
Introduction mlab VTK and TVTK Advanced features Introduction to VTK and TVTK TVTK datasets from numpy arrays
Structured Grid
r = numpy . l i n s p a c e ( 1 , 10 , 25) t h e t a = numpy . l i n s p a c e ( 0 , 2 numpy . p i , 51) z = numpy . l i n s p a c e ( 0 , 5 , 25) # C r r e a t e an annulus . x_plane = ( cos ( t h e t a ) r [ : , None ] ) . r a v e l ( ) y_plane = ( s i n ( t h e t a ) r [ : , None ] ) . r a v e l ( ) p t s = empty ( [ l e n ( x_plane ) l e n ( h e i g h t ) , 3 ] ) f o r i , z _ v a l i n enumerate ( z ) : s t a r t = i l e n ( x_plane ) p l a n e _ p o i n t s = p t s [ s t a r t : s t a r t + l e n ( x_plane ) ] p l a n e _ p o i n t s [ : , 0 ] = x_plane p l a n e _ p o i n t s [ : , 1 ] = y_plane plane_points [ : , 2 ] = z_val s g r i d = t v t k . S t r u c t u r e d G r i d ( dimensions =(51 , 25 , 2 5 ) ) sgrid . points = pts s = numpy . s q r t ( p t s [ : , 0 ] 2 + p t s [ : , 1 ] 2 + p t s [ : , 2 ] 2 ) s g r i d . p o i n t _ d a t a . s c a l a r s = numpy . r a v e l ( s . copy ( ) ) s g r i d . p o i n t _ d a t a . s c a l a r s . name = s c a l a r s
Prabhu Ramachandran, Gal Varoquaux Mayavi
Introduction mlab VTK and TVTK Advanced features Introduction to VTK and TVTK TVTK datasets from numpy arrays
StructuredGrid
Mayavi
Introduction mlab VTK and TVTK Advanced features Introduction to VTK and TVTK TVTK datasets from numpy arrays
UnstructuredGrid
Mayavi
Introduction mlab VTK and TVTK Advanced features Introduction to VTK and TVTK TVTK datasets from numpy arrays
Unstructured Grid
from numpy import a r r a y points = array ( [ [ 0 , 0 , 0 ] , [1 ,0 ,0] , [0 ,1 ,0] , [ 0 , 0 , 1 ] ] , f ) t e t s = array ( [ [ 0 , 1 , 2 , 3 ] ] ) t e t _ t y p e = t v t k . T e t r a ( ) . c e l l _ t y p e # VTK_TETRA == 10 # ug = t v t k . U n s t r u c t u r e d G r i d ( ) ug . p o i n t s = p o i n t s # T h i s s e t s up t h e c e l l s . ug . s e t _ c e l l s ( t e t _ t y p e , t e t s ) # A t t r i b u t e data . t em p e r at u r e = a r r a y ( [ 1 0 , 20 , 2 0 , 3 0 ] , f ) ug . p o i n t _ d a t a . s c a l a r s = te m p er a t ur e ug . p o i n t _ d a t a . s c a l a r s . name = t em p e r at u r e # Some v e c t o r s . v e l o c i t y = array ( [ [ 0 , 0 , 0 ] , [1 ,0 ,0] , [0 ,1 ,0] , [ 0 , 0 , 1 ] ] , f ) ug . p o i n t _ d a t a . v e c t o r s = v e l o c i t y ug . p o i n t _ d a t a . v e c t o r s . name = v e l o c i t y
Mayavi
Introduction mlab VTK and TVTK Advanced features Introduction to VTK and TVTK TVTK datasets from numpy arrays
UnstructuredGrid
Mayavi
Introduction mlab VTK and TVTK Advanced features Embedding mayavi/mlab in traits UIs The mayavi library
Outline
1
Introduction mlab VTK and TVTK Advanced features Embedding mayavi/mlab in traits UIs The mayavi library
General approach
Create class deriving from HasTraits . . . Add an Instance of MlabSceneModel trait: lets call it scene mlab is available as self.scene.mlab Use a SceneEditor as an editor for the MlabSceneModel trait Do the needful in trait handlers Thats it!
Mayavi
Introduction mlab VTK and TVTK Advanced features Embedding mayavi/mlab in traits UIs The mayavi library
An example
from enthought . t v t k . pyface . s c e n e _ e d i t o r import SceneEditor from enthought . mayavi . t o o l s . mlab_scene_model import MlabSceneMode from enthought . mayavi . core . u i . mayavi_scene import MayaviScene class A c to r V i e w er ( H a s T r a i t s ) : scene = I n s t a n c e ( MlabSceneModel , ( ) ) view = View ( Item ( name= scene , e d i t o r = SceneEditor ( scene_class=MayaviScene ) , show_label=False , r e s i z a b l e =True , w i d t h =500 , h e i g h t =500) , r e s i z a b l e =True ) def _ _ i n i t _ _ ( s e l f , t r a i t s ) : H a s T r a i t s . _ _ i n i t _ _ ( s e l f , t r a i t s ) s e l f . generate_data ( ) def generate_data ( s e l f ) : X , Y = mgrid [ 2:2:100 j , 2:2:100 j ] R = 10 s q r t (X 2 + Y 2) Z = s i n (R ) / R s e l f . scene . mlab . s u r f ( X , Y , Z , colormap= g i s t _ e a r t h ) a = A c t o r V i e we r ( ) ; a . c o n f i g u r e _ t r a i t s ( )
Prabhu Ramachandran, Gal Varoquaux Mayavi
Introduction mlab VTK and TVTK Advanced features Embedding mayavi/mlab in traits UIs The mayavi library
Exercise
Take the previous example sin(xyz )/xyz and allow a user to specify a function that is evaluated Hints: Use an Expression trait
Eval the expression given in a namespace with your x, y, z arrays along with numpy/scipy. Use mlab_source to update the scalars
Mayavi
Introduction mlab VTK and TVTK Advanced features Embedding mayavi/mlab in traits UIs The mayavi library
Outline
1
Introduction mlab VTK and TVTK Advanced features Embedding mayavi/mlab in traits UIs The mayavi library
Mayavi
Introduction mlab VTK and TVTK Advanced features Embedding mayavi/mlab in traits UIs The mayavi library
Containership relationship
Engine contains: list of Scene Scene contains: list of Source Source contains: list of Filter and/or ModuleManager ModuleManager contains: list of Module Module contains: list of Component
Mayavi
Introduction mlab VTK and TVTK Advanced features Embedding mayavi/mlab in traits UIs The mayavi library
Class hierarchy
Mayavi
Introduction mlab VTK and TVTK Advanced features Embedding mayavi/mlab in traits UIs The mayavi library
Class hierarchy
Mayavi
Introduction mlab VTK and TVTK Advanced features Embedding mayavi/mlab in traits UIs The mayavi library
Drag and drop The mayavi instance >>> mayavi . new_scene ( ) # Create a new scene >>> mayavi . s a v e _ v i s u a l i z a t i o n ( f o o . mv2 )
mayavi.engine:
>>> >>> >>> >>> e = mayavi . engine # Get t h e MayaVi engine . e . scenes [ 0 ] # f i r s t scene i n mayavi . e . scenes [ 0 ] . c h i l d r e n [ 0 ] # f i r s t scene s f i r s t source ( v t k f i l e )
Mayavi
Introduction mlab VTK and TVTK Advanced features Embedding mayavi/mlab in traits UIs The mayavi library
Scripting . . .
Mayavi
Introduction mlab VTK and TVTK Advanced features Embedding mayavi/mlab in traits UIs The mayavi library
Mayavi
Introduction mlab VTK and TVTK Advanced features Embedding mayavi/mlab in traits UIs The mayavi library
Mayavi
Introduction mlab VTK and TVTK Advanced features Embedding mayavi/mlab in traits UIs The mayavi library
Using the GenericModule we can easily create a custom module out of existing ones. Here we recreate the ScalarCutPlane module
Mayavi
Introduction mlab VTK and TVTK Advanced features Embedding mayavi/mlab in traits UIs The mayavi library
A ScalarCutPlaneModule
from enthought . mayavi . f i l t e r s . a p i import O p t i o n a l , WarpScalar , . . . from enthought . mayavi . components . c o n t o u r import Contour # i m p o r t Contour , A c t o r from components . from enthought . mayavi . modules . a p i import GenericModule cp = CutPlane ( ) w = WarpScalar ( ) warper = O p t i o n a l ( f i l t e r =w, l a b e l _ t e x t = Enable warping , enabled=False ) c = Contour ( ) c t r = O p t i o n a l ( f i l t e r =c , l a b e l _ t e x t = Enable c o n t o u r s , enabled=False ) p = PolyDataNormals ( name= Normals ) normals = O p t i o n a l ( f i l t e r =p , l a b e l _ t e x t = Compute normals , enabled=False ) a = Actor ( ) components = [ cp , warper , c t r , normals , a ] m = GenericModule ( name= ScalarCutPlane , components=components , c o n t o u r =c , a c t o r =a )
Prabhu Ramachandran, Gal Varoquaux Mayavi
Introduction mlab VTK and TVTK Advanced features Embedding mayavi/mlab in traits UIs The mayavi library
Additional features
Mayavi
Introduction mlab VTK and TVTK Advanced features Embedding mayavi/mlab in traits UIs The mayavi library
Offscreen rendering
Offscreen rendering: mayavi2 -x script.py -o Requires a sufciently recent VTK release (5.2 or CVS) No UI is shown May ignore any window shown Normal mayavi and mlab scripts ought to work
Mayavi
Introduction mlab VTK and TVTK Advanced features Embedding mayavi/mlab in traits UIs The mayavi library
Animation scripts
from os . path import j o i n , e x i s t s from os import mkdir def make_movie ( d i r e c t o r y = / tmp / movie , n_step = 3 6 ) : i f not e x i s t s ( d i r e c t o r y ) : mkdir ( d i r e c t o r y ) scene = mayavi . engine . c u r r e n t _ s c e n e . scene camera = scene . camera da = 3 6 0 . 0 / n_step f o r i i n range ( n_step ) : camera . azimuth ( da ) scene . r e n d e r ( ) scene . save ( j o i n ( d i r e c t o r y , anim%02d . png %i ) ) i f __name__ == __main__ : make_movie ( )
Introduction mlab VTK and TVTK Advanced features Embedding mayavi/mlab in traits UIs The mayavi library
Envisage plugins
Mayavi
Introduction mlab VTK and TVTK Advanced features Embedding mayavi/mlab in traits UIs The mayavi library
Customization
Global: site_mayavi.py; placed on sys.path Local: ~/.mayavi2/user_mayavi.py: this directory is placed in sys.path Two things can be done in this le:
Register new sources/modules/lters with mayavis registry:
See examples/mayavi/user_mayavi.py
Mayavi