Skip to content

Commit aa1dc7a

Browse files
committed
Calculate errorbars for disp metrics, fix unit, drop bins with too few events
1 parent 393a621 commit aa1dc7a

File tree

2 files changed

+36
-6
lines changed

2 files changed

+36
-6
lines changed

aict_tools/plotting.py

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
import matplotlib
44
import matplotlib.pyplot as plt
55
from matplotlib.colors import LogNorm
6+
import warnings
67

78
from sklearn import metrics
9+
from sklearn.exceptions import UndefinedMetricWarning
810
from sklearn.calibration import CalibratedClassifierCV
911

1012
from .preprocessing import horizontal_to_camera
@@ -97,6 +99,7 @@ def plot_bias_resolution(
9799
binned['upper_sigma'] = grouped['rel_error'].agg(lambda s: np.percentile(s, 85))
98100
binned['resolution_quantiles'] = (binned.upper_sigma - binned.lower_sigma) / 2
99101
binned['resolution'] = grouped['rel_error'].std()
102+
binned = binned[grouped.count() > 5] # at least five events
100103

101104
for key in ('bias', 'resolution', 'resolution_quantiles'):
102105
if matplotlib.get_backend() == 'pgf' or plt.rcParams['text.usetex']:
@@ -335,20 +338,45 @@ def r2(group):
335338
'e_width': np.diff(edges),
336339
}, index=pd.Series(np.arange(1, len(edges)), name='bin_idx'))
337340

338-
binned['accuracy'] = df.groupby('bin_idx').apply(accuracy)
339-
binned['r2_score'] = df.groupby('bin_idx').apply(r2)
341+
r2_scores = pd.DataFrame(index=binned.index)
342+
accuracies = pd.DataFrame(index=binned.index)
343+
counts = pd.DataFrame(index=binned.index)
344+
345+
with warnings.catch_warnings():
346+
# warns when there are less than 2 events for calculating metrics,
347+
# but we throw those away anyways
348+
warnings.filterwarnings('ignore', category=UndefinedMetricWarning)
349+
for cv_fold, cv in df.groupby('cv_fold'):
350+
grouped = cv.groupby('bin_idx')
351+
accuracies[cv_fold] = grouped.apply(accuracy)
352+
r2_scores[cv_fold] = grouped.apply(r2)
353+
counts[cv_fold] = grouped.size()
354+
355+
binned['r2_score'] = r2_scores.mean(axis=1)
356+
binned['r2_std'] = r2_scores.std(axis=1)
357+
binned['accuracy'] = accuracies.mean(axis=1)
358+
binned['accuracy_std'] = accuracies.std(axis=1)
359+
# at least 10 events in each crossval iteration
360+
binned['valid'] = (counts > 10).any(axis=1)
361+
binned = binned.query('valid')
340362

341363
fig = fig or plt.figure()
342364

343365
ax1 = fig.add_subplot(2, 1, 1)
344366
ax2 = fig.add_subplot(2, 1, 2, sharex=ax1)
345367

346368
ax1.errorbar(
347-
binned.e_center, binned.accuracy, xerr=binned.e_width / 2, ls='',
369+
binned.e_center, binned.accuracy,
370+
yerr=binned.accuracy_std, xerr=binned.e_width / 2,
371+
ls='',
348372
)
349373
ax1.set_ylabel(r'Accuracy for $\mathrm{sgn} \mathtt{disp}$')
350374

351-
ax2.errorbar(binned.e_center, binned.r2_score, xerr=binned.e_width / 2, ls='')
375+
ax2.errorbar(
376+
binned.e_center, binned.r2_score,
377+
yerr=binned.r2_std, xerr=binned.e_width / 2,
378+
ls='',
379+
)
352380
ax2.set_ylabel(r'$r^2$ score for $|\mathtt{disp}|$')
353381

354382
ax2.set_xlabel(

aict_tools/scripts/plot_disp_performance.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def main(configuration_path, performance_path, data_path, disp_model_path, sign_
4343
df = fact.io.read_data(performance_path, key=key)
4444

4545
columns = model_config.columns_to_read_train
46-
46+
4747
if model_config.coordinate_transformation == 'CTA':
4848
camera_unit = r'\mathrm{m}'
4949
else:
@@ -123,7 +123,9 @@ def main(configuration_path, performance_path, data_path, disp_model_path, sign_
123123
plot_true_delta_delta(df_data, model_config, ax)
124124

125125
if config.true_energy_column in df.columns:
126-
fig = plot_energy_dependent_disp_metrics(df, config.true_energy_column)
126+
fig = plot_energy_dependent_disp_metrics(
127+
df, config.true_energy_column, energy_unit=config.energy_unit
128+
)
127129
figures.append(fig)
128130

129131
if output is None:

0 commit comments

Comments
 (0)