-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
133 lines (119 loc) · 5.09 KB
/
utils.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
import math
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.manifold import TSNE
from sklearn.metrics import classification_report, confusion_matrix, roc_curve, auc, f1_score, cohen_kappa_score
from sklearn.metrics import average_precision_score, precision_recall_curve, roc_auc_score, accuracy_score
from metrics import *
def seed_everything(seed, workers = True):
print(f"Global seed set to {seed}")
os.environ["PL_GLOBAL_SEED"] = str(seed)
random.seed(seed)
np.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
os.environ["PL_SEED_WORKERS"] = f"{int(workers)}"
def plot_tsne(X):
tsne = TSNE(n_components=2, verbose=1, perplexity=50, n_iter=500)
tsne_results = tsne.fit_transform(X)
colors = plt.cm.get_cmap('inferno', l)
plt.scatter(tsne_results[:,0], tsne_results[:,1], c=Y, cmap= colors)
def plot_accuracy(tr_acc,val_acc):
# Plot training & validation accuracy values
plt.plot(tr_acc)
plt.plot(val_acc)
plt.title('Model accuracy',fontsize=10)
plt.ylabel('Accuracy',fontsize=10)
plt.xlabel('Epoch',fontsize=10)
plt.tick_params(axis='both', which='major', labelsize=10)
plt.legend(['Train', 'Validation'], loc='upper left',prop={'size': 10})
plt.savefig('accuracy_plot.png')
plt.show()
def plot_loss(tr_loss,val_loss):
# Plot training & validation loss values
plt.plot(tr_loss)
plt.plot(val_loss)
plt.title('Model loss',fontsize=10)
plt.ylabel('Loss',fontsize=10)
plt.xlabel('Epoch',fontsize=10)
plt.tick_params(axis='both', which='major', labelsize=10)
plt.legend(['Train', 'Validation'], loc='upper left',prop={'size': 10})
plt.savefig('loss_plot.png')
plt.show()
def get_roc_curve(gt, pred, target_names):
for i in range(len(target_names)):
curve_function = roc_curve
auc_roc = roc_auc_score(gt[:, i], pred[:, i])
label = str(target_names[i]) + " AUC: %.3f " % auc_roc
xlabel = "False positive rate"
ylabel = "True positive rate"
a, b, _ = curve_function(gt[:, i], pred[:, i])
plt.figure(1, figsize=(7, 7))
plt.plot([0, 1], [0, 1], 'k--')
plt.plot(a, b, label=label)
plt.xlabel(xlabel)
plt.ylabel(ylabel)
plt.legend(loc='upper center', bbox_to_anchor=(1.3, 1),
fancybox=True, ncol=1)
plt.savefig('ROC_Curve.png')
plt.show()
def get_PR_curve(gt, pred, target_names):
for i in range(len(target_names)):
precision, recall, _ = precision_recall_curve(gt[:, i], pred[:, i])
average_precision = average_precision_score(gt[:, i], pred[:, i])
label = str(target_names[i]) + " Avg.: %.3f " % average_precision
plt.figure(1, figsize=(7, 7))
plt.step(recall, precision, where='post', label=label)
plt.xlabel('Recall')
plt.ylabel('Precision')
plt.ylim([0.0, 1.05])
plt.xlim([0.0, 1.0])
plt.legend(loc='upper center', bbox_to_anchor=(1.3, 1),
fancybox=True, ncol=1)
plt.savefig('Precision_and_Recall_curve.png')
plt.show()
def plot_confusion_matrix(y_true,y_pred,class_labels):
cm = confusion_matrix(y_true, y_pred, labels=class_labels)
cm_sum = np.sum(cm, axis=1, keepdims=True)
cm_perc = cm / cm_sum.astype(float) * 100
annot = np.empty_like(cm).astype(str)
nrows, ncols = cm.shape
for i in range(nrows):
for j in range(ncols):
c = cm[i, j]
p = cm_perc[i, j]
if i == j:
s = cm_sum[i]
annot[i, j] = '%.1f%%\n%d/%d' % (p, c, s)
elif c == 0:
annot[i, j] = ''
else:
annot[i, j] = '%.1f%%\n%d' % (p, c)
cm = pd.DataFrame(cm, index=class_labels, columns=class_labels)
cm.index.name = 'Actual'
cm.columns.name = 'Predicted'
fig, ax = plt.subplots(figsize=(60,60))
sns.set(font_scale=3.0) # Adjust to fit
sns.heatmap(cm, cmap= "YlGnBu", annot=annot, fmt='', ax=ax)
ax.tick_params(axis='both', which='major', labelsize=10) # Adjust to fit
ax.xaxis.set_ticklabels(class_labels)
ax.yaxis.set_ticklabels(class_labels)
fig.savefig('Confusion_Matrix.png')
plt.show()
#04_03_Errorbar.ipynb
def plot_perf_metrics_errbars(y,pred,class_labels):
metric_dfs = get_confidence_intervals(y,pred,class_labels)
metrics = metric_dfs.keys()
fig,axs = plt.subplots(len(metrics),1,sharey=True)
for i in range(len(metrics)):
ci = metric_dfs[metrics[i]][['Mean '+metrics[i]+' (CI 5%-95%)']].values
ci_mean,ci_ints = np.array([c[0].split(' ') for c in ci]).T
ci_mean = ci_mean.astype(float)
ci_min,ci_max = np.array([ci_ints.strip('()').split('-')]).astype(float)
ci_err = (ci_max-ci_min)/2
axs[i].errorbar(class_labels,ci_mean,yerr=ci_err,capsize=5,fmt='dk')
axs[i].set_ylabel(metrics[i])
fig.savefig('Performance_Metrics_95percentCI.png')
plt.show()