From ffc99c465f9f4d191823af6959ac6ea0d8eb3dc4 Mon Sep 17 00:00:00 2001 From: Peter Corke Date: Mon, 23 Nov 2026 07:52:09 +1000 Subject: [PATCH] fix: give rankfilter a default rank of median The default rank=-1 was always rejected by the check that follows, so rankfilter() and the deprecated rank() raised unless rank was given. Co-Authored-By: Claude Sonnet 5.5 --- src/machinevisiontoolbox/ImageSpatial.py | 12 ++++++++---- tests/test_image_spatial_extra.py | 11 +++++++++++ 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/src/machinevisiontoolbox/ImageSpatial.py b/src/machinevisiontoolbox/ImageSpatial.py index 09cf220a..f7af1e11 100644 --- a/src/machinevisiontoolbox/ImageSpatial.py +++ b/src/machinevisiontoolbox/ImageSpatial.py @@ -802,7 +802,7 @@ def rankfilter( self, footprint: np.ndarray | None = None, h: int | None = None, - rank: int | str = -1, + rank: int | str = "median", border: str = "replicate", bordervalue: int | float = 0, ) -> Any: @@ -813,8 +813,8 @@ def rankfilter( :type footprint: ndarray(N,M), optional :param h: half width of structuring element :type h: int, optional - :param rank: rank of filter - :type rank: int, str + :param rank: rank of filter, defaults to ``"median"`` + :type rank: int, str, optional :param border: option for boundary handling, defaults to 'replicate' :type border: str, optional :param bordervalue: padding value, defaults to 0 @@ -855,6 +855,10 @@ def rankfilter( :references: - |RVC3|, Section 11.5.3. + .. versionchanged:: 2.5.0 + ``rank`` now defaults to ``"median"``. The previous default of -1 + was always rejected, so ``rank`` had to be given. + :seealso: :obj:`scipy.ndimage.rank_filter` """ if h is not None: @@ -898,7 +902,7 @@ def rank( self, footprint: np.ndarray | None = None, h: int | None = None, - rank: int | str = -1, + rank: int | str = "median", border: str = "replicate", bordervalue: int | float = 0, ) -> Any: diff --git a/tests/test_image_spatial_extra.py b/tests/test_image_spatial_extra.py index 8fe2f89b..bc2d5426 100644 --- a/tests/test_image_spatial_extra.py +++ b/tests/test_image_spatial_extra.py @@ -264,6 +264,17 @@ def test_named_ranks(self, noisy, rank, reference): expected = reference(noisy.array, size=5, mode="nearest") nt.assert_array_equal(out.array, expected) + def test_default_rank_is_the_median(self, noisy): + out = noisy.rankfilter(h=2) + expected = ndi.median_filter(noisy.array, size=5, mode="nearest") + nt.assert_array_equal(out.array, expected) + nt.assert_array_equal(out.array, noisy.rankfilter(h=2, rank="median").array) + + def test_deprecated_rank_method_has_the_same_default(self, noisy): + with pytest.warns(DeprecationWarning): + out = noisy.rank(h=1) + nt.assert_array_equal(out.array, noisy.rankfilter(h=1).array) + def test_integer_rank_zero_is_the_maximum(self, noisy): # rank 0 selects the largest value of the window out = noisy.rankfilter(h=1, rank=0)