np.void from 0d array + subclassing
All, * What is the most efficient way to get a np.void object from a 0d structured ndarray ? * Is there any way to subclass np.void ? Thanks a lot in advance ! P.
A Thursday 17 December 2009 15:16:29 Pierre GM escrigué:
All, * What is the most efficient way to get a np.void object from a 0d structured ndarray ?
I normally use `PyArray_GETITEM` C macro for general n-d structured arrays. I suppose that this will work with 0-d arrays too. -- Francesc Alted
On Dec 17, 2009, at 10:16 AM, Francesc Alted wrote:
A Thursday 17 December 2009 15:16:29 Pierre GM escrigué:
All, * What is the most efficient way to get a np.void object from a 0d structured ndarray ?
I normally use `PyArray_GETITEM` C macro for general n-d structured arrays. I suppose that this will work with 0-d arrays too.
Francesc, you're overestimating my knowledge of C... Can we stick to the Python implementation ? Here's the catch: IIUC, each individual element of a nD structured array is a void, provided the element can be accessed, ie that n>0. A 0D array cannot be indexed, so I don't know how capture the object below. The sad trick I found was to do a .reshape(1)[0], but that looks really overkill...
The standard way (more or less) works for me:
class myvoidclass(np.void): ... pass ...
David, what do you do w/ the __new__ of myvoidclass ? Just an empty class doesn't help me much, 'm'fraid.
On Thu, Dec 17, 2009 at 2:11 PM, Pierre GM <pgmdevlist@gmail.com> wrote:
Francesc, you're overestimating my knowledge of C... Can we stick to the Python implementation ? Here's the catch: IIUC, each individual element of a nD structured array is a void, provided the element can be accessed, ie that n>0. A 0D array cannot be indexed, so I don't know how
Unless someone can explain why it isn't, this sounds like an API inconsistency, which in turn I would characterize as a bug. But others may disagree and/or explain it away...
capture the object below. The sad trick I found was to do a .reshape(1)[0], but that looks really overkill...
The standard way (more or less) works for me:
class myvoidclass(np.void): ... pass ...
David, what do you do w/ the __new__ of myvoidclass ? Just an empty class doesn't help me much, 'm'fraid.
Presumably, whatever you want (i.e., override it, calling the base class constructor inside your __new__ if/when needed) - I've never done this, so I have no reason to believe it would/should behave any differently than any other Python subclass; your question merely provoked me to check to see if the normal subclassing syntax did not work for some reason, and since I found that it did, I thought I'd post that result as a "data point". Now, if you're generally unfamiliar (but it doesn't sound like you are) with what to do with a subclass' __new__, I'm sure someone else can more easily point you to a reference for that issue. Is there some reason you believe you have to override __new__ differently in your use-case? DG
_______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
On Thu, Dec 17, 2009 at 16:11, Pierre GM <pgmdevlist@gmail.com> wrote:
On Dec 17, 2009, at 10:16 AM, Francesc Alted wrote:
A Thursday 17 December 2009 15:16:29 Pierre GM escrigué:
All, * What is the most efficient way to get a np.void object from a 0d structured ndarray ?
I normally use `PyArray_GETITEM` C macro for general n-d structured arrays. I suppose that this will work with 0-d arrays too.
Francesc, you're overestimating my knowledge of C... Can we stick to the Python implementation ? Here's the catch: IIUC, each individual element of a nD structured array is a void, provided the element can be accessed, ie that n>0. A 0D array cannot be indexed, so I don't know how capture the object below. The sad trick I found was to do a .reshape(1)[0], but that looks really overkill...
a[()] -- Robert Kern "I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco
On Dec 17, 2009, at 6:35 PM, Robert Kern wrote:
On Thu, Dec 17, 2009 at 16:11, Pierre GM <pgmdevlist@gmail.com> wrote:
On Dec 17, 2009, at 10:16 AM, Francesc Alted wrote:
A Thursday 17 December 2009 15:16:29 Pierre GM escrigué:
All, * What is the most efficient way to get a np.void object from a 0d structured ndarray ?
I normally use `PyArray_GETITEM` C macro for general n-d structured arrays. I suppose that this will work with 0-d arrays too.
Francesc, you're overestimating my knowledge of C... Can we stick to the Python implementation ? Here's the catch: IIUC, each individual element of a nD structured array is a void, provided the element can be accessed, ie that n>0. A 0D array cannot be indexed, so I don't know how capture the object below. The sad trick I found was to do a .reshape(1)[0], but that looks really overkill...
a[()]
Well, that's slick and really neat !!! Typically Robert K's... Thanks a lot !!! And would you have anything as cool for the reverse operation (from np.void to 0d array) ?
On Thu, Dec 17, 2009 at 17:41, Pierre GM <pgmdevlist@gmail.com> wrote:
On Dec 17, 2009, at 6:35 PM, Robert Kern wrote:
On Thu, Dec 17, 2009 at 16:11, Pierre GM <pgmdevlist@gmail.com> wrote:
Here's the catch: IIUC, each individual element of a nD structured array is a void, provided the element can be accessed, ie that n>0. A 0D array cannot be indexed, so I don't know how capture the object below. The sad trick I found was to do a .reshape(1)[0], but that looks really overkill...
a[()]
Well, that's slick and really neat !!! Typically Robert K's... Thanks a lot !!! And would you have anything as cool for the reverse operation (from np.void to 0d array) ?
a = np.array(v) -- Robert Kern "I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco
On Thu, Dec 17, 2009 at 3:46 PM, Robert Kern <robert.kern@gmail.com> wrote:
On Thu, Dec 17, 2009 at 17:41, Pierre GM <pgmdevlist@gmail.com> wrote:
Well, that's slick and really neat !!! Typically Robert K's... Thanks a lot !!!
Yeah, sometimes I think the only reason we have a mailing list is so that we're not all just emailing Robert all the time... ;-) DG
On Thu, Dec 17, 2009 at 6:16 AM, Pierre GM <pgmdevlist@gmail.com> wrote:
All, * What is the most efficient way to get a np.void object from a 0d structured ndarray ? * Is there any way to subclass np.void ?
class myvoidclass(np.void): ... pass ... foo = myvoidclass() Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: function takes exactly 1 argument (0 given) foo = myvoidclass(1) dir(foo) ['T', '__abs__', '__add__', '__and__', '__array__', '__array_interface__', '__array_priority__', '__array_struct__', '__ array_wrap__', '__class__', '__copy__', '__deepcopy__', '__delattr__', '__delitem__', '__dict__', '__div__', '__divmod__ ', '__doc__', '__eq__', '__float__', '__floordiv__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__hex__', '__init__', '__int__', '__invert__', '__le__', '__len__', '__long__', '__lshift__', '__lt__', '__mod__', '__m odule__', '__mul__', '__ne__', '__neg__', '__new__', '__nonzero__', '__oct__', '__or__', '__pos__', '__pow__', '__radd__ ', '__rand__', '__rdiv__', '__rdivmod__', '__reduce__', '__reduce_ex__', '__repr__', '__rfloordiv__', '__rlshift__', '__ rmod__', '__rmul__', '__ror__', '__rpow__', '__rrshift__', '__rshift__', '__rsub__', '__rtruediv__', '__rxor__', '__seta ttr__', '__setitem__', '__setstate__', '__str__', '__sub__', '__truediv__', '__weakref__', '__xor__', 'all', 'any', 'arg max', 'argmin', 'argsort', 'astype', 'base', 'byteswap', 'choose', 'clip', 'compress', 'conj', 'conjugate', 'copy', 'cum
The standard way (more or less) works for me: prod', 'cumsum', 'data', 'diagonal', 'dtype', 'dump', 'dumps', 'fill', 'flags', 'flat', 'flatten', 'getfield', 'imag', ' item', 'itemset', 'itemsize', 'max', 'mean', 'min', 'nbytes', 'ndim', 'newbyteorder', 'nonzero', 'prod', 'ptp', 'put', ' ravel', 'real', 'repeat', 'reshape', 'resize', 'round', 'searchsorted', 'setfield', 'setflags', 'shape', 'size', 'sort', 'squeeze', 'std', 'strides', 'sum', 'swapaxes', 'take', 'tofile', 'tolist', 'tostring', 'trace', 'transpose', 'var', 'v iew'] Is there something more specific you want to do? DG
Thanks a lot in advance ! P. _______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
participants (4)
-
David Goldsmith -
Francesc Alted -
Pierre GM -
Robert Kern