Skip to content

Commit

Permalink
Handle division by zero properly
Browse files Browse the repository at this point in the history
  • Loading branch information
otto-ifak committed Oct 1, 2024
1 parent dc9feaa commit 5bba95f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
11 changes: 7 additions & 4 deletions fences/core/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,13 @@ def total(self) -> int:

def accuracy(self) -> float:
total = self.total()
if total == 0:
return 0
return (self.valid_accepted + self.invalid_rejected) / total

def balanced_accuracy(self) -> float:
return (
(self.valid_accepted / (self.valid_accepted + self.valid_rejected)) +
(self.invalid_rejected / (self.invalid_accepted + self.invalid_rejected))
) / 2
valid_total = self.valid_accepted + self.valid_rejected
invalid_total = self.invalid_accepted + self.invalid_rejected
if valid_total == 0 or invalid_total == 0:
return 0
return ((self.valid_accepted / valid_total) + (self.invalid_rejected / invalid_total)) / 2
5 changes: 5 additions & 0 deletions test/core/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,8 @@ def test_balanced_accuracy(self):
c.invalid_accepted = 7
c.invalid_rejected = 11
self.assertAlmostEqual(c.balanced_accuracy(), ((2/5) + (11/18)) / 2)

def test_zero(self):
c = ConfusionMatrix()
self.assertEqual(c.accuracy(), 0)
self.assertEqual(c.balanced_accuracy(), 0)

0 comments on commit 5bba95f

Please sign in to comment.