[Numpy-discussion] Misc Pyrex questions
Pierre GM
pgmdevlist at gmail.com
Mon Jan 22 18:59:03 EST 2007
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)
More information about the NumPy-Discussion
mailing list