ParaView/Users Guide/Python Calculator: Difference between revisions

From KitwarePublic
Jump to navigationJump to search
Line 125: Line 125:
The following is a list of functions available in the Python Calculator. Note that this list is partial since most of the NumPy and SciPy functions can be used in the Python Calculator.
The following is a list of functions available in the Python Calculator. Note that this list is partial since most of the NumPy and SciPy functions can be used in the Python Calculator.


'''min (array) :''' Returns the minimum value of the array as a single value. Note that this function returns the minimum within a block for AMR and multi-block datasets, not across blocks/grids. Also, this returns the minimum within each process when running in parallel.
'''abs (x) :''' Returns the absolute value(s) of x.


max (array): Returns the maximum value of the array as a single value. Note that this function returns the maximum within a block for AMR and multi-block datasets, not across blocks/grids. Also, this returns the maximum within each process when running in parallel.
'''add (x, y):''' Returns the sum of two values. x and y can be single values or arrays. Same as x+y.


abs (array) : Returns the absolute values of an array.
'''area (dataset) :''' Returns the surface area of each cell in a mesh.


area (dataset) : Returns the surface area of each cell in a mesh.
'''aspect (dataset) :'''  Returns the aspect ratio of each cell in a mesh.


aspect (dataset) : Returns the aspect ratio of each cell in a mesh.
'''aspect_gamma (dataset) :''' Returns the aspect ratio gamma of each cell in a mesh.


aspect_gamma (dataset) : Returns the aspect ratio gamma of each cell in a mesh.
'''condition (dataset) :''' Returns the condition number of each cell in a mesh.


condition (dataset) : Returns the condition number of each cell in a mesh.
'''cross (x, y) :''' Return the cross product for two 3D vectors from two arrays of 3D vectors.


cross (x, y) : Return the cross product for two 3D vectors from two arrays of 3D vectors.
'''curl (array):''' Returns the curl of an array of 3D vectors.


curl (array): Returns the curl of an array of 3D vectors.
'''divergence (array):'''  Returns the divergence of an array of 3D vectors.


divergence (array):
'''divide (x, y):''' Element by element division. x and y can be single values or arrays. Same as x/y.
    Returns the divergence of an array of 3D vectors.


det (array) :
'''det (array) :''' Returns the determinant of an array of 2D square matrices.
    Returns the determinant of an array of 2D square matrices.


determinant (array) :
'''determinant (array) :''' Returns the determinant of an array of 2D square matrices.
    Returns the determinant of an array of 2D square matrices.


diagonal (dataset) :
'''diagonal (dataset) :''' Returns the diagonal length of each cell in a dataset.
    Returns the diagonal length of each cell in a dataset.


dot (a1, a2):
'''dot (a1, a2):''' Returns the dot product of two scalars/vectors of two array of scalars/vectors.
    Returns the dot product of two scalars/vectors of two array of scalars/vectors.


eigenvalue (array) :
'''eigenvalue (array) :''' Returns the eigenvalue of an array of 2D square matrices.
    Returns the eigenvalue of an array of 2D square matrices.


eigenvector (array) :
'''eigenvector (array) :''' Returns the eigenvector of an array of 2D square matrices.
    Returns the eigenvector of an array of 2D square matrices.


global_max(array):
'''exp (x):''' Returns power(e, x).
    Returns the maximum value of an array of scalars/vectors/tensors among all process.


global_mean (array) :
'''global_max(array):''' Returns the maximum value of an array of scalars/vectors/tensors among all process. Not yet supported for multi-block and AMR datasets.
    Returns the mean value of an array of scalars/vectors/tensors among all process.


global_min(array):
'''global_mean (array) :''' Returns the mean value of an array of scalars/vectors/tensors among all process. Not yet supported for multi-block and AMR datasets.
    Returns the minimum value of an array of scalars/vectors/tensors among all process.
    m = min(array).astype(numpy.float64)
    if servermanager.vtkProcessModule.GetProcessModule().GetNumberOfLocalPartitions() > 1 :
      from mpi4py import MPI
      MPI.COMM_WORLD.Allreduce(None, [m, MPI.DOUBLE], MPI.MIN)
    return m


gradient(array):
'''global_min(array):''' Returns the minimum value of an array of scalars/vectors/tensors among all process. Not yet supported for multi-block and AMR datasets.
    Returns the gradient of an array of scalars/vectors.


inv (array) :
'''gradient(array):''' Returns the gradient of an array of scalars/vectors.
    Returns the inverse an array of 2D square matrices.


inverse (array) :
'''inv (array) :''' Returns the inverse an array of 2D square matrices.
    Returns the inverse of an array of 2D square matrices.


jacobian (dataset) :
'''inverse (array) :''' Returns the inverse of an array of 2D square matrices.
    Returns the jacobian of an array of 2D square matrices.


laplacian (array) :
'''jacobian (dataset) :''' Returns the jacobian of an array of 2D square matrices.
    Returns the jacobian of an array of scalars.


ln (array) :
'''laplacian (array) :''' Returns the jacobian of an array of scalars.
    Returns the natural logarithm of an array of scalars/vectors/tensors.


log (array) :
'''ln (array) :''' Returns the natural logarithm of an array of scalars/vectors/tensors.
    Returns the natural logarithm of an array of scalars/vectors/tensors.


log10 (array) :
'''log (array) :''' Returns the natural logarithm of an array of scalars/vectors/tensors.
    Returns the base 10 logarithm of an array of scalars/vectors/tensors.


max (array):
'''log10 (array) :''' Returns the base 10 logarithm of an array of scalars/vectors/tensors.
    Returns the maximum value of an array of scalars/vectors/tensors.


max_angle (dataset) :
'''max (array):''' Returns the maximum value of the array as a single value. Note that this function returns the maximum within a block for AMR and multi-block datasets, not across blocks/grids. Also, this returns the maximum within each process when running in parallel.
    Returns the maximum angle of each cell in a dataset.


mag (a) :
'''max_angle (dataset) :''' Returns the maximum angle of each cell in a dataset.
    Returns the magnigude of an array of scalars/vectors.


mean (array) :
'''mag (a) :''' Returns the magnigude of an array of scalars/vectors.
    Returns the mean value of an array of scalars/vectors/tensors.


min (array):
'''mean (array) :''' Returns the mean value of an array of scalars/vectors/tensors.
    Returns the min value of an array of scalars/vectors/tensors.


min_angle (dataset) :
'''min (array) :''' Returns the minimum value of the array as a single value. Note that this function returns the minimum within a block for AMR and multi-block datasets, not across blocks/grids. Also, this returns the minimum within each process when running in parallel.
    Returns the minimum angle of each cell in a dataset.
 
norm (a) :
    Returns the normalized values of an array of scalars/vectors.
 
shear (dataset) :
    Returns the shear of each cell in a dataset.
 
skew (dataset) :
    Returns the skew of each cell in a dataset.
 
strain (array) :
    Returns the strain of an array of 3D vectors.
 
surface_normal (dataset) :
    Returns the surface normal of each cell in a dataset.
 
trace (array) :
    Returns the trace of an array of 2D square matrices.
 
volume (dataset) :
    Returns the volume normal of each cell in a dataset.


vorticity(array):
'''min_angle (dataset) :''' Returns the minimum angle of each cell in a dataset.
    Returns the vorticity/curl of an array of 3D vectors.


vertex_normal (dataset) :
'''mod (x, y):''' Same as remainder (x, y).
    Returns the vertex normal of each point in a dataset.


add (x1, x2 [, y]) y = x1 + x2.  
'''multiply (x, y):''' Returns the product of x and y. x and y can be single values or arrays. Note that this is an element by element operation when x and y are both arrays. Same as x * y.


subtract (x1, x2 [, y]) y = x1 − x2.
'''negative (x):''' Same as -x.


multiply (x1, x2 [, y]) y = x1 · x2.  
'''norm (a) :''' Returns the normalized values of an array of scalars/vectors.


divide (x1, x2 [, y]) y = x1/x2
'''power (x, a):''' Exponentiation of x with a. Here both x and a can either be a single value or an array. If x and y are both arrays, a one-by-one mapping is used between two arrays.


negative (x [, y]) y = −x.  
'''reciprocal (x):''' Returns 1/x.


power (x1, x2 [, y]) y = xx2.  
'''remainder (x, y):''' Returns x − y*floor(x/y). x and y can be single values or arrays.


remainder (x1, x2 [, y]) Returns x − y*floor(x/y).
'''rint (x):''' Rounds x to the nearest integer(s).


mod (x1, x2 [, y]) Same as remainder (x1, x2 [, y]).
'''shear (dataset) :''' Returns the shear of each cell in a dataset.


fmod (x1, x2 [, y]) x1 = kx2 + y where k is the largest integer satisfying this equation.  
'''skew (dataset) :''' Returns the skew of each cell in a dataset.


absolute (x [, y]) y = |x| . Called to implement abs(x) for arrays.
'''square (x):''' Returns x*x.


rint (x, [, y]) Round x to the nearest integer.  
'''sqrt (x):''' Return square root of x.


conj (x [, y]) conjugate (x [, y])  in other words, the complex conjugate of x.  
'''strain (array) :''' Returns the strain of an array of 3D vectors.


exp (x [, y]) y = ex.
'''subtract (x, y):''' Returns the difference between two values.  x and y can be single values or arrays. Same as x - y.


log (x [, y]) y = log(x). In other words, y is the number so that ey = x.
'''surface_normal (dataset) :''' Returns the surface normal of each cell in a dataset.


expm1 (x, [, y]) y = ex − 1. Calculated so that it is accurate for small |x| .
'''trace (array) :''' Returns the trace of an array of 2D square matrices.


log1p (x, [, y]) y = log (1 + x) but accurate for small |x| . Returns the number y such that ey −1 =
'''volume (dataset) :''' Returns the volume normal of each cell in a dataset.
x


log10 (x [, y]) y = log10(x). In other words, y is the number so that 10y = x.  
'''vorticity(array):''' Returns the vorticity/curl of an array of 3D vectors.


sqrt (x [, y]) x. y=√
'''vertex_normal (dataset) :''' Returns the vertex normal of each point in a dataset.


square (x [,y]) y=x∗x
== Trigonometric Functions ==


reciprocal (x [, y]) y = 1/x
Below is a list of supported triginometric functions.


sin (x [, y])  
'''sin (x)'''


cos (x [, y])  
'''cos (x)'''


tan (x [, y])
'''tan (x)'''


arcsin (x [, y])  
'''arcsin (x)'''


arccos (x [, y])  
'''arccos (x)'''


arctan (x [, y])
'''arctan (x)'''


hypot (x1, x2 [, y])  
'''hypot (x1, x2)'''


sinh
'''sinh(x)'''


cosh (x [, y])  
'''cosh (x)'''


tanh (x [, y])  
'''tanh (x)'''


arcsinh (x [, y])  
'''arcsinh (x)'''


arccosh (x [, y])  
'''arccosh (x)'''


arctanh (x [, y])
'''arctanh (x)'''

Revision as of 16:02, 15 December 2010

Introduction

ParaView UG Python calculator.png

The Python Calculator is a ParaView filter that processes one or more input arrays based on an expression provided by the user to produce a new output array. The parameters of the filter include the expression, the association of the output array (Point or Cell Data), the name of output array and a toggle that controls whether the input arrays are copied to the output. In this document, we introduce the use of the Python Calculator and provide a list of functions available to the user.

Note that the Python Calculator depends on Python and NumPy. All ParaView binaries distributed by Kitware are built with these to enable the calculator. If you have built ParaView yourself, you have to make sure that NumPy is installed and that PARAVIEW_ENABLE_PYTHON is turned on when configuring the ParaView build.

Basic Tutorial

Start by creating a Sphere source and applying the Python Calculator to it. As the first expression, use the following and apply:

<source lang="python"> 5 </source>

This should create an array name "result" in the output point data. Note that this is an array that has a value of 5 for each point. When the expression results in a single value, the calculator will automatically make an constant array. Next, try the following:

<source lang="python"> Normals </source>

Now the "result" array should be the same as the input array Normals. As described in detail later, various functions are available through the calculator. For example, the following is a valid expression.

<source lang="python"> sin(Normals) + 5 </source>

It is very important to note that the Python Calculator has to produce one value per point or cell depending on the Array Association parameter. Most of the functions described here apply individually to all point or cell values and produce an array as the same dimensions as the input. However, some of them (such as min() and max()) produce single values.

Accessing Data

There are several ways of accessing input arrays within expressions. The simplest way is to access it by name:

<source lang="python"> sin(Normals) + 5 </source>

This is equivalent to:

<source lang="python"> sin(inputs[0].PointData['Normals']) + 5 </source>

The example above requires some explanation. Here inputs[0] refer to the first input (dataset) to the filter. Python Calculator can accept multiple inputs. Each input can be accessed as inputs[0], inputs[1], ... You can access the point or cell data of an input using the .PointData or .CellData qualifiers. You can then access individual arrays within the point or cell data containers using the [] operator. Make sure to use quotes or double-quotes around the array name. NOTE: Arrays that have names with certain characters (such as space, +, -, *, /) in their name can only be accessed using this method.

Certain functions apply directly on the input mesh. These filters expect an input dataset as argument. For example,

<source lang="python"> area(inputs[0]) </source>

For data types that explicitly define the point coordinates, you can access the coordinates array using the .Points qualifier. The following extracts the first component of the coordinates array:

<source lang="python"> inputs[0].Points[:,0] </source>

Note that certain data types, mainly image data (uniform rectilinear grid) and rectilinear grid, point coordinates are defined implicitly and cannot be accessed as an array.

Comparing Multiple Datasets

As described above, the Python Calculator can accept multiple inputs. Using this functionality, you can compare multiple datasets. There are a few things to note:

  • Python Calculator will always copy the mesh from the first input to its output,
  • All operations are applied point by point. In most cases, this requires that the input meshes (topology and geometry) are the same. At the least, it requires the the inputs have the same number of points or cells
  • In parallel execution mode, the inputs have to be distributed exactly the same way across processes

For example, to compare the temperature of 2 datasets, you can select both inputs, apply the Python Calculator and then use the following expression.

<source lang="python"> inputs[0].PointData['temperature'] - inputs[1].PointData['temperature'] </source>

Basic Operations

The Python calculator supports all of the basic arithmetic operations using the +, -, * and / operators. These are always applied element-by-element to point and cell data including scalars, vectors and tensors. These operations also work with single values. For example, the following adds 5 to all components of all Normals.

<source lang="python"> Normals + 5 </source>

The following adds 1 to the first component, 2 to the second component and 3 to the third component:

<source lang="python"> Normals + [1,2,3] </source>

This is specially useful when mixing functions that return single values. For example, the following normalizes the Normals array:

<source lang="python"> (Normals - min(Normals))/(max(Normals) - min(Normals)) </source>

A common use case in a calculator is to work on one component of an array. This can be accomplished with the following:

<source lang="python"> Normals[:, 0] </source>

The expression above extracts the first component of the Normals vector. Here, : is a placeholder for "all elements". One element can be extracted by replacing : with an index. For example, the following creates an constant array from the first component of the normal of the first point:

<source lang="python"> Normals[0, 0] </source>

Whereas the following, assigns the normal of the first point to all points:

<source lang="python"> Normals[0, :] </source>

It is also possible to merge multiple scalars into on array. For this, we can use the hstack() function:

<source lang="python"> hstack([velocity_x, velocity_y, velocity_z]) </source>

Note the use of square brackets ([]).

Under the cover, the Python Calculator uses NumPy. All arrays in the expression are compatible with NumPy arrays and can be used where NumPy arrays can be used. For more information on what you can do with these arrays, consult with the NumPy book, which can be downloaded here.

Functions

The following is a list of functions available in the Python Calculator. Note that this list is partial since most of the NumPy and SciPy functions can be used in the Python Calculator.

abs (x) : Returns the absolute value(s) of x.

add (x, y): Returns the sum of two values. x and y can be single values or arrays. Same as x+y.

area (dataset) : Returns the surface area of each cell in a mesh.

aspect (dataset) : Returns the aspect ratio of each cell in a mesh.

aspect_gamma (dataset) : Returns the aspect ratio gamma of each cell in a mesh.

condition (dataset) : Returns the condition number of each cell in a mesh.

cross (x, y) : Return the cross product for two 3D vectors from two arrays of 3D vectors.

curl (array): Returns the curl of an array of 3D vectors.

divergence (array): Returns the divergence of an array of 3D vectors.

divide (x, y): Element by element division. x and y can be single values or arrays. Same as x/y.

det (array) : Returns the determinant of an array of 2D square matrices.

determinant (array) : Returns the determinant of an array of 2D square matrices.

diagonal (dataset) : Returns the diagonal length of each cell in a dataset.

dot (a1, a2): Returns the dot product of two scalars/vectors of two array of scalars/vectors.

eigenvalue (array) : Returns the eigenvalue of an array of 2D square matrices.

eigenvector (array) : Returns the eigenvector of an array of 2D square matrices.

exp (x): Returns power(e, x).

global_max(array): Returns the maximum value of an array of scalars/vectors/tensors among all process. Not yet supported for multi-block and AMR datasets.

global_mean (array) : Returns the mean value of an array of scalars/vectors/tensors among all process. Not yet supported for multi-block and AMR datasets.

global_min(array): Returns the minimum value of an array of scalars/vectors/tensors among all process. Not yet supported for multi-block and AMR datasets.

gradient(array): Returns the gradient of an array of scalars/vectors.

inv (array) : Returns the inverse an array of 2D square matrices.

inverse (array) : Returns the inverse of an array of 2D square matrices.

jacobian (dataset) : Returns the jacobian of an array of 2D square matrices.

laplacian (array) : Returns the jacobian of an array of scalars.

ln (array) : Returns the natural logarithm of an array of scalars/vectors/tensors.

log (array) : Returns the natural logarithm of an array of scalars/vectors/tensors.

log10 (array) : Returns the base 10 logarithm of an array of scalars/vectors/tensors.

max (array): Returns the maximum value of the array as a single value. Note that this function returns the maximum within a block for AMR and multi-block datasets, not across blocks/grids. Also, this returns the maximum within each process when running in parallel.

max_angle (dataset) : Returns the maximum angle of each cell in a dataset.

mag (a) : Returns the magnigude of an array of scalars/vectors.

mean (array) : Returns the mean value of an array of scalars/vectors/tensors.

min (array) : Returns the minimum value of the array as a single value. Note that this function returns the minimum within a block for AMR and multi-block datasets, not across blocks/grids. Also, this returns the minimum within each process when running in parallel.

min_angle (dataset) : Returns the minimum angle of each cell in a dataset.

mod (x, y): Same as remainder (x, y).

multiply (x, y): Returns the product of x and y. x and y can be single values or arrays. Note that this is an element by element operation when x and y are both arrays. Same as x * y.

negative (x): Same as -x.

norm (a) : Returns the normalized values of an array of scalars/vectors.

power (x, a): Exponentiation of x with a. Here both x and a can either be a single value or an array. If x and y are both arrays, a one-by-one mapping is used between two arrays.

reciprocal (x): Returns 1/x.

remainder (x, y): Returns x − y*floor(x/y). x and y can be single values or arrays.

rint (x): Rounds x to the nearest integer(s).

shear (dataset) : Returns the shear of each cell in a dataset.

skew (dataset) : Returns the skew of each cell in a dataset.

square (x): Returns x*x.

sqrt (x): Return square root of x.

strain (array) : Returns the strain of an array of 3D vectors.

subtract (x, y): Returns the difference between two values. x and y can be single values or arrays. Same as x - y.

surface_normal (dataset) : Returns the surface normal of each cell in a dataset.

trace (array) : Returns the trace of an array of 2D square matrices.

volume (dataset) : Returns the volume normal of each cell in a dataset.

vorticity(array): Returns the vorticity/curl of an array of 3D vectors.

vertex_normal (dataset) : Returns the vertex normal of each point in a dataset.

Trigonometric Functions

Below is a list of supported triginometric functions.

sin (x)

cos (x)

tan (x)

arcsin (x)

arccos (x)

arctan (x)

hypot (x1, x2)

sinh(x)

cosh (x)

tanh (x)

arcsinh (x)

arccosh (x)

arctanh (x)