From 66d1c7c983b198e04745cff69ca8b50c3c5e39e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alberto=20Us=C3=B3n=20Andr=C3=A9s?= Date: Wed, 11 Mar 2020 17:28:16 +0100 Subject: [PATCH 01/15] Add function to save histograms as pdf files during map creation. --- krcal/core/io_functions.py | 45 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/krcal/core/io_functions.py b/krcal/core/io_functions.py index c3d2ca38..2187a761 100644 --- a/krcal/core/io_functions.py +++ b/krcal/core/io_functions.py @@ -1,5 +1,6 @@ import numpy as np import pandas as pd +import matplotlib.pyplot as plt from invisible_cities.core.core_functions import shift_to_bin_centers from typing import Tuple from . kr_types import ASectorMap @@ -50,3 +51,47 @@ def compute_and_save_hist_as_pd(values : np.array , out_file.put(hist_name, table, format='table', data_columns=True) return + + + +def compute_and_save_hist_as_pdf(values : np.array , + out_file : str , + n_bins : int , + range_hist : Tuple[float, float], + title : str , + x_label : str , + y_range : Tuple[float, float], + norm : bool = False )->None: + """ + Computes 1d-histogram and saves it as a pdf image. + Parameters + ---------- + values : np.array + Array with values to be plotted. + out_file: string + File where histogram will be saved. + n_bins: int + Number of bins to make the histogram. + range_hist: length-2 tuple (optional) + Range of the histogram. + title: str + Title for the plot. + x_label: str + Label for X-axis. + y_range: length-2 tuple + Limit fot Y-axis. + norm: bool + If True, histogram will be normalized. + """ + fig = plt.figure(); + plt.hist(values, bins = n_bins, + range = range_hist, density = norm, + histtype='step', linewidth=2); + plt.ylabel('Entries'); + plt.xlabel(x_label); + plt.title(title); + plt.ylim(y_range); + plt.grid(True, alpha=0.5, color='k', linestyle=':'); + fig.savefig(out_file.format(title).replace(" ", ""), bbox_inches='tight') + + return From 90fc3db5081974f64345eb4ca7880d9f9875ad92 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alberto=20Us=C3=B3n=20Andr=C3=A9s?= Date: Wed, 11 Mar 2020 17:30:52 +0100 Subject: [PATCH 02/15] Add function to plot and save as pdf evolution for a given parameter --- krcal/core/io_functions.py | 50 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/krcal/core/io_functions.py b/krcal/core/io_functions.py index 2187a761..cf7d37a6 100644 --- a/krcal/core/io_functions.py +++ b/krcal/core/io_functions.py @@ -1,6 +1,7 @@ import numpy as np import pandas as pd import matplotlib.pyplot as plt +import matplotlib.dates as md from invisible_cities.core.core_functions import shift_to_bin_centers from typing import Tuple from . kr_types import ASectorMap @@ -95,3 +96,52 @@ def compute_and_save_hist_as_pdf(values : np.array , fig.savefig(out_file.format(title).replace(" ", ""), bbox_inches='tight') return + + +def plot_and_save_evolution_figure(time : np.array , + param_name : str , + param : np.array , + param_u : np.array , + units : str , + file_name : str , + n_sigmas_lim: int = 1): + """ + Plot and save evolution of a given parameter vs time. + Parameters + ---------- + time : np.array + Array with the centers of each time interval (X-values). + param_name : string + Name of the parameter to plot. + param : np.array + Array with the magnitude to plot (Y-values). + param_u : np.array + Array with uncertainty values. + units : string + Units to be shown in x-label. + + Returns + ---------- + Nothing + """ + fig = plt.figure() + plt.errorbar(time, param, param_u, fmt='.') + plt.xlabel('Date') + plt.ylabel('{0} ({1})'.format(param_name, units)); + plt.title('{0}'.format(param_name)) + mean = np.mean(param) + std = np.std(param) + plt.ylim(param.min()-std*n_sigmas_lim, + param.max()+std*n_sigmas_lim) + plt.grid(True, alpha=0.5, color='k', linestyle=':') + ax = plt.gca() + xfmt = md.DateFormatter('%d-%m %H:%M') + ax.xaxis.set_major_formatter(xfmt) + plt.xticks( rotation=25 ) + plt.text(0.03,0.9, + 'Mean= {0} \n Std= {1}'.format(np.round(mean,4), np.round(std,4)), + fontsize=10, transform=ax.transAxes, + bbox={'facecolor': 'white', 'alpha': 1, 'pad': 5}) + fig.savefig(file_name.format(param_name), bbox_inches='tight') + + return From a2c490f327b5c7c37d6503a368784adfd7874f24 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alberto=20Us=C3=B3n=20Andr=C3=A9s?= Date: Wed, 11 Mar 2020 17:51:35 +0100 Subject: [PATCH 03/15] Add function to select parameter of temp_evol table to plot it --- krcal/core/plot_utils.py | 42 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 krcal/core/plot_utils.py diff --git a/krcal/core/plot_utils.py b/krcal/core/plot_utils.py new file mode 100644 index 00000000..473cb325 --- /dev/null +++ b/krcal/core/plot_utils.py @@ -0,0 +1,42 @@ +import pandas as pd +import numpy as np +import datetime +from . io_functions import plot_and_save_evolution_figure + +def par_selection_to_plot_vs_time(evol_table: pd.DataFrame, + file_name : str ): + """ + Selects parameters in time_evolution map table and + applies over them a function to plot all of them + Parameters + ---------- + evol_table : pd.DataFrame + Table with temporal evolution information. + file_name : string + Standard name for saved files. + + Returns + ---------- + Nothing + """ + time_ = list(map(datetime.datetime.fromtimestamp, evol_table.ts)) + units_vect = ['pes', 'mus', 'mm/mus', '%', 'ns', 'pes', + 'pes', 'mus', 'pes', 'pes', 'pes', '# SiPM'] + + for idx, par in enumerate(evol_table.columns[1:-7:2]): + plot_and_save_evolution_figure(time = time_ , + param_name = par , + param = evol_table[par] , + param_u = evol_table[par+'u'], + units = units_vect[idx] , + file_name = file_name , + n_sigmas_lim = 5 ) + for idx, par in enumerate(evol_table.columns[-3:]): + plot_and_save_evolution_figure(time = time_ , + param_name = par , + param = evol_table[par], + param_u = 0 , + units = '%' , + file_name = file_name , + n_sigmas_lim = 10 ) + return From e99e9826f46d12cb985adf59a023c5439ce76a43 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alberto=20Us=C3=B3n=20Andr=C3=A9s?= Date: Wed, 11 Mar 2020 18:07:40 +0100 Subject: [PATCH 04/15] Change checking function to save histogram files separately as pdf --- krcal/map_builder/map_builder_functions.py | 34 ++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/krcal/map_builder/map_builder_functions.py b/krcal/map_builder/map_builder_functions.py index 2c43771e..6f4b2b45 100644 --- a/krcal/map_builder/map_builder_functions.py +++ b/krcal/map_builder/map_builder_functions.py @@ -26,6 +26,7 @@ from .. core.kr_parevol_functions import get_number_of_time_bins from .. core.io_functions import write_complete_maps from .. core.io_functions import compute_and_save_hist_as_pd +from .. core.io_functions import compute_and_save_hist_as_pdf from .. core.histo_functions import compute_similar_histo from .. core.histo_functions import normalize_histo_and_poisson_error from .. core.histo_functions import ref_hist @@ -121,6 +122,7 @@ def selection_nS_mask_and_checking(dst : pd.DataFrame , column : type_of_signal , interval : Tuple[float, float] , output_f : pd.HDFStore , + monitoring : str = None , input_mask : np.array = None , nbins_hist : int = 10 , range_hist : Tuple[float, float] = (0,10), @@ -141,6 +143,8 @@ def selection_nS_mask_and_checking(dst : pd.DataFrame , (given by the config file) the map production will abort. output_f: pd.HDFStore File where histogram will be saved. + monitoring: str (optional) + If specified (with a file name) a histogram will be save with that name. input_mask: np.array (Optional) Selection mask of the previous cut. If this is the first selection cut, input_mask is set to be an all True array. @@ -172,6 +176,17 @@ def selection_nS_mask_and_checking(dst : pd.DataFrame , range_hist = range_hist, norm = norm) + if monitoring: + compute_and_save_hist_as_pdf(values = getattr(mod_dst, + column.value), + out_file = monitoring, + n_bins = nbins_hist, + range_hist = range_hist, + title = column.value, + x_label = column.value, + y_range = (0,1), + norm = norm) + message = "Selection efficiency of " message += column.value message += "==1 ({0}) out of range ".format(np.round(eff, 3)) @@ -220,6 +235,8 @@ def check_Z_dst(Z_vect : np.array , def check_rate_and_hist(times : np.array , output_f : pd.HDFStore , name_table : str , + hist_title : str , + monitoring : str = None , n_dev : float = 5 , bin_size : int = 180 , normed : bool = False)->None: @@ -234,6 +251,10 @@ def check_rate_and_hist(times : np.array , File where histogram will be saved. name_table: string Name for the histogram table inside file. + hist_title: sting + Name for the histogram title. + monitoring: str (optional) + If specified, histogram plots will be save with given name. n_dev: float Relative standard deviation to judge if distribution is correct. @@ -260,6 +281,16 @@ def check_rate_and_hist(times : np.array , range_hist= (min_time , max_time) , norm = normed ) + if monitoring: + compute_and_save_hist_as_pdf(values = times, + out_file = monitoring, + n_bins = ntimebins, + range_hist = (min_time , + max_time), + title = hist_title, + x_label = 'Time (s)', + y_range = (0,6000), + norm = normed) n, _ = np.histogram(times, bins=ntimebins, range = (min_time, max_time)) @@ -628,12 +659,14 @@ def apply_cuts(dst : pd.DataFrame , nsigmas_Zdst : float , bootstrapmap : ASectorMap , band_sel_params : dict , + monitoring : str , ) -> pd.DataFrame: n0 = dst.event.nunique() mask1 = selection_nS_mask_and_checking(dst = dst , column = S1_signal , interval = nS1_eff_interval, output_f = store_hist_s1 , + monitoring = monitoring , **ns1_histo_params ) nS1 = dst[mask1].event.nunique() print(" 1 S1 cut efficiency within the expectations ({0:2.2f}%)".format(nS1/n0*100)) @@ -641,6 +674,7 @@ def apply_cuts(dst : pd.DataFrame , column = S2_signal , interval = nS2_eff_interval, output_f = store_hist_s2 , + monitoring = monitoring , input_mask = mask1 , **ns2_histo_params ) nS2 = dst[mask2].event.nunique() From 8d325f01aa2e176a354db0704ac2b5bdda46278d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alberto=20Us=C3=B3n=20Andr=C3=A9s?= Date: Wed, 11 Mar 2020 18:10:17 +0100 Subject: [PATCH 05/15] Change code to not save histograms as DataFrame in one file --- krcal/map_builder/map_builder_functions.py | 104 ++++++++------------- 1 file changed, 39 insertions(+), 65 deletions(-) diff --git a/krcal/map_builder/map_builder_functions.py b/krcal/map_builder/map_builder_functions.py index 6f4b2b45..435e6a26 100644 --- a/krcal/map_builder/map_builder_functions.py +++ b/krcal/map_builder/map_builder_functions.py @@ -25,7 +25,6 @@ from .. core.kr_parevol_functions import cut_time_evolution from .. core.kr_parevol_functions import get_number_of_time_bins from .. core.io_functions import write_complete_maps -from .. core.io_functions import compute_and_save_hist_as_pd from .. core.io_functions import compute_and_save_hist_as_pdf from .. core.histo_functions import compute_similar_histo from .. core.histo_functions import normalize_histo_and_poisson_error @@ -121,7 +120,6 @@ def load_data(input_path : str , def selection_nS_mask_and_checking(dst : pd.DataFrame , column : type_of_signal , interval : Tuple[float, float] , - output_f : pd.HDFStore , monitoring : str = None , input_mask : np.array = None , nbins_hist : int = 10 , @@ -141,8 +139,6 @@ def selection_nS_mask_and_checking(dst : pd.DataFrame , interval: length-2 tuple If the selection efficiency is out of this interval (given by the config file) the map production will abort. - output_f: pd.HDFStore - File where histogram will be saved. monitoring: str (optional) If specified (with a file name) a histogram will be save with that name. input_mask: np.array (Optional) @@ -168,13 +164,6 @@ def selection_nS_mask_and_checking(dst : pd.DataFrame , eff = nevts_after / nevts_before mod_dst = dst[['event', column.value]].drop_duplicates() - compute_and_save_hist_as_pd(values = getattr(mod_dst, - column.value), - out_file = output_f, - hist_name = column.value, - n_bins = nbins_hist, - range_hist = range_hist, - norm = norm) if monitoring: compute_and_save_hist_as_pdf(values = getattr(mod_dst, @@ -233,8 +222,6 @@ def check_Z_dst(Z_vect : np.array , return; def check_rate_and_hist(times : np.array , - output_f : pd.HDFStore , - name_table : str , hist_title : str , monitoring : str = None , n_dev : float = 5 , @@ -247,10 +234,6 @@ def check_rate_and_hist(times : np.array , ---------- times: np.array Time of the events. - output_f: pd.HDFStore - File where histogram will be saved. - name_table: string - Name for the histogram table inside file. hist_title: sting Name for the histogram title. monitoring: str (optional) @@ -274,13 +257,6 @@ def check_rate_and_hist(times : np.array , min_time, max_time) - compute_and_save_hist_as_pd(values = times , - out_file = output_f , - hist_name = name_table , - n_bins = ntimebins , - range_hist= (min_time , - max_time) , - norm = normed ) if monitoring: compute_and_save_hist_as_pdf(values = times, out_file = monitoring, @@ -649,11 +625,9 @@ def compute_map(dst : pd.DataFrame, def apply_cuts(dst : pd.DataFrame , S1_signal : type_of_signal , nS1_eff_interval : Tuple[float, float], - store_hist_s1 : pd.HDFStore , ns1_histo_params : dict , S2_signal : type_of_signal , nS2_eff_interval : Tuple[float, float], - store_hist_s2 : pd.HDFStore , ns2_histo_params : dict , ref_Z_histo : pd.DataFrame , nsigmas_Zdst : float , @@ -665,7 +639,6 @@ def apply_cuts(dst : pd.DataFrame , mask1 = selection_nS_mask_and_checking(dst = dst , column = S1_signal , interval = nS1_eff_interval, - output_f = store_hist_s1 , monitoring = monitoring , **ns1_histo_params ) nS1 = dst[mask1].event.nunique() @@ -673,7 +646,6 @@ def apply_cuts(dst : pd.DataFrame , mask2 = selection_nS_mask_and_checking(dst = dst , column = S2_signal , interval = nS2_eff_interval, - output_f = store_hist_s2 , monitoring = monitoring , input_mask = mask1 , **ns2_histo_params ) @@ -710,43 +682,45 @@ def map_builder(config): quality_ranges = config.quality_ranges , **config.ref_Z_histogram ) - with pd.HDFStore(config.file_out_hists, "w", complib=str("zlib"), complevel=4) as store_hist: - print("Checking the dst and appling 1S1, 1S2 and z-band selections:") - - nev_before = dst.event.nunique() - print(" Number of events before any selection: {0}".format(nev_before)) - check_rate_and_hist(times = dst.time , - output_f = store_hist , - name_table = "rate_before_sel", - n_dev = config.n_dev_rate, - **config.rate_histo_params ) - - dst_passed_cut, masks = apply_cuts(dst = dst , - S1_signal = type_of_signal.nS1 , - nS1_eff_interval = (config.nS1_eff_min , - config.nS1_eff_max) , - store_hist_s1 = store_hist , - ns1_histo_params = config.ns1_histo_params, - S2_signal = type_of_signal.nS2 , - nS2_eff_interval = (config.nS2_eff_min , - config.nS2_eff_max) , - store_hist_s2 = store_hist , - ns2_histo_params = config.ns2_histo_params, - nsigmas_Zdst = config.nsigmas_Zdst , - ref_Z_histo = ref_histos. - Z_dist_hist, - bootstrapmap = bootstrapmap , - band_sel_params = config.band_sel_params ) - - check_rate_and_hist(times = dst_passed_cut.time, - output_f = store_hist , - name_table = "rate_after_sel" , - n_dev = config.n_dev_rate , - **config.rate_histo_params ) - - nev_after = dst_passed_cut.event.nunique() - ratio = nev_after/nev_before*100 - print(" Number of events passing the cuts: {0} ({1:2.2f}%)".format(nev_after, ratio)) + monitoring = config.monitoring_path + if monitoring: + monitoring = monitoring + 'Run{0}_{{0}}_plot.pdf'.format(config.run_number) + + print("Checking the dst and appling 1S1, 1S2 and z-band selections:") + + nev_before = dst.event.nunique() + print(" Number of events before any selection: {0}".format(nev_before)) + check_rate_and_hist(times = dst.time , + hist_title = "Rate Before Selection", + monitoring = monitoring, + n_dev = config.n_dev_rate, + **config.rate_histo_params ) + + dst_passed_cut, masks = apply_cuts(dst = dst , + S1_signal = type_of_signal.nS1 , + nS1_eff_interval = (config.nS1_eff_min , + config.nS1_eff_max) , + ns1_histo_params = config.ns1_histo_params, + S2_signal = type_of_signal.nS2 , + nS2_eff_interval = (config.nS2_eff_min , + config.nS2_eff_max) , + ns2_histo_params = config.ns2_histo_params, + nsigmas_Zdst = config.nsigmas_Zdst , + ref_Z_histo = ref_histos. + Z_dist_hist , + bootstrapmap = bootstrapmap , + band_sel_params = config.band_sel_params , + monitoring = monitoring ) + + check_rate_and_hist(times = dst_passed_cut.time , + hist_title = "Rate After Selection", + monitoring = monitoring, + n_dev = config.n_dev_rate, + **config.rate_histo_params ) + + nev_after = dst_passed_cut.event.nunique() + ratio = nev_after/nev_before*100 + print(" Number of events passing the cuts: {0} ({1:2.2f}%)".format(nev_after, ratio)) print("Map computation:") From 11e8dd358a128aeeb425515231d1ea13186d52c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alberto=20Us=C3=B3n=20Andr=C3=A9s?= Date: Wed, 11 Mar 2020 18:11:25 +0100 Subject: [PATCH 06/15] Add functionality to plot and save evolution of krypton parameters --- krcal/map_builder/map_builder_functions.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/krcal/map_builder/map_builder_functions.py b/krcal/map_builder/map_builder_functions.py index 435e6a26..39303867 100644 --- a/krcal/map_builder/map_builder_functions.py +++ b/krcal/map_builder/map_builder_functions.py @@ -29,6 +29,7 @@ from .. core.histo_functions import compute_similar_histo from .. core.histo_functions import normalize_histo_and_poisson_error from .. core.histo_functions import ref_hist +from .. core.plot_utils import par_selection_to_plot_vs_time from . checking_functions import check_if_values_in_interval from . checking_functions import check_failed_fits @@ -746,4 +747,8 @@ def map_builder(config): write_complete_maps(asm = final_map , filename = config.file_out_map) print("Map successfully computed and saved in : {0}".format(config.file_out_map)) - print("Control histograms saved in : {0}".format(config.file_out_hists)) + if monitoring: + par_selection_to_plot_vs_time(evol_table = final_map.t_evol, + file_name = monitoring ) + print("Control histograms saved in : {0}".format(monitoring. + replace(monitoring.split('/', -1)[-1], ''))) From ed36e665617c9aaa2f074f51dd5a256546406a61 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alberto=20Us=C3=B3n=20Andr=C3=A9s?= Date: Wed, 11 Mar 2020 19:14:06 +0100 Subject: [PATCH 07/15] Adapt config. files to new feature --- krcal/map_builder/config_HEcal.conf | 2 +- krcal/map_builder/config_HEcal_HighKrRate.conf | 2 +- krcal/map_builder/config_LBphys.conf | 2 +- krcal/map_builder/config_LBphys_run4.conf | 2 +- krcal/map_builder/config_NoChecks.conf | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/krcal/map_builder/config_HEcal.conf b/krcal/map_builder/config_HEcal.conf index b67cca60..34bf0b9a 100644 --- a/krcal/map_builder/config_HEcal.conf +++ b/krcal/map_builder/config_HEcal.conf @@ -2,7 +2,7 @@ folder = '{folderin}' file_in = '{filein}' file_bootstrap_map = '$ICDIR/database/test_data/kr_emap_xy_100_100_r_6573_time.h5' file_out_map = '{fileoutmap}' -file_out_hists = '{fileouthist}' +monitoring_path = '{folderouthists}' # High Energy Configuration File: ref_Z_histogram = dict( diff --git a/krcal/map_builder/config_HEcal_HighKrRate.conf b/krcal/map_builder/config_HEcal_HighKrRate.conf index 24d46f88..385948bc 100644 --- a/krcal/map_builder/config_HEcal_HighKrRate.conf +++ b/krcal/map_builder/config_HEcal_HighKrRate.conf @@ -2,7 +2,7 @@ folder = '{folderin}' file_in = '{filein}' file_bootstrap_map = '$ICDIR/database/test_data/kr_emap_xy_100_100_r_6573_time.h5' file_out_map = '{fileoutmap}' -file_out_hists = '{fileouthist}' +monitoring_path = '{folderouthists}' # High Energy Configuration File: ref_Z_histogram = dict( diff --git a/krcal/map_builder/config_LBphys.conf b/krcal/map_builder/config_LBphys.conf index 776ee2f6..e78caefa 100644 --- a/krcal/map_builder/config_LBphys.conf +++ b/krcal/map_builder/config_LBphys.conf @@ -2,7 +2,7 @@ folder = '{folderin}' file_in = '{filein}' file_bootstrap_map = '$ICDIR/database/test_data/kr_emap_xy_100_100_r_6573_time.h5' file_out_map = '{fileoutmap}' -file_out_hists = '{fileouthist}' +monitoring_path = '{folderouthists}' # Low Background Configuration File: ref_Z_histogram = dict( diff --git a/krcal/map_builder/config_LBphys_run4.conf b/krcal/map_builder/config_LBphys_run4.conf index 6b13710c..960580a0 100644 --- a/krcal/map_builder/config_LBphys_run4.conf +++ b/krcal/map_builder/config_LBphys_run4.conf @@ -2,7 +2,7 @@ folder = '{folderin}' file_in = '{filein}' file_bootstrap_map = '$ICDIR/database/test_data/kr_emap_xy_100_100_r_6573_time.h5' file_out_map = '{fileoutmap}' -file_out_hists = '{fileouthist}' +monitoring_path = '{folderouthists}' # Low Background Configuration File: ref_Z_histogram = dict( diff --git a/krcal/map_builder/config_NoChecks.conf b/krcal/map_builder/config_NoChecks.conf index 169cd9ac..27382dcc 100644 --- a/krcal/map_builder/config_NoChecks.conf +++ b/krcal/map_builder/config_NoChecks.conf @@ -2,7 +2,7 @@ folder = '{folderin}' file_in = '{filein}' file_bootstrap_map = '$ICDIR/database/test_data/kr_emap_xy_100_100_r_6573_time.h5' file_out_map = '{fileoutmap}' -file_out_hists = '{fileouthist}' +monitoring_path = '{folderouthists}' # High Energy Configuration File: ref_Z_histogram = dict( From f8c7355e224e9e629ad9ee9a478f2175b2aee2be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alberto=20Us=C3=B3n=20Andr=C3=A9s?= Date: Thu, 12 Mar 2020 09:13:31 +0100 Subject: [PATCH 08/15] Change test functions to fit new functionality --- .../map_builder/map_builder_functions_test.py | 92 +++++++++---------- 1 file changed, 46 insertions(+), 46 deletions(-) diff --git a/krcal/map_builder/map_builder_functions_test.py b/krcal/map_builder/map_builder_functions_test.py index 75a261a2..6c3b1e38 100644 --- a/krcal/map_builder/map_builder_functions_test.py +++ b/krcal/map_builder/map_builder_functions_test.py @@ -44,10 +44,10 @@ def test_scrip_runs_and_produces_correct_outputs(folder_test_dst , """ Run map creation script and check if an ASectormap is the output. """ - map_file_out = os.path.join(output_maps_tmdir, 'test_out_map.h5') - histo_file_out = os.path.join(output_maps_tmdir, 'test_out_histo.h5') - default_n_bins = 15 - run_number = 7517 + map_file_out = os.path.join(output_maps_tmdir, 'test_out_map.h5') + monitoring_path = None + default_n_bins = 15 + run_number = 7517 config = configure('maps $ICARO/krcal/map_builder/config_LBphys.conf'.split()) map_params_new = copy.copy(config.as_namespace.map_params) map_params_new['nmin'] = 100 @@ -149,7 +149,7 @@ def test_correct_map_with_unsorted_dst(folder_test_dst , """ map_file_sort = os.path.join(output_maps_tmdir, 'test_out_map.h5') map_file_unsort = os.path.join(output_maps_tmdir, 'test_out_unsort.h5') - histo_file_out = os.path.join(output_maps_tmdir, 'test_out_histo.h5') + monitoring_path = None dst = load_dst(folder_test_dst+test_dst_file, 'DST', 'Events') if "index" in dst:del dst["index"] @@ -190,18 +190,18 @@ def test_exception_s1(folder_test_dst, test_dst_file, output_maps_tmdir): This test checks if exception raises when ns1=1 efficiency is out of range. """ conf = configure('maps $ICARO/krcal/map_builder/config_LBphys.conf'.split()) - map_file_out = os.path.join(output_maps_tmdir, 'test_out_map_s1.h5' ) - histo_file_out = os.path.join(output_maps_tmdir, 'test_out_histo_s1.h5') + map_file_out = os.path.join(output_maps_tmdir, 'test_out_map_s1.h5' ) + monitoring_path = None min_eff_test = 0. max_eff_test = 0.8 run_number = 7517 - conf.update(dict(folder = folder_test_dst, - file_in = test_dst_file , - file_out_map = map_file_out , - file_out_hists = histo_file_out , - nS1_eff_min = min_eff_test , - nS1_eff_max = max_eff_test , - run_number = run_number )) + conf.update(dict(folder = folder_test_dst, + file_in = test_dst_file , + file_out_map = map_file_out , + monitoring_path = monitoring_path, + nS1_eff_min = min_eff_test , + nS1_eff_max = max_eff_test , + run_number = run_number )) assert_raises(AbortingMapCreation, map_builder , @@ -212,18 +212,18 @@ def test_exception_s2(folder_test_dst, test_dst_file, output_maps_tmdir): This test checks if exception raises when nS2=1 efficiency is out of range. """ conf = configure('maps $ICARO/krcal/map_builder/config_LBphys.conf'.split()) - map_file_out = os.path.join(output_maps_tmdir, 'test_out_map_s2.h5' ) - histo_file_out = os.path.join(output_maps_tmdir, 'test_out_histo_s2.h5') + map_file_out = os.path.join(output_maps_tmdir, 'test_out_map_s2.h5' ) + monitoring_path = None min_eff_test = 0. max_eff_test = 0.9 run_number = 7517 - conf.update(dict(folder = folder_test_dst, - file_in = test_dst_file , - file_out_map = map_file_out , - file_out_hists = histo_file_out , - nS2_eff_min = min_eff_test , - nS2_eff_max = max_eff_test , - run_number = run_number )) + conf.update(dict(folder = folder_test_dst , + file_in = test_dst_file , + file_out_map = map_file_out , + monitoring_path = monitoring_path , + nS2_eff_min = min_eff_test , + nS2_eff_max = max_eff_test , + run_number = run_number )) assert_raises(AbortingMapCreation, map_builder , @@ -234,16 +234,16 @@ def test_exception_rate(folder_test_dst, test_dst_file, output_maps_tmdir): This test checks if exception raises when rate distribution is not flat enough. """ conf = configure('maps $ICARO/krcal/map_builder/config_LBphys.conf'.split()) - map_file_out = os.path.join(output_maps_tmdir, 'test_out_map_rate.h5' ) - histo_file_out = os.path.join(output_maps_tmdir, 'test_out_histo_rate.h5') + map_file_out = os.path.join(output_maps_tmdir, 'test_out_map_rate.h5' ) + monitoring_path = None n_dev_rate = 0.5 run_number = 7517 - conf.update(dict(folder = folder_test_dst, - file_in = test_dst_file , - file_out_map = map_file_out , - file_out_hists = histo_file_out , - n_dev_rate = n_dev_rate , - run_number = run_number )) + conf.update(dict(folder = folder_test_dst, + file_in = test_dst_file , + file_out_map = map_file_out , + monitoring_path = monitoring_path, + n_dev_rate = n_dev_rate , + run_number = run_number )) assert_raises(AbortingMapCreation, map_builder , @@ -255,16 +255,16 @@ def test_exception_Zdst(folder_test_dst, test_dst_file, output_maps_tmdir): similar enough to the reference one. """ conf = configure('maps $ICARO/krcal/map_builder/config_LBphys.conf'.split()) - map_file_out = os.path.join(output_maps_tmdir, 'test_out_map_Z.h5' ) - histo_file_out = os.path.join(output_maps_tmdir, 'test_out_histo_Z.h5') + map_file_out = os.path.join(output_maps_tmdir, 'test_out_map_Z.h5' ) + monitoring_path = None nsigmas_Zdst = 0.5 run_number = 7517 - conf.update(dict(folder = folder_test_dst, - file_in = test_dst_file , - file_out_map = map_file_out , - file_out_hists = histo_file_out , - nsigmas_Zdst = nsigmas_Zdst , - run_number = run_number )) + conf.update(dict(folder = folder_test_dst, + file_in = test_dst_file , + file_out_map = map_file_out , + monitoring_path = monitoring_path, + nsigmas_Zdst = nsigmas_Zdst , + run_number = run_number )) assert_raises(AbortingMapCreation, map_builder , @@ -276,18 +276,18 @@ def test_exception_bandsel(folder_test_dst, test_dst_file, output_maps_tmdir): out of a given range. """ conf = configure('maps $ICARO/krcal/map_builder/config_LBphys.conf'.split()) - map_file_out = os.path.join(output_maps_tmdir, 'test_out_map_bandsel.h5' ) - histo_file_out = os.path.join(output_maps_tmdir, 'test_out_histo_bandsel.h5') + map_file_out = os.path.join(output_maps_tmdir, 'test_out_map_bandsel.h5' ) + monitoring_path = None band_sel_params_new = copy.copy(conf.as_namespace.band_sel_params) band_sel_params_new['eff_min'] = 0. band_sel_params_new['eff_max'] = 0.89 run_number = 7517 - conf.update(dict(folder = folder_test_dst, - file_in = test_dst_file , - file_out_map = map_file_out , - file_out_hists = histo_file_out , + conf.update(dict(folder = folder_test_dst , + file_in = test_dst_file , + file_out_map = map_file_out , + monitoring_path = monitoring_path , band_sel_params = band_sel_params_new, - run_number = run_number )) + run_number = run_number )) assert_raises(AbortingMapCreation, map_builder , conf.as_namespace ) From 2b33dcc3a2229a6922d199dd41e92d5c0e7c8a8e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alberto=20Us=C3=B3n=20Andr=C3=A9s?= Date: Thu, 12 Mar 2020 10:18:07 +0100 Subject: [PATCH 09/15] Add test to check that plots have been saved successfully --- .../map_builder/map_builder_functions_test.py | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/krcal/map_builder/map_builder_functions_test.py b/krcal/map_builder/map_builder_functions_test.py index 6c3b1e38..9e1e4494 100644 --- a/krcal/map_builder/map_builder_functions_test.py +++ b/krcal/map_builder/map_builder_functions_test.py @@ -291,3 +291,27 @@ def test_exception_bandsel(folder_test_dst, test_dst_file, output_maps_tmdir): assert_raises(AbortingMapCreation, map_builder , conf.as_namespace ) + + +def test_monitoring_plots_are_created(folder_test_dst, test_dst_file, output_maps_tmdir): + """ + This test checks that monitoring plots are created if a + folder to store them is provided. + """ + conf = configure('maps $ICARO/krcal/map_builder/config_LBphys.conf'.split()) + map_file_out = os.path.join(output_maps_tmdir, 'test_out_map.h5' ) + monitoring_path = output_maps_tmdir + default_n_bins = 15 + run_number = 7517 + conf.update(dict(folder = folder_test_dst, + file_in = test_dst_file , + file_out_map = map_file_out , + monitoring_path = monitoring_path, + default_n_bins = default_n_bins , + run_number = run_number )) + emaps = read_maps(map_file_out) + pars = emaps.t_evol.columns[1:-7:2].tolist() + pars = pars + emaps.t_evol.columns[-3:].tolist() + for par in pars: + plot_fname = output_maps_tmdir + 'Run{0}_{1}_plot.pdf'.format(run_number, par) + assert os.path.exists(output_maps_tmdir) From cd7c27a3aff970a0a6abfc56dc903a7d8ce58bdc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alberto=20Us=C3=B3n=20Andr=C3=A9s?= Date: Thu, 12 Mar 2020 12:31:24 +0100 Subject: [PATCH 10/15] Cosmetics --- krcal/core/io_functions.py | 32 +++++++++++-------- krcal/core/plot_utils.py | 2 +- krcal/map_builder/config_HEcal.conf | 2 +- .../map_builder/config_HEcal_HighKrRate.conf | 2 +- krcal/map_builder/config_LBphys.conf | 2 +- krcal/map_builder/config_LBphys_run4.conf | 2 +- krcal/map_builder/config_NoChecks.conf | 2 +- krcal/map_builder/map_builder_functions.py | 14 ++++---- .../map_builder/map_builder_functions_test.py | 20 ++++++------ 9 files changed, 41 insertions(+), 37 deletions(-) diff --git a/krcal/core/io_functions.py b/krcal/core/io_functions.py index cf7d37a6..089c911c 100644 --- a/krcal/core/io_functions.py +++ b/krcal/core/io_functions.py @@ -84,15 +84,18 @@ def compute_and_save_hist_as_pdf(values : np.array , norm: bool If True, histogram will be normalized. """ - fig = plt.figure(); - plt.hist(values, bins = n_bins, - range = range_hist, density = norm, - histtype='step', linewidth=2); - plt.ylabel('Entries'); - plt.xlabel(x_label); - plt.title(title); - plt.ylim(y_range); - plt.grid(True, alpha=0.5, color='k', linestyle=':'); + fig = plt.figure() + plt.hist(values , + bins = n_bins , + range = range_hist, + density = norm , + histtype = 'step' , + linewidth = 2 ); + plt.ylabel('Entries') + plt.xlabel(x_label) + plt.title(title) + plt.ylim(y_range) + plt.grid(True, alpha=0.5, color='k', linestyle=':') fig.savefig(out_file.format(title).replace(" ", ""), bbox_inches='tight') return @@ -127,21 +130,22 @@ def plot_and_save_evolution_figure(time : np.array , fig = plt.figure() plt.errorbar(time, param, param_u, fmt='.') plt.xlabel('Date') - plt.ylabel('{0} ({1})'.format(param_name, units)); + plt.ylabel('{0} ({1})'.format(param_name, units)) plt.title('{0}'.format(param_name)) mean = np.mean(param) std = np.std(param) plt.ylim(param.min()-std*n_sigmas_lim, param.max()+std*n_sigmas_lim) plt.grid(True, alpha=0.5, color='k', linestyle=':') - ax = plt.gca() + ax = plt.gca() xfmt = md.DateFormatter('%d-%m %H:%M') ax.xaxis.set_major_formatter(xfmt) plt.xticks( rotation=25 ) - plt.text(0.03,0.9, + plt.text(0.03, 0.9, 'Mean= {0} \n Std= {1}'.format(np.round(mean,4), np.round(std,4)), - fontsize=10, transform=ax.transAxes, - bbox={'facecolor': 'white', 'alpha': 1, 'pad': 5}) + fontsize = 10, + transform = ax.transAxes, + bbox = {'facecolor': 'white', 'alpha': 1, 'pad': 5}) fig.savefig(file_name.format(param_name), bbox_inches='tight') return diff --git a/krcal/core/plot_utils.py b/krcal/core/plot_utils.py index 473cb325..6d7454ff 100644 --- a/krcal/core/plot_utils.py +++ b/krcal/core/plot_utils.py @@ -19,7 +19,7 @@ def par_selection_to_plot_vs_time(evol_table: pd.DataFrame, ---------- Nothing """ - time_ = list(map(datetime.datetime.fromtimestamp, evol_table.ts)) + time_ = list(map(datetime.datetime.fromtimestamp, evol_table.ts)) units_vect = ['pes', 'mus', 'mm/mus', '%', 'ns', 'pes', 'pes', 'mus', 'pes', 'pes', 'pes', '# SiPM'] diff --git a/krcal/map_builder/config_HEcal.conf b/krcal/map_builder/config_HEcal.conf index 34bf0b9a..a8c0c36a 100644 --- a/krcal/map_builder/config_HEcal.conf +++ b/krcal/map_builder/config_HEcal.conf @@ -2,7 +2,7 @@ folder = '{folderin}' file_in = '{filein}' file_bootstrap_map = '$ICDIR/database/test_data/kr_emap_xy_100_100_r_6573_time.h5' file_out_map = '{fileoutmap}' -monitoring_path = '{folderouthists}' +monitoring_path = '{folderoutmonitorplots}' # High Energy Configuration File: ref_Z_histogram = dict( diff --git a/krcal/map_builder/config_HEcal_HighKrRate.conf b/krcal/map_builder/config_HEcal_HighKrRate.conf index 385948bc..84a21950 100644 --- a/krcal/map_builder/config_HEcal_HighKrRate.conf +++ b/krcal/map_builder/config_HEcal_HighKrRate.conf @@ -2,7 +2,7 @@ folder = '{folderin}' file_in = '{filein}' file_bootstrap_map = '$ICDIR/database/test_data/kr_emap_xy_100_100_r_6573_time.h5' file_out_map = '{fileoutmap}' -monitoring_path = '{folderouthists}' +monitoring_path = '{folderoutmonitorplots}' # High Energy Configuration File: ref_Z_histogram = dict( diff --git a/krcal/map_builder/config_LBphys.conf b/krcal/map_builder/config_LBphys.conf index e78caefa..64da92a9 100644 --- a/krcal/map_builder/config_LBphys.conf +++ b/krcal/map_builder/config_LBphys.conf @@ -2,7 +2,7 @@ folder = '{folderin}' file_in = '{filein}' file_bootstrap_map = '$ICDIR/database/test_data/kr_emap_xy_100_100_r_6573_time.h5' file_out_map = '{fileoutmap}' -monitoring_path = '{folderouthists}' +monitoring_path = '{folderoutmonitorplots}' # Low Background Configuration File: ref_Z_histogram = dict( diff --git a/krcal/map_builder/config_LBphys_run4.conf b/krcal/map_builder/config_LBphys_run4.conf index 960580a0..0c373fc2 100644 --- a/krcal/map_builder/config_LBphys_run4.conf +++ b/krcal/map_builder/config_LBphys_run4.conf @@ -2,7 +2,7 @@ folder = '{folderin}' file_in = '{filein}' file_bootstrap_map = '$ICDIR/database/test_data/kr_emap_xy_100_100_r_6573_time.h5' file_out_map = '{fileoutmap}' -monitoring_path = '{folderouthists}' +monitoring_path = '{folderoutmonitorplots}' # Low Background Configuration File: ref_Z_histogram = dict( diff --git a/krcal/map_builder/config_NoChecks.conf b/krcal/map_builder/config_NoChecks.conf index 27382dcc..47c15c18 100644 --- a/krcal/map_builder/config_NoChecks.conf +++ b/krcal/map_builder/config_NoChecks.conf @@ -2,7 +2,7 @@ folder = '{folderin}' file_in = '{filein}' file_bootstrap_map = '$ICDIR/database/test_data/kr_emap_xy_100_100_r_6573_time.h5' file_out_map = '{fileoutmap}' -monitoring_path = '{folderouthists}' +monitoring_path = '{folderoutmonitorplots}' # High Energy Configuration File: ref_Z_histogram = dict( diff --git a/krcal/map_builder/map_builder_functions.py b/krcal/map_builder/map_builder_functions.py index 39303867..77252003 100644 --- a/krcal/map_builder/map_builder_functions.py +++ b/krcal/map_builder/map_builder_functions.py @@ -167,7 +167,7 @@ def selection_nS_mask_and_checking(dst : pd.DataFrame , mod_dst = dst[['event', column.value]].drop_duplicates() if monitoring: - compute_and_save_hist_as_pdf(values = getattr(mod_dst, + compute_and_save_hist_as_pdf(values = getattr(mod_dst , column.value), out_file = monitoring, n_bins = nbins_hist, @@ -259,15 +259,15 @@ def check_rate_and_hist(times : np.array , max_time) if monitoring: - compute_and_save_hist_as_pdf(values = times, + compute_and_save_hist_as_pdf(values = times , out_file = monitoring, - n_bins = ntimebins, - range_hist = (min_time , + n_bins = ntimebins , + range_hist = (min_time, max_time), title = hist_title, x_label = 'Time (s)', - y_range = (0,6000), - norm = normed) + y_range = (0, 6000) , + norm = normed ) n, _ = np.histogram(times, bins=ntimebins, range = (min_time, max_time)) @@ -749,6 +749,6 @@ def map_builder(config): print("Map successfully computed and saved in : {0}".format(config.file_out_map)) if monitoring: par_selection_to_plot_vs_time(evol_table = final_map.t_evol, - file_name = monitoring ) + file_name = monitoring ) print("Control histograms saved in : {0}".format(monitoring. replace(monitoring.split('/', -1)[-1], ''))) diff --git a/krcal/map_builder/map_builder_functions_test.py b/krcal/map_builder/map_builder_functions_test.py index 9e1e4494..e60fd3c3 100644 --- a/krcal/map_builder/map_builder_functions_test.py +++ b/krcal/map_builder/map_builder_functions_test.py @@ -192,9 +192,9 @@ def test_exception_s1(folder_test_dst, test_dst_file, output_maps_tmdir): conf = configure('maps $ICARO/krcal/map_builder/config_LBphys.conf'.split()) map_file_out = os.path.join(output_maps_tmdir, 'test_out_map_s1.h5' ) monitoring_path = None - min_eff_test = 0. - max_eff_test = 0.8 - run_number = 7517 + min_eff_test = 0. + max_eff_test = 0.8 + run_number = 7517 conf.update(dict(folder = folder_test_dst, file_in = test_dst_file , file_out_map = map_file_out , @@ -214,9 +214,9 @@ def test_exception_s2(folder_test_dst, test_dst_file, output_maps_tmdir): conf = configure('maps $ICARO/krcal/map_builder/config_LBphys.conf'.split()) map_file_out = os.path.join(output_maps_tmdir, 'test_out_map_s2.h5' ) monitoring_path = None - min_eff_test = 0. - max_eff_test = 0.9 - run_number = 7517 + min_eff_test = 0. + max_eff_test = 0.9 + run_number = 7517 conf.update(dict(folder = folder_test_dst , file_in = test_dst_file , file_out_map = map_file_out , @@ -236,8 +236,8 @@ def test_exception_rate(folder_test_dst, test_dst_file, output_maps_tmdir): conf = configure('maps $ICARO/krcal/map_builder/config_LBphys.conf'.split()) map_file_out = os.path.join(output_maps_tmdir, 'test_out_map_rate.h5' ) monitoring_path = None - n_dev_rate = 0.5 - run_number = 7517 + n_dev_rate = 0.5 + run_number = 7517 conf.update(dict(folder = folder_test_dst, file_in = test_dst_file , file_out_map = map_file_out , @@ -257,8 +257,8 @@ def test_exception_Zdst(folder_test_dst, test_dst_file, output_maps_tmdir): conf = configure('maps $ICARO/krcal/map_builder/config_LBphys.conf'.split()) map_file_out = os.path.join(output_maps_tmdir, 'test_out_map_Z.h5' ) monitoring_path = None - nsigmas_Zdst = 0.5 - run_number = 7517 + nsigmas_Zdst = 0.5 + run_number = 7517 conf.update(dict(folder = folder_test_dst, file_in = test_dst_file , file_out_map = map_file_out , From 3869b6ce1caee61fee0d306f7ffc9854b0d893d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alberto=20Us=C3=B3n=20Andr=C3=A9s?= Date: Thu, 12 Mar 2020 12:43:10 +0100 Subject: [PATCH 11/15] Change message when saving monitoring plots --- krcal/map_builder/map_builder_functions.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/krcal/map_builder/map_builder_functions.py b/krcal/map_builder/map_builder_functions.py index 77252003..0e5df271 100644 --- a/krcal/map_builder/map_builder_functions.py +++ b/krcal/map_builder/map_builder_functions.py @@ -750,5 +750,4 @@ def map_builder(config): if monitoring: par_selection_to_plot_vs_time(evol_table = final_map.t_evol, file_name = monitoring ) - print("Control histograms saved in : {0}".format(monitoring. - replace(monitoring.split('/', -1)[-1], ''))) + print("Control plots saved in folder : {0}".format(config.monitoring_path)) From 83095182a6d64e12a42d19d9cb58f33b291caa5e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alberto=20Us=C3=B3n=20Andr=C3=A9s?= Date: Thu, 12 Mar 2020 12:50:49 +0100 Subject: [PATCH 12/15] Correct Docstrings --- krcal/core/io_functions.py | 5 +++++ krcal/map_builder/map_builder_functions.py | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/krcal/core/io_functions.py b/krcal/core/io_functions.py index 089c911c..6000b5c0 100644 --- a/krcal/core/io_functions.py +++ b/krcal/core/io_functions.py @@ -122,6 +122,11 @@ def plot_and_save_evolution_figure(time : np.array , Array with uncertainty values. units : string Units to be shown in x-label. + file_name : string + Name of file for plots. It must contain '{0}' to be name after + the plotted parameter. + n_sigmas_lim : int (optional) + Number of sigmas to set the Y-axis limits in plots. Returns ---------- diff --git a/krcal/map_builder/map_builder_functions.py b/krcal/map_builder/map_builder_functions.py index 0e5df271..83e0cb01 100644 --- a/krcal/map_builder/map_builder_functions.py +++ b/krcal/map_builder/map_builder_functions.py @@ -235,7 +235,7 @@ def check_rate_and_hist(times : np.array , ---------- times: np.array Time of the events. - hist_title: sting + hist_title: string Name for the histogram title. monitoring: str (optional) If specified, histogram plots will be save with given name. From 933dfd77ec424e8ed446738f504ed514abfde228 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alberto=20Us=C3=B3n=20Andr=C3=A9s?= Date: Thu, 12 Mar 2020 14:01:34 +0100 Subject: [PATCH 13/15] Change type hints from np.array to np.ndarray --- krcal/core/io_functions.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/krcal/core/io_functions.py b/krcal/core/io_functions.py index 6000b5c0..7dfa9c05 100644 --- a/krcal/core/io_functions.py +++ b/krcal/core/io_functions.py @@ -55,7 +55,7 @@ def compute_and_save_hist_as_pd(values : np.array , -def compute_and_save_hist_as_pdf(values : np.array , +def compute_and_save_hist_as_pdf(values : np.ndarray , out_file : str , n_bins : int , range_hist : Tuple[float, float], @@ -67,7 +67,7 @@ def compute_and_save_hist_as_pdf(values : np.array , Computes 1d-histogram and saves it as a pdf image. Parameters ---------- - values : np.array + values : np.ndarray Array with values to be plotted. out_file: string File where histogram will be saved. @@ -101,10 +101,10 @@ def compute_and_save_hist_as_pdf(values : np.array , return -def plot_and_save_evolution_figure(time : np.array , +def plot_and_save_evolution_figure(time : np.ndarray , param_name : str , - param : np.array , - param_u : np.array , + param : np.ndarray , + param_u : np.ndarray , units : str , file_name : str , n_sigmas_lim: int = 1): @@ -112,13 +112,13 @@ def plot_and_save_evolution_figure(time : np.array , Plot and save evolution of a given parameter vs time. Parameters ---------- - time : np.array + time : np.ndarray Array with the centers of each time interval (X-values). param_name : string Name of the parameter to plot. - param : np.array + param : np.ndarray Array with the magnitude to plot (Y-values). - param_u : np.array + param_u : np.ndarray Array with uncertainty values. units : string Units to be shown in x-label. From e99d3bc0d33aa1b4ef493e6e9fe159018ac93990 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alberto=20Us=C3=B3n=20Andr=C3=A9s?= Date: Thu, 12 Mar 2020 14:51:09 +0100 Subject: [PATCH 14/15] Change syntax to use f-string instead of format along map creation --- krcal/map_builder/checking_functions.py | 6 ++-- krcal/map_builder/map_builder_functions.py | 40 +++++++++++----------- 2 files changed, 23 insertions(+), 23 deletions(-) diff --git a/krcal/map_builder/checking_functions.py b/krcal/map_builder/checking_functions.py index 8b46e4ab..f6080029 100644 --- a/krcal/map_builder/checking_functions.py +++ b/krcal/map_builder/checking_functions.py @@ -52,9 +52,9 @@ def check_failed_fits(maps : ASectorMap, map_values_in_core = maps.lt.values[inner_core_mask] numFailed = np.count_nonzero(np.isnan(map_values_in_core)) if numFailed > maxFailed: - message = "Number of failed fits ({0}) ".format(numFailed) - message += "exceeds max. allowed ({0}).".format(maxFailed) + message = f"Number of failed fits ({numFailed}) " + message += f"exceeds max. allowed ({maxFailed})." raise AbortingMapCreation(message) else: - print(" Number of failing fits: {0}".format(numFailed)) + print(f" Number of failing fits: {numFailed}") return diff --git a/krcal/map_builder/map_builder_functions.py b/krcal/map_builder/map_builder_functions.py index 83e0cb01..a08485ed 100644 --- a/krcal/map_builder/map_builder_functions.py +++ b/krcal/map_builder/map_builder_functions.py @@ -179,8 +179,8 @@ def selection_nS_mask_and_checking(dst : pd.DataFrame , message = "Selection efficiency of " message += column.value - message += "==1 ({0}) out of range ".format(np.round(eff, 3)) - message += "({0} - {1}).".format(interval[0], interval[1]) + message += f"==1 ({eff:.3f}) out of range " + message += f"({interval[0]} - {interval[1]})." check_if_values_in_interval(values = np.array(eff), low_lim = interval[0] , up_lim = interval[1] , @@ -215,7 +215,7 @@ def check_Z_dst(Z_vect : np.array , diff_sig = diff / np.sqrt(err_N**2+ref_hist.err_bin_entries**2) message = "Z distribution very different to reference one. " - message += "At least 1 point out of {0} sigmas region. ".format(n_sigmas) + message += f"At least 1 point out of {n_sigmas} sigmas region. " check_if_values_in_interval(values = diff_sig , low_lim = -n_sigmas, up_lim = n_sigmas , @@ -275,8 +275,8 @@ def check_rate_and_hist(times : np.array , dev = np.std(n, ddof = 1) rel_dev = dev / mean * 100 - message = "Relative deviation ({0}) greater ".format(rel_dev) - message += "than the allowed one ({0}).".format(n_dev) + message = f"Relative deviation ({rel_dev}) greater " + message += f"than the allowed one ({n_dev})." check_if_values_in_interval(values = np.array(rel_dev), low_lim = 0 , up_lim = n_dev , @@ -348,8 +348,8 @@ def band_selector_and_check(dst : pd.DataFrame, nsigma = nsigma_sel) effsel = dst[sel_krband].event.nunique()/dst[input_mask].event.nunique() - message = "Band selection efficiency {0} ".format(np.round(effsel, 3)) - message += "out of range: ({0} - {1}).".format(eff_min, eff_max) + message = f"Band selection efficiency {effsel:.3f} " + message += f"out of range: ({eff_min} - {eff_max})." check_if_values_in_interval(values = np.array(effsel), low_lim = eff_min , up_lim = eff_max , @@ -563,8 +563,8 @@ def add_krevol(maps : ASectorMap, e0par = np.array([pars['e0'].mean(), pars['e0'].var()**0.5]) ltpar = np.array([pars['lt'].mean(), pars['lt'].var()**0.5]) - print(" Mean core E0: {0:.1f}+-{1:.1f} pes".format(*e0par)) - print(" Mean core Lt: {0:.1f}+-{1:.1f} mus".format(*ltpar)) + print(f" Mean core E0: {e0par[0]:.1f}+-{e0par[1]:.1f} pes") + print(f" Mean core Lt: {ltpar[0]:.1f}+-{ltpar[1]:.1f} mus") maps.t_evol = pars_ec @@ -643,7 +643,7 @@ def apply_cuts(dst : pd.DataFrame , monitoring = monitoring , **ns1_histo_params ) nS1 = dst[mask1].event.nunique() - print(" 1 S1 cut efficiency within the expectations ({0:2.2f}%)".format(nS1/n0*100)) + print(f" 1 S1 cut efficiency within the expectations ({nS1/n0*100:2.2f}%)") mask2 = selection_nS_mask_and_checking(dst = dst , column = S2_signal , interval = nS2_eff_interval, @@ -651,7 +651,7 @@ def apply_cuts(dst : pd.DataFrame , input_mask = mask1 , **ns2_histo_params ) nS2 = dst[mask2].event.nunique() - print(" 1 S2 cut efficiency within the expectations ({0:2.2f}%)".format(nS2/nS1*100)) + print(f" 1 S2 cut efficiency within the expectations ({nS2/nS1*100:2.2f}%)") check_Z_dst(Z_vect = dst[mask2].Z, ref_hist = ref_Z_histo , n_sigmas = nsigmas_Zdst) @@ -661,7 +661,7 @@ def apply_cuts(dst : pd.DataFrame , input_mask = mask2 , **band_sel_params ) nZb = dst[mask3].event.nunique() - print(" Z band cut efficiency within the expectations ({0:2.2f}%)".format(nZb/nS2*100)) + print(f" Z band cut efficiency within the expectations ({nZb/nS2*100:2.2f}%)") masks = masks_container(s1 = mask1, s2 = mask2, @@ -672,9 +672,9 @@ def map_builder(config): print("Map builder starting...") print("Reading input files:") - print(" Input dst folder : {}".format(config.folder)) - print(" Input boostrap map : {}".format(config.file_bootstrap_map)) - print(" Input histogram map: {}".format(config.ref_Z_histogram['ref_histo_file'])) + print(f" Input dst folder : {config.folder}") + print(f" Input boostrap map : {config.file_bootstrap_map}") + print(f" Input histogram map: {config.ref_Z_histogram['ref_histo_file']}") dst, bootstrapmap, ref_histos = load_data(input_path = config.folder , @@ -685,7 +685,7 @@ def map_builder(config): monitoring = config.monitoring_path if monitoring: - monitoring = monitoring + 'Run{0}_{{0}}_plot.pdf'.format(config.run_number) + monitoring = monitoring + f'Run{config.run_number}_{{0}}_plot.pdf' print("Checking the dst and appling 1S1, 1S2 and z-band selections:") @@ -721,7 +721,7 @@ def map_builder(config): nev_after = dst_passed_cut.event.nunique() ratio = nev_after/nev_before*100 - print(" Number of events passing the cuts: {0} ({1:2.2f}%)".format(nev_after, ratio)) + print(f" Number of events passing the cuts: {nev_after} ({ratio:2.2f}%)") print("Map computation:") @@ -729,7 +729,7 @@ def map_builder(config): thr_events_for_map_bins = config.thr_evts_for_sel_map_bins, n_bins = config.default_n_bins ) - print(" Number of bins: {0}x{0}".format(number_of_bins)) + print(f" Number of bins: {number_of_bins}x{number_of_bins}") final_map = compute_map(dst = dst_passed_cut , run_number = config.run_number, @@ -746,8 +746,8 @@ def map_builder(config): write_complete_maps(asm = final_map , filename = config.file_out_map) - print("Map successfully computed and saved in : {0}".format(config.file_out_map)) + print(f"Map successfully computed and saved in : {config.file_out_map}") if monitoring: par_selection_to_plot_vs_time(evol_table = final_map.t_evol, file_name = monitoring ) - print("Control plots saved in folder : {0}".format(config.monitoring_path)) + print(f"Control plots saved in folder : {config.monitoring_path}") From cd2e97f34d4424b44b5461f76b3642789592b21e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alberto=20Us=C3=B3n=20Andr=C3=A9s?= Date: Thu, 12 Mar 2020 17:21:58 +0100 Subject: [PATCH 15/15] Add comment in config to show how to run without monitoring plots --- krcal/map_builder/config_HEcal.conf | 2 +- krcal/map_builder/config_HEcal_HighKrRate.conf | 2 +- krcal/map_builder/config_LBphys.conf | 2 +- krcal/map_builder/config_LBphys_run4.conf | 2 +- krcal/map_builder/config_NoChecks.conf | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/krcal/map_builder/config_HEcal.conf b/krcal/map_builder/config_HEcal.conf index a8c0c36a..d9d45d7b 100644 --- a/krcal/map_builder/config_HEcal.conf +++ b/krcal/map_builder/config_HEcal.conf @@ -2,7 +2,7 @@ folder = '{folderin}' file_in = '{filein}' file_bootstrap_map = '$ICDIR/database/test_data/kr_emap_xy_100_100_r_6573_time.h5' file_out_map = '{fileoutmap}' -monitoring_path = '{folderoutmonitorplots}' +monitoring_path = '{folderoutmonitorplots}' #if monitoring_path = None, no monitoring plots produced # High Energy Configuration File: ref_Z_histogram = dict( diff --git a/krcal/map_builder/config_HEcal_HighKrRate.conf b/krcal/map_builder/config_HEcal_HighKrRate.conf index 84a21950..0b7158f2 100644 --- a/krcal/map_builder/config_HEcal_HighKrRate.conf +++ b/krcal/map_builder/config_HEcal_HighKrRate.conf @@ -2,7 +2,7 @@ folder = '{folderin}' file_in = '{filein}' file_bootstrap_map = '$ICDIR/database/test_data/kr_emap_xy_100_100_r_6573_time.h5' file_out_map = '{fileoutmap}' -monitoring_path = '{folderoutmonitorplots}' +monitoring_path = '{folderoutmonitorplots}' #if monitoring_path = None, no monitoring plots produced # High Energy Configuration File: ref_Z_histogram = dict( diff --git a/krcal/map_builder/config_LBphys.conf b/krcal/map_builder/config_LBphys.conf index 64da92a9..f817dc0a 100644 --- a/krcal/map_builder/config_LBphys.conf +++ b/krcal/map_builder/config_LBphys.conf @@ -2,7 +2,7 @@ folder = '{folderin}' file_in = '{filein}' file_bootstrap_map = '$ICDIR/database/test_data/kr_emap_xy_100_100_r_6573_time.h5' file_out_map = '{fileoutmap}' -monitoring_path = '{folderoutmonitorplots}' +monitoring_path = '{folderoutmonitorplots}' #if monitoring_path = None, no monitoring plots produced # Low Background Configuration File: ref_Z_histogram = dict( diff --git a/krcal/map_builder/config_LBphys_run4.conf b/krcal/map_builder/config_LBphys_run4.conf index 0c373fc2..a5b52d98 100644 --- a/krcal/map_builder/config_LBphys_run4.conf +++ b/krcal/map_builder/config_LBphys_run4.conf @@ -2,7 +2,7 @@ folder = '{folderin}' file_in = '{filein}' file_bootstrap_map = '$ICDIR/database/test_data/kr_emap_xy_100_100_r_6573_time.h5' file_out_map = '{fileoutmap}' -monitoring_path = '{folderoutmonitorplots}' +monitoring_path = '{folderoutmonitorplots}' #if monitoring_path = None, no monitoring plots produced # Low Background Configuration File: ref_Z_histogram = dict( diff --git a/krcal/map_builder/config_NoChecks.conf b/krcal/map_builder/config_NoChecks.conf index 47c15c18..7d345822 100644 --- a/krcal/map_builder/config_NoChecks.conf +++ b/krcal/map_builder/config_NoChecks.conf @@ -2,7 +2,7 @@ folder = '{folderin}' file_in = '{filein}' file_bootstrap_map = '$ICDIR/database/test_data/kr_emap_xy_100_100_r_6573_time.h5' file_out_map = '{fileoutmap}' -monitoring_path = '{folderoutmonitorplots}' +monitoring_path = '{folderoutmonitorplots}' #if monitoring_path = None, no monitoring plots produced # High Energy Configuration File: ref_Z_histogram = dict(