[vtk-developers] Inconsistency in numpy_support

Prabhu Ramachandran prabhu at aero.iitb.ac.in
Sat Jan 10 12:31:22 EST 2009


Berk Geveci wrote:
>> 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.

Well, try this:

from numpy import *
x = linspace(0, 10)
y = x.copy()
y.shape = 50,1
z = x+y
print z.shape

The shape of the array would be (50,50).  So there is a reason a 1D 
array is treated differently.

The issue isn't one of what works in VTK but more of one where VTK and 
numpy play nicely together.  Adding a linspace array to a 1D VTK array 
can lead to nasty surprises if not done carefully enough.

>>> 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

Well, it isn't a matter of it being my code or not.  If it is broken it 
needs to be fixed. :)    My fear is backwards compatibility and not an 
arbitrary decision of mine.  In any case I will ask some of the numpy 
experts and see if they agree with my reasoning above.

> 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.

Feel free, the only caveat is the one I have raised above. You may or 
may not run into it depending on what you are doing.  In the general 
case I think my approach is correct but I will check and see just to be 
sure.

cheers,
prabhu



More information about the vtk-developers mailing list