Skip to content

Commit

Permalink
Documentation of pairwise measures and correction of boundary iou
Browse files Browse the repository at this point in the history
  • Loading branch information
Carole Sudre authored and Carole Sudre committed Feb 14, 2023
1 parent a9eaf8f commit 50832fe
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 24 deletions.
61 changes: 44 additions & 17 deletions MetricsReloaded/metrics/pairwise_measures.py
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,7 @@ def youden_index(self):
YI = Specificity + Sensitivity - 1
:return: Youden index
"""
return self.specificity() + self.sensitivity() - 1

Expand Down Expand Up @@ -748,21 +749,21 @@ def negative_predictive_values(self):
return 0
return self.tn() / (self.fn() + self.tn())

def dice_score(self):
"""
This function returns the dice score coefficient between a reference
and prediction images
:return: dice score
"""
if not "fbeta" in self.dict_args.keys():
self.dict_args["fbeta"] = 1
elif self.dict_args["fbeta"] != 1:
warnings.warn("Modifying fbeta option to get dice score")
self.dict_args["fbeta"] = 1
else:
print("Already correct value for fbeta option")
return self.fbeta()
# def dice_score(self):
# """
# This function returns the dice score coefficient between a reference
# and prediction images

# :return: dice score
# """
# if not "fbeta" in self.dict_args.keys():
# self.dict_args["fbeta"] = 1
# elif self.dict_args["fbeta"] != 1:
# warnings.warn("Modifying fbeta option to get dice score")
# self.dict_args["fbeta"] = 1
# else:
# print("Already correct value for fbeta option")
# return self.fbeta()

def fppi(self):
"""
Expand Down Expand Up @@ -930,6 +931,15 @@ def boundary_iou(self):
"""
This functions determines the boundary iou
Bowen Cheng, Ross Girshick, Piotr Dollár, Alexander C Berg, and Alexander Kirillov. 2021. Boundary IoU: Improving
Object-Centric Image Segmentation Evaluation. In Proceedings of the IEEE/CVF Conference on Computer Vision and
Pattern Recognition. 15334–15342.
.. math::
B_{IoU}(A,B) = \dfrac{| A_{d} \cap B_{d} |}{|A_d| + |B_d| - |A_d \cap B_d|}
where :math:A_d are the pixels of A within a distance d of the boundary
:return: boundary_iou
"""
if "boundary_dist" in self.dict_args.keys():
Expand All @@ -943,12 +953,12 @@ def boundary_iou(self):
distance_border_pred = ndimage.distance_transform_edt(1 - border_pred)

lim_dbp = np.where(
distance_border_pred < distance,
np.logical_and(distance_border_pred < distance, self.pred>0),
np.ones_like(border_pred),
np.zeros_like(border_pred),
)
lim_dbr = np.where(
distance_border_ref < distance,
np.logical_and(distance_border_ref < distance, self.ref>0),
np.ones_like(border_ref),
np.zeros_like(border_ref),
)
Expand Down Expand Up @@ -995,6 +1005,10 @@ def normalised_surface_distance(self):
Calculates the normalised surface distance (NSD) between prediction and reference
using the distance parameter :math:`{\\tau}`
.. math::
NSD(A,B)^{(\\tau)} = \dfrac{|S_{A} \cap Bord_{B,\\tau}| + |S_{B} \cup Bord_{A,\\tau}|}{|S_{A}| + S_{B}}
:return: NSD
"""
if "nsd" in self.dict_args.keys():
Expand Down Expand Up @@ -1070,11 +1084,24 @@ def measured_average_distance(self):
This function returns only the average distance when calculating the
distances between prediction and reference
.. math::
ASSD(A,B) = \dfrac{\sum_{a\inA}d(a,B) + \sum_{b\inB}d(b,A)}{|A|+ |B|}
:return: assd
"""
return self.measured_distance()[1]

def measured_masd(self):
"""
This function returns only the mean average surface distance defined as
.. math::
MASD(A,B) = \dfrac{1}{2}\left(\dfrac{\sum_{a\in A}d(a,B)}{|A|} + \dfrac{\sum_{b\inB}d(b,A)}{|B|})
:return: masd
"""
return self.measured_distance()[3]

def measured_hausdorff_distance(self):
Expand Down
2 changes: 1 addition & 1 deletion MetricsReloaded/processes/mixed_measures_processes.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

"""
Mixed measures processes - :mod:`MetricsReloaded.processes.mixed_measures_processes`
====================================================================
===================================================================================
This module provides classes for performing the evaluation processes of
:ref:`instance segmentation <instanceseg>`, :ref:`multi label instance segmentation <mlinstanceseg>`,
Expand Down
2 changes: 1 addition & 1 deletion MetricsReloaded/utility/assignment_localization.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
.. _assignloc:
Performing the process associated with instance segmentation
------------------------------------
------------------------------------------------------------
.. autoclass:: AssignmentMapping
:members:
Expand Down
7 changes: 2 additions & 5 deletions test/test_metrics/test_pairwise_measures.py
Original file line number Diff line number Diff line change
Expand Up @@ -400,13 +400,10 @@ def test_dsc_fbeta():
value_test = bpm.fbeta()
print("DSC test", value_test)
expected_dsc = 0.862
value_test2 = bpm.dice_score()
match = "Modifying fbeta option to get dice score"
with pytest.warns(UserWarning, match=match):
value_test3 = bpm2.dice_score()
value_test2 = bpm.dsc()

assert_allclose(value_test, expected_dsc, atol=0.001)
assert_allclose(value_test2, expected_dsc, atol=0.001)
assert_allclose(value_test3, expected_dsc, atol=0.001)


def test_assd():
Expand Down

0 comments on commit 50832fe

Please sign in to comment.