# Using numpy arrays in Paraview programmable filter

How can I access a field in Paraview's programmable filter as a numpy array?

I want to:

• Import an existing field as a numpy array
• Create a similar array for output
• Register it as a new field for visualization

I'm familiar with the wiki, but it doesn't address the issue.

Given this example:

elev0 = inputs[0].PointData['Elevation']
elev1 = inputs[1].PointData['Elevation']
output.PointData.append(elev1 - elev0, 'difference')


I'd like to do (assuming elev0 and elev1 are numpy arrays):

diff = elev1 - elev0
diff[ diff > 1.0 ] = 1.0
output.PointData.append(diff, 'difference')


but this doesn't work as those are VTKArray objects.

With ParaView 4.2, your original code will work as expected. The following is now acceptable.

output.PointData.append(numpyarray, "name")


Reading the Paraview python API, found the following solution to convert back and forth between VTKArray and numpy arrays. This uses the numpy_support and vtk.dataset_adapter modules :

from paraview.numpy_support import vtk_to_numpy
import numpy as np