Pyrex and numarray

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Marco Bubke

    Pyrex and numarray

    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.NumArr ay [object PyArrayObject]:
    ValueError: numarray.NumArr ay 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.NumArr ay [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(GLen um 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_DIF FUSE))\
    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_par ams
    glMaterialfv(fa ce, 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

  • Simon Burton

    #2
    Re: Pyrex and numarray

    On Mon, 19 Jan 2004 19:33:28 +0100, Marco Bubke wrote:
    [color=blue]
    > 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.NumArr ay [object PyArrayObject]:
    > ValueError: numarray.NumArr ay does not appear to be the correct type
    > object
    >[/color]

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

    ctypedef class numarray._numar ray._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_ENDI AN
    cdef char _aligned # test override flag
    cdef char _contiguous # test override flag

    cdef extern from "numarray/libnumarray.h":
    void import_libnumar ray()
    object NA_NewAll(
    int ndim, maybelong* shape, NumarrayType type, void* buffer,
    maybelong byteoffset, maybelong bytestride, int byteorder, int aligned, int writeable )
    object NA_NewAllStride s(
    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_FromDim s(int nd, int *d, int type)
    object NA_updateDataPt r(object)
    object NA_getPythonSca lar(object, long)
    object NA_setFromPytho nScalar(object, int, object)
    #object NA_getPythonSca lar(PyArrayObje ct, long)
    #object NA_setFromPytho nScalar(PyArray Object, int, PyArrayObject)

    object PyArray_Contigu ousFromObject(o bject op, int type,
    int min_dim, int max_dim)
    #cdef void*NA_OFFSETD ATA(object)
    void*NA_OFFSETD ATA(_numarray)
    int NA_nameToTypeNo (char*)


    Simon.

    --
    Simon Burton, B.Sc.
    Licensed PO Box A66
    ANU Canberra 2601
    Australia
    Ph. 02 6249 6940




    Comment

    • Marco Bubke

      #3
      Re: Pyrex and numarray

      thx

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

      I do NA_OFFSETDATA(N A_InputArray(od ata, 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


      Comment

      • Simon Burton

        #4
        Re: Pyrex and numarray

        On Tue, 20 Jan 2004 13:48:42 +0100, Marco Bubke wrote:
        [color=blue]
        > thx
        >
        > but I need to extract the array to a float*.
        >
        > I do NA_OFFSETDATA(N A_InputArray(od ata, 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[/color]

        Use a variable?

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

        Simon.

        Comment

        • Magnus Lie Hetland

          #5
          Re: Pyrex and numarray

          In article <buj83b$sop$1@g raf.cs.uni-magdeburg.de>, Marco Bubke wrote:
          [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_libnumar ray(), 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? :]

          --
          Magnus Lie Hetland "The mind is not a vessel to be filled,
          http://hetland.org but a fire to be lighted." [Plutarch]

          Comment

          Working...