-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgenerate_roc_curve.py
128 lines (99 loc) · 4.12 KB
/
generate_roc_curve.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
"""The goal of this program is to generate an ROC curve based on trying
different critical values. I'm not sure how to evaluate the
'condition negative' value.
For now, this is a tabular summary of the results of varying the critical
values.
"""
import json
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
def generate_plot_tpvfp(all_results):
"""For now, generate a simple TP vs FP plot.
"""
# Generate data and labels.
x_values = [trial['false positives'] for trial in all_results]
y_values = [trial['true positives'] for trial in all_results]
labels = [trial['alpha name'] for trial in all_results]
# Labels overlap, so need to build series of labels and leave some blank.
# Build a dict of labels, and if coordinates already in, add to that label.
# Keys are point tuples, values are labels.
label_dict = {}
for x, y, label in zip(x_values, y_values, labels):
try:
label_dict[(x, y)] += label
except KeyError:
label_dict[(x, y)] = label
fig, ax = plt.subplots(figsize=(6, 6), dpi=128)
ax.scatter(x_values, y_values)
ax.set_title('True Positives vs False Positives')
ax.set_xlabel('False Positives')
ax.set_ylabel('True Positives')
# Add labels.
for point, label in label_dict.items():
x_label_pos = point[0] + 0.05
y_label_pos = point[1] + 0.025
ax.text(x_label_pos, y_label_pos, label)
# Make integer tick marks.
ax.xaxis.set_major_locator(ticker.MultipleLocator(1))
ax.yaxis.set_major_locator(ticker.MultipleLocator(1))
# Uncomment this to see plots during dev work, rather than opening
# file images.
# plt.show()
# Save to file.
filename = "other_output/tp_vs_fp_plot.png"
plt.savefig(filename)
def generate_plot_tpvfn(all_results):
"""For now, generate a simple TP vs FN plot.
"""
# Generate data and labels.
x_values = [trial['false negatives'] for trial in all_results]
y_values = [trial['true positives'] for trial in all_results]
labels = [trial['alpha name'] for trial in all_results]
# Labels overlap, so need to build series of labels and leave some blank.
# Build a dict of labels, and if coordinates already in, add to that label.
# Keys are point tuples, values are labels.
label_dict = {}
for x, y, label in zip(x_values, y_values, labels):
try:
label_dict[(x, y)] += label
except KeyError:
label_dict[(x, y)] = label
fig, ax = plt.subplots(figsize=(6, 6), dpi=128)
ax.scatter(x_values, y_values)
ax.set_title('True Postives vs False Negatives')
ax.set_xlabel('False Negatives')
ax.set_ylabel('True Positives')
# Add labels.
for point, label in label_dict.items():
x_label_pos = point[0] + 0.05
y_label_pos = point[1] + 0.025
ax.text(x_label_pos, y_label_pos, label)
# Make integer tick marks.
ax.xaxis.set_major_locator(ticker.MultipleLocator(1))
ax.yaxis.set_major_locator(ticker.MultipleLocator(1))
# Uncomment this to see plots during dev work, rather than opening
# file images.
# plt.show()
# Save to file.
filename = "other_output/tp_vs_fn_plot.png"
plt.savefig(filename)
if __name__ == '__main__':
# Get cached results of varying critical values.
filename = 'other_output/all_results.json'
with open(filename) as f:
all_results = json.load(f)
label_str = "Trial\tR_C\tM_C\tTP\tFP\tFN\tNotification Times"
print(label_str)
# Modify 'alpha name' for labeling purposes?
for trial in all_results:
trial['alpha name'] = trial['alpha name'].lower()
for trial in all_results:
# Generate a table of results. Print, and write to file.
value_str = f"{trial['alpha name']}\t{trial['critical rise']}\t"
value_str += f"{trial['critical slope']}\t{trial['true positives']}\t"
value_str += f"{trial['false positives']}\t"
value_str += f"{trial['false negatives']}\t"
value_str += f"{sorted(trial['notification times'])}"
print(value_str)
generate_plot_tpvfp(all_results)
generate_plot_tpvfn(all_results)