Skip to content

Commit 49905a2

Browse files
committed
fix: guard against ZeroDivisionError in Comparitor._calc_stats
When there are no reference annotations (tp + fn == 0) or no test annotations (n_test == 0), computing sensitivity and positive predictivity raises ZeroDivisionError. Return NaN instead, matching the convention used by sklearn.metrics for undefined ratios. Fixes #278
1 parent 6850ad1 commit 49905a2

1 file changed

Lines changed: 5 additions & 2 deletions

File tree

wfdb/processing/evaluate.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,11 @@ def _calc_stats(self):
123123
self.fn = self.n_ref - self.tp
124124
# No tn attribute
125125

126-
self.sensitivity = float(self.tp) / float(self.tp + self.fn)
127-
self.positive_predictivity = float(self.tp) / self.n_test
126+
denom_sens = self.tp + self.fn
127+
self.sensitivity = float(self.tp) / denom_sens if denom_sens > 0 else float("nan")
128+
self.positive_predictivity = (
129+
float(self.tp) / self.n_test if self.n_test > 0 else float("nan")
130+
)
128131

129132
def compare(self):
130133
"""

0 commit comments

Comments
 (0)