Repository navigation
FIX: bug in 'reflect' and 'wrap' coordinate mapping - #1583
Conversation
|
As an aside, is there any easy way during development to force cython to regenerate all .c files from the .pyx files? Several .pyx files in the codebase include For now, I just manually deleted the .c files and used cythonize from the command line to regenerate them. Is there a simpler way? |
|
Thanks for the PR and the very informative figure, @grlee77! That really makes it clear (and I like it as an example, too). Regarding cleaning compiled files, on platforms with makefile support |
|
@JDWarner: |
|
I was just looking at the In the meantime you can run the command behind [Edit: Removed because this command was dangerous to run. See #1584] |
|
@JDWarner: |
|
Now that I think about it more, it could be argued that the original 'reflect' behaviour is correct if you consider the point of reflection to be the center of the edge pixels as opposed to their edges. The original 'wrap' behaviour does not seem correct to me, though. |
|
@grlee77 I would indeed consider In [139]: np.pad([0, 1, 2, 3], 2, mode='reflect')
Out[139]: array([2, 1, 0, 1, 2, 3, 2, 1]) |
|
Thanks @jni: In[43]: np.pad([0, 1, 2, 3], 4, mode='wrap')
Out[43]: array([0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3])What I implemented as reflect in this PR is equivalent to In[44]: np.pad([0, 1, 2, 3], 4, mode='symmetric')
Out[44]: array([3, 2, 1, 0, 0, 1, 2, 3, 3, 2, 1, 0])As a further comparison, I looked at the behaviour implemented in the function Oddly, the 'wrap' behavior in I don't have a strong opinion on which reflect mode should be used (perhaps offer both as options?). This all came up because I was looking into the potential impact of changing the default mode for |
|
see #1585 for the case that lead me to start looking into this. (The images shown there look identical whether using current master or the code from this PR because the problem seems to be in the 'reflect' mode of ndi.map_coordinates itself) |
|
Thanks very much for this bug fix! This is extremely important. I hope to have some time later today to look over this. |
|
As implemented in the most recent commits, I propose to rename the old pre-PR 'reflect' to 'mirror' for consistency with Several other functions in The proposed 'reflect' behavior is also what is implemented in dask's array ghosting (used by However, this still leaves the question of which is the desired behaviour for mode 'wrap'. The proposed behavior matches |
|
For completeness, the following code demonstrates the behaviour in import scipy.ndimage as ndi
from matplotlib import pyplot as plt
img = np.zeros((9, 9))
img[:8, :8] += 1
img[:4, :4] += 1
img[:2, :2] += 1
img[:1, :1] += 2
img[8, 8] = 4
rows, cols = img.shape[0], img.shape[1]
map_rows, map_cols = np.mgrid[-rows:2*rows, -cols:2*cols]
coord_map = np.array([map_rows, map_cols])
order=1, cval=0
fig, axes = plt.subplots(1, 5, figsize=(15, 5))
for n, mode in enumerate(['constant', 'nearest', 'wrap', 'reflect', 'mirror']):
out = ndi.map_coordinates(img, coord_map, order=order,
mode=mode, cval=cval)
axes[n].imshow(out, cmap=plt.cm.gray, interpolation='nearest')
axes[n].plot([8.5, 8.5], [8.5, 17.5], 'y--', linewidth=0.5)
axes[n].plot([17.5, 17.5], [8.5, 17.5], 'y--', linewidth=0.5)
axes[n].plot([8.5, 17.5], [8.5, 8.5], 'y--', linewidth=0.5)
axes[n].plot([8.5, 17.5], [17.5, 17.5], 'y--', linewidth=0.5)
axes[n].set_axis_off()
axes[n].set_aspect('equal')
axes[n].set_title(mode)
plt.tight_layout() |
|
Just a few comments, but all looks good! Thanks for the fix. Regarding the mismatch in terminology between skimage, np.pad and scipy.ndimage: Can we make the behavior consistent with np.pad and internally make it consistent with scipy.ndimage whenever we use it for 3D (replace the |
|
See if you are satisfied with the naming/organization of _shared/interpolation.pyx now. It certainly would be possibile to do the following transformation of Does anyone else have an opinion on the best approach to take here? |
|
I have also (re)raised the issue of the unexpected wrap behaviour in |
|
Thanks for your amazing work. Let's wait for an opinion from at least one other core developer on this before you spend any additional time. |
|
@grlee77 I don't think anyone in their right mind would think ndimage's wrap is correct. That's definitely a bug in my book. I don't have a strong opinion about reflect/mirror vs symmetric/reflect, because both are in use by pretty major functions in this ecosystem. It's a shame that they diverged. Some thoughts to consider:
I'm leaning towards the second consideration, because I think there might be more of a chance to get ndimage to match numpy than vice-versa, especially since they are on 0.15 (not 1.x yet), while numpy is on 1.9, ie, mature. So we could bring about a unification, rather than continuing the schism. Whatever we end up deciding, thank you @grlee77 for this! |
|
I'm particularly impressed by your demo figures, they are super-clear, and make reviewing so much easier. It might be worth putting them in our docs! |
|
My guess is these were independently developed. I can't take credit (or blame) for the
I prefer |
|
We need a decision on the names for the border modes to move this forward. In summary, what was being called 'reflect' in the current codebase, matches the behavior of mode 'mirror' of In the current PR, I have made 'mirror' and 'reflect' match the If we go with the
|
|
I also favor the np.pad conventions. |
|
I think, we can go ahead and wrap the np.pad naming scheme. Gregory, would
you be willing to translate the relevant wrap modes in the warp function?
Thanks!
|
|
@ahojnnes: sure, I will update it soon |
…istent with numpy.pad naming conventions. Specifically 'nearest' was changed to 'edge' and 'mirror' was changed to 'reflect'. All functions with a mode argument that rely on these functions had their inputs changed accordingly. For now there is a deprecation warning if the user supplies mode 'nearest'. Mode 'mirror' never appeared in an official release of skimage and so has no corresponding deprecation warning.
b50acfc to
c7a82f8
Compare
|
okay just rebased off of current master and I think that may have fixed it. |
|
I apologize for taking so long into this PR. First, thanks so much for the effort you put into such clear explanations, Gregory. Now, this issue comes a long way. In fact, I should probably take the blame, because I re-enabled the "mirror" mode in ndimage which the original author, Peter Verveer, labeled with a warning comment. Here Be Dragons? Meh. So, the problem, partially, is that SciPy has what I think is a problematic representations of pixels as "tiny little blocks". Thinking about this off and on over a few years, I've come to expect values to represent data points in space. I.e., the first element of a numpy image array represents a value at the coordinate This issue is important enough that I mentioned it in the SciPy roadmap. Solving it will close no less than four (!) SciPy bugs. I think the scikit-image pixel model is more sane than ndimage's, and that we should aim at fixing SciPy if we can (and the broken modes in scikit-image, highlighted above, of course) since we fall back to it quite often. Finally, I am delighted to see that my inattention to the issue really made no difference, since you all figure this and more out yourselves--kudos! |
|
@ahojnnes: see if the additions to TODO.txt seems adequate (I think maybe I should have put it under 0.14 instead), then hopefully we are ready to merge! |
FIX: bug in 'reflect' and 'wrap' coordinate mapping
|
@grlee77 @ahojnnes Can you please have a look at scipy/scipy#5223 and confirm whether scikit-image is doing the right thing? The question is whether: is supposed to return as it currently does? |
|
That's what it is supposed to do. skimage uses the convention that the pixel centers are at 0.5. Is that enough? |
|
@ahojnnes I thought we agreed to use the data point system? The centers of pixels system seems a bit odd, and causes a lot of problems in, e.g., scipy.ndimage. |
|
I have a strong opinion of having the centers of pixels as (0.5, 0.5) - it
has always been like this in scikit-image and to my knowledge it is the
standard in any other image processing package. Otherwise you'll end up
with wierd effects for the first and last pixels in a row / column when
rescaling images.
|
|
What's your issue with that convention?
|
|
It assumes square pixels, for one. And it doesn't generalize very well
when pixel spacing changes.
|
|
I can see that, but I considers these as edge cases in which you have to
use ndimage.geometric_transform anyway. I would argue that 99% of the
images for which scikit-image is used have square pixels.
|
|
It's fine, but we should document the convention in the user guide.
I'll file an issue.
|
|
Agreed :-)
|




This pull request fixes subtle indexing errors in
coord_mapfrom_shared.interpolation.pyxfor modes 'reflect' and 'mirror'.
I added a small python wrapper to coord_map so I could create a test. The image example shown below was added to the documentation.
The following lists output for a set of 1D coordinates before and after the PR:
Note that coordinate 0 was not wrapped!
note that the edge coordinates 0 and 3 were not being duplicated in the mirrors
The difference is perhaps easier to see in images than the 1D example above. The original image extent is indicated by the region inside the yellow lines.
Prior to the PR:

After this PR:
