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. - 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. - What'd be a more pyrex-esque way to code the resize and reshape functions ? - Is there a standard way to deal w/ __getitem__ ? especially when the item is not an integer (but a slice or a sequence) ? Thanks a lot in advance for your help. P. PS: Sorry for the cross-posting
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/extensi... 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
On Monday 22 January 2007 11:18, Francesc Altet wrote:
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):
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.
OK, perfect. Turned out that it was quite silly: I had already two statements inlude 'Python.pxi' include 'numpy.pxi' taken from numpy.random.mtrand, and I was trying to subclass through `numpy.ndarray` when just `ndarray` was what I wanted... Wow, I'm impressed. [about guessing the dtype]
cdef ndarray myvar myvar = <ndarray>numpy.asarray(obj)
That easily ? What's the point of all the PyArray_FROM or _from or _ContiguousFrom implemented in mtrand.numpy.pxi ? I'm afraid I'm missing a point, here... Other example: what's more efficient ? myvar = <ndarray>numpy.empty(shape,dtype) or myvar = PyArray_EMPTY(dims, NPY_TYPE)
- What'd be a more pyrex-esque way to code the resize and reshape functions ?
Again, just use the python flavor:
myvar = myvar.reshape((...))
I used that so far and it works nicely, but I wonder whether it was that efficient, hence my question for a more pyrex-esque way. Corollary below.
- 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)):
OK, I guess a specific example will help me explain myself: One of the issues with the new implementation of MaskedArray is that we overload __getitem__, which slow things down. I just copied the corresponding portion of the python code, namely _data = self._data.__getitem__(index) _mask = self._data.__getitem__(index) pasted it in the .pyx I'm writing, and lo! It works ! But elsewhere in the manual is given the example of a loop using `range`, when one should use some explicit interval, and my understanding was that using python expressions was not as efficient as having more proper C expressions. Is this the case here ? Do I have to reimplement __getitem__ on arrays, or could I just keep on using the current approach ?
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/extens ion_types.html
Ahah ! Some more doc ! I couldn't find the link to it on the pyrex front page... (And obviously, I didn't look close enough in the overview page...) My bad, sorry. Francesc, thanks again. I'll probably contact you very soon off list (and will start thinking about a wiki page)
participants (2)
-
Francesc Altet -
Pierre GM