[vtk-developers] Inconsistency in numpy_support

Berk Geveci berk.geveci at kitware.com
Sun Jan 11 13:18:43 EST 2009


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

You are right. The current behavior is consistent with numpy.

 x = ones((5,3))
 y = x[:,0]

returns an array of shape (5,) instead of (5,1), for example. It also
make sense to be more consistent with numpy in this case. It is still
inconsistent with VTK though :-) In VTK, a scalar is still a 2D data
structure where the number of components is 1. So it is possible to
call GetComponent(n,0). I guess this is one of those we can live with.

So, how would you do normalize a VTK vector using numpy? Like this?

v / sqrt(v[:,0]**2+v[:,1]**2+v[:,2]**2).reshape(v.shape[0], 1)

It is the reshape call (or any other re-indexing) that I don't like.
Is there a better way of doing this?

-berk



More information about the vtk-developers mailing list