| 1 |
"""Contains pyvista_ndarray a numpy ndarray type used in pyvista.""" |
| 2 |
|
| 3 |
from __future__ import annotations |
| 4 |
|
| 5 |
from collections.abc import Iterable |
| 6 |
from typing import TYPE_CHECKING |
| 7 |
from typing import cast |
| 8 |
|
| 9 |
import numpy as np |
| 10 |
|
| 11 |
from pyvista import _vtk |
| 12 |
from pyvista.core._vtk_utilities import VTKObjectWrapperCheckSnakeCase |
| 13 |
|
| 14 |
from .utilities.arrays import FieldAssociation |
| 15 |
from .utilities.arrays import convert_array |
| 16 |
from .utilities.misc import _NoNewAttrMixin |
| 17 |
|
| 18 |
if TYPE_CHECKING: |
| 19 |
from typing import Any |
| 20 |
|
| 21 |
import numpy.typing as npt |
| 22 |
|
| 23 |
from pyvista import DataSet |
| 24 |
|
| 25 |
from ._typing_core import ArrayLike |
| 26 |
from ._typing_core import NumpyArray |
| 27 |
|
| 28 |
|
| 29 |
class pyvista_ndarray(_NoNewAttrMixin, np.ndarray): # numpydoc ignore=PR02 # noqa: N801 |
| 30 |
"""A ndarray which references the owning dataset and the underlying vtk array. |
| 31 |
|
| 32 |
This array can be acted upon just like a :class:`numpy.ndarray`. |
| 33 |
|
| 34 |
Parameters |
| 35 |
---------- |
| 36 |
array : ArrayLike or :vtk:`vtkAbstractArray` |
| 37 |
Array like. |
| 38 |
|
| 39 |
dataset : DataSet |
| 40 |
Input dataset. |
| 41 |
|
| 42 |
association : pyvista.core.utilities.arrays.FieldAssociation |
| 43 |
Field association. |
| 44 |
|
| 45 |
Examples |
| 46 |
-------- |
| 47 |
Return the points of a Sphere as a :class:`pyvista.pyvista_ndarray`. |
| 48 |
|
| 49 |
>>> import pyvista as pv |
| 50 |
>>> mesh = pv.Sphere() |
| 51 |
>>> mesh.points # doctest:+SKIP |
| 52 |
pyvista_ndarray([[-5.5511151e-17, 0.0000000e+00, -5.0000000e-01], |
| 53 |
[ 5.5511151e-17, 0.0000000e+00, 5.0000000e-01], |
| 54 |
[-5.4059509e-02, 0.0000000e+00, -4.9706897e-01], |
| 55 |
..., |
| 56 |
[-1.5616201e-01, -3.3193260e-02, 4.7382659e-01], |
| 57 |
[-1.0513641e-01, -2.2347433e-02, 4.8831028e-01], |
| 58 |
[-5.2878179e-02, -1.1239604e-02, 4.9706897e-01]], |
| 59 |
dtype=float32) |
| 60 |
|
| 61 |
""" |
| 62 |
|
| 63 |
def __new__( # noqa: PYI034 |
| 64 |
cls: type[pyvista_ndarray], |
| 65 |
array: ArrayLike[float] | _vtk.vtkAbstractArray, |
| 66 |
dataset: DataSet | _vtk.vtkDataSet | _vtk.VTKObjectWrapper | None = None, |
| 67 |
association: FieldAssociation = FieldAssociation.NONE, |
| 68 |
) -> pyvista_ndarray: |
| 69 |
"""Allocate the array.""" |
| 70 |
if isinstance(array, _vtk.vtkAbstractArray): |
| 71 |
obj = convert_array(array).view(cls) |
| 72 |
obj.VTKObject = array |
| 73 |
elif isinstance(array, Iterable): |
| 74 |
obj = np.asarray(array).view(cls) |
| 75 |
else: |
| 76 |
msg = ( # type: ignore[unreachable] |
| 77 |
f'pyvista_ndarray got an invalid type {type(array)}. ' |
| 78 |
'Expected an Iterable or vtk.vtkAbstractArray' |
| 79 |
) |
| 80 |
raise TypeError(msg) |
| 81 |
|
| 82 |
obj.association = association |
| 83 |
if dataset is None: |
| 84 |
obj.dataset = None |
| 85 |
else: |
| 86 |
obj.dataset = _vtk.vtkWeakReference() |
| 87 |
if isinstance(dataset, _vtk.VTKObjectWrapper): |
| 88 |
obj.dataset.Set(dataset.VTKObject) |
| 89 |
else: |
| 90 |
obj.dataset.Set(cast('_vtk.vtkDataSet', dataset)) |
| 91 |
return obj |
| 92 |
|
| 93 |
def __array_finalize__(self: pyvista_ndarray, obj: npt.NDArray[Any] | None) -> None: |
| 94 |
"""Finalize array (associate with parent metadata).""" |
| 95 |
# this is necessary to ensure that views/slices of pyvista_ndarray |
| 96 |
# objects stay associated with those of their parents. |
| 97 |
# |
| 98 |
# the VTKArray class uses attributes called `DataSet` and `Association` |
| 99 |
# to hold this data. I don't know why this class doesn't use the same |
| 100 |
# convention, but here we just map those over to the appropriate |
| 101 |
# attributes of this class |
| 102 |
_vtk.VTKArray.__array_finalize__(self, obj) # type: ignore[arg-type] |
| 103 |
if np.shares_memory(self, obj): |
| 104 |
self.dataset = getattr(obj, 'dataset', None) |
| 105 |
self.association = getattr(obj, 'association', FieldAssociation.NONE) |
| 106 |
self.VTKObject = getattr(obj, 'VTKObject', None) |
| 107 |
else: |
| 108 |
self.dataset = None |
| 109 |
self.association = FieldAssociation.NONE |
| 110 |
self.VTKObject = None |
| 111 |
|
| 112 |
def __setitem__(self: pyvista_ndarray, key: int | NumpyArray[int], value: Any) -> None: # type: ignore[override] |
| 113 |
"""Implement [] set operator. |
| 114 |
|
| 115 |
When the array is changed it triggers "Modified()" which updates |
| 116 |
all upstream objects, including any render windows holding the |
| 117 |
object. |
| 118 |
""" |
| 119 |
super().__setitem__(key, value) |
| 120 |
if self.VTKObject is not None: |
| 121 |
self.VTKObject.Modified() |
| 122 |
|
| 123 |
# the associated dataset should also be marked as modified |
| 124 |
dataset = self.dataset |
| 125 |
if dataset is not None and dataset.Get(): |
| 126 |
dataset.Get().Modified() |
| 127 |
|
| 128 |
def __array_wrap__(self: pyvista_ndarray, out_arr, context=None, return_scalar: bool = False): # noqa: ANN001, ANN204, FBT001, FBT002 |
| 129 |
"""Return a numpy scalar if array is 0d. |
| 130 |
|
| 131 |
See https://github.com/numpy/numpy/issues/5819 |
| 132 |
|
| 133 |
""" |
| 134 |
if out_arr.ndim: |
| 135 |
return super().__array_wrap__(out_arr, context, return_scalar) |
| 136 |
|
| 137 |
# Match numpy's behavior and return a numpy dtype scalar |
| 138 |
return out_arr[()] |
| 139 |
|
| 140 |
__getattr__ = VTKObjectWrapperCheckSnakeCase.__getattr__ |