-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ENH: Add more mixins + tests. #10460
Conversation
I still don't like this ad hoc split creation of multiple different mixins. It feels poorly motivated, like we're just doing it in case someone maybe wants to make a class less compatible with ndarray in some arbitrary way. Without concrete use cases, I'd rather see these combined into a single class. (We can always split it up later if someone finds a real use case.) |
numpy/lib/mixins.py
Outdated
|
||
It is useful for writing classes that do not inherit from `numpy.ndarray`, | ||
but that should support reductions like arrays as described in :ref:`A | ||
Mechanism for Overriding Ufuncs <neps.ufunc-overrides>`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This isn't really accurate - np.sum()
and other functions will work just fine on any object defining __array_ufunc__
- all this is doing is adding alias methods that match ndarray
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
True, I'll change that.
Agreed with @njsmith - I think we should have a clear use case. I certainly think in the absence of evidence to the contrary it makes more sense to somewhat expand |
Here's a use case (I'm fine merging the mixins, it can be handled in But, for example, in sparse arrays, accumulation leads to a dense array and won't be supported at first and reductions are supported (because reducing an all zero axis usually results in a zero.) |
@mhvk The point was to move the discussion here. When there's code to be seen, it always helps the discussion along. |
I was wondering about one more thing, whether it would be possible (or good?) to make |
Or maybe I can edit the implementations in |
@hameerabbasi - I like the idea of ndarray inheriting from the mixin and as a first step it certainly seems good to just use the functions from
|
Despite my liking above, I do feel what is really needed here is more discussion -- maybe even something like a NEP -- that lays out the levels of compatibility one likes to provide, including actual use cases, ideally with existing classes. I think there are a number:
cc @shoyer |
My use case for separating So in general I like this approach but I agree with @mhvk that more discussion may be warranted first. |
@shoyer If you subclass Another random thought that I should have had before: should this be an ABC? Basically the same as a mixin, but with a little more language-supported structure (e.g. you can declare that an object should count as a subclass without actually inheriting methods). |
Yes, that's mostly true. Though there's at least one extra method currently defined on the mixin that we don't implement (
Yes, it would be nice to define ABCs for cases where the interface is not already clear (e.g., shape compatibility). I don't know if all of these need to be ABCs, though. Python doesn't have any ABC for supporting arithmetic operators, for example -- it's just a matter of if the class implements the right special methods. |
https://docs.python.org/3.7/library/numbers.html ? IIUC an ABC does 4 things:
I'm not sure this is the right way to write |
Yes, almost, but numpy.ndarray doesn't implement it ( Anyways, I agree. There are lots of nice reasons to like ABCs. It would be great to figure out a small hierarchy that would suffice for duck-typing core NumPy operations. |
Well, not all ndarrays contain numbers :) |
If we have just one mixin, I'm pretty sure it's the right way. If multiple, then maybe not. Another way could be
How about
I assume Edit: +1 for |
Can someone point me to a short guide to how one would write a NEP? Maybe I could write one up for this purpose. |
@hameerabbasi take a look at https://github.com/numpy/numpy/blob/master/doc/neps/nep-0000.rst. I think there was an intention to put this on a website somewhere but I'm not sure what the status of that is. |
On second thought, maybe a NEP is overkill? I'm not sure about this, though. I do feel a lot of people would have a say on the design. |
What is the standing of this PR? On one level, It seems there is consensus to merge the various mixin classes together so this PR should be modified. Documentation of its use is also needed. On a higher level, are mixins a concept that has taken hold after their addition in 1.13? Should this somehow be unified with the idea of duck arrays now that NEP 22 has been accepted? |
If a consensus is reached I’d be happy to move this forward in any form. |
I think writing a separate mixin, as is done here, is the right approach. There is at least one clear use-case for a separate mixin like this that adds reductions (xarray). This goes under Principle 5: Make it easy to do the right thing. There are two specific changes that I'd like to see here:
|
Continuing on principle 5: The way I see it, there are three things to be decided:
@njsmith @shoyer @mhvk Can I have your answers to these particular questions, and I'll move this PR in the way this discussion goes. |
Re: Making it easy to do the right thing: ABCs do that. Of course, I'll add docs for these various things. :-) |
Re: How to split, I was considering splitting off things that require anything other than Re: Arbitrary reasons, there may be very good reasons someone isn't implementing something. For example, accumulations don't give a sparse array, there may be manpower required to implement everything at once and so we do it piece by piece, something may be immutable. Re: Out argument, I can change the implementation, but that would make it less efficient. One thing I've used in pydata/sparse is faux-inplace-operations, to support operators etc. Basically, I make |
@hameerabbasi I am going to close this for now. It seems to be stalled on the grounds that we are not sure whether it is a good idea to include. The main point for reconsideration might be to upstream things from pydata/sparse (I am not sure if this is what this is). On the other hand, maybe it would also be good to just list Please reopen/comment if you disagree (or just open a new PR), but at this point it seems unlikely the PR would move anyway without a clean start. |
Thanks @seberg. I think duck-array helpers common to all libraries have their place, maybe it isn't in NumPy though. I think there was a discussion on that at one point. |
Yeah, probably, I am not sure what the result of the discussion was though :). I do think it might be nice to rescue such duck-helpers somewhere. If even into an unpolished "informational" NEP, unfortunately it is one of those tasks that may be hard to find someone who has the cycles and will to do it :(. |
Fixes #10446
This is my first time contributing to Numpy, so I may not have checked every box (although I have skimmed the contributing guidelines).
Please feel free to poke and prod me to make this perfect.