vtkmodules.numpy_interface.vtk_partitioned_array#

VTKPartitionedArray — numpy-compatible wrapper for composite datasets.

This module provides the VTKPartitionedArray class that 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.

It also registers array_function overrides so that standard numpy calls (numpy.sum, numpy.max, etc.) are dispatched correctly when handed a VTKPartitionedArray.

Module Contents#

Classes#

VTKPartitionedArray

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

Functions#

bitwise_or

Implements element by element or (bitwise, | in C/C++) operation. If one of the arrays is a NoneArray, this will return the array that is not NoneArray, treating NoneArray as 0 in the or operation.

sum

Local (non-parallel) sum dispatched from numpy.sum().

max

Local (non-parallel) max dispatched from numpy.max().

min

Local (non-parallel) min dispatched from numpy.min().

all

Local (non-parallel) all dispatched from numpy.all().

mean

Local (non-parallel) mean dispatched from numpy.mean().

var

Local (non-parallel) var dispatched from numpy.var().

std

Local (non-parallel) std dispatched from numpy.std().

shape

Returns the shape (dimensions) of an array.

flatnonzero

Return indices that are non-zero in the flattened version of the input array.

_like_VTKPartitionedArray

Helper to construct like-structured VTKPartitionedArray.

zeros_like

Create a new VTKPartitionedArray filled with 0 of the same shape as array.

ones_like

Create a new VTKPartitionedArray filled with 1 of the same shape as array.

empty_like

Create a new uninitialized VTKPartitionedArray of the same shape as array.

nonzero

Return the indices of the non-zero elements of the input array. Indices are local to each chunk.

where

Returns the location (indices) of an array where the given expression is true. Indices are local to each chunk. For scalars, it returns a single array of indices. For vectors and matrices, it returns two arrays: first with tuple indices, second with component indices. The output of this method can be used to extract the values from the array also by using it as the index of the [] operator.

isin

Test whether each element of a 1-D array is also present in a second array.

any

Local (non-parallel) any dispatched from numpy.any().

prod

Local (non-parallel) prod dispatched from numpy.prod().

round_

Per-block round dispatched from numpy.round().

clip

Per-block clip dispatched from numpy.clip().

sort

Sort dispatched from numpy.sort(). Per-block for axis != 0.

argmin

argmin dispatched from numpy.argmin(). Per-block for axis > 0.

argmax

argmax dispatched from numpy.argmax(). Per-block for axis > 0.

cumsum

cumsum dispatched from numpy.cumsum(). Per-block for axis > 0.

cumprod

cumprod dispatched from numpy.cumprod(). Per-block for axis > 0.

unique

unique dispatched from numpy.unique().

concatenate

Concatenate all blocks of composite arrays into a flat array.

expand_dims

Insert a new dimension, corresponding to a given position in the array shape. In VTK, this function’s main use is to enable an operator to work on a vector and a scalar field. For example, say you want to divide each component of a vector by the magnitude of that vector. You might try this:

count_nonzero

Per-block count_nonzero, results summed.

full_like

Per-block full_like.

copy

Per-block copy.

average

Per-block weighted average.

histogram

Per-block histogram with consistent bins, counts summed.

bincount

Per-block bincount, counts summed.

Data#

API#

vtkmodules.numpy_interface.vtk_partitioned_array._builtin_min#

None

vtkmodules.numpy_interface.vtk_partitioned_array._builtin_max#

None

vtkmodules.numpy_interface.vtk_partitioned_array._builtin_sum#

None

vtkmodules.numpy_interface.vtk_partitioned_array._builtin_all#

None

vtkmodules.numpy_interface.vtk_partitioned_array._builtin_any#

None

class vtkmodules.numpy_interface.vtk_partitioned_array.VTKPartitionedArray(arrays=[], dataset=None, name=None, association=None)#

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

Initialization

Construct a composite array given a container of arrays, a dataset, name and association. It is sufficient to define a container of arrays to define a composite array. It is also possible to initialize an array by defining the dataset, name and array association. In that case, the underlying arrays will be created lazily when they are needed. It is recommended to use the latter method when initializing from an existing composite dataset.

__init_from_composite()#
_get_size()#

Returns the number of elements in the array.

size#

‘property(…)’

_get_arrays()#

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

arrays#

‘property(…)’

__setitem__(index, value) None#

Setter overwritten to defer indexing to underlying VTKArrays. For the most part, this will behave like Numpy.

__getitem__(index) Union[List, vtkmodules.numpy_interface.vtk_partitioned_array.VTKPartitionedArray, numpy.ndarray]#

Getter overwritten to refer indexing to underlying VTKArrays. For the most part, this will behave like Numpy.

_global_to_local_id(global_index, total_size, offsets)#

Returns the chunk index and the local index of the chunk from the global index

_slice_intersection(global_slice, chunk_offset, chunk_size, total_length)#

Returns a local slice into the chunk for the global_slice, or None if no overlap. Handles both positive and negative steps.

_binop(other, op)#

Used to implement numpy-style numerical operations such as add, mul, etc.

_rbinop(other, op)#

Used to implement numpy-style numerical operations such as add, mul, etc.

__add__(other)#
__radd__(other)#
__sub__(other)#
__rsub__(other)#
__mul__(other)#
__rmul__(other)#
__truediv__(other)#
__rtruediv__(other)#
__floordiv__(other)#
__rfloordiv__(other)#
__mod__(other)#
__rmod__(other)#
__pow__(other)#
__rpow__(other)#
__lshift__(other)#
__rlshift__(other)#
__rshift__(other)#
__rrshift__(other)#
__and__(other)#
__rand__(other)#
__xor__(other)#
__rxor__(other)#
__or__(other)#
__ror__(other)#
__lt__(other)#
__le__(other)#
__eq__(other)#
__ne__(other)#
__ge__(other)#
__gt__(other)#
__str__()#
astype(dtype)#

Implements numpy array’s as array method.

property dtype#

Return the dtype of the first non-NoneArray block.

property ndim#

Return the number of dimensions.

property nbytes#

Return the total number of bytes consumed by all blocks.

property T#

Return the transpose of the materialized array.

property range#

Return the (min, max) range across all blocks.

sum(axis=None, **kwargs)#
mean(axis=None, **kwargs)#
min(axis=None, **kwargs)#
max(axis=None, **kwargs)#
std(axis=None, **kwargs)#
var(axis=None, **kwargs)#
any(axis=None, **kwargs)#
all(axis=None, **kwargs)#
prod(axis=None, **kwargs)#
argmin(axis=None, **kwargs)#
argmax(axis=None, **kwargs)#
cumsum(axis=None, **kwargs)#
cumprod(axis=None, **kwargs)#
reshape(*args, **kwargs)#
flatten(order='C')#
ravel(order='C')#
copy(order='C')#
squeeze(axis=None)#
transpose(*axes)#
tolist()#
clip(a_min=None, a_max=None, **kwargs)#
round(decimals=0, **kwargs)#
sort(axis=0, **kwargs)#
dot(other)#
property shape: Tuple[int, ...]#

Return a tuple representing the shape of the composite array.

__len__() int#

Return the total number of elements in the composite array.

__contains__(item) bool#

Check if the item exists in any of the non-null sub-arrays.

__reversed__() Generator[numpy.ndarray, None, None]#

Iterate over all subarrays in the composite array in backward order.

__iter__() Generator[numpy.ndarray, None, None]#

Iterate over all subarrays in the composite array in forward order.

__array__(dtype=None, copy=None) numpy.ndarray#

Convert the composite array into a single NumPy array.

__array_function__(func, types, args, kwargs)#

Implements Numpy dispatch mechanism. Functions registered in PARTITIONED_OVERRIDE are captured here. See: https://numpy.org/doc/stable/user/basics.interoperability.html#the-array-function-protocol

__array_ufunc__(ufunc, method, *inputs, **kwargs)#

Handles Numpy functions calls that takes a fixed number of specific inputs and outputs.

vtkmodules.numpy_interface.vtk_partitioned_array.bitwise_or(array1, array2)#

Implements element by element or (bitwise, | in C/C++) operation. If one of the arrays is a NoneArray, this will return the array that is not NoneArray, treating NoneArray as 0 in the or operation.

vtkmodules.numpy_interface.vtk_partitioned_array.sum(array, axis=None, **kwargs)#

Local (non-parallel) sum dispatched from numpy.sum().

vtkmodules.numpy_interface.vtk_partitioned_array.max(array, axis=None, **kwargs)#

Local (non-parallel) max dispatched from numpy.max().

vtkmodules.numpy_interface.vtk_partitioned_array.min(array, axis=None, **kwargs)#

Local (non-parallel) min dispatched from numpy.min().

vtkmodules.numpy_interface.vtk_partitioned_array.all(array, axis=None, **kwargs)#

Local (non-parallel) all dispatched from numpy.all().

vtkmodules.numpy_interface.vtk_partitioned_array.mean(array, axis=None, **kwargs)#

Local (non-parallel) mean dispatched from numpy.mean().

vtkmodules.numpy_interface.vtk_partitioned_array.var(array, axis=None, **kwargs)#

Local (non-parallel) var dispatched from numpy.var().

vtkmodules.numpy_interface.vtk_partitioned_array.std(array, axis=None, **kwargs)#

Local (non-parallel) std dispatched from numpy.std().

vtkmodules.numpy_interface.vtk_partitioned_array.shape(array)#

Returns the shape (dimensions) of an array.

vtkmodules.numpy_interface.vtk_partitioned_array.flatnonzero(array)#

Return indices that are non-zero in the flattened version of the input array.

vtkmodules.numpy_interface.vtk_partitioned_array._like_VTKPartitionedArray(array, kind, dtype=None, value=None)#

Helper to construct like-structured VTKPartitionedArray.

vtkmodules.numpy_interface.vtk_partitioned_array.zeros_like(array, dtype=None)#

Create a new VTKPartitionedArray filled with 0 of the same shape as array.

vtkmodules.numpy_interface.vtk_partitioned_array.ones_like(array, dtype=None)#

Create a new VTKPartitionedArray filled with 1 of the same shape as array.

vtkmodules.numpy_interface.vtk_partitioned_array.empty_like(array, dtype=None)#

Create a new uninitialized VTKPartitionedArray of the same shape as array.

vtkmodules.numpy_interface.vtk_partitioned_array.nonzero(array, *args)#

Return the indices of the non-zero elements of the input array. Indices are local to each chunk.

vtkmodules.numpy_interface.vtk_partitioned_array.where(array, *args)#

Returns the location (indices) of an array where the given expression is true. Indices are local to each chunk. For scalars, it returns a single array of indices. For vectors and matrices, it returns two arrays: first with tuple indices, second with component indices. The output of this method can be used to extract the values from the array also by using it as the index of the [] operator.

For example:

algs.where(algs.array([1,2,3]) == 2) (array([1]),)

algs.where(algs.array([[1,2,3], [2,1,1]]) == 2) (array([0, 1]), array([1, 0]))

a = array([[1,2,3], [2,1,1]]) indices = algs.where(a > 2) a[indices] array([3])

vtkmodules.numpy_interface.vtk_partitioned_array.isin(array, *args)#

Test whether each element of a 1-D array is also present in a second array.

vtkmodules.numpy_interface.vtk_partitioned_array.any(array, axis=None, **kwargs)#

Local (non-parallel) any dispatched from numpy.any().

vtkmodules.numpy_interface.vtk_partitioned_array.prod(array, axis=None, **kwargs)#

Local (non-parallel) prod dispatched from numpy.prod().

vtkmodules.numpy_interface.vtk_partitioned_array.round_(array, decimals=0, **kwargs)#

Per-block round dispatched from numpy.round().

vtkmodules.numpy_interface.vtk_partitioned_array.clip(array, a_min=None, a_max=None, **kwargs)#

Per-block clip dispatched from numpy.clip().

vtkmodules.numpy_interface.vtk_partitioned_array.sort(array, axis=0, **kwargs)#

Sort dispatched from numpy.sort(). Per-block for axis != 0.

vtkmodules.numpy_interface.vtk_partitioned_array.argmin(array, axis=None, **kwargs)#

argmin dispatched from numpy.argmin(). Per-block for axis > 0.

vtkmodules.numpy_interface.vtk_partitioned_array.argmax(array, axis=None, **kwargs)#

argmax dispatched from numpy.argmax(). Per-block for axis > 0.

vtkmodules.numpy_interface.vtk_partitioned_array.cumsum(array, axis=None, **kwargs)#

cumsum dispatched from numpy.cumsum(). Per-block for axis > 0.

vtkmodules.numpy_interface.vtk_partitioned_array.cumprod(array, axis=None, **kwargs)#

cumprod dispatched from numpy.cumprod(). Per-block for axis > 0.

vtkmodules.numpy_interface.vtk_partitioned_array.unique(array, **kwargs)#

unique dispatched from numpy.unique().

vtkmodules.numpy_interface.vtk_partitioned_array.concatenate(arrays, axis=0, **kwargs)#

Concatenate all blocks of composite arrays into a flat array.

vtkmodules.numpy_interface.vtk_partitioned_array.expand_dims(array1, val2)#

Insert a new dimension, corresponding to a given position in the array shape. In VTK, this function’s main use is to enable an operator to work on a vector and a scalar field. For example, say you want to divide each component of a vector by the magnitude of that vector. You might try this:

v VTKArray([[ 1., 1., 1.], [ 1., 1., 1.], [ 1., 1., 1.], [ 1., 1., 1.], [ 1., 1., 1.]]) algs.mag(v) VTKArray([ 1.73205081, 1.73205081, 1.73205081, 1.73205081, 1.73205081]) v / algs.mag(v) Traceback (most recent call last): File “”, line 1, in ValueError: operands could not be broadcast together with shapes (5,3) (5)

The division operator does not know how to map a scalar to a vector due to a mismatch in dimensions. This can be solved by making the scalar a vector of 1 component (increasing its dimension to 2) as follows:

v / np.expand_dims(algs.mag(v), 1) VTKArray([[ 0.57735027, 0.57735027, 0.57735027], [ 0.57735027, 0.57735027, 0.57735027], [ 0.57735027, 0.57735027, 0.57735027], [ 0.57735027, 0.57735027, 0.57735027], [ 0.57735027, 0.57735027, 0.57735027]])

vtkmodules.numpy_interface.vtk_partitioned_array.count_nonzero(array, axis=None, **kwargs)#

Per-block count_nonzero, results summed.

vtkmodules.numpy_interface.vtk_partitioned_array.full_like(array, fill_value, dtype=None, **kwargs)#

Per-block full_like.

vtkmodules.numpy_interface.vtk_partitioned_array.copy(array, **kwargs)#

Per-block copy.

vtkmodules.numpy_interface.vtk_partitioned_array.average(array, axis=None, weights=None, **kwargs)#

Per-block weighted average.

vtkmodules.numpy_interface.vtk_partitioned_array.histogram(array, bins=10, range=None, density=None, weights=None, **kwargs)#

Per-block histogram with consistent bins, counts summed.

vtkmodules.numpy_interface.vtk_partitioned_array.bincount(array, weights=None, minlength=0, **kwargs)#

Per-block bincount, counts summed.