-
-
Notifications
You must be signed in to change notification settings - Fork 5.1k
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
ndimage routines behave badly when output has memory overlap with inputs #9553
Comments
@yuracpp Do you mind adding a reproducable code snippet that we can copy and paste to see the problem for ourselves? It is not easy for us to understand anything from this. |
import numpy as np a = np.ones((3,3)) |
In this part, when the results are always same. if numpy.array_equal(input, output):
tmp = grey_erosion(tmp, size, footprint, structure, None, mode,
cval, origin)
else:
tmp = grey_erosion(tmp, size, footprint, structure, output, mode,
cval, origin) issue reproduce code import numpy as np
import scipy.ndimage as ndi
a = np.ones((3,3))
a[1,1] = 0
a=ndi.black_tophat(a, size=3)
print('result1')
print(a)
a = np.ones((3,3))
a[1,1] = 0
a=ndi.black_tophat(a, size=3, output=a)
print('result2')
print(a)
a = np.ones((3,3))
a[1,1] = 0
b=np.ones((3,3))
a=ndi.black_tophat(a, size=3, output=b)
print('result3')
print(a)
print(b) results
|
thanks, the output is correct with this fix. a = np.zeros((3,3)) |
Yes, |
The problem is that these routines do not check if the output array overlaps in memory with the inputs. As mentioned in #9578, same issue is in Since the ndimage code is not written to enable inplace operations, the fix has to be to make copies along the lines of |
input/output arrays should not overlap in calls to scipy.ndimage.median see scipy/scipy#9553 will be fixed upstream in scipy/scipy#11986, but the solution here will avoid repeated temporary array creation in each iteration.
Maybe the problem is following
...
tmp = grey_erosion(tmp, size, footprint, structure, output, mode, cval, origin)
tmp and input is the same now
so
numpy.subtract(tmp, input, out=tmp)
gives zero
Thank you
The text was updated successfully, but these errors were encountered: