Skip to content

Commit dfa122f

Browse files
Benedikt Volkelsawenzel
authored andcommitted
Fixes and protections
* Fix InfluxDB file structure * stop plotting early if no data provided
1 parent 2c0f2f2 commit dfa122f

File tree

1 file changed

+25
-3
lines changed

1 file changed

+25
-3
lines changed

MC/utils/o2dpg_sim_metrics.py

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -210,12 +210,17 @@ def make_histo(x, y, xlabel, ylabel, ax=None, cmap=None, norm=True, title=None,
210210
"""
211211
figure, ax = make_default_figure(ax)
212212

213+
if not len(x) or not len(y):
214+
print("No data for plotting...")
215+
return figure, ax
216+
213217
y = y.copy()
214218
x = [i for _, i in sorted(zip(y, x))]
215219
y.sort()
216220
if norm:
217221
total = sum(y)
218-
y = [i / total for i in y]
222+
if total > 0:
223+
y = [i / total for i in y]
219224
colors = None
220225
if cmap:
221226
step = 1. / len(y)
@@ -254,6 +259,10 @@ def make_plot(x, y, xlabel, ylabel, ax=None, title=None, **kwargs):
254259
"""
255260
figure, ax = make_default_figure(ax)
256261

262+
if not len(x) or not len(y):
263+
print("No data for plotting...")
264+
return figure, ax
265+
257266
ax.plot(x, y)
258267
ax.tick_params("both", labelsize=30)
259268
ax.tick_params("x", rotation=45)
@@ -281,6 +290,11 @@ def make_pie(labels, y, ax=None, cmap=None, title=None, **kwargs):
281290
title to be put for figure
282291
"""
283292
figure, ax = make_default_figure(ax)
293+
294+
if not len(labels) or not len(y):
295+
print("No data for plotting...")
296+
return figure, ax
297+
284298
y = y.copy()
285299
labels = [l for _, l in sorted(zip(y, labels))]
286300
y.sort()
@@ -319,6 +333,11 @@ def plot_histo_and_pie(x, y, xlabel, ylabel, path, **kwargs):
319333
scale before plotting
320334
"""
321335
figure, axes = plt.subplots(1, 3, figsize=(60, 20))
336+
337+
if not len(x) or not len(y):
338+
print("No data for plotting...")
339+
return
340+
322341
title = kwargs.pop("title", None)
323342
scale = kwargs.pop("scale", 1.)
324343
y = [i * scale for i in y]
@@ -391,10 +410,13 @@ def make_for_influxDB(full_map, table_base_name, save_path):
391410
fields = ",".join([f"{k}={v}" for k, v in tags.items()])
392411
db_string = f"{tab_name},{fields}"
393412
total = 0
413+
# fields are separated from the tags by a whitespace
414+
fields = []
394415
for cat, val in metrics.items():
395-
db_string += f",{cat}={val['sum'][metric_id]}"
416+
fields.append(f"{cat}={val['sum'][metric_id]}")
396417
total += val["sum"][metric_id]
397-
db_string += f",total={total}"
418+
fields = ",".join(fields)
419+
db_string += f" {fields},total={total}"
398420
f.write(f"{db_string}\n")
399421

400422

0 commit comments

Comments
 (0)