-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
328 lines (267 loc) · 11.8 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
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
import csv
import sys
import matplotlib.pyplot as plt
import matplotlib.ticker as mticker
import numpy as np
def invalid_param(param_name):
print(f'Error in config. Invalid or missing {param_name}!')
sys.exit(1)
def read_config_param(config, param_name, converter_fun, valid_fun):
if param_name in config:
param = converter_fun(config[param_name])
if valid_fun(param):
return param
invalid_param(param_name)
RESET = '\033[0m'
def get_color_escape(color_hex, background=False):
rgb = [int(color_hex[i:i+2], 16) for i in range(1, len(color_hex), 2)]
return '\033[{};2;{};{};{}m'.format(48 if background else 38, rgb[0], rgb[1], rgb[2])
def string_with_color(string, color_hex):
return get_color_escape(color_hex) + string + RESET
def print_with_color(string, color_hex):
print(get_color_escape(color_hex) + string + RESET)
# Formatter taken from
# https://stackoverflow.com/questions/25750170/show-decimal-places-and-scientific-notation-on-the-axis-of-a-matplotlib-plot
class MathTextSciFormatter(mticker.Formatter):
def __init__(self, fmt="%1.2e"):
self.fmt = fmt
def __call__(self, x, pos=None):
s = self.fmt % x
dec_point = '.'
pos_sign = '+'
tup = s.split('e')
significand = tup[0].rstrip(dec_point)
sign = tup[1][0].replace(pos_sign, '')
exponent = tup[1][1:].lstrip('0')
if not exponent: exponent = 0
exponent = '10^{%s%s}' % (sign, exponent)
if significand and exponent:
s = r'%s{\times}%s' % (significand, exponent)
else:
s = r'%s%s' % (significand, exponent)
return "${}$".format(s)
def init_plotter():
plt.rcParams.update({'font.size': 20})
def plot_boxplot(values, labels, y_label, save_name=None):
fig, ax = plt.subplots(figsize=(12, 10)) # Create a figure containing a single axes.
ax.boxplot(values)
ax.set_ylabel(y_label)
plt.xticks(range(1, values.shape[1] + 1), labels)
plt.grid()
plt.tight_layout()
if save_name:
plt.savefig(save_name)
else:
plt.show(block=False)
def plot_values(x_values, x_label, y_values, y_label, precision=2, sci_x=False, sci_y=True, min_val=None, max_val=None, log=False, save_name=None, ticks=None):
fig, ax = plt.subplots(figsize=(12, 10)) # Create a figure containing a single axes.
ax.plot(x_values, y_values, marker='o', markersize=3) # Plot some data on the axes
ax.set_xlabel(x_label)
ax.set_ylabel(y_label)
if min_val is not None and max_val is not None:
ax.set_xlim([min_val, max_val])
ax.set_ylim([min_val, max_val])
if log:
ax.set_yscale('log')
if sci_x:
if not log: ax.ticklabel_format(axis="x", style="sci", scilimits=(0,0))
ax.xaxis.set_major_formatter(MathTextSciFormatter(f'%1.{precision}e'))
if sci_y:
if not log: ax.ticklabel_format(axis="y", style="sci", scilimits=(0,0))
ax.yaxis.set_major_formatter(MathTextSciFormatter(f'%1.{precision}e'))
if ticks:
plt.xticks(ticks)
plt.grid()
plt.tight_layout()
if save_name:
plt.savefig(save_name)
else:
plt.show(block=False)
def plot_horiz_bar(labels, values, x_label, save_name=None):
fig, ax = plt.subplots(figsize=(12, 10)) # Create a figure containing a single axes.
ax.barh(labels, values)
ax.set_xlabel(x_label)
plt.grid()
plt.tight_layout()
if save_name:
plt.savefig(save_name)
else:
plt.show(block=False)
def plot_two_components(pca_1, pca_2, comp_1, comp_2, var_labels, val_labels, scale=True, save_name=None):
fig, ax = plt.subplots(figsize=(12, 10)) # Create a figure containing a single axes.
# Scale values if needed
scale_x, scale_y = 1.0, 1.0
scale_arrows = 1.0
if scale:
scale_x = 1.0 / (pca_1.max() - pca_1.min())
scale_y = 1.0 / (pca_2.max() - pca_2.min())
ax.set_xlim(-1, 1)
ax.set_ylim(-1, 1)
else:
scale_arrows = 3
# Plot values
ax.scatter(pca_1 * scale_x, pca_2 * scale_y)
# Annotate values with value labels
for i in range(len(pca_1)):
ax.annotate(val_labels[i], (pca_1[i] * scale_x, pca_2[i] * scale_y), fontsize=10)
# Plot variable arrows with variable labels
for i in range(len(comp_1)):
ax.arrow(0, 0, comp_1[i] * scale_arrows, comp_2[i] * scale_arrows, color='purple', alpha=0.5)
ax.text(comp_1[i] * scale_arrows * 1.12, comp_2[i] * scale_arrows * 1.12, var_labels[i], color='orange', ha='center', va='center', fontsize=15)
ax.set_xlabel("PC1")
ax.set_ylabel("PC2")
plt.grid()
plt.tight_layout()
if save_name:
plt.savefig(save_name)
else:
plt.show(block=False)
def plot_matrix(matrix, cmap='inferno', not_exp=False, vmin=None, vmax=None):
fig, ax = plt.subplots(figsize=(12, 10)) # Create a figure containing a single axes.
caxes = ax.matshow(matrix, interpolation='none', cmap=cmap, vmin=vmin, vmax=vmax)
plt.colorbar(caxes)
notation = '{:0.1E}' if not_exp else '{:0.1f}'
for (i, j), z in np.ndenumerate(matrix):
ax.text(j, i, notation.format(z), ha='center', va='center', color='green')
plt.tight_layout()
plt.show(block=False)
def plot_mult_histogram_density(values_1, values_2, n_bins, x_label, y_label, precision=2, sci_x=False, sci_y=True):
fig, ax = plt.subplots(figsize=(12, 10)) # Create a figure containing a single axes.
weights = np.full(len(values_1), 1.0 / len(values_1))
ax.hist(values_1, bins=n_bins, alpha=0.7, weights=weights, label='Initial') # Plot some data on the axes
weights = np.full(len(values_2), 1.0 / len(values_2))
ax.hist(values_2, bins=n_bins, alpha=0.7, weights=weights, label='Last third') # Plot some data on the axes
ax.set_xlabel(x_label)
ax.set_ylabel(y_label)
if sci_x:
ax.ticklabel_format(axis="x", style="sci", scilimits=(0,0))
ax.xaxis.set_major_formatter(MathTextSciFormatter(f'%1.{precision}e'))
if sci_y:
ax.ticklabel_format(axis="y", style="sci", scilimits=(0,0))
ax.yaxis.set_major_formatter(MathTextSciFormatter(f'%1.{precision}e'))
fig.legend(loc='upper right')
plt.grid()
plt.tight_layout()
plt.show(block=False)
def plot_histogram_density(values, n_bins, x_label, y_label, precision=2, sci_x=False, sci_y=True, log=False):
fig, ax = plt.subplots(figsize=(12, 10)) # Create a figure containing a single axes.
weights = np.full(len(values), 1.0 / len(values))
_n, _bins, _patches = ax.hist(values, bins=n_bins, weights=weights) # Plot some data on the axes
ax.set_xlabel(x_label)
ax.set_ylabel(y_label)
if sci_x:
ax.ticklabel_format(axis="x", style="sci", scilimits=(0,0))
ax.xaxis.set_major_formatter(MathTextSciFormatter(f'%1.{precision}e'))
if sci_y:
ax.ticklabel_format(axis="y", style="sci", scilimits=(0,0))
ax.yaxis.set_major_formatter(MathTextSciFormatter(f'%1.{precision}e'))
plt.grid()
plt.tight_layout()
if log:
step = n_bins[1]
bin_center = [x + step for x in n_bins]
fig, ax = plt.subplots(figsize=(12, 10)) # Create a figure containing a single axes.
ax.plot(bin_center[:len(bin_center) - 1], _n)
ax.set_xlabel(x_label)
ax.set_ylabel(y_label)
ax.set_yscale('symlog', linthresh=1e-3)
ax.ticklabel_format(axis="x", style="sci", scilimits=(0,0))
ax.xaxis.set_major_formatter(MathTextSciFormatter(f'%1.{precision}e'))
plt.grid()
plt.tight_layout()
plt.show(block=False)
def plot_values_with_adjust(x_values, x_label, y_values, y_label, precision=2, sci=True, min_val=None, max_val=None, plot=True, save_name=None):
# adj_coef = np.polyfit(x_values, y_values, 1)
# poly1d_fn = np.poly1d(adj_coef)
c, err = calculate_regression(x_values, y_values, plot)
print(c, err)
if not plot: return c
fig, ax = plt.subplots(figsize=(12, 10)) # Create a figure containing a single axes.
ax.plot(x_values, y_values, 'yo', x_values, [f_adj(x, c) for x in x_values], '-k') # Plot some data on the axes
ax.set_xlabel(x_label)
ax.set_ylabel(y_label)
if min_val is not None and max_val is not None:
ax.set_xlim([min_val, max_val])
ax.set_ylim([min_val, max_val])
if sci:
ax.ticklabel_format(scilimits=(0,0))
ax.xaxis.set_major_formatter(MathTextSciFormatter(f'%1.{precision}e'))
ax.yaxis.set_major_formatter(MathTextSciFormatter(f'%1.{precision}e'))
plt.grid()
plt.tight_layout()
if save_name:
plt.savefig(save_name)
else:
plt.show(block=False)
return c
def plot_multiple_values(x_values_superlist, x_label, y_values_superlist, y_label, legend_list, precision=2, sci_x=False, sci_y=True, min_val_x=None, max_val_x=None, min_val_y=None, max_val_y=None, log_x=False, log_y=False, legend_loc='upper right', save_name=None):
fig, ax = plt.subplots(figsize=(12, 10)) # Create a figure containing a single axes.
colors = []
for i in range(len(x_values_superlist)):
p = ax.plot(x_values_superlist[i], y_values_superlist[i], label=legend_list[i]) # Plot some data on the axes
colors.append(p[-1].get_color())
if log_x:
ax.set_xscale('log')
if log_y:
ax.set_yscale('log')
ax.set_xlabel(x_label)
ax.set_ylabel(y_label)
if min_val_x is not None and max_val_x is not None:
ax.set_xlim([min_val_x, max_val_x])
if min_val_y is not None and max_val_y is not None:
ax.set_ylim([min_val_y, max_val_y])
if sci_x:
if not log_x: ax.ticklabel_format(axis="x", style="sci", scilimits=(0,0))
ax.xaxis.set_major_formatter(MathTextSciFormatter(f'%1.{precision}e'))
if sci_y:
if not log_y: ax.ticklabel_format(axis="y", style="sci", scilimits=(0,0))
ax.yaxis.set_major_formatter(MathTextSciFormatter(f'%1.{precision}e'))
plt.tight_layout()
plt.grid()
plt.legend(loc=legend_loc)
if save_name:
plt.savefig(save_name)
else:
plt.show(block=False)
return colors
def plot_error_bars_summary(x_values, x_label, sum_values, attribute, y_label, x_prec=2, sci_x=False, sci_y=True, y_min=None, y_max=None, log=False, save_name=None):
values = []
values_err = []
min_dec = getattr(sum_values[0], attribute).dec_count
for x in sum_values:
attr = getattr(x, attribute)
values.append(attr.media)
values_err.append(attr.std)
if attr.dec_count < min_dec:
min_dec = attr.dec_count
# min_dec += 1
if sci_y: min_dec = 1
print(y_label)
print(values)
print(values_err)
print(min_dec)
plot_error_bars(x_values, x_label, values, y_label, values_err, x_prec, min_dec, sci_x, sci_y, y_min, y_max, log, save_name)
def plot_error_bars(x_values, x_label, y_values, y_label, y_error, x_prec=2, y_prec=2, sci_x=False, sci_y=True, y_min=None, y_max=None, log=False, save_name=None):
fig, ax = plt.subplots(figsize=(12, 10)) # Create a figure containing a single axes.
(_, caps, _) = plt.errorbar(x_values, y_values, yerr=y_error, markersize=6, capsize=20, elinewidth=0.75, linestyle='-', marker='o') # Plot some data on the axes
for cap in caps:
cap.set_markeredgewidth(1)
ax.set_ylim([y_min, y_max])
if log:
ax.set_yscale('symlog', linthresh=1e-3)
ax.set_xlabel(x_label)
ax.set_ylabel(y_label)
if sci_x:
if not log: ax.ticklabel_format(axis="x", style="sci", scilimits=(0,0))
ax.xaxis.set_major_formatter(MathTextSciFormatter(f'%1.{x_prec}e'))
if sci_y:
if not log: ax.ticklabel_format(axis="y", style="sci", scilimits=(0,0))
ax.yaxis.set_major_formatter(MathTextSciFormatter(f'%1.{y_prec}e'))
plt.grid()
plt.tight_layout()
if save_name:
plt.savefig(save_name)
else:
plt.show(block=False)
def hold_execution():
plt.show(block=True)