Repository navigation
add several functions to cupyx.scipy.ndimage.measurements - #3979
Conversation
|
pfnCI, test this please. |
|
Jenkins CI test (for commit d68c270, target branch master) succeeded! |
takagi
left a comment
There was a problem hiding this comment.
Would you check a comment on the test?
|
It is fine that |
|
Would you add the reference entries on |
|
@grlee77 Kindly ping. |
adds maximum, maximum_position, minimum, minimum_position, extrema
Fast path is only faster when CUB is enabled so run tests with and without CUB
when index is None, minimum_position and maximum_position will give the same position as SciPy
d68c270 to
50f3783
Compare
I rebased to fix the merge conflict. The last two commits were added to update the API docs and avoid unintentionally skipping dtype tests. I apparently did this awhile back, but forgot to ever push the changes to the remote branch! |
takagi
left a comment
There was a problem hiding this comment.
Would you make minor fixes on docstrings?
takagi
left a comment
There was a problem hiding this comment.
Would you check another comment on extrema's docstring?
Co-authored-by: Masayuki Takagi <[email protected]>
|
Thanks, it's fine! |
|
pfnCI, test this please. |
|
Jenkins CI test (for commit 1d11969, target branch master) succeeded! |
takagi
left a comment
There was a problem hiding this comment.
I totally checked the docstrings, and would you make additional fixes? Others look good to me.
Co-authored-by: Masayuki Takagi <[email protected]>
|
pfnCI, test this please. |
|
Jenkins CI test (for commit 750e677, target branch master) succeeded! |
|
LGTM! |


Overview
This PR adds a series of related
cupyx.scipy.ndimage.measurementfunctions:maximum,maximum_position,minimum,minimum_position,medianandextrema.Implementation Notes
All of these rely on a single
_selecthelper function that is patterned after SciPy's, but deviates in some implementation details. Specifically, SciPy's implementation relies on NumPy-specific behavior regarding indexing with duplicate indices that is a documented difference from CuPy behaviorExample SciPy code:
For NumPy arrays, assuming
inputhas already been sorted in ascending order, the line above results in storage of the minimum value at each integer values inlabelsto the corresponding index inmins. For CuPy, I instead relied onlexsortto get around the need for such repeated assignment to find the minimum or maximum (in contrast, SciPy only relies onlexsortfor themediancase, but only uses a simplerargsortof the labels for the other cases).I later added an alternative code path (the
_select_via_loopinghelper) that does not requirelexsortand is much faster when there are relatively few labels to measure. It becomes slower than thelexsortapproach when there are many (>30 or so) labels to measure. I think the slowdown for a larger number of labels is due to increasing numbers of kernel launches (but with relatively small work per kernel).One difference in behavior vs. SciPy
If there are multiple identical minima or maxima within a labeled region, the functions
minimum_positionandmaximum_positionas proposed here are not guaranteed to return the coordinates of the same extrema as the SciPy implementation. I think any of these duplicate extrema are equally valid choices, so I don't see this as a bug, but it is a deviation in behavior.I couldn't see a very simple way to ensure we always get the same coordinate as SciPy. For that reason,
unittest.SkipTestis used to skip cases where the input has non-unique values.The other difference is that any global values returned by
minimum,maximumandmedianare left as 0-dimensional device arrays rather than transferring to host scalars. I did choose to store theminimum_positionandmaximum_positionvalues as tuples of int on the host, though I am not certain if that is preferred?Benchmarks
Here are some benchmarks when CUB is enabled, showing that there is always substantial acceleration for large images, with the acceleration becoming particularly large (>100) when there are relatively few labels. Without CUB, there is reduced performance, but still an overall substantial acceleration. Results for
maximumandmaximum_positionare not shown in the table because they should be the same as theirminimumcounterparts.Benchmarks with CUB enabled
Benchmarking functions from cupyx.scipy.ndimage.measurements
Benchmarks with CUB disabled
Benchmarking functions from cupyx.scipy.ndimage.measurements
Benchmark script