Proposal: add axes argument to ndimage API
Hi all,
Most functions in scipy.ndimage can be applied to ndimensional images, but applying the functions along a subset of axes is not as easy as it could be. For separable filters there tends to already be a 1D variant where a single axis can be specified, but the ndimensional versions do not currently have a corresponding axes argument.
I am proposing that we can expose an axes argument on functions to simplify the process of filtering along a subset of axes (e.g. a common scenario would be filtering along only the spatial axes of an array that may contain other axes corresponding to color channels, timepoints or an image batch). Users can technically apply filters to a subset of axes now by manipulating the input arguments. For a few concrete examples of this:
For gaussian filter, set sigma=0 along any axis that should not be filtered. For rank filters set size 1 along the nonfiltered axes. For convolutionbased filters or morphology the weights array can have singleton axes inserted.
None of these are particularly difficult to implement, but it may not always be apparent to library users and an axes argument would be more convenient. The proposed implementation would be purely at the Python API level and is straightforward to implement and maintain.
I opened a proof of concept PR implementing only *gaussian_filter *here: https://github.com/scipy/scipy/pull/18016
I am happy to extend this to the rest of the filtering module in a follow up PR if the general approach looks good. It can also potentially be extended to morphology or other submodules as well.
Let me know what you think
Cheers, Greg
On Fri, Mar 3, 2023 at 3:37 PM Gregory Lee grlee77@gmail.com wrote:
Hi all,
Most functions in scipy.ndimage can be applied to ndimensional images, but applying the functions along a subset of axes is not as easy as it could be. For separable filters there tends to already be a 1D variant where a single axis can be specified, but the ndimensional versions do not currently have a corresponding axes argument.
I am proposing that we can expose an axes argument on functions to simplify the process of filtering along a subset of axes (e.g. a common scenario would be filtering along only the spatial axes of an array that may contain other axes corresponding to color channels, timepoints or an image batch). Users can technically apply filters to a subset of axes now by manipulating the input arguments. For a few concrete examples of this:
For gaussian filter, set sigma=0 along any axis that should not be filtered. For rank filters set size 1 along the nonfiltered axes. For convolutionbased filters or morphology the weights array can have singleton axes inserted.
None of these are particularly difficult to implement, but it may not always be apparent to library users and an axes argument would be more convenient. The proposed implementation would be purely at the Python API level and is straightforward to implement and maintain.
I opened a proof of concept PR implementing only *gaussian_filter *here: https://github.com/scipy/scipy/pull/18016
I am happy to extend this to the rest of the filtering module in a follow up PR if the general approach looks good. It can also potentially be extended to morphology or other submodules as well.
Let me know what you think
+1 from me, this sounds like a useful feature to add. Thanks for working on this Greg!
Cheers, Ralf
participants (2)

Gregory Lee

Ralf Gommers