<br><br><div class="gmail_quote">On Wed, Mar 17, 2010 at 5:26 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;">
<div><div></div><div class="h5">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>> 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__ 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 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 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 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 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>
</div></div>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>
<div class="im"><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 ma?<br>
> Beyond that, the PEP that you pointed to looks interesting. Maybe some sort<br>
> of decorator around ufunc calls could also be made to work.<br>
<br>
</div>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></blockquote><div><br>What bothers me here is the opposing desire to separate ufuncs from their ndarray dependency, having them operate on buffer objects instead. As I see it ufuncs would be split into layers, with a lower layer operating on buffer objects, and an upper layer tying them together with ndarrays where the "business" logic -- kinds, casting, etc -- resides. It is in that upper layer that what you are proposing would reside. Mind, I'm not sure that having matrices and masked arrays subclassing ndarray was the way to go, but given that they do one possible solution is to dump the whole mess onto the subtype with the highest priority. That subtype would then be responsible for casts and all the other stuff needed for the call and wrapping the result. There could be library routines to help with that. It seems to me that that would be the most general way to go. In that sense ndarrays themselves would just be another subtype with especially low priority.<br>
<br></div><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
In the long run, we could consider if ufuncs should be instances of a<br>
class, perhaps implemented in Cython. This way the ufunc will be able<br>
to pass itself to the special array methods as part of the context<br>
tuple, as is currently done. Maybe an alternative approach would be<br>
for ufuncs to provide methods where subclasses could register routines<br>
for the various steps I specified based on the types of the inputs,<br>
similar to the PEP. This way, the ufunc would determine the context<br>
based on the input (rather than the current way of the ufunc<br>
determining part of the context based on the input by inspecting<br>
__array_priority__ and then the input with highest priority<br>
determining the context based on the identity of the ufunc and the<br>
rest of the input.) This new (half baked) approach could be<br>
backward-compatible with the old one: if the combination of inputs<br>
isn't found in the registry, it would fall back on the existing<br>
input-/array_prepare array_wrap mechanisms (which in principle could<br>
then be deprecated, and at that point __array_priority__ might no<br>
longer be necessary). I don't see anything to indicate that we would<br>
regret implementing a special __input_prepare__ method down the road.<br>
<div><div></div><div class="h5"><br></div></div></blockquote><div><br>Chuck <br></div><br></div>