Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 104 additions & 0 deletions krcal/core/io_functions.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
42 changes: 42 additions & 0 deletions krcal/core/plot_utils.py
Original file line number Diff line number Diff line change
@@ -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
6 changes: 3 additions & 3 deletions krcal/map_builder/checking_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion krcal/map_builder/config_HEcal.conf
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion krcal/map_builder/config_HEcal_HighKrRate.conf
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion krcal/map_builder/config_LBphys.conf
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion krcal/map_builder/config_LBphys_run4.conf
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion krcal/map_builder/config_NoChecks.conf
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Loading