<br><br><div class="gmail_quote">On Wed, Mar 17, 2010 at 7:39 PM, Darren Dale <span dir="ltr"><<a href="mailto:dsdale24@gmail.com">dsdale24@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
On Wed, Mar 17, 2010 at 8:22 PM, Charles R Harris<br>
<div><div></div><div class="h5"><<a href="mailto:charlesr.harris@gmail.com">charlesr.harris@gmail.com</a>> wrote:<br>
><br>
><br>
> On Wed, Mar 17, 2010 at 5:26 PM, Darren Dale <<a href="mailto:dsdale24@gmail.com">dsdale24@gmail.com</a>> wrote:<br>
>><br>
>> On Wed, Mar 17, 2010 at 5:43 PM, Charles R Harris<br>
>> <<a href="mailto:charlesr.harris@gmail.com">charlesr.harris@gmail.com</a>> wrote:<br>
>> > On Wed, Mar 17, 2010 at 3:13 PM, Darren Dale <<a href="mailto:dsdale24@gmail.com">dsdale24@gmail.com</a>> wrote:<br>
>> >> On Wed, Mar 17, 2010 at 4:48 PM, Pierre GM <<a href="mailto:pgmdevlist@gmail.com">pgmdevlist@gmail.com</a>><br>
>> >> wrote:<br>
>> >> > On Mar 17, 2010, at 8:19 AM, Darren Dale wrote:<br>
>> >> >><br>
>> >> >> I started thinking about a third method called __input_prepare__<br>
>> >> >> that<br>
>> >> >> would be called on the way into the ufunc, which would allow you to<br>
>> >> >> intercept the input and pass a somehow modified copy back to the<br>
>> >> >> ufunc. The total flow would be:<br>
>> >> >><br>
>> >> >> 1) Call myufunc(x, y[, z])<br>
>> >> >> 2) myufunc calls ?.__input_prepare__(myufunc, x, y), which returns<br>
>> >> >> x',<br>
>> >> >> y' (or simply passes through x,y by default)<br>
>> >> >> 3) myufunc creates the output array z (if not specified) and calls<br>
>> >> >> ?.__array_prepare__(z, (myufunc, x, y, ...))<br>
>> >> >> 4) myufunc finally gets around to performing the calculation<br>
>> >> >> 5) myufunc calls ?.__array_wrap__(z, (myufunc, x, y, ...)) and<br>
>> >> >> returns<br>
>> >> >> the result to the caller<br>
>> >> >><br>
>> >> >> Is this general enough for your use case? I haven't tried to think<br>
>> >> >> about how to change some global state at one point and change it<br>
>> >> >> back<br>
>> >> >> at another, that seems like a bad idea and difficult to support.<br>
>> >> ><br>
>> >> ><br>
>> >> > Sounds like a good plan. If we could find a way to merge the first<br>
>> >> > two<br>
>> >> > (__input_prepare__ and __array_prepare__), that'd be ideal.<br>
>> >><br>
>> >> I think it is better to keep them separate, so we don't have one<br>
>> >> method that is trying to do too much. It would be easier to explain in<br>
>> >> the documentation.<br>
>> >><br>
>> >> I may not have much time to look into this until after Monday. Is<br>
>> >> there a deadline we need to consider?<br>
>> >><br>
>> ><br>
>> > I don't think this should go into 2.0, I think it needs more thought.<br>
>><br>
>> Now that you mention it, I agree that it would be too rushed to try to<br>
>> get it in for 2.0. Concerning a later release, is there anything in<br>
>> particular that you think needs to be clarified or reconsidered?<br>
>><br>
>> > And<br>
>> > 2.0 already has significant code churn. Is there any reason beyond a big<br>
>> > hassle not to set/restore the error state around all the ufunc calls in<br>
>> > ma?<br>
>> > Beyond that, the PEP that you pointed to looks interesting. Maybe some<br>
>> > sort<br>
>> > of decorator around ufunc calls could also be made to work.<br>
>><br>
>> I think the PEP is interesting, but it is languishing. There were some<br>
>> questions and criticisms on the mailing list that I do not think were<br>
>> satisfactorily addressed, and as far as I know the author of the PEP<br>
>> has not pursued the matter further. There was some interest on the<br>
>> python-dev mailing list in the numpy community's use case, but I think<br>
>> we need to consider what can be done now to meet the needs of ndarray<br>
>> subclasses. I don't see PEP 3124 happening in the near future.<br>
>><br>
>> What I am proposing is a simple extension to our existing framework to<br>
>> let subclasses hook into ufuncs and customize their behavior based on<br>
>> the context of the operation (using the __array_priority__ of the<br>
>> inputs and/or outputs, and the identity of the ufunc). The steps I<br>
>> listed allow customization at the critical steps: prepare the input,<br>
>> prepare the output, populate the output (currently no proposal for<br>
>> customization here), and finalize the output. The only additional step<br>
>> proposed is to prepare the input.<br>
>><br>
><br>
> What bothers me here is the opposing desire to separate ufuncs from their<br>
> ndarray dependency, having them operate on buffer objects instead. As I see<br>
> it ufuncs would be split into layers, with a lower layer operating on buffer<br>
> objects, and an upper layer tying them together with ndarrays where the<br>
> "business" logic -- kinds, casting, etc -- resides. It is in that upper<br>
> layer that what you are proposing would reside. Mind, I'm not sure that<br>
> having matrices and masked arrays subclassing ndarray was the way to go, but<br>
> given that they do one possible solution is to dump the whole mess onto the<br>
> subtype with the highest priority. That subtype would then be responsible<br>
> for casts and all the other stuff needed for the call and wrapping the<br>
> result. There could be library routines to help with that. It seems to me<br>
> that that would be the most general way to go. In that sense ndarrays<br>
> themselves would just be another subtype with especially low priority.<br>
<br>
</div></div>I'm sorry, I didn't understand your point. What you described sounds<br>
identical to how things are currently done. What distinction are you<br>
making, aside from operating on the buffer object? How would adding a<br>
method to modify the input to a ufunc complicate the situation?<br>
<div><div></div><div class="h5"><br></div></div></blockquote><div><br>Just *one* function to rule them all and on the subtype dump it. No __array_wrap__, __input_prepare__, or __array_prepare__, just something like __handle_ufunc__. So it is similar but perhaps more radical. I'm proposing having the ufunc upper layer do nothing but decide which argument type will do all the rest of the work, casting, calling the low level ufunc base, providing buffers, wrapping, etc. Instead of pasting bits and pieces into the existing framework I would like to lay out a line of attack that ends up separating ufuncs into smaller pieces that provide low level routines that work on strided memory while leaving policy implementation to the subtype. There would need to be some default type (ndarray) when the functions are called on nested lists and scalars and I'm not sure of the best way to handle that.<br>
<br>I'm just sort of thinking out loud, don't take it too seriously.<br><br>Chuck <br></div></div>