[Numpy-discussion] Misc Pyrex questions

Francesc Altet faltet at carabos.com
Mon Jan 22 10:18:24 CST 2007


El dl 22 de 01 del 2007 a les 10:07 -0500, en/na Pierre GM va escriure:
> Dear All,
> I started playing with Pyrex this week-end, to see how easy it would be to 
> port some subclasses to C. A good thing is that it's not as bad as I was 
> dreading. I do have a lot of question, however:
> 
> - Can I create a subclass w/ Pyrex ? If yes, how ? I haven't been able to find 
> any example in the numerous tutorials I browsed through, and my naive attempt 
> to:
> cdef NewSubclass(ndarray)
> didn't work at all.

You should first inform to Pyrex about the definition of ndarray. For
this, it's only necessary to declare this in a file (say
definitions.pxd):

cdef extern from "numpy/arrayobject.h":

  # Types
  ctypedef int npy_intp

  # Classes
  ctypedef extern class numpy.dtype [object PyArray_Descr]:
    cdef int type_num, elsize, alignment
    cdef char type, kind, byteorder, hasobject
    cdef object fields, typeobj

  ctypedef extern class numpy.ndarray [object PyArrayObject]:
    cdef char *data
    cdef int nd
    cdef npy_intp *dimensions
    cdef npy_intp *strides
    cdef object base
    cdef dtype descr
    cdef int flags

  # The NumPy initialization funtion
  void import_array()

and then include this in your sources:

from definitions cimport import_array, ndarray

from now on, Pyrex knows about the ndarray object and you should be able
to derive a class from it. Note that you should not forget to do a call
to import_array() prior to access the numpy machinery.

> -  I want to create an output ndarray from a given an input sequence/ndarray.
>  I'm using PyArray_FROM_OTF, which asks me for a dtype. How can I guess the 
> dtype of the input sequence ? In Python, a numpy.asarray(obj) would do the 
> trick, the dtype would be set to the maximum possible.

Just use the same syntax as in python:

cdef ndarray myvar
myvar = <ndarray>numpy.asarray(obj)

and then you can access myvar as a regular ndarray object. For example:

cdef char type
mytype = myvar.descr.type

That's one of the beauties of pyrex.

> 
> - What'd be a more pyrex-esque way to code the resize and reshape functions ?

Again, just use the python flavor:

myvar = myvar.reshape((...))

> 
> - Is there a standard way to deal w/ __getitem__ ? especially when the item is 
> not an integer (but a slice or a sequence) ?

  def __getitem__(self, fieldName):      
     if isinstance(key, int):
       ...
     elif isinstance(key, slice):
       ...
     elif isinstance(key, (list, tuple)):
       ...

Remember, the pyrex manual is your friend. In particular, for object
oriented programmimg with pyrex be sure to have a look at:

http://www.cosc.canterbury.ac.nz/greg.ewing/python/Pyrex/version/Doc/extension_types.html

Cheers,

-- 
Francesc Altet    |  Be careful about using the following code --
Carabos Coop. V.  |  I've only proven that it works, 
www.carabos.com   |  I haven't tested it. -- Donald Knuth



More information about the Numpy-discussion mailing list