Skip to content

handle np.nan and np.inf constant values properly in ndimage functions - #4083

Merged
mergify[bot] merged 4 commits into
cupy:masterfrom
grlee77:ndimage_interp_cval_nan_inf
Oct 14, 2020
Merged

mergify[bot] merged 4 commits into
cupy:masterfrom
grlee77:ndimage_interp_cval_nan_inf

Conversation

@grlee77

@grlee77 grlee77 commented Oct 6, 2020

Copy link
Copy Markdown
Member

SciPy's interpolation routines with mode='constant' allow the constant value to be np.nan, np.inf or -np.inf, but these currently cause compilation failure in CuPy.

closes #4082

I will add test cases in a bit and look into whether we might want to have a similar change for ndimage filters, etc.

@kmaehashi kmaehashi added cat:bug Bugs prio:high to-be-backported Pull-requests to be backported to stable branch labels Oct 7, 2020
Currently non-finite cval is only supported in cases where the output is floating point.
Otherwise NotImplementedError is returned.
Note: rank filters do not support cval=np.nan.
It was observed that NaN handling in general is not SciPy compatible for the rank filters
@grlee77 grlee77 changed the title handle np.nan and np.inf constant values properly in ndimage interpolation functions handle np.nan and np.inf constant values properly in ndimage functions Oct 7, 2020
@grlee77

grlee77 commented Oct 7, 2020 •

Copy link
Copy Markdown
Member Author

This PR now covers all ndimage filters and not only the interpolation functions.

The behavior with non-finite cval should match SciPy for any cases where the output array has a floating point dtype. New tests were added to cover this.

The behavior with the change made here did NOT match SciPy if the output array had integer dtype. It seems conversion from nan or inf to an integer did not occur in the same way as for SciPy and I am not sure SciPy is even intending to support this case (it is definitely not tested). Given that integer types cannot represent Inf or NaN, I think it is an edge case we don't need to support, so I have just set the functions to raise a NotImplementedError in that case.

One other corner case discovered here is that all of the rank filters do not preserve NaNs as in SciPy (see #4090), so unless that is fixed we also cannot support cval=numpy.nan for those filters. Due to this they will also raise a NotImplementedError now in this case.

@emcastillo

Copy link
Copy Markdown
Member

Jenkins, test this please

@chainer-ci

Copy link
Copy Markdown
Member

Jenkins CI test (for commit fec9b41, target branch master) succeeded!

@emcastillo emcastillo added this to the v9.0.0a1 milestone Oct 14, 2020
@emcastillo emcastillo added the st:test-and-merge (deprecated) Ready to merge after test pass. label Oct 14, 2020
@mergify
mergify Bot merged commit 90e73f2 into cupy:master Oct 14, 2020
emcastillo pushed a commit to emcastillo/cupy that referenced this pull request Oct 14, 2020
handle np.nan and np.inf constant values properly in ndimage functions
@grlee77
grlee77 deleted the ndimage_interp_cval_nan_inf branch December 18, 2020 00:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cat:bug Bugs prio:high st:test-and-merge (deprecated) Ready to merge after test pass. to-be-backported Pull-requests to be backported to stable branch

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Compilation error with cval=cp.nan in scipy.ndimage.interpolation.shift

4 participants