{"action":"created","issue":{"url":"https://api.github.com/repos/numpy/numpy/issues/8194","repository_url":"https://api.github.com/repos/numpy/numpy","labels_url":"https://api.github.com/repos/numpy/numpy/issues/8194/labels{/name}","comments_url":"https://api.github.com/repos/numpy/numpy/issues/8194/comments","events_url":"https://api.github.com/repos/numpy/numpy/issues/8194/events","html_url":"https://github.com/numpy/numpy/pull/8194","id":184493969,"number":8194,"title":"np.piecewise to also work with lists of scalars","user":{"login":"alvarosg","id":12649253,"avatar_url":"https://avatars.githubusercontent.com/u/12649253?v=3","gravatar_id":"","url":"https://api.github.com/users/alvarosg","html_url":"https://github.com/alvarosg","followers_url":"https://api.github.com/users/alvarosg/followers","following_url":"https://api.github.com/users/alvarosg/following{/other_user}","gists_url":"https://api.github.com/users/alvarosg/gists{/gist_id}","starred_url":"https://api.github.com/users/alvarosg/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/alvarosg/subscriptions","organizations_url":"https://api.github.com/users/alvarosg/orgs","repos_url":"https://api.github.com/users/alvarosg/repos","events_url":"https://api.github.com/users/alvarosg/events{/privacy}","received_events_url":"https://api.github.com/users/alvarosg/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/numpy/numpy/labels/09%20-%20Enhancement","name":"09 - Enhancement","color":"fef2c0"},{"url":"https://api.github.com/repos/numpy/numpy/labels/component:%20numpy.lib","name":"component: numpy.lib","color":"009800"}],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":5,"created_at":"2016-10-21T14:07:14Z","updated_at":"2016-10-23T11:28:21Z","closed_at":null,"pull_request":{"url":"https://api.github.com/repos/numpy/numpy/pulls/8194","html_url":"https://github.com/numpy/numpy/pull/8194","diff_url":"https://github.com/numpy/numpy/pull/8194.diff","patch_url":"https://github.com/numpy/numpy/pull/8194.patch"},"body":"I was using np.piecewise to create a piecewise lambda function, and I realized that the way it is written it only allows to work with ndarrays, and not with list of scalars for the condlist argument. \r\n\r\nSimilarly to may other functions in np which can handle both ndarrays and native types, this also should.\r\n\r\nI am making the following function:\r\n```python\r\nimport numpy as np\r\n\r\ndef buildPieceWise(x):\r\n    condlist=[x<-1,(x>=-1)*(x<1),x>1]\r\n    funclist=[lambda x: -x, 0, lambda x:x]\r\n    return np.piecewise(x,condlist,funclist)\r\n```\r\n\r\nThis works fine:\r\n```python\r\n>>>print buildPieceWise(np.linspace(-2,2,11))\r\n[ 2.   1.6  1.2  0.   0.   0.   0.   0.   1.2  1.6  2. ]\r\n```\r\nBut this fails:\r\n```python\r\n>>>print buildPieceWise(-1.5)\r\nC:\\Anaconda\\lib\\site-packages\\numpy\\lib\\function_base.pyc in piecewise(x, condlist, funclist, *args, **kw)\r\n    773     if (n != n2):\r\n    774         raise ValueError(\r\n--> 775                 \"function list and condition list must be the same\")\r\nValueError: function list and condition list must be the same\r\n```\r\n\r\nAnd the reason is that internally, it is detecting that my masklist is ```[False,False,True]```, this part of the code:\r\n```python\r\nif (isscalar(condlist) or not (isinstance(condlist[0], list) or\r\n                                   isinstance(condlist[0], ndarray))):\r\n        condlist = [condlist]\r\n```\r\nmakes len(condlist)==1.\r\n\r\nI have replaced that condition by:\r\n```python\r\nif (isscalar(condlist) or not (isinstance(condlist[0], list) or\r\n                                   isinstance(condlist[0], ndarray))):\r\n        if not isscalar(condlist) and x.size == 1 and len(x.shape) == 0:\r\n            condlist = [[c] for c in condlist]\r\n        else:\r\n            condlist = [condlist]\r\n```\r\nThis makes it work in both cases:\r\n```python\r\nimport numpy as np\r\n\r\ndef buildPieceWise(x):\r\n    condlist=[x<-1,(x>=-1)*(x<1),x>1]\r\n    funclist=[lambda x: -x, 0, lambda x:x]\r\n    return np.piecewise(x,condlist,funclist)\r\n```\r\n```    \r\n>>>print buildPieceWise(np.linspace(-2,2,11))\r\n>>>print buildPieceWise(-1.5)\r\n[ 2.   1.6  1.2  0.   0.   0.   0.   0.   1.2  1.6  2. ]\r\n1.5\r\n```\r\n"},"comment":{"url":"https://api.github.com/repos/numpy/numpy/issues/comments/255583392","html_url":"https://github.com/numpy/numpy/pull/8194#issuecomment-255583392","issue_url":"https://api.github.com/repos/numpy/numpy/issues/8194","id":255583392,"user":{"login":"alvarosg","id":12649253,"avatar_url":"https://avatars.githubusercontent.com/u/12649253?v=3","gravatar_id":"","url":"https://api.github.com/users/alvarosg","html_url":"https://github.com/alvarosg","followers_url":"https://api.github.com/users/alvarosg/followers","following_url":"https://api.github.com/users/alvarosg/following{/other_user}","gists_url":"https://api.github.com/users/alvarosg/gists{/gist_id}","starred_url":"https://api.github.com/users/alvarosg/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/alvarosg/subscriptions","organizations_url":"https://api.github.com/users/alvarosg/orgs","repos_url":"https://api.github.com/users/alvarosg/repos","events_url":"https://api.github.com/users/alvarosg/events{/privacy}","received_events_url":"https://api.github.com/users/alvarosg/received_events","type":"User","site_admin":false},"created_at":"2016-10-23T11:28:21Z","updated_at":"2016-10-23T11:28:21Z","body":"@seberg @charris \r\n\r\nI actually realized, based on one of the old existing tests:\r\n\r\n```\r\nx = 3\r\npiecewise(x, [x <= 3, x > 3], [4, 0])  # Should succeed.\r\n```\r\n\r\nthat the behaviour that I was implementing was already expected to work. So this is not really an enhancement, but a bug correction.\r\n\r\nEssentially that test was not failing becuase if only two intervals are given, then after condlist=[condlist], len(condlist)==1 which is still allowed for len(functlist)==2. However, as soon as there are three intervals len(condlist) is still 1, but len(functlist)==3, making it stop working."}}
{"actor":{"display_login":"alvarosg"}}