[vtk-developers] Inconsistency in numpy_support

Berk Geveci berk.geveci at kitware.com
Tue Jan 6 13:27:00 EST 2009


Hi Prabhu,

>> I have been playing around with numpy-vtk array conversion. I think
>> there is an inconsistency in the way numpy_support converts VTK arrays
>> to numpy arrays. If the array has 1 component, vtk_to_numpy returns an
>> array of shape (10,), whereas if the array has 3 components,
>> vtk_to_numpy returns an array of shape (10,3). This makes the
>> following impossible
>>
>> vtk_to_numpy(scalar) + vtk_to_numpy(vector)
>
> Try this:
>
> vtk_to_numpy(scalar)[:,None] + vtk_to_numpy(vector)
>
> This is the way to broadcast the 1D array so the addition is possible.

Sure, I can do that or reshape but I think it is a matter of
consistency. Your example or another version with reshape() look ugly
and confusing. VTK does not differentiate between a 1 component and an
n component array. Why would the numpy conversion do so? As far as VTK
is concerned, a scalar is a column vector and an n-component array is
a n-column matrix.

>> Shouldn't  vtk_to_numpy return an array of shape (10,1) when converting
>> scalars?
>
> Not really since a (10,1) array is like a column vector.  If you did
>
>  x = linspace(0, 10)
>
> you end up with an array with shape (50,) and not (50,1) so it seems more
> natural to return a 1 component array with 10 elements as a (10,) shape
> array.

I disagree but I will defer to your decision since it is your code
originally. However, I will use a modified version of the
numpy_support in my code since I want the users to be able to intermix
scalars with other arrays without having to reshape them. My goal is
to replace the array calculator with Python so I want to get as close
as possible to simple expressions.

Best,
-berk



More information about the vtk-developers mailing list