-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathmetrics.py
69 lines (51 loc) · 2.26 KB
/
metrics.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
61
62
63
64
65
66
67
68
69
def compute_class_sens_spec(pred, label, class_num):
"""
Compute sensitivity and specificity for a particular example
for a given class.
Args:
pred (np.array): binary arrary of predictions, shape is
(num classes, height, width, depth).
label (np.array): binary array of labels, shape is
(num classes, height, width, depth).
class_num (int): number between 0 - (num_classes -1) which says
which prediction class to compute statistics
for.
Returns:
sensitivity (float): precision for given class_num.
specificity (float): recall for given class_num
"""
# extract sub-array for specified class
class_pred = pred[class_num]
class_label = label[class_num]
### START CODE HERE (REPLACE INSTANCES OF 'None' with your code) ###
# compute true positives, false positives,
# true negatives, false negatives
tp = np.sum((class_pred == 1) & (class_label == 1))
tn = np.sum((class_pred == 0) & (class_label == 1))
fp = np.sum((class_pred == 1) & (class_label == 0))
fn = np.sum((class_pred == 0) & (class_label == 0))
# compute sensitivity and specificity
sensitivity = tp / (tp + fn)
specificity = tn / (tn + fp)
### END CODE HERE ###
return sensitivity, specificity
def get_sens_spec_df(pred, label):
patch_metrics = pd.DataFrame(
columns = ['Nothing'
'Edema',
'Non-Enhancing Tumor',
'Enhancing Tumor'],
index = ['Sensitivity',
'Specificity'])
for i, class_name in enumerate(patch_metrics.columns):
sens, spec = compute_class_sens_spec(pred, label, i)
patch_metrics.loc['Sensitivity', class_name] = round(sens,4)
patch_metrics.loc['Specificity', class_name] = round(spec,4)
return patch_metrics
whole_scan_label = keras.utils.to_categorical(label, num_classes = 4)
whole_scan_pred = pred
# move axis to match shape expected in functions
#whole_scan_label = np.moveaxis(whole_scan_label, 3 ,0)[1:4]
#whole_scan_pred = np.moveaxis(whole_scan_pred, 3, 0)[1:4]
whole_scan_df = get_sens_spec_df(whole_scan_pred, whole_scan_label)
print(whole_scan_df)