[vtk-developers] numpy_to_vtk - contiguous array error
Eric E. Monson
emonson at cs.duke.edu
Mon Nov 8 15:27:56 EST 2010
Hey Randy,
I'm not an expert on this, but the issue is with whether the numpy array is in "C" or "Fortran" order in memory -- numpy_to_vtk() expects "C" ordering. You can see whether the array is "C_CONTIGUOUS" or "F_CONTIGUOUS" by typing array_name.flags
There might be a better way, but you can get around this by making a copy that has the right order, either by invoking .copy() which uses the "C" default, or by explicitly copying with the correct order flag.
-Eric
# =================================
In [1]: import numpy as N
In [2]: from vtk.util import numpy_support as VN
In [3]: xx = N.array([[1,2],[3,4]])
In [4]: xx.flags
Out[4]:
C_CONTIGUOUS : True
F_CONTIGUOUS : False
OWNDATA : True
WRITEABLE : True
ALIGNED : True
UPDATEIFCOPY : False
In [5]: VN.numpy_to_vtk(xx)
Out[5]: (vtkLongArray)0x10bea32a0
In [6]: xx = N.array([[1,2],[3,4]], order='F')
In [7]: xx
Out[7]:
array([[1, 2],
[3, 4]])
In [8]: VN.numpy_to_vtk(xx)
---------------------------------------------------------------------------
AssertionError Traceback (most recent call last)
/Users/emonson/Programming/Python/VTK/Charts/<ipython console> in <module>()
/Users/emonson/Programming/VTK_git/VTK/build/Wrapping/Python/vtk/util/numpy_support.pyc in numpy_to_vtk(num_array, deep, array_type)
130
131 shape = z.shape
--> 132 assert z.flags.contiguous, 'Only contiguous arrays are supported.'
133 assert len(shape) < 3, \
134 "Only arrays of dimensionality 2 or lower are allowed!"
AssertionError: Only contiguous arrays are supported.
In [9]: xx.flags
Out[9]:
C_CONTIGUOUS : False
F_CONTIGUOUS : True
OWNDATA : True
WRITEABLE : True
ALIGNED : True
UPDATEIFCOPY : False
In [10]: yy = N.array(xx, order='C')
In [11]: VN.numpy_to_vtk(yy)
Out[11]: (vtkLongArray)0x10c017270
In [12]: zz = xx.copy()
In [13]: VN.numpy_to_vtk(yy)
Out[13]: (vtkLongArray)0x10c0178d0
On Nov 8, 2010, at 2:27 PM, Randy Heiland wrote:
> Can someone shed light on the following error?
>
> >>> teMtx
> array([[ 0.00000000e+00, 2.21433476e-07, 1.29724158e-06, ...,
> 1.13706443e-06, 1.22732023e-06, 1.80498142e-06],
> [ 3.49344691e-07, -1.63000845e-07, 4.27805878e-06, ...,
> 5.69157035e-07, 1.44623813e-07, 1.07077057e-07],
> [ 1.12878051e-08, 7.65250526e-06, -3.56727587e-09, ...,
> 4.44896483e-08, 5.03697289e-07, 2.19654679e-07],
> ...,
> [ 9.13691496e-08, 8.34874369e-08, 6.68844738e-07, ...,
> 0.00000000e+00, 4.14336786e-06, 1.54018126e-07],
> [ 1.56768727e-06, 1.26927141e-07, 1.86173239e-06, ...,
> 1.76178961e-06, -2.94978281e-06, 2.25371326e-06],
> [ 2.13822621e-06, 2.01947558e-08, 6.85551555e-07, ...,
> 3.93482720e-07, 8.21207577e-07, 0.00000000e+00]])
> >>> type(teMtx)
> <type 'numpy.ndarray'>
> >>> teMtx.size
> 106929
> >>> teMtx.shape
> (327, 327)
> >>> vtk_arr2 = numpy_to_vtk(teMtx)
> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> File "<my-path>/Wrapping/Python/vtk/util/numpy_support.py", line 132, in numpy_to_vtk
> assert z.flags.contiguous, 'Only contiguous arrays are supported.'
> AssertionError: Only contiguous arrays are supported.
>
> However, the following works fine:
> >>> m2 = numpy.array([[1.0,2.0],[3.0,4.0]])
> >>> type(m2)
> <type 'numpy.ndarray'>
> >>> m2.size
> 4
> >>> m2.shape
> (2, 2)
> >>> vtk_arr_m2 = numpy_to_vtk(m2)
> >>>
>
> >>> vtk.vtkVersion.GetVTKVersion()
> '5.7.0'
>
>
> teMtx happens to come from a 'scipy.io.loadmat', however, via inspection, it appears to be the same as the simpler m2 array.
>
> thanks, Randy
>
> _______________________________________________
> Powered by www.kitware.com
>
> Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html
>
> Follow this link to subscribe/unsubscribe:
> http://www.vtk.org/mailman/listinfo/vtk-developers
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/vtk-developers/attachments/20101108/b0e33a12/attachment.html>
More information about the vtk-developers
mailing list