{"action":"created","issue":{"url":"https://api.github.com/repos/scipy/scipy/issues/7644","repository_url":"https://api.github.com/repos/scipy/scipy","labels_url":"https://api.github.com/repos/scipy/scipy/issues/7644/labels{/name}","comments_url":"https://api.github.com/repos/scipy/scipy/issues/7644/comments","events_url":"https://api.github.com/repos/scipy/scipy/issues/7644/events","html_url":"https://github.com/scipy/scipy/issues/7644","id":244862722,"number":7644,"title":"Unexpected results from scipy.ndimage.filters.gaussian_filter","user":{"login":"kostrykin","id":6557139,"avatar_url":"https://avatars1.githubusercontent.com/u/6557139?v=4","gravatar_id":"","url":"https://api.github.com/users/kostrykin","html_url":"https://github.com/kostrykin","followers_url":"https://api.github.com/users/kostrykin/followers","following_url":"https://api.github.com/users/kostrykin/following{/other_user}","gists_url":"https://api.github.com/users/kostrykin/gists{/gist_id}","starred_url":"https://api.github.com/users/kostrykin/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/kostrykin/subscriptions","organizations_url":"https://api.github.com/users/kostrykin/orgs","repos_url":"https://api.github.com/users/kostrykin/repos","events_url":"https://api.github.com/users/kostrykin/events{/privacy}","received_events_url":"https://api.github.com/users/kostrykin/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2017-07-22T18:15:33Z","updated_at":"2017-07-22T19:38:04Z","closed_at":null,"body":"The `gaussian_filter` routine from `scipy.ndimage.filters` produces unexpected results.\r\n\r\nConsider the following input image:\r\n![01](https://user-images.githubusercontent.com/6557139/28493574-3846838c-6f19-11e7-9dac-1ebe171e21b4.png)\r\nLets call this image `f`.\r\n\r\nThen, `gaussian_filter(g, sigma, order=[0, 1], mode='constant', cval=1)` evaluates to\r\n![06](https://user-images.githubusercontent.com/6557139/28493599-9e459b5a-6f19-11e7-816d-3b0d7eda20c5.png)\r\nThis is the expected result.\r\n\r\nFrom `gaussian_filter(g, sigma, order=[1, 0], mode='constant', cval=1)` we should expect that it evaluates to the same image, but rotated. However, this is what I get:\r\n![07](https://user-images.githubusercontent.com/6557139/28493609-ce71e4a0-6f19-11e7-8bb6-e4807c493553.png)\r\n\r\nFor `cval=0` it behaves as expected.\r\n\r\n### Reproducing code example:\r\n```python\r\nfrom numpy import *\r\nfrom scipy.ndimage.filters import gaussian_laplace, gaussian_filter1d, gaussian_filter\r\nfrom skimage.draw import circle\r\nfrom matplotlib.pyplot import *\r\n\r\ng = zeros((500, 500)) + 1\r\nc = circle(250, 250, 50)\r\ng[c] = 2\r\n\r\nsigma = 50\r\n\r\nimshow(gaussian_filter(g, sigma, order=[1, 0], mode='constant', cval=1), 'gray')\r\ncolorbar()\r\nshow()\r\n```\r\n\r\n### Scipy/Numpy/Python version information:\r\n```\r\n('0.13.3', '1.8.2', sys.version_info(major=2, minor=7, micro=6, releaselevel='final', serial=0))\r\n```\r\n\r\n"},"comment":{"url":"https://api.github.com/repos/scipy/scipy/issues/comments/317205729","html_url":"https://github.com/scipy/scipy/issues/7644#issuecomment-317205729","issue_url":"https://api.github.com/repos/scipy/scipy/issues/7644","id":317205729,"user":{"login":"normalhuman","id":13082030,"avatar_url":"https://avatars1.githubusercontent.com/u/13082030?v=4","gravatar_id":"","url":"https://api.github.com/users/normalhuman","html_url":"https://github.com/normalhuman","followers_url":"https://api.github.com/users/normalhuman/followers","following_url":"https://api.github.com/users/normalhuman/following{/other_user}","gists_url":"https://api.github.com/users/normalhuman/gists{/gist_id}","starred_url":"https://api.github.com/users/normalhuman/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/normalhuman/subscriptions","organizations_url":"https://api.github.com/users/normalhuman/orgs","repos_url":"https://api.github.com/users/normalhuman/repos","events_url":"https://api.github.com/users/normalhuman/events{/privacy}","received_events_url":"https://api.github.com/users/normalhuman/received_events","type":"User","site_admin":false},"created_at":"2017-07-22T19:38:04Z","updated_at":"2017-07-22T19:38:04Z","body":"Copying this from [Stack Overflow](https://stackoverflow.com/a/45258444) where I wrote that [the bug is here](https://github.com/scipy/scipy/blob/v0.19.1/scipy/ndimage/filters.py#L343): \r\n\r\n    for axis, sigma, order, mode in axes:\r\n        gaussian_filter1d(input, sigma, axis, order, output, mode, cval, truncate)\r\n        input = output\r\n\r\nThe filter performs repeated 1d convolution, and each time it passes in `cval` to 1d filter. Problem is, if there were any derivatives taken, then `cval` should be set to 0 because the derivative of any constant is zero. This is why the result is wrong with `order=[1, 0]` but not with `order=[0, 1]`. Without testing (don't have SciPy dev environment), I think the following would be correct: \r\n\r\n    for axis, sigma, order, mode in axes:\r\n        gaussian_filter1d(input, sigma, axis, order, output, mode, cval, truncate)\r\n        if order > 0: \r\n            cval = 0.0\r\n        input = output\r\n"}}
{"actor":{"display_login":"normalhuman"}}