Skip to content

BUG: incorrect origin tuple handling in ndimage minimum_filter and maximum_filter with footprint covering a subset of axes #20652

@grlee77

Description

@grlee77

Describe your issue.

In SciPy 1.11, axes support was added to a number of ndimage filters. While porting this to CuPy I found that for minimum_filter and maximum_filter when all three of the following criteria are true, there is a bug in origin argument handling:

  1. len(axes) < ndim
  2. a non-separable footprint array is provided
  3. origin is provided as a tuple

I have a fix for the issue and will submit a small MR for it.

Reproducing Code Example

import numpy as np
from scipy.ndimage import maximum_filter

img = np.arange((5 * 6 * 7), dtype=np.float64).reshape(5, 6, 7)
footprint = np.ones((3, 5), dtype=bool)
footprint[1, 0] = 0
out = maximum_filter(img, footprint=footprint, axes=(-2, -1), origin=(0, 1))

Error message

RuntimeError                              Traceback (most recent call last)
Cell In[3], line 7
      5 footprint = np.ones((3, 5), dtype=bool)
      6 footprint[1, 0] = 0
----> 7 out = maximum_filter(img, footprint=footprint, axes=(-2, -1), origin=(0, 1))

File ~/mambaforge/envs/cucim310/lib/python3.10/site-packages/scipy/ndimage/_filters.py:1331, in maximum_filter(input, size, footprint, output, mode, cval, origin, axes)
   1287 @_ni_docstrings.docfiller
   1288 def maximum_filter(input, size=None, footprint=None, output=None,
   1289                    mode="reflect", cval=0.0, origin=0, *, axes=None):
   1290     """Calculate a multidimensional maximum filter.
   1291 
   1292     Parameters
   (...)
   1329     >>> plt.show()
   1330     """
-> 1331     return _min_or_max_filter(input, size, footprint, None, output, mode,
   1332                               cval, origin, 0, axes)

File ~/mambaforge/envs/cucim310/lib/python3.10/site-packages/scipy/ndimage/_filters.py:1200, in _min_or_max_filter(input, size, footprint, structure, output, mode, cval, origin, minimum, axes)
   1198         output[...] = input[...]
   1199 else:
-> 1200     origins = _ni_support._normalize_sequence(origin, input.ndim)
   1201     if num_axes < input.ndim:
   1202         if footprint.ndim != num_axes:

File ~/mambaforge/envs/cucim310/lib/python3.10/site-packages/scipy/ndimage/_ni_support.py:68, in _normalize_sequence(input, rank)
     66     if len(normalized) != rank:
     67         err = "sequence argument must have length equal to input rank"
---> 68         raise RuntimeError(err)
     69 else:
     70     normalized = [input] * rank

RuntimeError: sequence argument must have length equal to input rank


### SciPy/NumPy/Python version and system information

```shell
1.11.1 1.26.4 sys.version_info(major=3, minor=10, micro=12, releaselevel='final', serial=0)

Metadata

Metadata

Assignees

Labels

defectA clear bug or issue that prevents SciPy from being installed or used as expectedscipy.ndimage

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions