-
-
Notifications
You must be signed in to change notification settings - Fork 11k
Fixed bug in numpy.piecewise() for 0-d array handling #331
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
Conversation
Updated numpy/lib/function_base.py to fix bug in numpy.piecewise() for 0-d array handling and boolean indexing with scalars.
The current version of this pull request appears to fail the existing tests (e.g., this build reports failures: http://travis-ci.org/#!/numpy/numpy/jobs/1788964), so that would need to be fixed. Also, some sort of test needs to be added to make sure that the bug is fixed (and stays that way). |
Corrected tests for numpy.piecewise(), made them more systematic, and altered numpy.piecewise fix to hit all test cases.
Hi njsmith, thanks. I've fixed both my change to numpy.piecewise() as well as the test cases to cover both my bug case above as well as making sure that the tests are all logical. |
I guess you rearranged the order of all the tests in the source? It makes the diff unreadable unfortunately -- I can't tell at all what you've actually changed without reverse-engineering it by hand, and I'm afraid I'm too lazy for that. Can you revert the no-op changes, or split them into a separate commit, or at least describe what changes were actually made? |
Is this issue (i.e. this pull request) going to be fixed anytime soon? It's a bit anoying to construct a 1d array with one element each time I want to call this function with a scalar. |
I guess it will be fixed as soon as someone does the work :-) |
Hm, got it :) Should an issue be filed first? |
The main point of filing an issue is to make sure that something doesn't get forgotten, and this PR is already sort of doing that (i.e., it shows up on the list of open issues if anyone goes to look at it). But feel free to file one if you like. If you're thinking about fixing the bug yourself, then we're pretty laid back about these things -- you can just go ahead and submit a new PR without filing an issue if you want. We'd rather get the fix than worry about the process :-). |
The main thing that needs done is just to rewrite the unit tests. In my own testing and usage (and when I've tested with the original unit tests), the code works fine, but I just haven't had the time to sit down and rewrite them to be more similar to the original set and still be complete and systematic. I may get to it next week sometime, but until then I'm swamped. -- Eric On Nov 15, 2012, at 9:33 AM, njsmith notifications@github.com wrote:
|
That's one of the things I was about to ask, it is ok to rewrite the tests? Because right now IMHO they don't seem very meaningful for the purpose of the function. I'd like to try it myself this weekend, or next one as much. |
It's OK to make any changes whatsoever, the requirements are just that you
|
What is the status of this? |
Well actually I didn't do it any of the weekends as you can notice :( But anyway now that you ask about it, if I recall correctly there was some discussion recently about rank-0 arrays in the mailing list but I am not able to find it. Are there any constraints on how to do this? Other than that it would be my first contribution to NumPy and I am a bit unsecure but I am willing to give it a try. |
Yes, I was referring to this: http://www.mail-archive.com/numpy-discussion@scipy.org/msg39968.html My question was if there are any near future plans for rank-0 arrays that relate this and other issues in such a way that it requires changing low-level code or if we can address this individually. |
No, I don't think there will be any near-future changes in how 0d arrays
|
Fixed one bug in fix, changed unit tests of numpy.piecewise() to be the original set except for the addition of one regression test. Revisions of unit tests to include spelling out undocumented assumptions of behavior of numpy.piecewise() will come later.
Sorry for the delay of months. Finishing up my degree and job hunting, so I've been...occupied. I reverted most of the unit test changes so that it's clearer what I changed. Maybe I'll re-up the unit test changes at some point, but this particular commit should be easier to understand when you diff between this state and the state 3 commits ago. |
@njsmith Can this go in. |
Ping Travisbot just because it was a long time ago. |
@@ -674,20 +674,13 @@ def piecewise(x, condlist, funclist, *args, **kw): | |||
x = asanyarray(x) | |||
n2 = len(funclist) | |||
if isscalar(condlist) or \ | |||
not (isinstance(condlist[0], list) or | |||
isinstance(condlist[0], ndarray)): | |||
(isinstance(condlist, np.ndarray) and condlist.ndim == 0) or \ |
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 changes the behavior and needs to be documented. I've also looked at the following version:
def isscalarlike(a):
return isscalar(a) or (isinstance(a, np.ndarray) and a.ndim == 0)
x = asanyarray(x)
n2 = len(funclist)
if isscalarlike(condlist) or (x.ndim > 0 and isscalarlike(condlist[0])):
condlist = [condlist]
Which is a bit different again, but clearer I think.
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.
Yeah, I suspect this change is probably fine but I haven't wrapped my head
around that complex conditional...
On Sun, Aug 18, 2013 at 5:01 PM, Charles Harris notifications@github.comwrote:
In numpy/lib/function_base.py:
@@ -674,20 +674,13 @@ def piecewise(x, condlist, funclist, _args, *_kw):
x = asanyarray(x)
n2 = len(funclist)
if isscalar(condlist) or \
not (isinstance(condlist[0], list) or
isinstance(condlist[0], ndarray)):
(isinstance(condlist, np.ndarray) and condlist.ndim == 0) or \
This changes the behavior and needs to be documented. I've also looked at
the following version:def isscalarlike(a): return isscalar(a) or (isinstance(a, np.ndarray) and a.ndim == 0) x = asanyarray(x) n2 = len(funclist) if isscalarlike(condlist) or (x.ndim > 0 and isscalarlike(a[0])): condlist = [condlist]
Which is a bit different again, but clearer I think.
—
Reply to this email directly or view it on GitHubhttps://github.com//pull/331/files#r5831760
.
@ericsuh Made some comments. The commits should also be squashed into one. |
I've put up a cleaned version at https://github.com/charris/numpy in the branch gh-331 |
Needs finishing up. |
I seem to have deleted my cleanup somewhere along the line. |
In the docstring: "Each boolean array in But actually even the tests do not respect this (and still everything works): numpy/numpy/lib/tests/test_function_base.py Line 1468 in b1c69df
There are other bugs too. For instance, there's |
I added a new PR (see #4792). |
feat: Add vsubl_high_[s8|s16|s32|u8|u16|u32]
Updated numpy/lib/function_base.py to fix bug in numpy.piecewise() for 0-d array handling and boolean indexing with scalars.
Bug test case:
After fix: