diff --git a/krcal/core/io_functions.py b/krcal/core/io_functions.py index c3d2ca38..7dfa9c05 100644 --- a/krcal/core/io_functions.py +++ b/krcal/core/io_functions.py @@ -1,5 +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 @@ -50,3 +52,105 @@ 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.ndarray , + 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.ndarray + 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 + + +def plot_and_save_evolution_figure(time : np.ndarray , + param_name : str , + param : np.ndarray , + param_u : np.ndarray , + units : str , + file_name : str , + n_sigmas_lim: int = 1): + """ + Plot and save evolution of a given parameter vs time. + Parameters + ---------- + time : np.ndarray + Array with the centers of each time interval (X-values). + param_name : string + Name of the parameter to plot. + param : np.ndarray + Array with the magnitude to plot (Y-values). + param_u : np.ndarray + 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 + ---------- + 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 diff --git a/krcal/core/plot_utils.py b/krcal/core/plot_utils.py new file mode 100644 index 00000000..6d7454ff --- /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 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/config_HEcal.conf b/krcal/map_builder/config_HEcal.conf index b67cca60..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}' -file_out_hists = '{fileouthist}' +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 24d46f88..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}' -file_out_hists = '{fileouthist}' +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 776ee2f6..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}' -file_out_hists = '{fileouthist}' +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 6b13710c..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}' -file_out_hists = '{fileouthist}' +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 169cd9ac..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}' -file_out_hists = '{fileouthist}' +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/map_builder_functions.py b/krcal/map_builder/map_builder_functions.py index 2c43771e..a08485ed 100644 --- a/krcal/map_builder/map_builder_functions.py +++ b/krcal/map_builder/map_builder_functions.py @@ -25,10 +25,11 @@ 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 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 @@ -120,7 +121,7 @@ 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 , range_hist : Tuple[float, float] = (0,10), @@ -139,8 +140,8 @@ 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) Selection mask of the previous cut. If this is the first selection cut, input_mask is set to be an all True array. @@ -164,18 +165,22 @@ 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 , + 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)) - 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] , @@ -210,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 , @@ -218,8 +223,8 @@ 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 , bin_size : int = 180 , normed : bool = False)->None: @@ -230,10 +235,10 @@ 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: string + 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. @@ -253,13 +258,16 @@ 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, + 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)) @@ -267,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 , @@ -340,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 , @@ -555,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 @@ -618,33 +626,32 @@ 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 , 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)) + 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, - output_f = store_hist_s2 , + monitoring = monitoring , 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) @@ -654,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, @@ -665,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 , @@ -676,43 +683,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 + f'Run{config.run_number}_{{0}}_plot.pdf' + + 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(f" Number of events passing the cuts: {nev_after} ({ratio:2.2f}%)") print("Map computation:") @@ -720,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, @@ -737,5 +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("Control histograms saved in : {0}".format(config.file_out_hists)) + 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(f"Control plots saved in folder : {config.monitoring_path}") diff --git a/krcal/map_builder/map_builder_functions_test.py b/krcal/map_builder/map_builder_functions_test.py index 75a261a2..e60fd3c3 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') - 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 )) + 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 , + 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') - 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 )) + 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 , + 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') - 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 )) + 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 , + 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') - 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 )) + 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 , + monitoring_path = monitoring_path, + nsigmas_Zdst = nsigmas_Zdst , + run_number = run_number )) assert_raises(AbortingMapCreation, map_builder , @@ -276,18 +276,42 @@ 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 ) + + +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)