Skip to content

Implement ndarray.__array_ufunc__ - #5

Merged
charris merged 2 commits into
charris:enable-numpy_ufuncfrom
mhvk:ndarray_array_ufunc
Mar 30, 2017
Merged

charris merged 2 commits into
charris:enable-numpy_ufuncfrom
mhvk:ndarray_array_ufunc

Conversation

@mhvk

@mhvk mhvk commented Mar 13, 2017 •

Copy link
Copy Markdown

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]

Comment thread numpy/core/src/private/ufunc_override.h Outdated

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@mhvk

mhvk commented Mar 13, 2017 •

Copy link
Copy Markdown
Author

As a sanity check, with this, the following does work:

a = np.arange(10)
a.__array_ufunc__(np.add, 'reduce', a, 0)
# 45

@mhvk
mhvk force-pushed the ndarray_array_ufunc branch 2 times, most recently from 9c1fdca to a4768f9 Compare March 15, 2017 00:30
@mhvk mhvk changed the title DO NOT MERGE ENH: implement ndarray.__array_ufunc__ Implement ndarray.__array_ufunc__ Mar 15, 2017
@mhvk
mhvk force-pushed the ndarray_array_ufunc branch 2 times, most recently from 0545982 to f1d37e9 Compare March 15, 2017 02:57
@charris

charris commented Mar 16, 2017

Copy link
Copy Markdown
Owner

What order should these PRs go in? I note that they touch some of the same files, are some based on others?

@mhvk

mhvk commented Mar 16, 2017

Copy link
Copy Markdown
Author

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 *inputs thing (and also seemed uncontroversial), and after that this one (#5), which provides __array_input__.

But if you want all three in one go, I can rebase this one on #7 and it will include everything.

@charris

charris commented Mar 16, 2017

Copy link
Copy Markdown
Owner

That's fine, just wanted to know as I was tempted to the reverse order already ;)

@mhvk

mhvk commented Mar 16, 2017

Copy link
Copy Markdown
Author

OK, let me know if you have any comments on #7 (or just merge to check it passes all tests -- it does pass locally).

@charris
charris force-pushed the enable-numpy_ufunc branch from a3130f1 to 648e711 Compare March 17, 2017 16:15
@mhvk
mhvk force-pushed the ndarray_array_ufunc branch 3 times, most recently from 5786465 to e778753 Compare March 21, 2017 18:59
@charris
charris force-pushed the enable-numpy_ufunc branch from 8973822 to 7c86078 Compare March 23, 2017 20:29
@charris
charris force-pushed the enable-numpy_ufunc branch from 51cdbe5 to 3bbaf90 Compare March 23, 2017 22:12
@mhvk
mhvk force-pushed the ndarray_array_ufunc branch from e778753 to 7241990 Compare March 23, 2017 23:40
Comment thread doc/source/reference/arrays.classes.rst Outdated

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Comment thread numpy/core/src/multiarray/methods.c Outdated

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, no trailing comments, or just omit the comment.

Comment thread numpy/core/src/multiarray/methods.c Outdated

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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...

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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)

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Multiple inheritance would also be a hassle, as it already is...

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I'm missing some context here to understand the question – can you elaborate?

@charris charris Mar 29, 2017 •

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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!).

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I see we both answered. Note that this is not specific to Quantity -- it just follows standard python practice.

@mhvk
mhvk force-pushed the ndarray_array_ufunc branch from 7241990 to dfea5da Compare March 24, 2017 14:35
@mhvk

mhvk commented Mar 24, 2017

Copy link
Copy Markdown
Author

@charris - on super: I've found that consistent use makes for much cleaner (certainly much better than direct calls to ndarray.__* -- see previous PRs to remove such calls in MaskedArray...)

@mhvk
mhvk force-pushed the ndarray_array_ufunc branch 2 times, most recently from eaed3e5 to ee4e601 Compare March 24, 2017 15:09
@charris
charris force-pushed the enable-numpy_ufunc branch from f743da3 to 148d42c Compare March 25, 2017 02:31
@charris

charris commented Mar 25, 2017

Copy link
Copy Markdown
Owner

Needs rebase.

Comment thread doc/source/reference/arrays.classes.rst Outdated

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's strange: I did try make html and it looked OK:
spectacle i11318

Comment thread numpy/core/src/private/ufunc_override.c Outdated

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Really don't need the blank line here.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had it because it also initialized the variable. But I rewrote it now to make the flow more logical.

Comment thread numpy/core/src/private/ufunc_override.c Outdated

@charris charris Mar 25, 2017 •

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd move this up under npy_cache_import. You can alway do a NULL check around both.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that import errors can be detected by the variables being NULL after attempt.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.
@mhvk
mhvk force-pushed the ndarray_array_ufunc branch 2 times, most recently from 975837b to 545d10e Compare March 25, 2017 15:14
@mhvk

mhvk commented Mar 25, 2017

Copy link
Copy Markdown
Author

@charris - rebased this, except not yet the link in the documentation, since I cannot reproduce the problem...

@charris

charris commented Mar 28, 2017

Copy link
Copy Markdown
Owner

The documentation may be OK, I was judging by the display, which may be a Github problem.

Comment thread numpy/core/src/private/ufunc_override.c Outdated

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about something like is_ndarray_array_ufunc?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It checks two things: whether the class has __array_ufunc__ and, if so, whether it is different from that of ndarray...

Comment thread numpy/core/tests/test_multiarray.py Outdated

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

perhaps it is clearer to use type(obj) here too?

That was my thought.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, replaced it with type(obj). Also raised numpy#8877 for the OT topic.

@mhvk
mhvk force-pushed the ndarray_array_ufunc branch from 545d10e to b7ae204 Compare March 30, 2017 22:48
@charris
charris merged this pull request into charris:enable-numpy_ufunc Mar 30, 2017
@charris

charris commented Mar 30, 2017

Copy link
Copy Markdown
Owner

Well, let's give it a shot. Thanks Marten.

@mhvk
mhvk deleted the ndarray_array_ufunc branch March 30, 2017 23:45
@mhvk

mhvk commented Mar 30, 2017

Copy link
Copy Markdown
Author

Great! Were you thinking of having another note to the mailing list for final comments (after rebase)?

@charris

charris commented Mar 31, 2017

Copy link
Copy Markdown
Owner

Hadn't occurred to me, but that's a good idea. I'll do it.

charris pushed a commit that referenced this pull request Nov 21, 2021
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]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants