Repository navigation
Implement ndarray.__array_ufunc__ - #5
Conversation
There was a problem hiding this comment.
This is where I need the right comparison of the actual method, i.e., something better rather than just bailing out on an object being ndarray.
|
As a sanity check, with this, the following does work: |
9c1fdca to
a4768f9
Compare
0545982 to
f1d37e9
Compare
|
What order should these PRs go in? I note that they touch some of the same files, are some based on others? |
|
In reverse order, i.e., first #7 (if you think that is indeed a good idea). After that, #6 (which I can quickly rebase once #7 is in), which does the But if you want all three in one go, I can rebase this one on #7 and it will include everything. |
|
That's fine, just wanted to know as I was tempted to the reverse order already ;) |
|
OK, let me know if you have any comments on #7 (or just merge to check it passes all tests -- it does pass locally). |
a3130f1 to
648e711
Compare
5786465 to
e778753
Compare
8973822 to
7c86078
Compare
51cdbe5 to
3bbaf90
Compare
e778753 to
7241990
Compare
There was a problem hiding this comment.
Why call if you override it? Also, this form does it work with python 2.7 and, apart from that, there seems to be an argument as to whether super should be used at all.
I find this description pretty confusing. Perhaps keep it short and put an extended explanation with code example in numpy/doc/subclassing.py
There was a problem hiding this comment.
Please, no trailing comments, or just omit the comment.
There was a problem hiding this comment.
Hmm, why doesn't this loop? My understanding is that ufuncs cannot be used by ndarray subclasses using __array_override__ unless they clean themselve up, i.e., do not pass themselves to the call again so as to avoid looping. What am I missing?
There was a problem hiding this comment.
You're not missing anything: the subclass has to clean the arguments up before calling super. It really just replaces getattr(ufunc, method). Indeed, the main advantage over it is for a class like
class MaskedQuantity(MaskedArray, Quantity):
def __array_ufunc__(self, ufunc, method, *inputs, **kwargs):
...
result = super().__array_ufunc__(ufunc, method, *handled_inputs, **handled_kwargs)
...
return handled_result
For this class, super passes on to MaskedArray, and MaskedArray's super will not go to ndarray as it normally would, but to Quantity.
There was a problem hiding this comment.
p.s. The looping was of course why I needed to be sure that the ndarray.__array_ufunc__ method is not seen as an override...
There was a problem hiding this comment.
Since the base function is never called by the ufunc, why not just give it another name, say __array_ufunc_base__ or some such. Subclasses can then call it if they want.
There was a problem hiding this comment.
@charris - renaming it would beat the whole point of using super --the subclass cannot really know which class is above it! See the example above: MaskedArray cannot know whether its super will go to ndarray or to Quantity, and by insisting that the names are the same it doesn't have to check.
(but I agree that it is a bit strange that the base __array_ufunc__ is not simply part of the ufunc machinery -- with its arguments already normalized at the very least it could enter the chain later. But that is for a later PR... For now, I'll just add a TODO)
There was a problem hiding this comment.
Multiple inheritance would also be a hassle, as it already is...
There was a problem hiding this comment.
Sorry, I'm missing some context here to understand the question – can you elaborate?
There was a problem hiding this comment.
The idea here is to add __array_ufunc__ to ndarray. That base implementation then calls the relevant ufunc, which in turn, would normally call __array_ufunc__ again. To avoid that endless loop, PyUFunc_CheckOverride is modified so that it does not make the call when __array_ufunc__ is the version defined in the ndarray base class.
@mhvk wants the function in the base class so he can call it from subclasses in his units implementation.
There was a problem hiding this comment.
@charris - ndarray.__array_ufunc__ never gets called by the ufunc directly, only by a subclass that does result = super(subclass, self).__array_ufunc__(*inputs, **kwargs). In this use, it simply replaces the getattr(ufunc, method)(*inputs, **kwargs) that was initially recommended (indeed, as is, it just does that; this can be sped up later). The main advantage of all this is that by making super possible, we allow multiple inheritance using the standard python method
@njsmith - for context, maybe most useful to look at the updated docs, in particular https://github.com/charris/numpy/pull/5/files#diff-5a79b101110fd040a0cd5a254b4742f6R515 (if that isn't clear, let me know -- it is supposed to explain why we do things this way!).
There was a problem hiding this comment.
Ah, I see we both answered. Note that this is not specific to Quantity -- it just follows standard python practice.
7241990 to
dfea5da
Compare
|
@charris - on |
eaed3e5 to
ee4e601
Compare
f743da3 to
148d42c
Compare
|
Needs rebase. |
There was a problem hiding this comment.
Hmm, this doesn't render correctly. Maybe the link cannot be split across lines, or maybe github has a problem. We can allow long lines for links if needed.
There was a problem hiding this comment.
Really don't need the blank line here.
There was a problem hiding this comment.
I had it because it also initialized the variable. But I rewrote it now to make the flow more logical.
There was a problem hiding this comment.
I'd move this up under npy_cache_import. You can alway do a NULL check around both.
There was a problem hiding this comment.
Note that import errors can be detected by the variables being NULL after attempt.
There was a problem hiding this comment.
Moved. If this import fails there are worse problems to worry about than this routine giving the wrong answer....
This includes the use of super everywhere, and in the brief description of __array_ufunc__ in the reference section.
975837b to
545d10e
Compare
|
@charris - rebased this, except not yet the link in the documentation, since I cannot reproduce the problem... |
|
The documentation may be OK, I was judging by the display, which may be a Github problem. |
There was a problem hiding this comment.
How about something like is_ndarray_array_ufunc?
There was a problem hiding this comment.
It checks two things: whether the class has __array_ufunc__ and, if so, whether it is different from that of ndarray...
There was a problem hiding this comment.
Could that be obj.__array_ufunc__ instead? It shouldn't be in the instance dictionary unless the __init__ function puts it there, in which case it is an override.
There was a problem hiding this comment.
Or rather type(obj).__array_ufunc__?
In [9]: class foo(ndarray): pass
In [10]: b = foo((2,2))
In [11]: type(b).__array__ is ndarray.__array__
Out[11]: True
There was a problem hiding this comment.
I need to look on the class since the instance has a wrapped version (which calls the class version with self as first argument), so one cannot compare anymore.
Here, obj.__class__.__array_ufunc__ does exactly the same thing as type(obj).__array_ufunc__. But since I use Py_Type(obj) in the C code, perhaps it is clearer to use type(obj) here too?
There was a problem hiding this comment.
OT:
In [3]: np.add(int, int)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-3-979cc31d12eb> in <module>()
----> 1 np.add(int, int)
TypeError: unsupported operand type(s) for +: 'type' and 'type'
In [4]: np.add(ndarray, ndarray)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-4-6dcac5b0c00d> in <module>()
----> 1 np.add(ndarray, ndarray)
ValueError: invalid __array_struct__
Should really both be TypeError.
There was a problem hiding this comment.
perhaps it is clearer to use type(obj) here too?
That was my thought.
There was a problem hiding this comment.
OK, replaced it with type(obj). Also raised numpy#8877 for the OT topic.
545d10e to
b7ae204
Compare
|
Well, let's give it a shot. Thanks Marten. |
|
Great! Were you thinking of having another note to the mailing list for final comments (after rebase)? |
|
Hadn't occurred to me, but that's a good idea. I'll do it. |
commit 9c833bed5879d77e625556260690c349de18b433 Author: Thomas Li <[email protected]> Date: Wed Nov 17 16:21:27 2021 -0800 Add Windows config to GHA update script [wheel build] typo [wheel build] fix typo? [wheel build] fix linux builds? [wheel build] typo [wheel build] add license and pin to windows 2016 skip tests [wheel build] pin to windows 2019 instead [wheel build] try to find out the error on windows [wheel build] maybe fix? [wheel build] maybe fix? [wheel build] fix? [wheel build] cleanup [wheel build] Add Windows config to GHA update script [wheel build] typo [wheel build] fix typo? [wheel build] fix linux builds? [wheel build] typo [wheel build] add license and pin to windows 2016 skip tests [wheel build] pin to windows 2019 instead [wheel build] try to find out the error on windows [wheel build] maybe fix? [wheel build] maybe fix? [wheel build] fix? [wheel build] cleanup [wheel build] Update LICENSE_win32.txt Update LICENSE_win32.txt Add Windows config to GHA update script [wheel build] typo [wheel build] fix typo? [wheel build] fix linux builds? [wheel build] typo [wheel build] add license and pin to windows 2016 skip tests [wheel build] pin to windows 2019 instead [wheel build] try to find out the error on windows [wheel build] maybe fix? [wheel build] maybe fix? [wheel build] fix? [wheel build] cleanup [wheel build] Update LICENSE_win32.txt Update LICENSE_win32.txt Update cibw_test_command.sh commit 4bd12df Author: Thomas Li <[email protected]> Date: Mon Nov 15 17:28:47 2021 -0800 # This is a combination of 14 commits. # This is the 1st commit message: Add Windows config to GHA # This is the commit message #2: update script [wheel build] # This is the commit message #3: typo [wheel build] # This is the commit message #4: fix typo? [wheel build] # This is the commit message #5: fix linux builds? [wheel build] # This is the commit message #6: typo [wheel build] # This is the commit message #7: add license and pin to windows 2016 # This is the commit message #8: skip tests [wheel build] # This is the commit message #9: pin to windows 2019 instead [wheel build] # This is the commit message #10: try to find out the error on windows [wheel build] # This is the commit message #11: maybe fix? [wheel build] # This is the commit message #12: maybe fix? [wheel build] # This is the commit message #13: fix? [wheel build] # This is the commit message #14: cleanup [wheel build]

Now rebased on #6, and more or less ready: it passes all the tests. It still could do with a careful look (in particular, at d5cbeae [EDITED to update commit and status]