Code Monkey home page Code Monkey logo

Comments (5)

akaszynski avatar akaszynski commented on May 17, 2024

You can’t load Boolean arrays directly into vtk objects, so you have to convert them to and from uint8.

from pyvista.

banesullivan avatar banesullivan commented on May 17, 2024

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.

akaszynski avatar akaszynski commented on May 17, 2024

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.

banesullivan avatar banesullivan commented on May 17, 2024

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.

akaszynski avatar akaszynski commented on May 17, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.