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.

VTKPartitionedPoints

Composite-dataset analog of vtkPoints.

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.

_argreduce_VTKPartitionedArray

Block-wise argmin/argmax without materializing the whole array.

_cumulative_VTKPartitionedArray

Block-wise cumulative scan, carrying the running total across blocks.

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(). Computed block by block.

argmax

argmax dispatched from numpy.argmax(). Computed block by block.

cumsum

cumsum dispatched from numpy.cumsum(). Computed block by block.

cumprod

cumprod dispatched from numpy.cumprod(). Computed block by block.

dot

Dot product, computed block by block when the operands line up.

norm

Vector norm. The default 2-norm over every element is the sum of the per-block sums of squares, so no block leaves its own memory. Other norms and axis arguments materialize once.

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=None, 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 are resolved from the composite dataset during construction. It is recommended to use the latter method when initializing from an existing composite dataset.

Only a weak reference to the dataset is kept, so the composite dataset is not kept alive by the arrays it hands out.

_dataset#

None

_set_dataset(dataset)#

Store a weak reference to the owning composite dataset.

The reference must stay weak: this is a child-to-parent back reference, and a strong one would be pinned in vtkPythonUtil’s ghost map once this wrapper is released, leaking the whole dataset.

property dataset#

Return the owning composite dataset (dereferenced from weak ref).

_init_from_composite()#

Populate the per-block arrays from the composite dataset.

Called once from init. The dataset is only held weakly, so the blocks cannot be resolved lazily the first time they are needed.

_get_size()#

Returns the number of elements in the array.

size#

‘property(…)’

_get_arrays()#

Returns the internal container of VTKArrays.

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)#
_unop(ufunc)#

Used to implement numpy-style unary operations such as neg, abs, etc. The ufunc is applied per block so the result stays partitioned.

__neg__()#
__pos__()#
__abs__()#
__repr__()#
__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.

__bool__() → bool#
__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.

to_numpy(dtype=None) → numpy.ndarray#

Materialize all blocks into a single numpy ndarray.

__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 and handled block by block. Everything else falls back to materializing the blocks into a single contiguous array and re-calling the numpy function. 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._argreduce_VTKPartitionedArray(array, axis, reducer, argreducer)#

Block-wise argmin/argmax without materializing the whole array.

Each block contributes its own extreme value and local index; the best one wins and its index is shifted by the block’s offset. Ties keep the first occurrence, matching numpy, because blocks are visited in order and the comparison is strict. Returns None when there is nothing to reduce so the caller can defer to numpy for the usual error.

vtkmodules.numpy_interface.vtk_partitioned_array._cumulative_VTKPartitionedArray(array, axis, scan, combine, identity, **kwargs)#

Block-wise cumulative scan, carrying the running total across blocks.

scan is numpy.cumsum or numpy.cumprod, combine folds the carry into a block’s result, and identity seeds the carry.

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(). Computed block by block.

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

argmax dispatched from numpy.argmax(). Computed block by block.

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

cumsum dispatched from numpy.cumsum(). Computed block by block.

With axis=0 the result has the same shape as the input and stays partitioned; with axis=None numpy flattens, so a single array is returned, but it is still filled one block at a time.

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

cumprod dispatched from numpy.cumprod(). Computed block by block.

With axis=0 the result has the same shape as the input and stays partitioned; with axis=None numpy flattens, so a single array is returned, but it is still filled one block at a time.

vtkmodules.numpy_interface.vtk_partitioned_array.dot(a, b, **kwargs)#

Dot product, computed block by block when the operands line up.

Two partitioned arrays with matching 1-D blocks reduce to a sum of per-block dot products. A partitioned array of tuples against a plain array reduces to one dot product per block, and the result stays partitioned. Anything else materializes once.

vtkmodules.numpy_interface.vtk_partitioned_array.norm(array, ord=None, axis=None, keepdims=False)#

Vector norm. The default 2-norm over every element is the sum of the per-block sums of squares, so no block leaves its own memory. Other norms and axis arguments materialize once.

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.

vtkmodules.numpy_interface.vtk_partitioned_array._INVALID_BOUNDS#

(1e+299,)

class vtkmodules.numpy_interface.vtk_partitioned_array.VTKPartitionedPoints(points)#

Bases: object

Composite-dataset analog of vtkPoints.

Aggregates the vtkPoints from each non-null leaf of a composite dataset and exposes a subset of the vtkPoints API. The data property returns a VTKPartitionedArray wrapping the per-leaf coordinate arrays, so composite.points.data is numpy-indexable in the same way dataset.points.data is for non-composite datasets.

Initialization

property data#

Numpy-indexable VTKPartitionedArray of per-leaf data arrays.

property number_of_points#

Total number of points across all leaves.

property bounds#

Union of per-leaf bounds as (xmin, xmax, ymin, ymax, zmin, zmax).

Returns VTK’s invalid-bounds sentinel when no leaf has points.

__len__()#
__iter__()#

Iterate over the per-leaf vtkPoints (skipping None leaves).

__repr__()#