From 5a6c454ef8c19c0aba2a553dbe006d3311562230 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ander=20Sim=C3=B3n?= Date: Wed, 13 Jun 2018 16:12:03 +0200 Subject: [PATCH] 482 Reduction of memory usage for monitor plotting https://github.com/nextic/IC/pull/482 [author: Aretno] Quick fix to reduce memory usage for histogram plotting for the monitoring [reviewer: gonzaponte] Nice. Desperately needed to avoid crashes on a daily basis. --- .../icaro/histogram_plot_functions.py | 34 ++++++++++--------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/invisible_cities/icaro/histogram_plot_functions.py b/invisible_cities/icaro/histogram_plot_functions.py index 6ab6b03f9e..4159889de2 100644 --- a/invisible_cities/icaro/histogram_plot_functions.py +++ b/invisible_cities/icaro/histogram_plot_functions.py @@ -149,27 +149,28 @@ def plot_histograms(histo_manager, histonames='all', n_columns=3, plot_errors=Fa if histonames == 'all': histonames = histo_manager.histos - n_histos = len(histonames) - n_columns = min(3, n_histos) - n_rows = int(np.ceil(n_histos / n_columns)) + if out_path is None: + n_histos = len(histonames) + n_columns = min(3, n_histos) + n_rows = int(np.ceil(n_histos / n_columns)) - fig, axes = plt.subplots(n_rows, n_columns, figsize=(8 * n_columns, 6 * n_rows)) + fig, axes = plt.subplots(n_rows, n_columns, figsize=(8 * n_columns, 6 * n_rows)) - if n_histos == 1: - axes = np.array([axes]) for i, histoname in enumerate(histonames): if reference_histo: - if len(reference_histo[histoname].bins) == 1: - plot_histogram(reference_histo[histoname], ax=axes.flatten()[i], plot_errors=plot_errors, normed=normed, draw_color='red', stats=False) - plot_histogram (histo_manager [histoname], ax=axes.flatten()[i], plot_errors=plot_errors, normed=normed) - - fig.tight_layout() - - if out_path: - for i, histoname in enumerate(histonames): - extent = axes.flatten()[i].get_tightbbox(fig.canvas.get_renderer()).transformed(fig.dpi_scale_trans.inverted()) - fig.savefig(out_path + histoname + '.png', bbox_inches=extent) + if out_path: + fig, ax = plt.subplots(1, 1, figsize=(8, 6)) + else: + ax = axes.flatten()[i] if isinstance(axes, np.ndarray) else axes + if len(reference_histo[histoname].bins) == 1: + plot_histogram(reference_histo[histoname], ax=ax, plot_errors=plot_errors, normed=normed, draw_color='red', stats=False) + plot_histogram (histo_manager [histoname], ax=ax, plot_errors=plot_errors, normed=normed) + if out_path: + fig.tight_layout() + fig.savefig(out_path + histoname + '.png') + fig.clf() + plt.close(fig) def get_percentage(a, b): """ @@ -177,6 +178,7 @@ def get_percentage(a, b): """ return 100 * a / b if b else -100 + def average_empty(x, bins): """ Returns the weighted mean. If all weights are 0, the mean is considered to be 0.