Pyrex and numarray

M

Marco Bubke

Hi

I have tried to include numarray in Pyrex but I get allways
this error:

Traceback (most recent call last):
File "gears.py", line 9, in ?
import gl
File "/home/marco/projects/test_pyrex/gl.pyx", line 40, in gl
ctypedef class numarray.NumArray [object PyArrayObject]:
ValueError: numarray.NumArray does not appear to be the correct type object

The code is:

cdef extern from "numarray/numarray.h":
struct PyArray_Descr:
int type_num, elsize
char type


ctypedef class numarray.NumArray [object PyArrayObject]:
cdef char *data
cdef int nd
cdef int *dimensions, *strides
cdef object base
cdef PyArray_Descr *descr
cdef int flags
cdef object _data
cdef object _shadows
cdef long int nstrides
cdef long byteoffset
cdef long bytestride
cdef long itemsize
cdef char byteorder
cdef char _aligned
cdef char _contiguous
cdef char *wptr


def materialfv(GLenum face, GLenum pname, NumArray params):
"""
Set the material.

materialfv(enum face, enum pname, params)

params - should be a tuple

"""
assert (len(params) == 4 and pname in \
(AMBIENT, DIFFUSE, SPECULAR, EMISSION, AMBIENT_AND_DIFFUSE))\
or (len(params) == 1 and pname == SHININESS) \
or (len(param) == 1 and pname == COLOR_INDEXES),\
"arguments are not right"
str_params = params.tostring()
print str_params
cdef float* flat_params
flat_params = <float*>str_params
glMaterialfv(face, pname, flat_params)

I write a little OpenGL wrapper of my own(I know PyOpenGL but it doesn't fit
to me). Now I try to get the numarray to this function and cast it to a
float pointer(ugly hack, will be cleaned later).

regards

Marco
 
S

Simon Burton

Hi

I have tried to include numarray in Pyrex but I get allways this error:

Traceback (most recent call last):
File "gears.py", line 9, in ?
import gl
File "/home/marco/projects/test_pyrex/gl.pyx", line 40, in gl
ctypedef class numarray.NumArray [object PyArrayObject]:
ValueError: numarray.NumArray does not appear to be the correct type
object

Yes, i had that problem. Here is what I'm using now:

ctypedef class numarray._numarray._numarray [object PyArrayObject]:
# Compatibility with Numeric
cdef char *data
cdef int nd
#cdef int dimensions[MAXDIM], strides[MAXDIM]
cdef int *dimensions, *strides
cdef object base
cdef PyArray_Descr *descr
cdef int flags
# New attributes for numarray objects
cdef object _data # object must meet buffer API
cdef object _shadows # ill-behaved original array.
cdef int nstrides # elements in strides array
cdef long byteoffset # offset into buffer where array data begins
cdef long bytestride # basic seperation of elements in bytes
cdef long itemsize # length of 1 element in bytes
cdef char byteorder # NUM_BIG_ENDIAN, NUM_LITTLE_ENDIAN
cdef char _aligned # test override flag
cdef char _contiguous # test override flag

cdef extern from "numarray/libnumarray.h":
void import_libnumarray()
object NA_NewAll(
int ndim, maybelong* shape, NumarrayType type, void* buffer,
maybelong byteoffset, maybelong bytestride, int byteorder, int aligned, int writeable )
object NA_NewAllStrides(
int ndim, maybelong* shape, maybelong* strides, NumarrayType type, void* buffer,
int byteorder, int byteoffset, int aligned, int writeable )
object NA_New( void* buffer, NumarrayType type, int ndim,... )
object NA_Empty( int ndim, maybelong* shape, NumarrayType type )
object NA_NewArray( void* buffer, NumarrayType type, int ndim, ... )
object NA_vNewArray( void* buffer, NumarrayType type, int ndim, maybelong *shape )

object NA_InputArray (object, NumarrayType, int)
object NA_OutputArray (object, NumarrayType, int)
object NA_IoArray (object, NumarrayType, int)
object PyArray_FromDims(int nd, int *d, int type)
object NA_updateDataPtr(object)
object NA_getPythonScalar(object, long)
object NA_setFromPythonScalar(object, int, object)
#object NA_getPythonScalar(PyArrayObject, long)
#object NA_setFromPythonScalar(PyArrayObject, int, PyArrayObject)

object PyArray_ContiguousFromObject(object op, int type,
int min_dim, int max_dim)
#cdef void*NA_OFFSETDATA(object)
void*NA_OFFSETDATA(_numarray)
int NA_nameToTypeNo (char*)


Simon.

--
Simon Burton, B.Sc.
Licensed PO Box A66
ANU Canberra 2601
Australia
Ph. 02 6249 6940
http://arrowtheory.com
 
M

Marco Bubke

thx

but I need to extract the array to a float*.

I do NA_OFFSETDATA(NA_InputArray(odata, tFloat64, C_ARRAY)) but this isn't
working because Pyrex says he can't storage it. There is a unknown size. I
only need the pointer tp a C array of floats. Thats all.

ragards

Marco
 
S

Simon Burton

thx

but I need to extract the array to a float*.

I do NA_OFFSETDATA(NA_InputArray(odata, tFloat64, C_ARRAY)) but this isn't
working because Pyrex says he can't storage it. There is a unknown size. I
only need the pointer tp a C array of floats. Thats all.

ragards

Marco

Use a variable?

cdef float* data
array = NA_InputArray( array, natype, C_ARRAY )
data = <float *>NA_OFFSETDATA(array)

Simon.
 
M

Magnus Lie Hetland

[snip]

I tried using the set-up with Pyrex, but I'm having trouble
referencing some of the symbols. (I actually put all of it inside the
cdef extern declaration -- maybe I shouldn't have? I couldn't make it
compile otherwise...)

I manage to reference some of the symbols (such as maybelong) but
others trip me up... If I try to define a variable of type
PyArray_Descr gcc crashes, and I'm not sure why. The only thing that
isn't a warning is

na_wrap.c:43: storage size of `__pyx_v_x' isn't known

If I run import_libnumarray(), Python can't find the symbol. (It does
find libnumarray.so, though.) The same happens when I try to use
NA_InputArray:

symbol NA_InputArray: referenced symbol not found

Why, oh why? :]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top