-
-
Notifications
You must be signed in to change notification settings - Fork 11k
Piecewise fix for 0d #4792
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
Piecewise fix for 0d #4792
Conversation
if n == n2-1: # compute the "otherwise" condition. | ||
totlist = condlist[0] | ||
for k in range(1, n): | ||
totlist |= condlist[k] |
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 could be a np.logical_or.reduce(condlist, axis=0)
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.
I'm not used to functional thinking but definitely seems legit.
thanks this should probably still go into 1.9, can probably also be done post beta. please squash the commits together and write a commit message like this (with BUG: in the title): |
I'm making the changes you suggested. After that, do I have to locally squash all the commits and do a |
yes e.g.
|
Ready, waiting for Travis to give the green light. |
When `x` has more than one element the condlist `[True, False]` is being made equivalent to `[[True, False]]`, which is correct. However, when `x` is zero dimensional the expected condlist is `[[True], [False]]`: this commit addresses the issue. Besides, the documentation stated that there could be undefined values but actually these are 0 by default: using `nan` would be desirable, but for the moment the docs were corrected. Closes numpy#331.
There, seems to be an issue with scalar domains and three or more conditions: >>> np.piecewise(3, [True, False, False],[4,2,0])
ValueError: function list and condition list must be the same Funny enough it works fine with two conditions (this is what has been tested): >>> np.piecewise(3, [True, False],[4,2])
array(4) The three conditions version works fine with select: >>> np.select([True, False, False],[4,2,0])
array(4) |
I won't be able to fix this for some time, I fear :( |
The changes applied from this pull request don't seem to work well with multidimensional arrays that have an extra "otherwise" condition: >>> x = array([[-2.5, -1.5, -0.5],
[ 0.5, 1.5, 2.5]])
>>> np.piecewise(x, [x < 0, x >= 2], [-1, 1, 3])
ValueError: all the input arrays must have same number of dimensions The same code produces the following result in numpy 1.8.2: >>> array([[-1., -1., -1.],
[ 3., 3., 1.]]) If you reshape the array to a single dimension, it works as intended. Also, I noticed there aren't any multidimensional test cases, so maybe this can be one of them? |
@jakobgager, @bastula Can you convert your comments here into issues? It is the only way to not lose track of them. |
@@ -1505,6 +1506,15 @@ def test_0d(self): | |||
assert_(y.ndim == 0) | |||
assert_(y == 0) | |||
|
|||
x = 5 | |||
y = piecewise(x, [[True], [False]], [1, 0]) |
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 doesn't make any sense. If x
is 0d, the conditions should be 0d too, as [True, False]
…h a single boolean is ok Also fix the test added in numpygh-4792, which didn't make sense, but passed anyway
I provide a fix for #331. Actually little changes were needed, but I had to carefully think about the assumptions being made by the function. Some additional tests were added and the behavior remains backwards compatible.
Besides, I corrected the documentation because there was an inconsistent bit, see 0d1f7d3.