-
Notifications
You must be signed in to change notification settings - Fork 1
/
metrics_seg.py
60 lines (46 loc) · 2.34 KB
/
metrics_seg.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import numpy as np
from sklearn import metrics
def _assert_valid_lists(groundtruth_list, predicted_list):
assert len(groundtruth_list) == len(predicted_list)
for unique_element in np.unique(groundtruth_list).tolist():
assert unique_element in [0, 1]
def _all_class_1_predicted_as_class_1(groundtruth_list, predicted_list):
_assert_valid_lists(groundtruth_list, predicted_list)
return np.unique(groundtruth_list).tolist() == np.unique(predicted_list).tolist() == [1]
def _all_class_0_predicted_as_class_0(groundtruth_list, predicted_list):
_assert_valid_lists(groundtruth_list, predicted_list)
return np.unique(groundtruth_list).tolist() == np.unique(predicted_list).tolist() == [0]
def get_confusion_matrix_elements(groundtruth_list, predicted_list):
"""returns confusion matrix elements i.e TN, FP, FN, TP as floats
See example code for helper function definitions
"""
_assert_valid_lists(groundtruth_list, predicted_list)
if _all_class_1_predicted_as_class_1(groundtruth_list, predicted_list) is True:
tn, fp, fn, tp = 0, 0, 0, np.float64(len(groundtruth_list))
elif _all_class_0_predicted_as_class_0(groundtruth_list, predicted_list) is True:
tn, fp, fn, tp = np.float64(len(groundtruth_list)), 0, 0, 0
else:
tn, fp, fn, tp = metrics.confusion_matrix(groundtruth_list, predicted_list).ravel()
tn, fp, fn, tp = np.float64(tn), np.float64(fp), np.float64(fn), np.float64(tp)
return tn, fp, fn, tp
def precision(y_true, y_pred):
intersection = (y_true * y_pred).sum()
return (intersection + 1e-15) / (y_pred.sum() + 1e-15)
def recall(y_true, y_pred):
intersection = (y_true * y_pred).sum()
return (intersection + 1e-15) / (y_true.sum() + 1e-15)
def F2(y_true, y_pred, beta=2):
p = precision(y_true,y_pred)
r = recall(y_true, y_pred)
return (1+beta**2.) *(p*r) / float(beta**2*p + r + 1e-15)
def PPV(y_true,y_pred):
# TP/(TP + FP)
TP = (y_true * y_pred).sum()
FP = np.sum(y_true[y_pred>0]==0)
return TP / float(TP+FP+1e-15)
def dice_score(y_true, y_pred):
return (2 * (y_true * y_pred).sum() + 1e-15) / (y_true.sum() + y_pred.sum() + 1e-15)
def jac_score(y_true, y_pred):
intersection = (y_true * y_pred).sum()
union = y_true.sum() + y_pred.sum() - intersection
return (intersection + 1e-15) / (union + 1e-15)