Scientific Data Analysis and Visualization With Python, VTK, and ParaView
Scientific Data Analysis and Visualization With Python, VTK, and ParaView
https://www.youtube.com/watch?v=8ugmkKaYKxM
F
AbstractVTK and ParaView are leading software packages for data analysis
and visualization. Since their early years, Python has played an important role
in each package. In many use cases, VTK and ParaView serve as modules
used by Python applications. In other use cases, Python modules are used to
generate visualization components within VTK. In this paper, we provide an
overview of Python integration in VTK and ParaView and give some concrete
examples of usage. We also provide a roadmap for additional Python integration
in VTK and ParaView in the future.
Index Termsdata analysis, scientific visualization, VTK, ParaView
Introduction
SCIENTIFIC DATA ANALYSIS AND VISUALIZATION WITH PYTHON, VTK, AND PARAVIEW
33
Python classes for VTK classes and special types are generated using a shared lex/yacc-based parser tailored for VTK
programming conventions and custom code generation utilities
for Python wrapping. VTK is organized into a number of C++
modules. When built with shared libraries enabled, a library
containing C++ classes is generated at build time for each C++
module. Each Python-wrapped source file is likewise compiled
into a shared library corresponding to the C++ module. All
wrapped VTK C++ modules are provided in a single vtk
Python package.
VTK Usage in Python
For convenience, an executable named vtkpython is provided in VTK binaries. This is the standard Python executable
with environment variables set to make it simple to import
the vtk package. It is also possible to use VTK in the
same python executable from the Python installation against
which VTK was built by prepending the location of VTKs
shared libraries and the location of the parent directory of the
import vtk
For VTK classes that have operators <, <=, ==, >=, > defined,
equivalent Python operators are provided.
Some functions in VTK return information via parameters
passed by reference. For example, in the following code
block, the parameter t is a return parameter from the member
function IntersectWithLine.
double t, x[3]
plane->IntersectWithLine(point1, point2, t, x);
Class and function documentation is processed by the wrapping infrastructure to make it available via Pythons built-in
help system.
>>> help(vtk.vtkSphereSource)
34
SetCenter(...)
V.SetCenter(float, float, float)
C++: void SetCenter(double, double, double)
V.SetCenter((float, float, float))
C++: void SetCenter(double a[3])
In
this
code,
ds
is
an
instance
of
a
dataset_adapter.PolyData
that
wraps
the
vtkPolyData output of the vtkXMLPolyDataReader.
Point and cell arrays are available in member variables
PointData and CellData, respectively, that provide the
dictionary interface.
>>> ds.PointData.keys()
[pressure]
>>> pressure = ds.PointData[pressure]
SCIENTIFIC DATA ANALYSIS AND VISUALIZATION WITH PYTHON, VTK, AND PARAVIEW
35
import sys
import vtk
from PyQt4 import QtCore, QtGui
from vtk.qt4.QVTKRenderWindowInteractor \
import QVTKRenderWindowInteractor
class MainWindow(QtGui.QMainWindow):
def __init__(self, parent = None):
QtGui.QMainWindow.__init__(self, parent)
self.frame = QtGui.QFrame()
layout = QtGui.QVBoxLayout()
self.vtkWidget = \
QVTKRenderWindowInteractor(self.frame)
layout.addWidget(self.vtkWidget)
self.renderer = vtk.vtkRenderer()
rw = self.vtkWidget.GetRenderWindow()
rw.AddRenderer(self.renderer)
self.interactor = rw.GetInteractor()
cylinder = vtk.vtkCylinderSource()
mapper = vtk.vtkPolyDataMapper()
mapper.SetInputConnection( \
cylinder.GetOutputPort())
actor = vtk.vtkActor()
actor.SetMapper(mapper)
self.renderer.AddActor(actor)
self.renderer.ResetCamera()
self.frame.setLayout(layout)
self.setCentralWidget(self.frame)
self.show()
self.interactor.Initialize()
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
window = MainWindow()
sys.exit(app.exec_())
36
ParaView is a suite of scalable parallel visualization executables that use VTK to read data, process it, and create
visualizations. One of the executables includes a graphical user
interface (GUI) to make it possible to create visualizations
without programming (when ParaView is mentioned in this
section, it is the executable with a GUI unless otherwise
specified). Data processing in ParaView follows the same dataflow paradigm that VTK follows. In ParaView, sources and
filters are chained together in a Pipeline Browser as shown
in Figure 1. Visualization controls are modified with user
interaction widgets provided by Qt.
SCIENTIFIC DATA ANALYSIS AND VISUALIZATION WITH PYTHON, VTK, AND PARAVIEW
37
ipts = inputs[0].Points
normals = inputs[0].PointData[Normals]
output.Points = ipts + normals
38
ParaViews Python Calculator filter is a light-weight alternative to the Programmable Filter used to compute
additional point or cell arrays using NumPy or the
numpy_interface.algorithms module. The following
expression computes the areas of polygons in a surface mesh:
algs.area(inputs[0])
Note that the numpy_interface.algorithms is imported with the name algs in the Python environment in
which the expression is evaluated. In the Python Calculator,
the property Array Association, which indicates whether the
output array should be a point or cell array, must be set to
Cell Data because one area value is produced per cell. Note
that like the Programmable Filter, the inputs are wrapped
with the vtk.numpy_interface.dataset_adapter
module functions and stored in an inputs list.
Python Annotation
Python View
It is often desirable to annotate visualizations with numerical values taken either directly from the data set or
computed from the data. The Python Annotation filter in
ParaView provides this capability in a convenient way. The
filter takes a Python expression that is evaluated when the
filter is executed and the value returned by the expression
is displayed in the render view. Importantly, these annotations can come from data analysis results from NumPy
or numpy_interface.algorithms. Figure 2 shows an
example using the Python Annotation filter.
ax = figure.add_subplot(1,1,1)
ax.minorticks_on()
ax.set_title(Plot title)
ax.set_xlabel(X label)
ax.set_ylabel(Y label)
# Process only the first visible object in the
# pipeline browser
do = view.GetVisibleDataObjectForRendering(0)
dens = do.GetPointData().GetArray(Density)
# Convert VTK data array to numpy array
from paraview.numpy_support import vtk_to_numpy
ax.hist(vtk_to_numpy(dens), bins=10)
return python_view.figure_to_image(figure)
SCIENTIFIC DATA ANALYSIS AND VISUALIZATION WITH PYTHON, VTK, AND PARAVIEW
ParaViewWeb
ParaViewWeb is a framework for remote VTK and ParaView processing and visualization via a web browser. The
framework on the server side is based on the Autobahn,
Twisted, Six, and ZopeInterface Python libraries. On the client
side, ParaViewWeb provides a set of JavaScript libraries that
use WebGL, JQuery, and Autobahn.js. Images are typically
generated on the server and sent to the client for display, but
if the visualized geometry is small enough, geometry can be
sent to the client and rendered with WebGL.
A nice feature of ParaViewWeb is that the server component
can be launched with pvpython. No separate web server
is needed. For example, on Linux, the following command
launches the ParaViewWeb server from the ParaView installation directory
./bin/pvpython
\
lib/paraview-4.1/site-packages/paraview/\
web/pv_web_visualizer.py --port 8080
\
--content ./share/paraview-4.1/www \
--data-dir /path-to-share/ &
\
39
Python has been integrated into VTK and ParaView for many
years. The integration continues to mature and expand as
Python is used in an increasing number of ways in both
software packages. As Python continues to grow in popularity
among the scientific community, so too does the need for
providing easy-to-use Pythonic interfaces to scientific visualization tools. As demonstrated in this paper, VTK and
ParaView are well-positioned to continue adapting to the future
needs of scientific Python programmers.
Future Work
R EFERENCES
Fig. 3: The ParaViewWeb Visualizer application web interface.
[Aya14]
[Aya15]
[Gev14]
[Kit15]
[Pvw15]
[PyQt15]
[PyS15]
[Qua13]
[Sch04]
[Sni99]
[VTK15]
[Wik15]