Python Filters

From VisItusers.org

Jump to: navigation, search

Contents

[edit] Python Filters Intro

Starting with version 2.0 VisIt includes a new Python Filter infrastructure that provides users with the ability to write python scripts to quickly implement new Expressions & Queries. These scripts access python wrapped versions of the vtk objects VisIt's pipeline uses internally to represent and manipulate datasets. This page provides a quick intro this new functionality.

[edit] Python Expression Tutorial

[edit] Create a new expression filter via the 'Expressions' Window

  • Open rect2d.silo
  • Open the 'Expressions' window
  • Create a new expression 'd_wave'.
  • Click on the 'Python Expression Editor' Tab & you should see the following:
  • Like regular VisIt expressions, python filter expressions operate on database variables and other expressions. We want to use the field 'd' as an input to our new expression. Add 'd' to the 'Arguments' text box.
  • Copy and paste the example expression script below into the 'Python Expression Script' text box and click 'Apply'. This expression perturbs an input scalar (in our case 'd') by a simple wave pattern.
from math import sin, pi
class MyExpression(SimplePythonExpression):
    def __init__(self):
        SimplePythonExpression.__init__(self)
        self.name = "MyExpression"
        self.description = "Perturb input scalar by simple wave pattern."
        self.output_is_point_var  = False
        self.output_dimension = 1
    def modify_contract(self,contract):
        pass
    def derive_variable(self,ds_in,domain_id):
        # ds_in is a vtk dataset, we want
        # to create and return a new vtkDataArray
        # that contains a simple sine wave pattern
        ds_bounds = ds_in.GetBounds()
        x_ext = ds_bounds[1] - ds_bounds[0]
        y_ext = ds_bounds[3] - ds_bounds[2]
        z_ext = ds_bounds[5] - ds_bounds[4]
        cell_vals = ds_in.GetCellData().GetArray(self.input_var_names[0])
        ncells = ds_in.GetNumberOfCells()
        res = vtk.vtkFloatArray()
        res.SetNumberOfComponents(1)
        res.SetNumberOfTuples(ncells)
        for i in xrange(ncells):
            cell = ds_in.GetCell(i)
            bounds = cell.GetBounds()
            xv = bounds[0] + bounds[1] / 2.0
            yv = bounds[2] + bounds[3] / 2.0
            zv = bounds[4] + bounds[5] / 2.0
            val = .05*sin(xv*3*pi/x_ext) + .05*sin(yv * 3*pi / y_ext)
            if z_ext != 0:
                val+= .05*sin(zv * 3*pi / z_ext)
            val += cell_vals.GetTuple1(i)
            res.SetTuple1(i,val)
        return res
 
py_filter = MyExpression
  • Create a Pseudocolor plot of 'd_wave', add an Elevate Operator and click draw. Here is an example the output of 'd' vs the result of the new expression 'd_wave'
d d_wave


  • We want to reuse this script from VisIt's cli, so click 'Save Script' and save it to the file 'py_example_expression.py'.

Note: You can use the 'Load Script' button to load a filter script from a file or start with a simple template expression.

[edit] Example Script Breakdown

The meat of the new expression is in the 'derive_variable' function. It is called on each domain (or chunk) of your input dataset. The argument 'ds_in' is a python wrapped instance of vtkDataSet & 'domain_id' is an integer providing the id of the current domain.

Recall this script perturbs 'd' by simple wave pattern related, first we obtain the extents of the mesh:

ds_bounds = ds_in.GetBounds()
x_ext = ds_bounds[1] - ds_bounds[0]
y_ext = ds_bounds[3] - ds_bounds[2]
z_ext = ds_bounds[5] - ds_bounds[4]

Next we obtain the vtkDataArray containing the scalar values for the variable 'd':

cell_vals = ds_in.GetCellData().GetArray(self.input_var_names[0])

Notice we use the tuple 'self.input_var_names' to access the name of the variable we passed as an argument.

Construct an array to hold the results that has the same number cells as the input scalar:

ncells = ds_in.GetNumberOfCells()
res = vtk.vtkFloatArray()
res.SetNumberOfComponents(1)
res.SetNumberOfTuples(ncells)

Now scan over the cells and apply the wave pattern:

for i in xrange(ncells):
            cell = ds_in.GetCell(i)
            bounds = cell.GetBounds()
            xv = bounds[0] + bounds[1] / 2.0
            yv = bounds[2] + bounds[3] / 2.0
            zv = bounds[4] + bounds[5] / 2.0
            val = .05*sin(xv*3*pi/x_ext) + .05*sin(yv * 3*pi / y_ext)
            if z_ext != 0:
                val+= .05*sin(zv * 3*pi / z_ext)
            val += cell_vals.GetTuple1(i)
            res.SetTuple1(i,val)
        return res

Finally we return the result array to VisIt:

return res

Another important detail is the assignment of 'py_filter':

py_filter = MyExpression

This tells the Python Filter Infrastructure which class to instantiate when the expression is run.

[edit] Creating a filter via the cli

[edit] Python Query Tutorial

[edit] Creating a filter in the 'Query' Window

[edit] Creating a filter via the cli

For more example scripts see Python Filter Scripts

If you are looking for the old development page see: Python Filters Development

Personal tools