-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
461 lines (373 loc) · 17.5 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
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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
import sys
import random
import matplotlib.pyplot as plt
import matplotlib.ticker as mticker
import numpy as np
def set_random_seed(seed):
random.seed(seed)
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 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_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)
# Linear regression for Beverloo, linear regression of modified values with b = 0
def f_adj(d, rmed, b):
C = 0.5
EXP = 1.5
return b * ((d - C * rmed) ** EXP)
def calculate_regression(x_values, rmed_values, y_values, plot_error=False):
min_error, min_c = float("Inf"), 10
error_list = []
b_list = []
for b in np.arange(-1, 4, 0.0001):
error_sum = 0
for i in range(0, len(x_values)):
error_sum += (y_values[i] - f_adj(x_values[i], rmed_values[i], b)) ** 2
error_list.append(error_sum)
b_list.append(b)
if error_sum < min_error:
min_error = error_sum
min_b = b
if plot_error:
# Plot Error = f(b)
fig, ax = plt.subplots(figsize=(12, 10)) # Create a figure containing a single axes.
ax.plot(b_list, error_list)
ax.set_xlabel('b')
ax.set_ylabel('Error')
plt.grid()
plt.tight_layout()
plt.show(block=False)
return min_b, min_error
def plot_values_with_adjust(x_values, x_label, y_values, y_label, rmed_values, precision=2, sci=True, min_val=None, max_val=None, plot=True, save_name=None):
b, err = calculate_regression(x_values, rmed_values, y_values, plot)
print("Adjusting, b=", b, "Error(b)=", err)
if not plot: return b
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, r, b) for x,r in zip(x_values, rmed_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 b
def plot_values_with_adjust_and_err(x_values, x_label, y_values, y_label, y_error, rmed_values, precision=2, sci=True, min_val=None, max_val=None, plot=True, save_name=None):
b, err = calculate_regression(x_values, rmed_values, y_values, plot)
print("Adjusting, b=", b, "Error(b)=", err)
if not plot: return b
fig, ax = plt.subplots(figsize=(12, 10)) # Create a figure containing a single axes.
ax.plot(x_values, [f_adj(x, r, b) for x,r in zip(x_values, rmed_values)], 'or', markersize=8) # Plot some data on the axes
d_adj_values = []
f_adj_values = []
for i in range(len(x_values) - 1):
for d in np.arange(x_values[i], x_values[i + 1], 0.02):
f_adj_values.append(f_adj(d, rmed_values[i] + (rmed_values[i + 1] - rmed_values[i]) * (d - x_values[i]) / (x_values[i + 1] - x_values[i]), b))
d_adj_values.append(d)
f_adj_values.append(f_adj(x_values[-1], rmed_values[-1], b))
d_adj_values.append(x_values[-1])
ax.plot(d_adj_values, f_adj_values, '-r', label='ajuste') # Plot some data on the axes
(_, caps, _) = plt.errorbar(x_values, y_values, yerr=y_error, markersize=8, capsize=20, elinewidth=0.75, linestyle='-', marker='o', label='caudal') # Plot some data on the axes
for cap in caps:
cap.set_markeredgewidth(1)
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 b
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_multiple_values_with_scatter(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', scatter_superlist=None, 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 scatter_superlist is not None:
plt.scatter(scatter_superlist[0], scatter_superlist[1], c=scatter_superlist[2], s=8)
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_values(x_values, x_label, y_values, y_label, precision=2, sci_x=False, sci_y=True, log=False, min_val_x=None, max_val_x=None, min_val_y=None, max_val_y=None, save_name=None):
fig, ax = plt.subplots(figsize=(12, 10)) # Create a figure containing a single axes.
ax.plot(x_values, y_values) # Plot some data on the axes
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 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'))
plt.grid()
plt.tight_layout()
if save_name:
plt.savefig(save_name)
else:
plt.show(block=False)
def plot_values_with_scatter(x_values, x_label, y_values, y_label, precision=2, sci_x=False, sci_y=True, log=False, min_val_x=None, max_val_x=None, min_val_y=None, max_val_y=None, scatter_superlist=None, save_name=None):
fig, ax = plt.subplots(figsize=(12, 10)) # Create a figure containing a single axes.
ax.plot(x_values, y_values) # Plot some data on the axes
ax.set_xlabel(x_label)
ax.set_ylabel(y_label)
if scatter_superlist is not None:
plt.scatter(scatter_superlist[0], scatter_superlist[1], c=scatter_superlist[2], s=8)
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 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'))
plt.grid()
plt.tight_layout()
if save_name:
plt.savefig(save_name)
else:
plt.show(block=False)
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 plot_error_bars_x(x_values, x_label, y_values, y_label, x_error, x_prec=2, y_prec=2, sci_x=False, sci_y=False, 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, xerr=x_error, markersize=3, capsize=0, elinewidth=0.75, ecolor='r', linestyle='-', marker='o') # Plot some data on the axes
# plt.plot(x_values, y_values)
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 plot_multiple_error_bars(x_values_superlist, x_label, y_values_superlist, y_label, y_error, legend_list, x_prec=2, y_prec=2, sci_x=False, sci_y=True, y_min=None, y_max=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.
for i in range(len(x_values_superlist)):
(_, caps, _) = plt.errorbar(x_values_superlist[i], y_values_superlist[i], yerr=y_error[i], markersize=4, capsize=20, elinewidth=0.75, linestyle='-', marker='o', label=legend_list[i]) # Plot some data on the axes
for cap in caps:
cap.set_markeredgewidth(1)
ax.set_ylim([y_min, y_max])
if log_x:
ax.set_xscale('symlog', linthresh=1e-20)
if log_y:
ax.set_yscale('symlog', linthresh=1e-20)
ax.set_xlabel(x_label)
ax.set_ylabel(y_label)
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.{x_prec}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.{y_prec}e'))
plt.legend(loc=legend_loc)
plt.grid()
plt.tight_layout()
if save_name:
plt.savefig(save_name)
else:
plt.show(block=False)
def hold_execution():
plt.show(block=True)