From 84fb8604ae6741677b13fb84ace0cd09d94c3411 Mon Sep 17 00:00:00 2001 From: Joeran Bosma Date: Fri, 4 Oct 2024 11:30:36 +0200 Subject: [PATCH] Bugfix `lesion_TPR_at_FPR` The `lesion_TPR_at_FPR` function threw an error when a too-low FPR was requested. Now the function will return a TPR of 0 when the minimum operating point FPR is higher than the requested value. --- setup.py | 2 +- src/picai_eval/metrics.py | 2 +- tests/test_lesion_tpr_at_fpr.py | 30 ++++++++++++++++++++++++++++++ 3 files changed, 32 insertions(+), 2 deletions(-) create mode 100644 tests/test_lesion_tpr_at_fpr.py diff --git a/setup.py b/setup.py index c9b347f..ad63c74 100644 --- a/setup.py +++ b/setup.py @@ -63,7 +63,7 @@ def run(self): long_description = fh.read() setuptools.setup( - version='1.4.7', # also update version in metrics.py -> version + version='1.4.8', # also update version in metrics.py -> version author_email='Joeran.Bosma@radboudumc.nl', long_description=long_description, long_description_content_type="text/markdown", diff --git a/src/picai_eval/metrics.py b/src/picai_eval/metrics.py index 0ec9bbe..b5c9636 100644 --- a/src/picai_eval/metrics.py +++ b/src/picai_eval/metrics.py @@ -121,7 +121,7 @@ def score(self): def lesion_TPR_at_FPR(self, FPR: float) -> float: """Calculate the lesion-level true positive rate (sensitivity) at a given false positive rate (average number of false positives per examimation)""" - if np.max(self.lesion_FPR) < FPR: + if np.min(self.lesion_FPR) > FPR: return 0 return self.lesion_TPR[self.lesion_FPR <= FPR][-1] diff --git a/tests/test_lesion_tpr_at_fpr.py b/tests/test_lesion_tpr_at_fpr.py new file mode 100644 index 0000000..8583e40 --- /dev/null +++ b/tests/test_lesion_tpr_at_fpr.py @@ -0,0 +1,30 @@ +import numpy as np + +from picai_eval import Metrics + + +def test_lesion_tpr_at_fpr(): + """ + Test the lesion TPR at FPR function + """ + lesion_results = { + "0": [(0, 1, 1.)], + "1": [(0, 2, 1.)], + "2": [(1, 3, 1.)], + "3": [(1, 1, 1.), (1, 1, 1.)], + "4": [(0, 3, 1.)], + "5": [(1, 2.5, 1.), (1, 1.5, 1.)], + } + metrics = Metrics(lesion_results=lesion_results) + np.testing.assert_allclose(metrics.lesion_TPR, [0.2, 0.4, 0.4, 0.6, 0.6]) + np.testing.assert_allclose(metrics.lesion_FPR, [0.16666667, 0.16666667, 0.33333334, 0.33333334, np.inf]) + + # test with FPR = 0.3 + np.testing.assert_almost_equal(metrics.lesion_TPR_at_FPR(0.3), 0.4) + + # test with too low FPR + np.testing.assert_almost_equal(metrics.lesion_TPR_at_FPR(0.1), 0.0) + + +if __name__ == "__main__": + test_lesion_tpr_at_fpr()