vtk.numpy_interface.dataset_adapter Module

This module provides classes that allow Numpy-type access to VTK datasets and arrays. This is best described with some examples.

To normalize a VTK array:

import vtk import vtk.numpy_interface.dataset_adapter as dsa import vtk.numpy_interface.algorithms as algs

rt = vtk.vtkRTAnalyticSource() rt.Update() image = dsa.WrapDataObject(rt.GetOutput()) rtdata = image.PointData[‘RTData’] rtmin = algs.min(rtdata) rtmax = algs.max(rtdata) rtnorm = (rtdata - rtmin) / (rtmax - rtmin) image.PointData.append(rtnorm, ‘RTData - normalized’) print image.GetPointData().GetArray(‘RTData - normalized’).GetRange()

To calculate gradient:

grad= algs.gradient(rtnorm)

To access subsets:

>>> grad[0:10]
VTKArray([[ 0.10729134,  0.03763443,  0.03136338],
       [ 0.02754352,  0.03886006,  0.032589  ],
       [ 0.02248248,  0.04127144,  0.03500038],
       [ 0.02678365,  0.04357527,  0.03730421],
       [ 0.01765099,  0.04571581,  0.03944477],
       [ 0.02344007,  0.04763837,  0.04136734],
       [ 0.01089381,  0.04929155,  0.04302051],
       [ 0.01769151,  0.05062952,  0.04435848],
       [ 0.002764  ,  0.05161414,  0.04534309],
       [ 0.01010841,  0.05221677,  0.04594573]])
>>> grad[:, 0]
VTKArray([ 0.10729134,  0.02754352,  0.02248248, ..., -0.02748174,
       -0.02410045,  0.05509736])

All of this functionality is also supported for composite datasets even though their data arrays may be spread across multiple datasets. We have implemented a VTKCompositeDataArray class that handles many Numpy style operators and is supported by all algorithms in the algorithms module.

This module also provides an API to access composite datasets. For example:

mb = vtk.vtkMultiBlockDataSet() mb.SetBlock(0, image.VTKObject) mb.SetBlock(1e, image.VTKObject) cds = dsa.WrapDataObject(mb) for block in cds:

print block

Note that this module implements only the wrappers for datasets and arrays. The classes implement many useful operators. However, to make best use of these classes, take a look at the algorithms module.

class paraview.vtk.numpy_interface.dataset_adapter.ArrayAssociation[source]

Easy access to vtkDataObject.AttributeTypes

CELL = 1
FIELD = 2
POINT = 0
ROW = 6
class paraview.vtk.numpy_interface.dataset_adapter.CompositeDataIterator(cds)[source]

Bases: object

Wrapper for a vtkCompositeDataIterator class to satisfy the python iterator protocol. This iterator iterates over non-empty leaf nodes. To iterate over empty or non-leaf nodes, use the vtkCompositeDataIterator directly.

next()[source]
class paraview.vtk.numpy_interface.dataset_adapter.CompositeDataSet(vtkobject)[source]

Bases: paraview.vtk.numpy_interface.dataset_adapter.DataObject

A wrapper for vtkCompositeData and subclasses that makes it easier to access Point/Cell/Field data as VTKCompositeDataArrays. It also provides a Python type iterator.

CellData

This property returns the cell data of a dataset.

FieldData

This property returns the field data of a dataset.

GetAttributes(type)[source]

Returns the attributes specified by the type as a CompositeDataSetAttributes instance.

GetCellData()[source]

Returns the cell data as a DataSetAttributes instance.

GetFieldData()[source]

Returns the field data as a DataSetAttributes instance.

GetNumberOfCells()[source]

Returns the total number of cells of all datasets in the composite dataset. Note that this traverses the whole composite dataset every time and should not be called repeatedly for large composite datasets.

GetNumberOfElements(assoc)[source]

Returns the total number of cells or points depending on the value of assoc which can be ArrayAssociation.POINT or ArrayAssociation.CELL.

GetNumberOfPoints()[source]

Returns the total number of points of all datasets in the composite dataset. Note that this traverses the whole composite dataset every time and should not be called repeatedly for large composite datasets.

GetPointData()[source]

Returns the point data as a DataSetAttributes instance.

GetPoints()[source]

Returns the points as a VTKCompositeDataArray instance.

PointData

This property returns the point data of the dataset.

Points

This property returns the points of the dataset.

class paraview.vtk.numpy_interface.dataset_adapter.CompositeDataSetAttributes(dataset, association)[source]

This is a python friendly wrapper for vtkDataSetAttributes for composite datsets. Since composite datasets themselves don’t have attribute data, but the attribute data is associated with the leaf nodes in the composite dataset, this class simulates a DataSetAttributes interface by taking a union of DataSetAttributes associated with all leaf nodes.

GetArray(idx)[source]

Given a name, returns a VTKCompositeArray.

PassData(other)[source]

Emulate PassData for composite datasets.

append(narray, name)[source]

Appends a new array to the composite dataset attributes.

keys()[source]

Returns the names of the arrays as a list.

class paraview.vtk.numpy_interface.dataset_adapter.DataObject(vtkobject)[source]

Bases: paraview.vtk.numpy_interface.dataset_adapter.VTKObjectWrapper

A wrapper for vtkDataObject that makes it easier to access FielData arrays as VTKArrays

FieldData

This property returns the field data of a data object.

GetAttributes(type)[source]

Returns the attributes specified by the type as a DataSetAttributes instance.

GetFieldData()[source]

Returns the field data as a DataSetAttributes instance.

class paraview.vtk.numpy_interface.dataset_adapter.DataSet(vtkobject)[source]

Bases: paraview.vtk.numpy_interface.dataset_adapter.DataObject

This is a python friendly wrapper of a vtkDataSet that defines a few useful properties.

CellData

This property returns the cell data of a dataset.

GetCellData()[source]

Returns the cell data as a DataSetAttributes instance.

GetPointData()[source]

Returns the point data as a DataSetAttributes instance.

PointData

This property returns the point data of the dataset.

class paraview.vtk.numpy_interface.dataset_adapter.DataSetAttributes(vtkobject, dataset, association)[source]

Bases: paraview.vtk.numpy_interface.dataset_adapter.VTKObjectWrapper

This is a python friendly wrapper of vtkDataSetAttributes. It returns VTKArrays. It also provides the dictionary interface.

GetArray(idx)[source]

Given an index or name, returns a VTKArray.

PassData(other)[source]

A wrapper for vtkDataSet.PassData.

append(narray, name)[source]

Appends a new array to the dataset attributes.

keys()[source]

Returns the names of the arrays as a list.

values()[source]

Returns the arrays as a list.

class paraview.vtk.numpy_interface.dataset_adapter.MultiCompositeDataIterator(cds)[source]

Bases: paraview.vtk.numpy_interface.dataset_adapter.CompositeDataIterator

Iterator that can be used to iterate over multiple composite datasets together. This iterator works only with arrays that were copied from an original using CopyStructured. The most common use case is to use CopyStructure, then iterate over input and output together while creating output datasets from corresponding input datasets.

next()[source]
class paraview.vtk.numpy_interface.dataset_adapter.PointSet(vtkobject)[source]

Bases: paraview.vtk.numpy_interface.dataset_adapter.DataSet

This is a python friendly wrapper of a vtkPointSet that defines a few useful properties.

GetPoints()[source]

Returns the points as a VTKArray instance. Returns None if the dataset has implicit points.

Points

This property returns the point coordinates of dataset.

SetPoints(pts)[source]

Given a VTKArray instance, sets the points of the dataset.

class paraview.vtk.numpy_interface.dataset_adapter.PolyData(vtkobject)[source]

Bases: paraview.vtk.numpy_interface.dataset_adapter.PointSet

This is a python friendly wrapper of a vtkPolyData that defines a few useful properties.

GetPolygons()[source]

Returns the polys as a VTKArray instance.

Polygons

This property returns the connectivity of polygons.

class paraview.vtk.numpy_interface.dataset_adapter.Table(vtkobject)[source]

Bases: paraview.vtk.numpy_interface.dataset_adapter.DataObject

A wrapper for vtkFielData that makes it easier to access RowData array as VTKArrays

GetRowData()[source]

Returns the row data as a DataSetAttributes instance.

RowData

This property returns the row data of the table.

class paraview.vtk.numpy_interface.dataset_adapter.UnstructuredGrid(vtkobject)[source]

Bases: paraview.vtk.numpy_interface.dataset_adapter.PointSet

This is a python friendly wrapper of a vtkUnstructuredGrid that defines a few useful properties.

CellLocations

This property returns the locations of cells.

CellTypes

This property returns the types of cells.

Cells

This property returns the connectivity of cells.

GetCellLocations()[source]

Returns the cell locations as a VTKArray instance.

GetCellTypes()[source]

Returns the cell types as a VTKArray instance.

GetCells()[source]

Returns the cells as a VTKArray instance.

SetCells(cellTypes, cellLocations, cells)[source]

Given cellTypes, cellLocations, cells as VTKArrays, populates the unstructured grid data structures.

class paraview.vtk.numpy_interface.dataset_adapter.VTKArray[source]

Bases: numpy.ndarray

This is a sub-class of numpy ndarray that stores a reference to a vtk array as well as the owning dataset. The numpy array and vtk array should point to the same memory location.

and(other)
or(other)
class paraview.vtk.numpy_interface.dataset_adapter.VTKArrayMetaClass[source]

Bases: type

class paraview.vtk.numpy_interface.dataset_adapter.VTKCompositeDataArray(arrays=[], dataset=None, name=None, association=2)[source]

Bases: object

This class manages a set of arrays of the same name contained within a composite dataset. Its main purpose is to provide a Numpy-type interface to composite data arrays which are naturally nothing but a collection of vtkDataArrays. A VTKCompositeDataArray makes such a collection appear as a single Numpy array and support all array operations that this module and the associated algorithm module support. Note that this is not a subclass of a Numpy array and as such cannot be passed to native Numpy functions. Instead VTK modules should be used to process composite arrays.

Arrays

Returns the internal container of VTKArrays. If necessary, this will populate the array list from a composite dataset.

GetArrays()[source]

Returns the internal container of VTKArrays. If necessary, this will populate the array list from a composite dataset.

GetSize()[source]

Returns the number of elements in the array.

astype(dtype)[source]

Implements numpy array’s as array method.

size

Returns the number of elements in the array.

class paraview.vtk.numpy_interface.dataset_adapter.VTKCompositeDataArrayMetaClass[source]

Bases: type

class paraview.vtk.numpy_interface.dataset_adapter.VTKNoneArray[source]

Bases: object

VTKNoneArray is used to represent a “void” array. An instance of this class (NoneArray) is returned instead of None when an array that doesn’t exist in a DataSetAttributes is requested. All operations on the NoneArray return NoneArray. The main reason for this is to support operations in parallel where one of the processes may be working on an empty dataset. In such cases, the process is still expected to evaluate a whole expression because some of the functions may perform bulk MPI communication. None cannot be used in these instances because it cannot properly override operators such as __add__, __sub__ etc. This is the main raison d’etre for VTKNoneArray.

astype(dtype)[source]

Implements numpy array’s astype method.

class paraview.vtk.numpy_interface.dataset_adapter.VTKNoneArrayMetaClass[source]

Bases: type

class paraview.vtk.numpy_interface.dataset_adapter.VTKObjectWrapper(vtkobject)[source]

Bases: object

Superclass for classes that wrap VTK objects with Python objects. This class holds a reference to the wrapped VTK object. It also forwards unresolved methods to the underlying object by overloading __get__attr.

paraview.vtk.numpy_interface.dataset_adapter.WrapDataObject(ds)[source]

Returns a Numpy friendly wrapper of a vtkDataObject.

paraview.vtk.numpy_interface.dataset_adapter.buffer_shared()

Check if two objects share the same buffer, meaning that they point to the same block of memory. An TypeError exception will be raised if either of the objects does not provide a buffer.

paraview.vtk.numpy_interface.dataset_adapter.numpyTovtkDataArray(array, name='numpy_array', array_type=None)[source]

Given a numpy array or a VTKArray and a name, returns a vtkDataArray. The resulting vtkDataArray will store a reference to the numpy array through a DeleteEvent observer: the numpy array is released only when the vtkDataArray is destroyed.

paraview.vtk.numpy_interface.dataset_adapter.reshape_append_ones(a1, a2)[source]

Returns a list with the two arguments, any of them may be processed. If the arguments are numpy.ndarrays, append 1s to the shape of the array with the smallest number of dimensions until the arrays have the same number of dimensions. Does nothing if the arguments are not ndarrays or the arrays have the same number of dimensions.

paraview.vtk.numpy_interface.dataset_adapter.vtkDataArrayToVTKArray(array, dataset=None)[source]

Given a vtkDataArray and a dataset owning it, returns a VTKArray.