10000 Piecewise fix for 0d by astrojuanlu · Pull Request #4792 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

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

Merged
merged 1 commit into from
Jun 8, 2014
Merged

Conversation

astrojuanlu
Copy link
Contributor

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.

if n == n2-1: # compute the "otherwise" condition.
totlist = condlist[0]
for k in range(1, n):
totlist |= condlist[k]
Copy link
Contributor

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

Copy link
Contributor Author

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.

@juliantaylor
Copy link
Contributor

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):
http://docs.scipy.org/doc/numpy/dev/gitwash/development_workflow.html#writing-the-commit-message

@astrojuanlu
Copy link
Contributor Author

I'm making the changes you suggested. After that, do I have to locally squash all the commits and do a git push --force to my branch?

@juliantaylor
Copy link
Contributor

yes e.g.

git rebase -i master
#squash the commits (s), edit commit log
git push -f ...

@astrojuanlu
Copy link
Contributor Author

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.
juliantaylor added a commit that referenced this pull request Jun 8, 2014
@juliantaylor juliantaylor merged commit 621b14e into numpy:master Jun 8, 2014
@jakobgager
Copy link

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)

@astrojuanlu
Copy link
Contributor Author

I won't be able to fix this for some time, I fear :(

@bastula
Copy link
Contributor
bastula commented Mar 28, 2015

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?

@jaimefrio
Copy link
Member

@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])
Copy link
Member

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]

eric-wieser added a commit to eric-wieser/numpy that referenced this pull request Oct 21, 2017
…h a single boolean is ok

Also fix the test added in numpygh-4792, which didn't make sense, but passed anyway
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.

6 participants
0