-
Notifications
You must be signed in to change notification settings - Fork 3
/
plots.py
224 lines (189 loc) · 7.31 KB
/
plots.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
import logging
import numpy as np
import pandas as pd
from sklearn.metrics import auc
logger = logging.getLogger()
lw=1
def plot_performance(ax, result, metric, title, _process_index=None):
"""Plot mean and standard deviation (stddev) of metrics.
Parameters
----------
ax : matplotlib.Axes
Axes to draw on.
result : pandas.DataFrame
results. Rows are models. Each metric has a mean and stddev in a MultiIndex
columns object of the type ('metric', ('mean', 'stddev'))
metric : pandas.DataFrame
The metric to select from the columns of the `result` DataFrame.
title : str
Title of the axes
_process_index : function, optional
Function to process model names, by default None
Returns
-------
matplotlib.Axes
Return reference to the passed ax of the argument `ax`
"""
df = result.copy()
df = df.sort_values(by=[(metric, 'mean')])
colors = np.where(['prot' in row for row in df.index], 'navy', 'white')
if _process_index is not None:
df.index = _process_index(df.index)
y = df.index
width = df[(metric, 'mean')]
xerr = df[(metric, 'std')]
ax.set_xlim(0, 1.1)
ax.tick_params(labelsize=15)
ax.barh(y=y, width=width, xerr=xerr, capsize=4,
color=colors, height=0.6, edgecolor='black', lw=lw)
metric_name = " ".join(metric.split('_')).capitalize()
if metric == 'f1':
metric_name += ' score'
ax.set_title('{}\n{}'.format(title, metric_name), fontsize=15)
return ax
def plot_performance_adddots(ax, result, result_data, metric, title, _process_index=None):
"""Plot mean and standard deviation (stddev) of metrics.
Parameters
----------
ax : matplotlib.Axes
Axes to draw on.
result : pandas.DataFrame
results. Rows are models. Each metric has a mean and stddev in a MultiIndex
columns object of the type ('metric', ('mean', 'stddev'))
metric : pandas.DataFrame
The metric to select from the columns of the `result` DataFrame.
title : str
Title of the axes
_process_index : function, optional
Function to process model names, by default None
Returns
-------
matplotlib.Axes
Return reference to the passed ax of the argument `ax`
"""
df = result.copy()
df = df.sort_values(by=[(metric, 'mean')])
df_data= pd.DataFrame.from_dict(result_data)[df.index]
colors = np.where(['prot' in row for row in df.index], 'navy', 'white')
if _process_index is not None:
df.index = _process_index(df.index)
y = df.index
width = df[(metric, 'mean')]
xerr = df[(metric, 'std')]
ax.set_xlim(0, 1.1)
ax.tick_params(labelsize=15)
ax.barh(y=y, width=width, xerr=xerr, capsize=4,
color=colors, height=0.6, edgecolor='black', lw=lw)
ax.plot(df_data.loc[metric].tolist(), np.arange(df_data.shape[1]), 'b.', )
metric_name = " ".join(metric.split('_')).capitalize()
if metric == 'f1':
metric_name += ' score'
ax.set_title('{}\n{}'.format(title, metric_name), fontsize=15)
return ax
def plot_roc_curve(ax, runs_roc_scores, endpoint='', verbose=False):
"""Plot the Receiver Operation Curve for a run.
Parameters
----------
ax : matplotlib.Axes
Axes to draw on.
runs_roc_scores : list, Iterable
List of (fpr, tpr, threshold) values obtained from
sklearn.metrics.sklearn.metrics.roc_curve
endpoint : str
Selected endpoint for evaluation, e.g. "F2", by default ''
Only used for labeling.
verbose : bool
Log Confidence Interval, by default False
Returns
-------
matplotlib.Axes
Return reference to the passed ax of the argument `ax`
"""
tprs = []
base_fpr = np.linspace(0, 1, 101)
roc_aucs = []
for fpr, tpr, threshold in runs_roc_scores:
roc_auc = auc(fpr, tpr)
roc_aucs.append(roc_auc)
ax.plot(fpr, tpr, 'royalblue', alpha=0.05)
tpr = np.interp(base_fpr, fpr, tpr)
tpr[0] = 0.0
tprs.append(tpr)
tprs = np.array(tprs)
mean_tprs = tprs.mean(axis=0)
std = tprs.std(axis=0)
tprs_upper = mean_tprs + std
tprs_lower = mean_tprs - std
mean_rocauc = np.mean(roc_aucs).round(2)
sd_rocauc = np.std(roc_aucs).round(2)
se_rocauc = sd_rocauc/np.sqrt(len(roc_aucs))
if verbose:
CI = (mean_rocauc-1.96 * se_rocauc, mean_rocauc + 1.96 * se_rocauc)
logger.info("95% CI:{}".format(CI))
ax.plot(base_fpr, mean_tprs, color='royalblue',
label='Mean ROC\n(AUC = {}±{})'.format(mean_rocauc, sd_rocauc), lw=lw)
ax.fill_between(base_fpr, tprs_lower, tprs_upper,
color='grey', alpha=0.4, label='±1 std. dev')
ax.plot([0, 1], [0, 1], 'r--')
ax.set_xlim([-0.01, 1.02])
ax.set_ylim([-0.01, 1.02])
ax.set_ylabel('True Positive Rate', fontsize=15)
ax.set_xlabel('False Positive Rate', fontsize=15)
ax.tick_params(labelsize=15)
ax.legend(fontsize=12)
ax.set_title('{}\nProteomic panel'.format(endpoint), fontsize=15)
return ax
def plot_prc_curve(ax, runs_prc_scores, endpoint='', verbose=False):
"""Plot Precision Recall Curve (PRC) for a list of scores.
Parameters
----------
ax : matplotlib.Axes
Axes to draw on.
runs_prc_scores : list, Iterable
List with tuples of
(precision, recall, thresholds, average_precision)
obtained from sklearn.metrics.average_precision_score
and sklearn.metrics.average_precision_score
endpoint : str, optional
[description], by default ''
verbose : bool, optional
log confidence interval, by default False
Returns
-------
matplotlib.Axes
Return reference to the passed ax of the argument `ax`
"""
precisions = []
base_recall = np.linspace(0, 1, 101)
avg_precision = []
for precision, recall, threshold, _average_precision in runs_prc_scores:
avg_precision.append(_average_precision)
ax.plot(recall, precision, 'royalblue', alpha=0.05)
precision = np.interp(base_recall, recall[::-1], precision[::-1])
precision[-1] = 0.0
precisions.append(precision)
ax.set(xlabel="Recall", ylabel="Precision")
precisions = np.array(precisions)
mean_precisions = precisions.mean(axis=0)
std_precisions = precisions.std(axis=0)
precisions_upper = mean_precisions + std_precisions
precisions_lower = mean_precisions - std_precisions
mean_avg_prec = np.mean(avg_precision).round(2)
sd_avg_prec = np.std(avg_precision).round(2)
se_avg_prec = sd_avg_prec/np.sqrt(len(avg_precision))
if verbose:
CI = (mean_avg_prec-1.96 * se_avg_prec,
mean_avg_prec + 1.96 * se_avg_prec)
logger.info("95% CI:{}".format(CI))
ax.plot(base_recall, mean_precisions, color='royalblue',
label='Mean Avg. Prec.= {}±{})'.format(mean_avg_prec, sd_avg_prec))
ax.fill_between(base_recall, precisions_lower, precisions_upper,
color='grey', alpha=0.4, label='±1 std. dev')
ax.plot([0, 1], [1, 0], 'r--')
ax.set_xlim([-0.01, 1.02])
ax.set_ylim([-0.01, 1.02])
ax.set_ylabel('Precision', fontsize=15)
ax.set_xlabel('True Positive Rate', fontsize=15)
ax.tick_params(labelsize=15)
ax.legend(fontsize=12)
ax.set_title('{}\nProteomic panel'.format(endpoint), fontsize=15)