-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path__init__.py
389 lines (300 loc) · 13.4 KB
/
__init__.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
from __future__ import division
import inspect
import os
import sys
import types
import warnings
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import seaborn as sns
import sklearn.metrics as metrics
from IPython.display import display
from jinja2 import Environment, FileSystemLoader
from matplotlib.colors import ListedColormap
from pandas import Series
from pylab import annotate
PATH = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
TEMPLATE_ENVIRONMENT = Environment(autoescape=False, loader=FileSystemLoader(PATH), trim_blocks=False)
class Attribute:
def __init__(self, attribute, value):
self.html = '<b>' + str(attribute) + ' </b>' + str(value)
self.latex = '\\textbf{' + str(attribute) + '} ' + str(value)
def _repr_html_(self):
return self.html
def _repr_latex_(self):
return self.latex
class LabelValueTable:
def __init__(self, title, labels_and_values):
html_template = TEMPLATE_ENVIRONMENT.get_template('label_value_table_template.html')
latex_template = TEMPLATE_ENVIRONMENT.get_template('label_value_table_template.tex')
self.html = html_template.render(title=title, labels_and_values=labels_and_values)
self.latex = latex_template.render(title=title, labels_and_values=labels_and_values)
def _repr_html_(self):
return self.html
def _repr_latex_(self):
return self.latex
class Bold:
def __init__(self, value):
self.html = '<b>' + str(value) + ' </b>'
self.latex = '\\textbf{' + str(value) + '} '
def _repr_html_(self):
return self.html
def _repr_latex_(self):
return self.latex
def map_new_categories(categories, groundtruth, predictions, labels, other_label='other'):
categories = sorted(list(categories))
# Creating new categories vectorization function
new_categories = {cat: i for i, cat in enumerate(categories)}
other_category = len(new_categories)
def map_function(x):
return new_categories.get(x, other_category)
to_new_categories = np.vectorize(map_function)
new_groundtruth = to_new_categories(groundtruth)
new_predictions = to_new_categories(predictions)
new_labels = [labels[i] for i in categories]
new_labels.append(other_label)
return new_groundtruth, new_predictions, new_labels
def show_confusion_matrix(true_labels, predicted_labels, labels, figsize=None,
normalize=True, annot=False, cmap=None,
square=False, linecolor='white', ticks_size=None,
linewidths=0, show_yticks=True, show_xticks=False,
cbar=True, cbar_kws=None):
if not figsize:
figsize = (5, 5)
if type(true_labels[0]) == type(labels[0]):
cm = metrics.confusion_matrix(true_labels, predicted_labels, labels)
else:
cm = metrics.confusion_matrix(true_labels, predicted_labels, np.arange(np.size(labels)))
# normalize confusion matrix
if normalize:
num_instances_per_class = cm.sum(axis=1)
zero_indices = num_instances_per_class == 0
if any(zero_indices):
num_instances_per_class[zero_indices] = 1
warnings.warn('One or more classes does not have instances')
cm = cm / num_instances_per_class[:, np.newaxis]
vmax = 1.
else:
vmax = np.max(cm)
if not cmap:
cmap = list(sns.color_palette("RdBu_r", 200).as_hex())
cmap = ListedColormap(cmap)
fig = plt.figure(figsize=figsize)
plt.clf()
ax = fig.add_subplot(111)
ax.set_aspect(1)
if show_yticks:
yticklabels = labels
else:
yticklabels = []
if show_xticks:
xticklabels = labels
else:
xticklabels = []
ax = sns.heatmap(cm, annot=annot, cmap=cmap, linewidths=linewidths, xticklabels=xticklabels,
yticklabels=yticklabels, square=square,
linecolor=linecolor, vmax=vmax, cbar=cbar,
cbar_kws=cbar_kws)
if show_yticks:
for ticklabel in ax.get_yaxis().get_ticklabels():
ticklabel.set_rotation('horizontal')
if ticks_size:
ticklabel.set_fontsize(ticks_size)
if show_xticks:
ax.xaxis.tick_top()
for ticklabel in ax.get_xaxis().get_ticklabels():
ticklabel.set_rotation('vertical')
if ticks_size:
ticklabel.set_fontsize(ticks_size)
return fig, ax
def show_sequences(sequences, labels_colors=None, figsize=None, tight_layout=None, mask_value=None, ylabel=None,
xlabel=None, yticklabels=True, xticklabels=True, leg_square_size=10, annot=False, aspect_ratio=None,
show_box=False, plot_ylabel=None, plot_xlabel=None, title=None, sequence_ind=None, legends=True,
twin_yticklabels=None):
fig = plt.figure(figsize=figsize)
plt.clf()
ax = fig.add_subplot(111)
if title:
plt.title(title)
if aspect_ratio:
ax.set_aspect(aspect_ratio)
if sequence_ind is None:
if isinstance(sequences, pd.DataFrame):
sequence_ind = Series(sequences.values.ravel()).unique()
sequence_ind = np.sort(sequence_ind)
else:
sequence_ind = np.unique(sequences)
elif isinstance(sequence_ind, list):
sequence_ind = np.asarray(sequence_ind)
# Removing mask value from unique sequences
if mask_value:
mask_ind = np.argwhere(sequence_ind == mask_value)
if len(mask_ind) > 0:
sequence_ind = np.delete(sequence_ind, mask_ind)
if not labels_colors:
colors = sns.color_palette("hls", len(sequence_ind))
labels_colors = {seq_ind: ("", colors[i]) for i, seq_ind in enumerate(sequence_ind)}
vmax = np.max(sequence_ind)
vmin = np.min(sequence_ind)
else:
vmax = np.max(labels_colors.keys())
vmin = np.min(labels_colors.keys())
if sys.version_info >= (3, 0):
cmap = ListedColormap([color for k, (label, color) in labels_colors.items()])
else:
cmap = ListedColormap([color for k, (label, color) in labels_colors.iteritems()])
sns.set_style("white", {'grid.color': '.9', 'axes.edgecolor': '.2', 'axes.linewidth': 1})
if xticklabels:
if not isinstance(sequences, pd.DataFrame):
pass
if not type(xticklabels):
xticklabels = np.linspace(0, sequences.shape[1], xticklabels)
if mask_value:
ax = sns.heatmap(sequences,
cmap=cmap, cbar=False,
annot=annot,
vmin=vmin, vmax=vmax,
xticklabels=xticklabels,
yticklabels=yticklabels,
mask=sequences == mask_value)
else:
ax = sns.heatmap(sequences,
cmap=cmap, cbar=False,
annot=annot,
vmin=vmin, vmax=vmax,
xticklabels=xticklabels,
yticklabels=yticklabels)
if show_box:
ax.axhline(y=0, color='k', linewidth=2)
ax.axhline(y=sequences.shape[0], color='k', linewidth=2)
ax.axvline(x=0, color='k', linewidth=2)
ax.axvline(x=sequences.shape[1], color='k', linewidth=2)
if plot_ylabel:
plt.ylabel(plot_ylabel, fontweight='bold', fontsize=12)
if plot_xlabel:
plt.xlabel(plot_xlabel, fontweight='bold', fontsize=12)
if ylabel:
ax.set_ylabel(ylabel)
if xlabel:
ax.set_xlabel(xlabel)
def create_proxy(color):
line = matplotlib.lines.Line2D([0], [0], linestyle='none', mfc=color,
mec='black',
markersize=leg_square_size,
marker='s', )
return line
if legends:
proxies = [create_proxy(labels_colors[ind][1]) for ind in sequence_ind]
if annot:
descriptions = ['{} {}'.format(ind, labels_colors[ind][0]) for ind in sequence_ind]
else:
descriptions = ['{}'.format(labels_colors[ind][0]) for ind in sequence_ind]
leg = ax.legend(proxies, descriptions, numpoints=1, markerscale=2, frameon=True,
bbox_transform=plt.gcf().transFigure, bbox_to_anchor=(1, 0.5), loc=10)
leg.get_frame().set_edgecolor('#000000')
leg.get_frame().set_linewidth(0)
leg.get_frame().set_facecolor('#FFFFFF')
else:
leg = None
if twin_yticklabels:
ax2 = ax.twinx()
num_sequences = sequences.shape[0]
plt.yticks(np.arange(1 / (2 * num_sequences), 1, step=1 / num_sequences))
ax2.set_yticklabels(reversed(twin_yticklabels))
if tight_layout:
plt.tight_layout()
return fig, ax, leg
def plot_datasets_summary(stats, figsize=None, ylabel="Number of Instances", xlabel="Categories", annot_rotation=90,
annot_fontsize=9, axis_fontsize=12, legend=False, width=0.5, annotate_cols=True, title=None,
colormap=None):
if not figsize:
figsize = (1, 1)
sns.set_style("whitegrid", {'grid.color': '.9', 'axes.edgecolor': '.2', 'axes.linewidth': 1})
sns.set_context("notebook", font_scale=1, rc={"lines.linewidth": 1})
fig, ax = plt.subplots(1, 1, figsize=figsize, sharex=True)
if title:
plt.title(title)
if stats.shape[1] == 1 and not colormap:
stats.plot(kind='bar', ax=ax, legend=legend, width=width, color=[sns.color_palette("PuBu", 10)[-3]])
else:
stats.plot(kind='bar', ax=ax, legend=legend, width=width, colormap=colormap)
plt.setp(ax.xaxis.get_majorticklabels(), rotation=annot_rotation, fontsize=axis_fontsize, ha='right')
plt.setp(ax.yaxis.get_majorticklabels(), fontsize=axis_fontsize)
if annotate_cols:
for i, (index, row) in enumerate(stats.iterrows()):
for col in stats.columns.values:
annotate('{:.0f}'.format(row[col]), (i, row[col] + 25), ha='center', fontsize=annot_fontsize)
plt.ylim([0, 1.1 * stats.values.max()])
SHIFT = -0.3 # Data coordinates
for label in ax.xaxis.get_majorticklabels():
label.customShiftValue = SHIFT
label.set_x = types.MethodType(lambda self, x: matplotlib.text.Text.set_x(self, x - self.customShiftValue),
label, matplotlib.text.Text)
plt.ylabel(ylabel, fontweight='bold', fontsize=12)
plt.xlabel(xlabel, fontweight='bold', fontsize=12)
return fig, ax
def plot_results(values, labels=None, iters=None, epochs=None, figsize=None, plot_type='accuracy', xlabel=None,
subplot=None):
if not isinstance(values, list):
values = [values]
if iters is not None:
x_value = iters
if not xlabel:
xlabel = u'Iteration'
elif epochs is not None:
x_value = epochs
if not xlabel:
xlabel = u'Epoch'
else:
x_value = np.arange(1, len(values[0]) + 1)
if not xlabel:
xlabel = u'Time'
if not subplot:
if not figsize:
figsize = (1, 1)
fig, ax = plt.subplots(1, 1, figsize=figsize, sharex=True)
else:
fig, ax = subplot
sns.set_style("whitegrid", {'axes.edgecolor': '.1', 'axes.linewidth': 0.8})
sns.set_palette("muted")
sns.set_context("notebook", font_scale=1, rc={"lines.linewidth": 1})
ax.grid(b=True, which='major', color='#555555', linestyle=':', linewidth=0.5)
for i, v in enumerate(values):
v_size = len(v) if type(v) == list else v.size
x_value_size = len(x_value) if type(x_value) == list else x_value.size
num_values = np.min((v_size, x_value_size))
if labels:
plt.plot(x_value[:num_values], v[:num_values], linewidth=1.25, linestyle='-', marker='o', markersize=4,
label=labels[i])
else:
plt.plot(x_value[:num_values], v[:num_values], linewidth=1.25, linestyle='-', marker='o', markersize=4)
if plot_type == 'accuracy':
plt.xlim(x_value[0], x_value[-1])
plt.ylim(0, 1)
plt.yticks(np.linspace(0, 1, 11, endpoint=True))
plt.ylabel(u'Accuracy', fontweight='bold', fontsize=12)
plt.xlabel(xlabel, fontweight='bold', fontsize=12)
ax.legend(loc=4, fontsize=11, frameon=True)
elif plot_type == 'loss':
plt.xlim(x_value[0], x_value[-1])
plt.ylabel(u'Error', fontweight='bold', fontsize=12)
plt.xlabel(xlabel, fontweight='bold', fontsize=12)
plt.legend(loc=1, fontsize=11, frameon=True)
return fig, ax
def plot_accuracy(values, labels=None, iters=None, epochs=None, figsize=None, xlabel=None, subplot=None):
return plot_results(values, labels, iters, epochs, figsize, plot_type='accuracy', xlabel=xlabel, subplot=subplot)
def plot_loss(values, labels=None, iters=None, epochs=None, figsize=None, xlabel=None, subplot=None):
return plot_results(values, labels, iters, epochs, figsize, plot_type='loss', xlabel=xlabel, subplot=subplot)
def print_attribute(tag, value):
display(Attribute(tag, value))
def print_bold(value):
display(Bold(value))
def print_table(title, labels_and_values):
if isinstance(labels_and_values, dict):
if sys.version_info >= (3, 0):
labels_and_values = [(l, v) for l, v in labels_and_values.items()]
else:
labels_and_values = [(l, v) for l, v in labels_and_values.iteritems()]
display(LabelValueTable(title, labels_and_values))