[vtkusers] Cython
David Gobbi
david.gobbi at gmail.com
Mon Jan 18 15:04:35 EST 2016
Hi Nil,
Classes like vtkCellArray and vtkPoints use a vtkDataArray to store their
data, and vtkDataArray provides a python buffer interface via the
wrappers. It is this buffer interface that the vtk numpy_interface package
uses to efficiently integrate numpy with VTK.
At the low level, it works like this: vtkDataArray provides a python buffer
interface, so a vtkDataArray can be passed as a parameter to any python
method that expects a buffer object, which allows a numpy array to be
constructed from vtkDataArray.
To get at the vtkDataArray inside of a vtkPoints object, you can use
SetData()/GetData():
SetData(vtkDataArray) // sets the data array used by vtkPoints
GetData() -> vtkDataArray // gets the data array used by vtkPoints
In addition to making a numpy array from a vtkDataArray, It is also
possible to make a vtkDataArray use the memory allocated for a numpy array
by calling vtkDataArray.SetVoidArray(). This has to be done with extreme
caution, however, so numpy_interface is probably the way to go.
- David
On Mon, Jan 18, 2016 at 12:06 PM, Berk Geveci <berk.geveci at kitware.com>
wrote:
> I am assuming that there is a way of passing numpy arrays from regular
> Python to Cython compiled code? If there is, you can always get access to
> VTK pointers as numpy arrays through the numpy interface. This would look
> like this:
>
> In (C) Python:
>
> Get VTK object
> Get various data structures (arrays, cell arrays etc.) as numpy arrays
> Call Cython code
>
> In Cython
>
> Do some processing
> Generate new arrays
> Return as numpy arrays to (C) Python
>
> In (C) Python
>
> Stick numpy arrays back into VTK data structures such as arrays, cell
> arrays etc.
>
> Best,
> -berk
>
>
> On Mon, Jan 18, 2016 at 2:02 PM, Nil Goyette <nil.goyette at imeka.ca> wrote:
>
>> Hi Cory,
>>
>> Thank you for the answer. Yes, I know that vtk is coded in C++ and that
>> python-vtk is only a binding. I thought that maybe there was a way to avoid
>> some of the expensive calls. For example, I have this loop in a cython file:
>>
>> # streamline is a ndarray of 3d points
>> for i in range(nb_points):
>> point_3d = streamline[i]
>> vtk_points.InsertNextPoint(point_3d)
>>
>> I know that InsertNextPoint is as fast as it can be, but the call itself
>> is expensive. As you said, there's probably nothing to be done here using
>> cython.
>>
>> Which bring me to another question! As I said above, I have a 2D ndarray
>> (list of 3d points) that I need to put in a vtkPoints object. I think it's
>> a waste of time to copy the data in a loop because both vtk and numpy
>> represent their data the same way and should be able to read the data of
>> the other library.
>>
>> I found the "VTK - numpy integration" posts this morning
>> http://www.kitware.com/blog/home/post/709 and I thought that maybe there
>> a way to create a vtkPoints from a numpy array without a loop. I couldn't
>> find how to do it though. It seems we can only create vtkDataSets and put
>> them in bbtk objects. Do you know if what I want to do is possible?
>>
>> Nil
>>
>> Le 2016-01-16 07:59, Cory Quammen a écrit :
>>
>> Nil,
>>
>> VTK is C++ library that provides Python language bindings, so all the
>> VTK parts you call in your Python code are invoking compiled C++
>> functions. There is no need to cythonize them.
>>
>> Cory
>>
>> On Fri, Jan 15, 2016 at 11:30 AM, Nil Goyette <nil.goyette at imeka.ca> <nil.goyette at imeka.ca> wrote:
>>
>> Hi all,
>>
>> I tried making a part of my pytthon code faster and vtk is the only part
>> which I can't change anything. In fact, I can't find any information on
>> cythonizing vtk. Maybe because one should use C++ if he wants a fast version
>> :) or maybe because there's simply not much information on the subject.
>>
>> Is there some specific advices to vtk? Where to find the cdef?
>> Or it's simply the general advices like: get a pointer to the data asap to
>> be in C mode, then cdef everything?
>>
>> Nil
>>
>>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/vtkusers/attachments/20160118/593baf72/attachment.html>
More information about the vtkusers
mailing list