# Difference between revisions of "ParaView/Users Guide/Python Calculator"

Line 50: | Line 50: | ||

area(inputs[0]) | area(inputs[0]) | ||

</source> | </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 == | == Comparing Multiple Datasets == |

## Revision as of 11:07, 15 December 2010

## Contents

## Introduction

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:

```
5
```

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:

```
Normals
```

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.

```
sin(Normals) + 5
```

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:

```
sin(Normals) + 5
```

This is equivalent to:

```
sin(inputs[0].PointData['Normals']) + 5
```

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,

```
area(inputs[0])
```

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:

```
inputs[0].Points[:,0]
```

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.

```
inputs[0].PointData['temperature'] - inputs[1].PointData['temperature']
```

## 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.

```
Normals + 5
```

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

```
Normals + [1,2,3]
```

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

```
(Normals - min(Normals))/(max(Normals) - min(Normals))
```

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

```
Normals[:, 0]
```

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:

```
Normals[0, 0]
```

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

```
Normals[0, :]
```

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

```
hstack([velocity_x, velocity_y, velocity_z])
```

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.

**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.

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.

abs (array) : Returns the absolute values of an array.

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.

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.

global_max(array):

Returns the maximum value of an array of scalars/vectors/tensors among all process.

global_mean (array) :

Returns the mean value of an array of scalars/vectors/tensors among all process.

global_min(array):

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):

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 an array of scalars/vectors/tensors.

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 min value of an array of scalars/vectors/tensors.

min_angle (dataset) :

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):

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

vertex_normal (dataset) :

Returns the vertex normal of each point in a dataset.

add (x1, x2 [, y]) y = x1 + x2.

subtract (x1, x2 [, y]) y = x1 − x2.

multiply (x1, x2 [, y]) y = x1 · x2.

divide (x1, x2 [, y]) y = x1/x2

negative (x [, y]) y = −x.

power (x1, x2 [, y]) y = xx2.

remainder (x1, x2 [, y]) Returns x − y*floor(x/y).

mod (x1, x2 [, y]) Same as remainder (x1, x2 [, y]).

fmod (x1, x2 [, y]) x1 = kx2 + y where k is the largest integer satisfying this equation.

absolute (x [, y]) y = |x| . Called to implement abs(x) for arrays.

rint (x, [, y]) Round x to the nearest integer.

conj (x [, y]) conjugate (x [, y]) in other words, the complex conjugate of x.

exp (x [, y]) y = ex.

log (x [, y]) y = log(x). In other words, y is the number so that ey = x.

expm1 (x, [, y]) y = ex − 1. Calculated so that it is accurate for small |x| .

log1p (x, [, y]) y = log (1 + x) but accurate for small |x| . Returns the number y such that ey −1 = x

log10 (x [, y]) y = log10(x). In other words, y is the number so that 10y = x.

sqrt (x [, y]) x. y=√

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

reciprocal (x [, y]) y = 1/x

sin (x [, y])

cos (x [, y])

tan (x [, y])

arcsin (x [, y])

arccos (x [, y])

arctan (x [, y])

hypot (x1, x2 [, y])

sinh

cosh (x [, y])

tanh (x [, y])

arcsinh (x [, y])

arccosh (x [, y])

arctanh (x [, y])