8000 BUG: Let linspace accept input that has an array_interface. by kiwifb · Pull Request #6659 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

BUG: Let linspace accept input that has an array_interface. #6659

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

Merged
merged 5 commits into from
Nov 6, 2016

Conversation

kiwifb
Copy link
Contributor
@kiwifb kiwifb commented Nov 10, 2015

Needed to support sagemath which passes non-ndarray arguments that have array_interface methods.

Fixes i#6634.

kiwifb added a commit to cschwan/sage-on-gentoo that referenced this pull request Nov 10, 2015
@jaimefrio
Copy link
Member

This would need a test. Otherwise, LGTM.

@kiwifb
Copy link
Contributor Author
kiwifb commented Nov 10, 2015

That sounds a good idea but I am not sure how to generate something that would break otherwise without involving sage.

@jaimefrio
Copy link
Member

In [numpy/lib/stride_tricks.py'](https://github.com/numpy/numpy/blob/master/numpy/lib/stride_tricks.py) there are examples of how to define a new class that exposes the array interface, look for DummyArrayand its use inas_strided`.

start = start * 1.
stop = stop * 1.
# Make sure one can use variables that have an __array_interface__, gh-6634
from numpy import asarray
Copy link
Contributor

Choose a reason for hiding this comment

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

Please use asanyarray here, so that numpy subclasses get passed through (np.linspace works with Quanity right now; this would break it). Note also that you can just add this import to the ones imported from numeric at the top.

Actually, the multiplication with 1. is not necessary anymore if you just do

start = asanyarray(start, dtype=np.float64)

Copy link
Member

Choose a reason for hiding this comment

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

No, dtype=float will break complex.

Please do add a test for a simple array subclass though... @mhvk can you contribute one?

Copy link
Contributor

Choose a reason for hiding this comment

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

Arg, yes, of course, should have thought about complex numbers. But, actually, why bother with the multiplication at all? Below, float(num) is entered into result_type, so it cannot be integer anyway.

Copy link
Contributor

Choose a reason for hiding this comment

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

for inspiration for tests, see ma/tests/test_subclassing.py

Copy link
Member

Choose a reason for hiding this comment

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

I think there are some special cases that would break otherwise such as single return value. But there should be tests for that, so go ahead and try ;).

@mhvk
Copy link
Contributor
mhvk commented Nov 10, 2015

See my comment about asanyarray to ensure ndarray subclasses get passed through. I don't know if it is a problem for more general duck-type arrays that the behaviour would change.

@kiwifb
Copy link
Contributor Author
kiwifb commented Nov 10, 2015

Thank you for all those contributions and comments while I was having my daily sleep, I will use asanyarray and hopefully it will work for my use case. May take a little bit more time to come with the test case, I am not that great at writing those.

@seberg
Copy link
Member
seberg commented Nov 29, 2015

Is this a regression in 1.10 and we should try to get it into 1.10.2? Still needs a test!

@kiwifb
Copy link
Contributor Author
kiwifb commented Nov 29, 2015

I know! I have been a little bit overworked the last few days (more like the last 2 weeks), and I have to learn a few things before I am even able to write a test.

@embray
Copy link
Contributor
embray commented Apr 4, 2016

I could work on a test for this. Just to be clear, was the issue in #6634 that the sage type does not coerce to float when right-multiplied by a float?

@kiwifb
Copy link
Contributor Author
kiwifb commented Apr 4, 2016

In essence yes, although I don't remember checking left multiplication. And as you can see it attracted a small wish list.

@embray
Copy link
Contributor
embray commented Apr 4, 2016

@kiwifb I'm not sure what you mean by "it attracted a small wish list"?

@embray
Copy link
Contributor
embray commented Apr 4, 2016

(Left multiplication isn't an issue here either--all the test really needs is a dummy class that behaves similarly to the sage type in this regard, and check that it works as start stop (and step?) for linspace.

@kiwifb
Copy link
Contributor Author
kiwifb commented Apr 4, 2016

By wishlist, I mean people have asked for specific tests for other cases. My initial patch was using asarray then it was suggested I replace it by asanyarray to catch other cases (not necessarily relevant to sage) and test them too.

@embray
Copy link
Contributor
embray commented Apr 4, 2016

Ah ok--the asanyarray distinction does't make a big difference here--it's just to ensure that an ndarray subclass isn't forced to be a plain ndarray.

@homu
Copy link
Contributor
homu commented Jun 22, 2016

☔ The latest upstream changes (presumably #7268) made this pull request unmergeable. Please resolve the merge conflicts.

@charris
Copy link
Member
charris commented Jun 22, 2016

This looks simple enough to finish up if someone wants to take it on.

@kiwifb
Copy link
Contributor Author
kiwifb commented Jun 22, 2016

I currently don't have time or the knowledge to write the requested tests case. Erik Bray was going to look at it but some other stuff is keeping him busy.

@embray
Copy link
Contributor
embray commented Aug 22, 2016

@kiwifb If you'll just rebase this PR I can make a PR to your PR to add a test.

@kiwifb
Copy link
Contributor Author
kiwifb commented Aug 22, 2016

OK will do in the next 24 hours.

@kiwifb
Copy link
Contributor Author
kiwifb commented Aug 23, 2016

Rebased. Your turn @embray!

@embray
Copy link
Contributor
embray commented Aug 29, 2016

Added a regression test at kiwifb#1 for @kiwifb to merge in.

One problem I ran into in writing this test is that __array_interface__ at the Python level does not allow using 'shape': (), so I went with 'shape': (1,)since the length 1 arrays are still convertible to scalars in the end. But really'shape': ()` should probably be allowed. This might be a similar/related issue to #6430.

@seberg
Copy link
Member
seberg commented Aug 29, 2016

Hmmm, yeah, seems like a bug. It appears that the empty tuple is supported if data is given as a tuple instead of a buffer though (so using data.__array_interface__["data"] instead).

Adds a regression test that demonstrates the issue.
@kiwifb
Copy link
Contributor Author
kiwifb commented Aug 30, 2016

OK it looks like this is finally ready to be merged!

@tobihan
Copy link
tobihan commented Oct 10, 2016

So can this be merged now?

@kiwifb
Copy link
Contributor Author
kiwifb commented Oct 10, 2016

Yes, I am not really wanting to invite more criticism because it would delay it further ;)

@rgommers rgommers merged commit 9aff656 into numpy:master Nov 6, 2016
@rgommers
Copy link
Member
rgommers commented Nov 6, 2016

This looks fine, so merging. Thanks @kiwifb @embray, all.

@rgommers
Copy link
Member
rgommers commented Nov 6, 2016

Just note that imho this wasn't a numpy bug. The function clearly asks for a scalar, so that it doesn't work with something that is neither a scalar nor an array scalar is not surprising. I'd expect multiple functions in numpy and scipy to fail if they get passed Sage reals.

@rgommers rgommers added this to the 1.13.0 release milestone Nov 6, 2016
@charris charris changed the title BUG: Let linspace accept input that has an array_interface but is not othe… BUG: Let linspace accept input that has an array_interface. Dec 31, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

10 participants
0