Skip to content

Commit

Permalink
Macenko pca out of bounds fix (#1007)
Browse files Browse the repository at this point in the history
* Fix out-of-bounds bug in argpercentile

* Include argpercentile tests

* Update tests/test_color_deconvolution.py

---------

Co-authored-by: gum31714 <[email protected]>
  • Loading branch information
m101010 and gum101010 authored Jul 12, 2023
1 parent 15262df commit a076c44
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -121,5 +121,5 @@ def _get_angles(m):
def argpercentile(arr, p):
"""Calculate index in arr of element nearest the pth percentile."""
# Index corresponding to percentile
i = int(p * arr.size + 0.5)
i = min(int(p * arr.size + 0.5), arr.size - 1)
return numpy.argpartition(arr, i)[i]
25 changes: 25 additions & 0 deletions tests/test_color_deconvolution.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import skimage.io

from histomicstk.preprocessing import color_deconvolution as htk_dcv
from histomicstk.preprocessing.color_deconvolution.separate_stains_macenko_pca import \
argpercentile as htk_ap

from .datastore import datastore

Expand All @@ -20,6 +22,29 @@ def test_macenko(self):

np.testing.assert_allclose(w, w_expected, atol=1e-6)

def test_argpercentile(self):
arr = np.array([])
with np.testing.assert_raises(IndexError):
htk_ap(arr, 0.5)

arr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])

w = htk_ap(arr, 0.5)
w_expected = 5
np.testing.assert_equal(w, w_expected)

w = htk_ap(arr, 0.1)
w_expected = 1
np.testing.assert_equal(w, w_expected)

w = htk_ap(arr, 0.0)
w_expected = 0
np.testing.assert_equal(w, w_expected)

w = htk_ap(arr, 0.99)
w_expected = 9
np.testing.assert_equal(w, w_expected)


class TestColorDeconvolution:

Expand Down

0 comments on commit a076c44

Please sign in to comment.