Comments (5)
You can’t load Boolean arrays directly into vtk objects, so you have to convert them to and from uint8.
from pyvista.
Ahh, I see. Unfortunately, this comes at a price. I had some data arrays from using the vtkTIFFReader
that produced a data array of unit8
and this forced it to a boolean when I wrapped the output object:
>>> import vtk
>>> import vtki
>>> from vtk.util.numpy_support import vtk_to_numpy
>>> reader = vtk.vtkTIFFReader()
>>> reader.SetFileName('sample.tif')
>>> reader.Update()
>>> image = vtki.wrap(reader.GetOutputDataObject(0))
>>> image.point_arrays['Tiff Scalars'].dtype
dtype('bool')
>>> arr = image.GetPointData().GetArray('Tiff Scalars')
>>> vtk_to_numpy(arr).dtype
dtype('uint8')
from pyvista.
Ah, I can see why this is an issue. I’ll have to make some changes internally to account for this issue and remove this from the code. You can feel free to remove it if you wish.
from pyvista.
What about vtkBitArray
? We could send boolean arrays to this VTK type. I'm thinking to do this we'd need to implement our own vtk_to_numpy
and numpy_to_vtk
methods in the utilities to check the data types since the numpy_support
module in VTK cannot handle boolean/bit data arrays. Here's an example of going back and forth between bit arrays and boolean arrays (I implemented something similar in PVGeo for string arrays which might be good to transfer here as well):
import vtk
from vtk.util.numpy_support import vtk_to_numpy, numpy_to_vtk
import numpy as np
# Create some arbitrary vtkBitArray
arr = vtk.vtkBitArray()
n = 10
arr.SetNumberOfTuples(n)
for i in range(n):
arr.SetValue(i, i%2)
# Convert the vtkBitArray to NumPy boolean array
iarr = vtk.vtkCharArray()
iarr.DeepCopy(arr)
narr = np.frombuffer(iarr, dtype=bool)
# Convert NumPy boolean array back to a vtkBitArray
carr = numpy_to_vtk(narr.astype(np.uint8))
foo = vtk.vtkBitArray()
foo.DeepCopy(carr)
# Make sure the initial and final arrays are equal:
for i in range(n):
assert foo.GetValue(i) == arr.GetValue(i)
from pyvista.
This is probably the best approach since numpy doesn't support casting bitarray to uint8 casting, and frombuffer doesn't support it either.
I'm still leaving the old implementation to allow for in-place updating of native np.bool arrays, but I'm adding support for vtkBitArrays and uint8 will remain internally as uint8.
Fixed in dfacba2.
from pyvista.
Related Issues (20)
- `black` and `flake8` disagree on code style HOT 3
- pyvista.Plane results in approximate coordinates, slightly off prescribed ones HOT 3
- Add `precision` param for geometric objects to support 64bit float point meshes HOT 1
- pre-commit `mypy` config is not catching errors reliably
- split_bodies() generates a body that has 0 volume HOT 5
- [Feature Request, Camera] enabling parallel projection, roll, elevation, and azimuth in global/custom theme
- export mesh to Cesium (glTF/glb) HOT 11
- Terminal hijacking bug with latex-formatted plot labels HOT 1
- `PyVistaQt` and `MNE-Python` integration tests are failing
- Matplotlib axes instead of show_bounds
- Bounds `axes_range` is not modifiable
- VTKHDF plot in matplotlib
- before_close_callback Error HOT 3
- Wrong legend being add.
- Dataset filter contour not working properly HOT 4
- Missing top-level sphinx py:module reference for pyvista
- Add `autodoc-typehints` to docs.
- Crinkle clip duplicates cells in the unclipped surface
- Update docs for `Plotter.bounds`
- Wireframe visible when drawing a transparent object over a scalar grid
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pyvista.