vtkmodules.numpy_interface.vtk_affine_array#

VTKAffineArray — numpy-compatible mixin for vtkAffineArray.

This module provides the VTKAffineArray mixin class and registers overrides for all vtkAffineArray instantiations so that affine arrays coming from VTK automatically have numpy-compatible operations.

vtkAffineArray computes values using an affine function: value = slope * index + intercept

Arithmetic with scalars produces new affine arrays (staying lazy). Reductions like sum, mean, min, max, std use closed-form formulas. The full array is never materialized unless explicitly requested.

Module Contents#

Classes#

VTKAffineArray

A memory-efficient array whose values follow an affine function.

Functions#

_lazy_result_dtype

Return the numpy scalar type of ufunc(*inputs), or None.

_affine_sum

sum = nintercept + slopen*(n-1)/2.

_affine_mean

mean = intercept + slope*(n-1)/2.

_affine_min

min depends on slope sign.

_affine_max

max depends on slope sign.

_affine_std

std using closed-form: |slope| * sqrt((n^2-1)/12).

_affine_var

var using closed-form: slope^2 * (n^2-1) / 12.

_affine_any

O(1): any is True unless all values are zero (slope==0 and intercept==0).

_affine_all

O(1) when possible: check if any value in the sequence is zero.

_affine_prod

No useful closed form — materialize.

_register_affine_overrides

Register VTKAffineArray mixin as override for all vtkAffineArray template types.

API#

vtkmodules.numpy_interface.vtk_affine_array._lazy_result_dtype(ufunc, inputs, affine)#

Return the numpy scalar type of ufunc(*inputs), or None.

The ufunc is applied to an empty array of the affine array’s dtype in its place, so numpy’s own promotion rules decide the result type (and raise the same errors, such as for an out-of-bounds Python int) without materializing anything. None means VTK has no affine array of that type and the caller should materialize instead.

class vtkmodules.numpy_interface.vtk_affine_array.VTKAffineArray(shape=None, slope=0, intercept=0, **kwargs)#

Bases: vtkmodules.numpy_interface._vtk_array_mixin.VTKDataArrayMixin

A memory-efficient array whose values follow an affine function.

Each element is computed as slope * index + intercept, using O(1) memory regardless of array size. This is useful for representing regularly-spaced sequences (coordinates, time steps, indices) without allocating storage for every element.

This mixin is automatically applied to all vtkAffineArray instantiations. Any affine array that crosses from C++ to Python is already a VTKAffineArray instance.

Parameters

shape : int or tuple of int, optional Number of tuples (single int or 1-tuple), or (ntuples, ncomps) for a multi-component array. When omitted, creates an empty array. slope : scalar, optional Slope of the affine function (default 0). intercept : scalar, optional Intercept of the affine function (default 0).

Construction

Create with bracket notation and positional arguments::

from vtkmodules.vtkCommonCore import vtkAffineArray

# [0, 1, 2, ..., 99]
a = vtkAffineArray[numpy.float64](100, 1.0, 0.0)

# [10, 12, 14, ..., 30]  (11 elements)
b = vtkAffineArray[numpy.float64](11, 2.0, 10.0)

Available dtypes: 'float32', 'float64', 'int8', 'int16', 'int32', 'int64', 'uint8', 'uint16', 'uint32', 'uint64'.

Properties

shape : tuple of int (ntuples,) for 1-component, (ntuples, ncomps) otherwise. dtype : numpy.dtype The element data type. dataset : vtkDataSet or None The owning VTK dataset, when the array is attached to one. association : int or None The attribute association (POINT, CELL, etc.).

Lazy Arithmetic

Several operations produce a new affine array instead of materialising the full data:

  • arr * scalar scales slope and intercept.

  • arr + scalar shifts the intercept.

  • arr / scalar scales slope and intercept.

  • -arr negates slope and intercept.

  • Slicing with a step produces a new affine array with adjusted parameters.

O(1) Reductions

sum, mean, min, max, std, var, any, all, argmin, argmax use closed-form arithmetic series formulas and never materialise the array.

NumPy Integration

  • numpy.asarray(arr) materialises the full array.

  • numpy.sum(arr), numpy.mean(arr), etc. are intercepted and computed in O(1).

  • Ufuncs with scalar operands stay lazy when possible.

  • Unoptimized numpy functions trigger a warning and fall back to materialisation.

Indexing

Scalar indexing returns the computed value. Slicing a single-component array returns a new VTKAffineArray with adjusted parameters. The array is read-only; __setitem__ raises TypeError.

See Also

vtkAffineArray : The underlying C++ implicit array class. VTKConstantArray : Similar lazy evaluation for uniform-value arrays. VTKAOSArray : Mixin for regular (array-of-structures) VTK arrays.

Initialization

Create a new affine array.

Parameters

shape : int or tuple of int, optional Number of tuples, or (ntuples, ncomps) tuple. slope : scalar, optional Slope of the affine function (default 0). intercept : scalar, optional Intercept of the affine function (default 0). **kwargs Forwarded to the VTK base-class initializer.

_require_backend()#

Raise if backend is not constructed.

property _slope#
property _intercept#
property _num_values#
property _num_tuples#
property _num_components#
property _dtype#
classmethod _create_vtk_array(slope, intercept, num_values, dtype, name=None)#

Create a vtkAffineArray with the given parameters.

classmethod _from_params(slope, intercept, num_values, dtype)#

Create a new affine array from parameters.

property dtype#
property nbytes#
__repr__()#
__str__()#
to_numpy(dtype=None)#

Return the full materialized array as a numpy ndarray.

__array__(dtype=None, **kwargs)#

Materialize the full array.

__buffer__(flags)#

Override C-level buffer protocol to materialize via array.

__getitem__(index)#

Index into the array.

__setitem__(key, value)#
__iter__()#
__array_ufunc__(ufunc, method, *inputs, **kwargs)#

Handle numpy ufuncs. Many operations stay lazy.

__array_function__(func, types, args, kwargs)#

Handle numpy functions like sum, min, max, mean with optimized paths.

__add__(other)#
__radd__(other)#
__sub__(other)#
__rsub__(other)#
__mul__(other)#
__rmul__(other)#
__truediv__(other)#
__rtruediv__(other)#
__floordiv__(other)#
__rfloordiv__(other)#
__pow__(other)#
__rpow__(other)#
__mod__(other)#
__rmod__(other)#
__lt__(other)#
__le__(other)#
__eq__(other)#
__ne__(other)#
__ge__(other)#
__gt__(other)#
__neg__()#
__pos__()#
__abs__()#
_axis_is_flat(axis)#

True when axis reduces the entire 1-component array (axis=0 ≡ None).

sum(axis=None, **kwargs)#
mean(axis=None, **kwargs)#
min(axis=None, **kwargs)#
max(axis=None, **kwargs)#
std(axis=None, ddof=0, **kwargs)#
var(axis=None, ddof=0, **kwargs)#
any(axis=None, **kwargs)#
all(axis=None, **kwargs)#
prod(axis=None, **kwargs)#
argmin(axis=None, **kwargs)#
argmax(axis=None, **kwargs)#
vtkmodules.numpy_interface.vtk_affine_array._affine_sum(a, axis=None, **kwargs)#

sum = nintercept + slopen*(n-1)/2.

vtkmodules.numpy_interface.vtk_affine_array._affine_mean(a, axis=None, **kwargs)#

mean = intercept + slope*(n-1)/2.

vtkmodules.numpy_interface.vtk_affine_array._affine_min(a, axis=None, **kwargs)#

min depends on slope sign.

vtkmodules.numpy_interface.vtk_affine_array._affine_max(a, axis=None, **kwargs)#

max depends on slope sign.

vtkmodules.numpy_interface.vtk_affine_array._affine_std(a, axis=None, ddof=0, **kwargs)#

std using closed-form: |slope| * sqrt((n^2-1)/12).

vtkmodules.numpy_interface.vtk_affine_array._affine_var(a, axis=None, ddof=0, **kwargs)#

var using closed-form: slope^2 * (n^2-1) / 12.

vtkmodules.numpy_interface.vtk_affine_array._affine_any(a, axis=None, **kwargs)#

O(1): any is True unless all values are zero (slope==0 and intercept==0).

vtkmodules.numpy_interface.vtk_affine_array._affine_all(a, axis=None, **kwargs)#

O(1) when possible: check if any value in the sequence is zero.

vtkmodules.numpy_interface.vtk_affine_array._affine_prod(a, axis=None, **kwargs)#

No useful closed form — materialize.

vtkmodules.numpy_interface.vtk_affine_array._register_affine_overrides()#

Register VTKAffineArray mixin as override for all vtkAffineArray template types.