ParaView/Users Guide/Python Programmable Filter: Difference between revisions

From KitwarePublic
Jump to navigationJump to search
(Replaced content with "{{ParaView/Template/DeprecatedUsersGuide}}")
 
(14 intermediate revisions by 2 users not shown)
Line 1: Line 1:
== Introduction ==
{{ParaView/Template/DeprecatedUsersGuide}}
 
[[Image:ParaView_UG_ProgrammableFilter.png|400px|left]]
 
The Programmable Filter is a ParaView filter that processes one or more input datasets based on a Python script provided by the user. The parameters of the filter include the output data type, the script and a toggle that controls whether the input arrays are copied to the output. In this document, we introduce the use of the Programmable Filter and give a summary of the API available to the user.
 
Note that the Programmable Filter depends on Python. All ParaView binaries distributed by Kitware are built with Python enabled. If you have built ParaView yourself, you have to make sure that PARAVIEW_ENABLE_PYTHON is turned on when configuring the ParaView build.
 
Since the entire VTK API as well as any module that can be imported through Python is available through this filter, we can only skim the surface of what can be accomplished with this filter here. If you are not familiar with Python, we recommend first taking a look at one of the introductory guides such as the [http://docs.python.org/tutorial/ official Python Tutorial]. Also, if you are going to do any programming beyond the very basic stuff, we recommend reading up on the VTK API. [http://www.vtk.org The VTK website] has links to VTK books and online documentation. For reference, you may need to look at the [http://www.vtk.org/doc/release/5.6/html/ VTK class documentation]. There is also more information about the Programmable Filter and some good recipes on the ParaView Wiki ([[Python_Programmable_Filter]]).
 
== Basic Use ==
 
Requirements:
# You are applying Programmable Filter to a "simple" dataset and not a composite dataset such as multi-block or AMR.
# You have NumPy installed.
 
The most basic reason to use the Programmable Filter is to add a new array by possible deriving it from arrays in the input. This can be achieved by using the Python Calculator. One reason to use the Programmable Filter instead may be that the calculation is more involved and trying to do it in one expression may be difficult. Another reason may be that you need access to a program flow construct such as if or for. In any case. the Programmable Filter can be used to do everything the Calculator does and more.
 
'''Note:''' Since what we describe here builds on some of the concepts introduced in the [[ParaView/Users_Guide/Python_Calculator | Python Calculator section]], please read it first if you are not familiar with the Calculator.
 
If you leave the "Output Dataset Type" parameter in the default setting of "Same as Input", the Programmable Filter will copy the topology and geometry of the input to the output before calling your Python script. Therefore, if you Apply the filter without filling the script, you should see a copy of the input without any of its arrays in the output. If you also check the "Copy Arrays" option, the output will have all of the input arrays. This behavior allows you to focus on creating new arrays without worrying about the mesh.
 
Let's try an example. Create a Sphere source and then apply the Programmable Filter. Use the following script.
 
<source lang="python">
normals = inputs[0].PointData['Normals']
output.PointData.append(normals[:,0], "Normals_x")
</source>
 
This should create a sphere with on array called "Normals_x". There a few things to note here:
 
* You cannot refer to arrays directly by name as in the Python Calculator. You need to access arrays using the .PointData and .CellData qualifiers.
* Unlike the Python Calculator, you have to explicitly add an array to the output using the append function. Note that this function takes the name of the array as the second argument.
 
You can use any of the functions available in the Calculator in the Programmable Filter. For example, the following code creates two new arrays and adds them to the output.
 
<source lang="python">
normals = inputs[0].PointData['Normals']
output.PointData.append(sin(normals[:,0]), "sin of Normals_x")
output.PointData.append(normals[:,1] + 1, "Normals_y + 1")
</source>
 
== Intermediate Use ==
 
The examples above demonstrate how the Programmable Filter can be used as an advanced Python Calculator. However, the full power of the Programmable Filter can only be harnessed by using the VTK API. Let's start with a simple example. Create a Sphere source and apply the Programmable Filter with the following script.
 
<source lang="python">
input = inputs[0]
 
newPoints = vtk.vtkPoints()
numPoints = input.GetNumberOfPoints()
for i in range(numPoints):
    x, y, z = input.GetPoint(i)
    newPoints.InsertPoint(i, x, y, 1 + z*0.3)
 
output.SetPoints(newPoints)
</source>
 
This requires some explanation. We start with creating a new instance of vtkPoints.
 
<source lang="python">
newPoints = vtk.vtkPoints()
</source>
 
vtkPoints is a data structure that VTK uses to store the coordinates of points. Next, we loop over all points of the input and insert a new point in the output with coordinates (x, y, 1+z*0.3)
 
<source lang="python">
for i in range(numPoints):
    x, y, z = input.GetPoint(i)
    newPoints.InsertPoint(i, x, y, 1 + z*0.3)
</source>
 
Finally, we replace the output points with the new points we created using the following.
 
<source lang="python">
output.SetPoints(newPoints)
</source>
 
'''Note:''' Python is an interpreted language and Python scripts do not execute as efficiently as compiled C++ code. Therefore, using a for loop that iterates over all points or cells may be a significant bottleneck when processing large datasets.
 
The NumPy and VTK APIs can be mixed to achieve good performance. Even though this may seem a bit complicated at first, it can be used with great effect. For example, the example above can be rewritten as follows.
 
<source lang="python">
from paraview.vtk.dataset_adapter import numpyTovtkDataArray
 
input = inputs[0]
 
newPoints = vtk.vtkPoints()
 
zs = 1 + input.Points[:,2]*0.3
coords = hstack([input.Points[:,0:2],zs])
output.PointData.append(coords, 'coords')
newPoints.SetData(numpyTovtkDataArray(coords))
output.SetPoints(newPoints)
</source>
 
== Advanced (but read it anyway) ==

Latest revision as of 18:37, 14 January 2015

PAGE DELETED
The Users Guide has been moved from the Wiki to The ParaView Guide. Please use the history if you want to access the old version of this document.