3d data visualization with mayavi and...
Post on 20-Apr-2020
2 Views
Preview:
TRANSCRIPT
3D Data visualization with Mayaviand TVTK
Prabhu Ramachandran
Department of Aerospace EngineeringIIT Bombay
Advanced tutorials at SciPy09Caltech, Pasadena
Aug. 18, 2009
Prabhu Ramachandran (IIT Bombay) Mayavi2 tutorial 1 / 73
Objectives
At the end of this session you will be able to:1 Use mlab effectively to visualize numpy array data
of various kinds2 Apply some of mayavi’s advanced features3 Embed mayavi visualizations in your dialogs4 Create TVTK datasets for more effective
visualization (if time permits)
Outline
1 Quick introduction to Mayavi
2 mlab
3 Embedding mayavi
4 Creating and working with datasets
Prabhu Ramachandran (IIT Bombay) Mayavi2 tutorial 3 / 73
Outline
1 Quick introduction to Mayavi
2 mlab
3 Embedding mayavi
4 Creating and working with datasets
Prabhu Ramachandran (IIT Bombay) Mayavi2 tutorial 4 / 73
Who are we?
Prabhu Ramachandran Creator and lead, 2001 –Gaël Varoquaux Mlab, documentation, usability,
2007 –Enthought Inc. ETS, Hosting, support, sprints,
initial funding, distribution
Prabhu Ramachandran (IIT Bombay) Mayavi2 tutorial 5 / 73
History
Mayavi-1.x: 2001TVTK: 2004, EnthoughtMayavi2: 2005, Enthought, IITB2008: Mayavi sprint
Prabhu Ramachandran (IIT Bombay) Mayavi2 tutorial 6 / 73
Overview of features
mlab: interactive work
from enthought . mayavi import mlabfrom numpy import ogr idx , y , z = ogr id [ −5:5:64 j ,
−5:5:64 j ,−5:5:64 j ]
mlab . contour3d ( x∗x∗0.5 + y∗y +z∗z∗2)
mlab . show ( )
Live in your dialogs
Mayavi in applications
Exploring the documentation
Other features
Easy customizationOffscreen animationsAutomatic script generationPowerful command line options
Summary
http://code.enthought.com/projects/mayavi
Uses VTK (www.vtk.org)BSD licenseLinux, win32 and Mac OS XHighly scriptableEmbed in Traits UIs (wxPython and PyQt4)Envisage PluginsDebian/Ubuntu/FedoraPythonic
10
Outline
1 Quick introduction to Mayavi
2 mlab
3 Embedding mayavi
4 Creating and working with datasets
Prabhu Ramachandran (IIT Bombay) Mayavi2 tutorial 15 / 73
Overview
SimpleConvenientFull-featured
Prabhu Ramachandran (IIT Bombay) Mayavi2 tutorial 16 / 73
Getting started
Vanilla:$ ipy thon −wthread
with Pylab:$ ipy thon −pylab −wthread
Using mlab:
>>> from enthought . mayavi import mlab
Try these:
>>> mlab . tes t_ <TAB>>>> mlab . tes t_contour3d ( )>>> mlab . tes t_contour3d ??
Exploring the view
MouseKeyboardToolbarMayavi icon
15
mlab plotting functions
0D data
>>> from numpy import ∗>>> t = l inspace (0 , 2∗pi , 50)>>> u = cos ( t )∗ p i>>> x , y , z = s in ( u ) , cos ( u ) , s in ( t )
>>> mlab.points3d(x, y, z)
Changing how things look
Clearing the view>>> mlab.clf()
IPython is your friend!>>> mlab.points3d?
Extra argument: ScalarsKeyword argumentsUI
>>> mlab . po in ts3d ( x , y , z , t ,scale_mode= ’ none ’ )
Changing how things look
Clearing the view>>> mlab.clf()
IPython is your friend!>>> mlab.points3d?
Extra argument: ScalarsKeyword argumentsUI
>>> mlab . po in ts3d ( x , y , z , t ,scale_mode= ’ none ’ )
Changing how things look
Clearing the view>>> mlab.clf()
IPython is your friend!>>> mlab.points3d?
Extra argument: ScalarsKeyword argumentsUI
>>> mlab . po in ts3d ( x , y , z , t ,scale_mode= ’ none ’ )
1D data
>>> mlab.plot3d(x, y, z, t )
Plots lines between the points
2D data
>>> x = mgrid [ −3:3:100 j , −3:3:100 j ]>>> z = s in ( x∗x + y∗y )
>>> mlab.surf(x, y, z)
Assumes the points are rectilinear
2D data: mlab.mesh
>>> mlab.mesh(x, y, z)
Points needn’t be regular
>>> phi , the ta = numpy . mgrid [ 0 : p i :20 j ,. . . 0:2∗ p i :20 j ]>>> x = s in ( ph i )∗ cos ( the ta )>>> y = s in ( ph i )∗ s in ( the ta )>>> z = cos ( ph i )>>> mlab . mesh( x , y , z ,. . . r ep resen ta t i on = ’ wireframe ’ )
3D data
>>> x , y , z = ogr id [ −5:5:64 j ,. . . −5:5:64 j ,. . . −5:5:64 j ]>>> mlab . contour3d ( x∗x∗0.5 + y∗y +
z∗z∗2)
3D vector data: mlab.quiver3d
>>> mlab . tes t_qu ive r3d ( )
obj = mlab.quiver3d(x, y, z, u, v, w)
3D vector data: mlab.flow
>>> x , y , z = mgrid [ −2:3 , −2:3 , −2:3]>>> r = s q r t ( x∗∗2 + y∗∗2 + z∗∗4)>>> u = y∗ s in ( r ) / ( r +0.001)>>> v = −x∗ s in ( r ) / ( r +0.001)>>> w = ze ros_ l i ke ( z )>>> ob j = mlab . f low ( x , y , z , u , v , w,
seedtype= ’ plane ’ )>>> ob j . s t ream_tracer . i n t e g r a t o r _ t y p e = \
’ runge_kutta45 ’
35
Exercise: Lorenz equationdxdt
= s(y − x)
dydt
= rx − y − xz
dzdt
= xy − bz
Let s = 10, r = 28, b = 8./3.
Region of interestx , y , z = mgrid [ −50:50:20 j , −50:50:20 j ,
−10:60:20 j ]
Use mlab.quiver3d
Solutiondef l o renz ( x , y , z , s =10. , r =28. , b = 8 . / 3 . ) :
u = s ∗ ( y−x )v = r ∗x −y − x∗zw = x∗y − b∗zreturn u , v , w
x , y , z = mgrid [ −50:50:20 j , −50:50:20 j ,−10:60:20 j ]
u , v , w = lo renz ( x , y , z )
# Your p l o t here#mlab . show ( )
45
Issues and solutions
Basic visualization: not very usefulTweak parameters:mask_points, scale_factorExplore parameters on UImlab.flow is a lot better!
Good visualization involves work
Other utility functions
gcf: get current figuresavefig, figureaxes, outlinetitle , xlabel , ylabel , zlabel
colorbar, scalarbar, vectorbarshow: Standalone mlab scriptsOthers, see UG
Other utility functions
gcf: get current figuresavefig, figureaxes, outlinetitle , xlabel , ylabel , zlabel
colorbar, scalarbar, vectorbarshow: Standalone mlab scriptsOthers, see UG
Other utility functions
gcf: get current figuresavefig, figureaxes, outlinetitle , xlabel , ylabel , zlabel
colorbar, scalarbar, vectorbarshow: Standalone mlab scriptsOthers, see UG
Other utility functions
gcf: get current figuresavefig, figureaxes, outlinetitle , xlabel , ylabel , zlabel
colorbar, scalarbar, vectorbarshow: Standalone mlab scriptsOthers, see UG
Other utility functions
gcf: get current figuresavefig, figureaxes, outlinetitle , xlabel , ylabel , zlabel
colorbar, scalarbar, vectorbarshow: Standalone mlab scriptsOthers, see UG
Other utility functions
gcf: get current figuresavefig, figureaxes, outlinetitle , xlabel , ylabel , zlabel
colorbar, scalarbar, vectorbarshow: Standalone mlab scriptsOthers, see UG
Other utility functions
gcf: get current figuresavefig, figureaxes, outlinetitle , xlabel , ylabel , zlabel
colorbar, scalarbar, vectorbarshow: Standalone mlab scriptsOthers, see UG
Can we do more?
Yes!
quiver3d ( x , y , z ,u , v , w,sca l e_ fac to r =0.01 ,mask_points =5)
Looking inside
The pipeline
55
Lookup tables
List of Modules
TVTK Scene
Filter
Source
Mayavi Engine
ModuleManager
Changing the pipeline
On UIRight click on nodedrag drop
Script
Or use mlab.pipelineExample: mlab.pipeline. outline ()obj.remove()
Exercise
>>> mlab . tes t_qu ive r3d ( )
Hide vectors, add a Vector Cut Plane
>>> mlab . t e s t _ f l o w ( )
Add a Vector Cut Plane
Exercise
>>> mlab . tes t_qu ive r3d ( )
Hide vectors, add a Vector Cut Plane
>>> mlab . t e s t _ f l o w ( )
Add a Vector Cut Plane
Surprised?
So what is the problem?
Points?
Curve?
Surface?
Interior of sphere?
Datasets
Quiver v/s Flow
Get back to this later!70
Recap
mlab gets you startedPipeline and data flowDatasets are important
Changing the pipeline
On UIRight click on nodedrag drop
Script
Or use mlab.pipelineExample: mlab.pipeline. outline ()obj.remove()
mlab and Mayavi2?
mlab is just a thin layer over the Mayavi OO APImlab commands return mayavi objects
Exercise
1 Start with flow for the Lorenz system2 Now extract the vector norm (use a filter)3 Plot iso-contours of this4 Figure out how to do this from the UI and
mlab.pipeline
80
Prabhu Ramachandran (IIT Bombay) Mayavi2 tutorial 49 / 73
So how do you make a fancier script?
Use script recordingDemo
So how do you make a fancier script?
Use script recordingDemo
Animating data
>>> s = mlab . f low ( x , y , z , u , v , w)>>> s . mlab_source . u = u∗z
mlab_source.set: multiple attributesIf you change the shape of the arrays use the resetmethod
Setting the view
>>> pr in t mlab . view ( )>>> mlab . view ( azimuth=None ,
e l ev a t i on =None ,d is tance=None ,f o c a l p o i n t =None )
95
Outline
1 Quick introduction to Mayavi
2 mlab
3 Embedding mayavi
4 Creating and working with datasets
Prabhu Ramachandran (IIT Bombay) Mayavi2 tutorial 53 / 73
General approach
Embed Mayavi into a dialog boxUse traits to wire up everythingFull power of mayavi at your disposal
Simple example
105
Exercise: Lorenz trajectory
Use the provided skeleton script1 Create a simple UI to show a trajectory2 Create sliders to change the position of the initial
condition3 Create a UI element to change the integration time
110
Prabhu Ramachandran (IIT Bombay) Mayavi2 tutorial 56 / 73
Outline
1 Quick introduction to Mayavi
2 mlab
3 Embedding mayavi
4 Creating and working with datasets
Prabhu Ramachandran (IIT Bombay) Mayavi2 tutorial 57 / 73
Motivation
Datasets are fundamental to doing visualizationcorrectly
Motivational problemAtmospheric data of temperature over the surface ofthe earth.Let temperature (T ) vary linearly with height (z):
T = 288.15 − 6.5z
Simple solution
l a t = l inspace (−89, 89 , 37)lon = l inspace (0 , 360 , 37)z = l inspace (0 , 100 , 11)
x , y , z = mgrid [0 :360 :37 j , −89:89:37 j ,0:100:11 j ]
t = 288.15 − 6.5∗zmlab . contour3d ( x , y , z , t )mlab . o u t l i n e ( )mlab . co lo rba r ( )
Simple solution
l a t = l inspace (−89, 89 , 37)lon = l inspace (0 , 360 , 37)z = l inspace (0 , 100 , 11)
x , y , z = mgrid [0 :360 :37 j , −89:89:37 j ,0:100:11 j ]
t = 288.15 − 6.5∗zmlab . contour3d ( x , y , z , t )mlab . o u t l i n e ( )mlab . co lo rba r ( )
What happens underneath?
P = mlab . p i p e l i n esrc = P. s c a l a r _ f i e l d ( x , y , z , t )i so = P. iso_sur face ( src )# Try t h i s .pr in t src
The underlying datasetfrom enthought . t v t k . ap i import t v t ko r i g = (0 , −90, 0)spacing = (10 , 5 , 10)dims = (37 , 37 , 11)i d = t v t k . ImageData ( o r i g i n =or ig ,
spacing=spacing ,dimensions=dims )
i d . po in t_data . sca la rs = t . T . f l a t t e n ( )i d . po in t_data . sca la rs . name = ’T ’# View i t .src = P. add_dataset ( i d )i so = P. iso_sur face ( src )
The general ideaSpecify the points (explicitly or implicitly)Specify the connectivity between the points(explicit/implicit)The connectivity lets you build “cells” that breakthe space into piecesSpecify “attribute” data at the points or cells
Cell data
10 20
3040
Points Rectangular cell
Triangular cells Point data
2010
Types of datasets
Implicit topology (structured):Image data (structured points)Rectilinear gridsStructured grids
Explicit topology (unstructured):Polygonal data (surfaces)Unstructured grids
Implicit versus explicit topology
Implicit topology associated with points:The X co-ordinate increases first, Y next and Z last
Easiest example: a rectangular meshNon-rectangular mesh certainly possible
Prabhu Ramachandran (IIT Bombay) Mayavi2 tutorial 64 / 73
On a sphere?lon , l a t , h t = x∗ p i /180 , (90+y )∗ p i /180 , zr = (1+0.005∗ ht )tmp = r ∗ s in ( l a t )# Poin ts on the spherex , y , z = tmp∗cos ( lon ) , tmp∗ s in ( lon ) , r ∗cos ( l a t )p ts = empty ( x . shape + ( 3 , ) , dtype= f l o a t )p ts [ . . . , 0 ] = xpts [ . . . , 1 ] = ypts [ . . . , 2 ] = z
# Reorder the po in t s / sca la rs f o r VTKpts = pts . t ranspose (2 , 1 , 0 , 3 ) . copy ( )p ts . shape = pts . s i ze /3 , 3t = t . T . copy ( )
sg = t v t k . S t ruc tu redGr id ( )sg . dimensions = x . shapesg . po in t s = ptssg . po in t_data . sca la rs = t . r ave l ( )sg . po in t_data . sca la rs . name = ’T ’
P = mlab . p i p e l i n esrc = P. add_dataset ( sg )P . gr id_p lane ( src )P . i so_sur face ( src , contours =1)
mlab . show ( )
# Save a dataset to d isk .from enthought . t v t k . ap i import wr i te_da tawr i te_da ta ( dataset , fname )
# Open back the data .mlab . p i p e l i n e . open ( fname )
Try right clicking a node!
Unstructured grids
Explicit topology specificationSpecified via connectivity listsDifferent number of neighbors, different types ofcells
PolyDatafrom enthought . t v t k . ap i import t v t k# The po in t s i n 3D.po in t s = ar ray ( [ [ 0 , 0 , 0 ] , [ 1 , 0 , 0 ] , [ 0 , 1 , 0 ] , [ 0 , 0 , 1 ] ] , ’ f ’ )# Connec t i v i t y v ia i nd i ces to the po in t s .t r i a n g l e s = ar ray ( [ [ 0 , 1 , 3 ] , [ 0 , 3 , 2 ] , [ 1 , 2 , 3 ] , [ 0 , 2 , 1 ] ] )# Creat ing the data ob jec t .
mesh = t v t k . PolyData ( )mesh . po in t s = po in t s # the po in t smesh . polys = 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 / ve r t s use : mesh . l i n e s = l i n e s ; mesh . ve r t s = v e r t i c e s# Now create some po in t data .temperature = ar ray ( [ 1 0 , 20 ,20 , 30 ] , ’ f ’ )mesh . po in t_data . sca la rs = temperaturemesh . po in t_data . sca la rs . name = ’ temperature ’
Prabhu Ramachandran (IIT Bombay) Mayavi2 tutorial 69 / 73
PolyDatafrom enthought . t v t k . ap i import t v t k# The po in t s i n 3D.po in t s = ar ray ( [ [ 0 , 0 , 0 ] , [ 1 , 0 , 0 ] , [ 0 , 1 , 0 ] , [ 0 , 0 , 1 ] ] , ’ f ’ )# Connec t i v i t y v ia i nd i ces to the po in t s .t r i a n g l e s = ar ray ( [ [ 0 , 1 , 3 ] , [ 0 , 3 , 2 ] , [ 1 , 2 , 3 ] , [ 0 , 2 , 1 ] ] )# Creat ing the data ob jec t .
mesh = t v t k . PolyData ( )mesh . po in t s = po in t s # the po in t smesh . polys = 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 / ve r t s use : mesh . l i n e s = l i n e s ; mesh . ve r t s = v e r t i c e s# Now create some po in t data .temperature = ar ray ( [ 1 0 , 20 ,20 , 30 ] , ’ f ’ )mesh . po in t_data . sca la rs = temperaturemesh . po in t_data . sca la rs . name = ’ temperature ’
Prabhu Ramachandran (IIT Bombay) Mayavi2 tutorial 69 / 73
Summary
125
Advanced features
Command line arguments, timeseries,scripting
A demo with files
$ mayavi2 −h
$ mayavi2 −d boundary . xml −m Surface \−d f l u i d _ 0 . xml −m Surface
Thank you!
top related