<div class="gmail_quote">On Thu, Jul 28, 2011 at 8:54 AM, Martin Ling <span dir="ltr"><<a href="mailto:martin-numpy@earth.li">martin-numpy@earth.li</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
Hi,<br>
<br>
I'd like to kick off some discussion on general issues I've encountered<br>
while developing the quaternion dtype (see other thread, and the code<br>
at: <a href="https://github.com/martinling/numpy_quaternion" target="_blank">https://github.com/martinling/numpy_quaternion</a>)<br>
<br>
The basic issue is that the attributes of ndarray cannot be adapted<br>
to the dtype of a given array. Indeed, their behaviour can't be changed<br>
at all without patching numpy itself.<br>
<br>
There are essentially four cases of the problem:<br>
<br>
1. Attributes which do the wrong thing even though there is a mechanism<br>
   that should let them do the right thing, e.g:<br>
<br>
   >>> a = array([quaternion(1,2,3,4), quaternion(5,6,7,8)])<br>
<br>
   >>> conjugate(a) # correct, calls conjugate ufunc I defined<br>
   array([quaternion(1, -2, -3, -4), quaternion(5, -6, -7, -8)], dtype=quaternion)<br>
<br>
   >>> a.conjugate() # incorrect, why doesn't it do the same?<br>
   array([quaternion(1, 2, 3, 4), quaternion(5, 6, 7, 8)], dtype=quaternion)<br>
<br>
   >>> min(a) # works, calls min ufunc I defined<br>
   quaternion(1, 2, 3, 4)<br>
<br>
   >>> a.min() # fails, why doesn't it do the same?<br>
   ValueError: No cast function available.<br>
<br>
2. Attributes that do the wrong thing with no mechanism to override them:<br>
<br>
   >>> array([q.real for q in a])<br>
   array([ 1.,  5.])<br>
<br>
   >>> a.real # would like this to return the same, can't make it do so<br>
   array([quaternion(1, 2, 3, 4), quaternion(5, 6, 7, 8)], dtype=quaternion)<br>
<br>
3. Attributes that don't exist and could be added to suit the dtype:<br>
<br>
   >>> array([q.y for q in a])<br>
   array([ 3.,  7.])<br>
<br>
   >>> a.y # would like this to return the same, can't make it do so<br>
   AttributeError: 'numpy.ndarray' object has no attribute 'y'<br>
<br>
4. Attributes that already exist and make no sense for some dtypes:<br>
<br>
   >>> sa = array(['foo', 'bar', 'baz'])<br>
<br>
   >>> sa.imag # why can I even do this?<br>
   array(['', '', ''], dtype='|S3')<br>
<br>
We had ѕome discussion about this at the SciPy conference sprints and<br>
the consensus seemed to be that allowing dtypes to customize the<br>
attributes of ndarrays would be a good thing. This would also be useful<br>
for struct arrays, datetime arrays, etc.<br>
<br>
What do people think?<br></blockquote><div><br></div><div>I was part of this discussion at SciPy, and while I was initially skeptical of giving dtypes the ability to add properties and functions to arrays built with them, the discussion at the SciPy sprint convinced me otherwise. Since the author of a dtype is fully aware of what properties and functions an array already have, they can avoid name collisions in a straightforward way. This is different from the recarray case, where assigning field names can be a lot more haphazard, and it's perfectly sane to want a field called 'sum' conflicting with the arr.sum() array method.</div>
<div><br></div><div>One example where this would help is with the datetime64 type. I suggested that it might be good to automatically convert Python's datetime objects into datetime64 arrays. Here's a pull request Ben Walsh did towards that:</div>
<div><br></div><div><meta http-equiv="content-type" content="text/html; charset=utf-8"><a href="https://github.com/numpy/numpy/pull/111">https://github.com/numpy/numpy/pull/111</a></div><div><br></div><div>The point he raises, that np.array([datetime.date(2000, 1, 1)])[0].year would fail, could be addressed through this mechanism.</div>
<div><br></div><div>-Mark</div><meta http-equiv="content-type" content="text/html; charset=utf-8"><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<br>
<br>
Martin<br>
_______________________________________________<br>
NumPy-Discussion mailing list<br>
<a href="mailto:NumPy-Discussion@scipy.org">NumPy-Discussion@scipy.org</a><br>
<a href="http://mail.scipy.org/mailman/listinfo/numpy-discussion" target="_blank">http://mail.scipy.org/mailman/listinfo/numpy-discussion</a><br>
</blockquote></div><br>