Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Backwards compatible mixin like NDArrayOperatorsMixin as a separate project #9016

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

Closed
shoyer opened this issue Apr 28, 2017 · 7 comments
Closed

Comments

@shoyer
Copy link
Member

shoyer commented Apr 28, 2017

As a follow up on #8247, I would like to write another backwards compatible mixin class like NDArrayOperatorsMixin suitable for vendoring in third-party libraries such as xarray and dask (and maybe pandas) that want to support __array_ufunc__ but aren't yet willing to require NumPy v1.13.

My intended usage pattern is as a drop-in replacement:

try:
    from numpy.lib.mixins import NDArrayOperatorsMixin
except ImportError:
    from my_library.legacy_numpy_compat import NDArrayOperatorsMixin

class MyArray(NDArrayOperatorsMixin):
    def __numpy_ufunc__(self, ufunc, method, *inputs, **kwargs):
        ...

For simplicity and ease of use (not to mention decoupling from the NumPy release cycle), I think this should be a separate repository (e.g., "legacy-ndarray-operators-mixin") in the numpy github organization. I'm happy to help put this together, but wanted to make sure this makes sense to others first.

@njsmith
Copy link
Member

njsmith commented Apr 29, 2017

Makes sense to me.

Can I nudge you to consider bumping xarrays minimum required version to 1.13 though? :-)

@shoyer
Copy link
Member Author

shoyer commented Apr 30, 2017

Can I nudge you to consider bumping xarrays minimum required version to 1.13 though? :-)

This might actually be a better idea. These days, upgrading numpy is easier than ever :). So after some contemplation, I'm no longer convinced this is essential.

It also occurs to me that many existing projects will just add a __array_ufunc__ method without adjusting their special operator overloads, and that's probably OK. (Even though many/most existing special method implementations are broken in various subtle ways, in practice that isn't a big deal.)

@mrocklin would you find such a mixin useful for dask?

@mrocklin
Copy link
Contributor

@mrocklin would you find such a mixin useful for dask?

What reusable logic would this mixin have? I think that the competition to this idea is just to rebuild this logic each time in each codebase. Hopefully this logic isn't that complex. I haven't yet looked at the full spec though so I don't know if this is overly naive.

My initial inclination (again without heavy research) would be to build the __array_ufunc__ method in one project first (likely xarray is my guess) and then use that as an example for other projects to copy. If we find that we're rebuilding almost exactly the same code then we would choose to coordinate on an external package. I think that you have a deeper understanding of __array_ufunc__ than I do though.

@shoyer
Copy link
Member Author

shoyer commented May 1, 2017

What reusable logic would this mixin have?

Mostly this is pretty routine: defining every special operator in terms of it's matching numpy ufunc: https://github.com/numpy/numpy/blob/master/numpy/lib/mixins.py

My initial inclination (again without heavy research) would be to build the array_ufunc method in one project first (likely xarray is my guess) and then use that as an example for other projects to copy.

Sounds good. We'll probably defer for this now, then.

@njsmith
Copy link
Member

njsmith commented May 1, 2017

@mrocklin: it's mostly that the logic is a bit tiresome (defining all the binop special methods), has a few gotchas if you want to correctly interoperate with arbitrary third-party classes, and is exactly the same for every object that wants to quack like an ndarray, so it makes sense to have a standard implementation everyone shares. But the standard implementation that ships with numpy assumes that you have a recent numpy (both to have the mixin, and because that implementation assumes that it's being used with a numpy that supports __array_ufunc__). So @shoyer has been wondering if we should make another version that does something slightly-wrong-but-not-too-wrong and that works with older numpys. Anyway it sounds like you have a plan but that's what the question was :-)

@mrocklin
Copy link
Contributor

mrocklin commented May 1, 2017

OK, if there is a great deal of boiler plate then yes, I'd probably appreciate having it around.

I guess my general answer is "I'm not able to answer this without better understanding the costs."

@shoyer
Copy link
Member Author

shoyer commented May 20, 2022

I never got around to this and at this point it isn't worth the trouble -- NumPy 1.13 was released almost 5 years ago!

@shoyer shoyer closed this as not planned Won't fix, can't repro, duplicate, stale May 20, 2022
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

No branches or pull requests

3 participants