From 2d593d959931f3ca275d7929ca93fff16e446fa7 Mon Sep 17 00:00:00 2001 From: David Anthony Date: Fri, 9 Jul 2021 11:59:20 +0100 Subject: [PATCH 1/4] poissonian error for 0 yield --- fast_plotter/plotting.py | 11 ++++++----- fast_plotter/utils.py | 6 +++++- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/fast_plotter/plotting.py b/fast_plotter/plotting.py index 5594caa..e37e7dd 100644 --- a/fast_plotter/plotting.py +++ b/fast_plotter/plotting.py @@ -374,10 +374,11 @@ def plot_1d_many(df, prefix="", data="data", signal=None, dataset_col="dataset", (in_df_data, plot_data, kind_data, data_legend, "plot_data"), (in_df_signal, plot_signal, kind_signal, "Signal", "plot_signal"), ] + kwargs.setdefault("is_null_poissonian", False) for df, combine, style, label, var_name in config: if df is None or len(df) == 0: continue - merged = _merge_datasets(df, combine, dataset_col, param_name=var_name, err_from_sumw2=err_from_sumw2) + merged = _merge_datasets(df, combine, dataset_col, param_name=var_name, err_from_sumw2=err_from_sumw2, is_null_poissonian=kwargs['is_null_poissonian']) actually_plot(merged, x_axis=x_axis, y=y, yerr=yerr, kind=style, label=label, ax=main_ax, dataset_col=dataset_col, dataset_colours=dataset_colours, @@ -392,9 +393,9 @@ def plot_1d_many(df, prefix="", data="data", signal=None, dataset_col="dataset", if summary.startswith("ratio"): main_ax.set_xlabel("") summed_data = _merge_datasets( - in_df_data, "sum", dataset_col=dataset_col, err_from_sumw2=err_from_sumw2) + in_df_data, "sum", dataset_col=dataset_col, err_from_sumw2=err_from_sumw2, is_null_poissonian=kwargs['is_null_poissonian']) summed_sims = _merge_datasets( - in_df_sims, "sum", dataset_col=dataset_col, err_from_sumw2=err_from_sumw2) + in_df_sims, "sum", dataset_col=dataset_col, err_from_sumw2=err_from_sumw2, is_null_poissonian=kwargs['is_null_poissonian']) if summary == "ratio-error-both": error = "both" elif summary == "ratio-error-markers": @@ -411,7 +412,7 @@ def plot_1d_many(df, prefix="", data="data", signal=None, dataset_col="dataset", return main_ax, summary_ax -def _merge_datasets(df, style, dataset_col, param_name="_merge_datasets", err_from_sumw2=False): +def _merge_datasets(df, style, dataset_col, param_name="_merge_datasets", err_from_sumw2=False, is_null_poissonian=False): if style == "stack": df = utils.stack_datasets(df, dataset_level=dataset_col) elif style == "sum": @@ -419,7 +420,7 @@ def _merge_datasets(df, style, dataset_col, param_name="_merge_datasets", err_fr elif style: msg = "'{}' must be either 'sum', 'stack' or None. Got {}" raise RuntimeError(msg.format(param_name, style)) - utils.calculate_error(df, do_rel_err=not err_from_sumw2) + utils.calculate_error(df, do_rel_err=not err_from_sumw2, is_null_poissonian=is_null_poissonian) return df diff --git a/fast_plotter/utils.py b/fast_plotter/utils.py index b765452..d845d96 100644 --- a/fast_plotter/utils.py +++ b/fast_plotter/utils.py @@ -91,7 +91,7 @@ def split_data_sims(df, data_labels=["data"], dataset_level="dataset"): return split_df(df, first_values=data_labels, level=dataset_level) -def calculate_error(df, sumw2_label="sumw2", err_label="err", inplace=True, do_rel_err=True): +def calculate_error(df, sumw2_label="sumw2", err_label="err", inplace=True, do_rel_err=True, is_null_poissonian=False): if not inplace: df = df.copy() if do_rel_err: @@ -105,6 +105,10 @@ def calculate_error(df, sumw2_label="sumw2", err_label="err", inplace=True, do_r elif not do_rel_err and sumw2_label in column: err_name = column.replace(sumw2_label, err_label) df[err_name] = np.sqrt(df[column]) + if is_null_poissonian: + print(err_name) + print(df.loc[df[err_name]<=0]) + df[err_name] = df[err_name].apply(lambda x: x if x > 0 else 1.15) if not inplace: return df From 18064d190c233ca0502c498b064c59083b3d39d7 Mon Sep 17 00:00:00 2001 From: David Anthony Date: Fri, 9 Jul 2021 12:23:10 +0100 Subject: [PATCH 2/4] Minimum error is 1.15 for n >=0 --- fast_plotter/utils.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/fast_plotter/utils.py b/fast_plotter/utils.py index d845d96..ee3c53b 100644 --- a/fast_plotter/utils.py +++ b/fast_plotter/utils.py @@ -102,13 +102,12 @@ def calculate_error(df, sumw2_label="sumw2", err_label="err", inplace=True, do_r errs = np.true_divide(df[column], root_n) errs.loc[~np.isfinite(errs)] = np.nan df[err_name] = errs - elif not do_rel_err and sumw2_label in column: + else: + #elif not do_rel_err and sumw2_label in column: err_name = column.replace(sumw2_label, err_label) df[err_name] = np.sqrt(df[column]) - if is_null_poissonian: - print(err_name) - print(df.loc[df[err_name]<=0]) - df[err_name] = df[err_name].apply(lambda x: x if x > 0 else 1.15) + if is_null_poissonian: + df[err_name] = df[err_name].apply(lambda x: x if x > 1.15 else np.sqrt(1.15**2+x**2)) if not inplace: return df From 25d87bb481aff6400b78845d0ac81f0c04b1f04a Mon Sep 17 00:00:00 2001 From: David Anthony Date: Fri, 9 Jul 2021 12:51:10 +0100 Subject: [PATCH 3/4] pep8 --- fast_plotter/plotting.py | 12 ++++++++---- fast_plotter/utils.py | 6 ++++-- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/fast_plotter/plotting.py b/fast_plotter/plotting.py index e37e7dd..15a0ef4 100644 --- a/fast_plotter/plotting.py +++ b/fast_plotter/plotting.py @@ -378,7 +378,8 @@ def plot_1d_many(df, prefix="", data="data", signal=None, dataset_col="dataset", for df, combine, style, label, var_name in config: if df is None or len(df) == 0: continue - merged = _merge_datasets(df, combine, dataset_col, param_name=var_name, err_from_sumw2=err_from_sumw2, is_null_poissonian=kwargs['is_null_poissonian']) + merged = _merge_datasets(df, combine, dataset_col, param_name=var_name, err_from_sumw2=err_from_sumw2, + is_null_poissonian=kwargs['is_null_poissonian']) actually_plot(merged, x_axis=x_axis, y=y, yerr=yerr, kind=style, label=label, ax=main_ax, dataset_col=dataset_col, dataset_colours=dataset_colours, @@ -393,9 +394,11 @@ def plot_1d_many(df, prefix="", data="data", signal=None, dataset_col="dataset", if summary.startswith("ratio"): main_ax.set_xlabel("") summed_data = _merge_datasets( - in_df_data, "sum", dataset_col=dataset_col, err_from_sumw2=err_from_sumw2, is_null_poissonian=kwargs['is_null_poissonian']) + in_df_data, "sum", dataset_col=dataset_col, err_from_sumw2=err_from_sumw2, + is_null_poissonian=kwargs['is_null_poissonian']) summed_sims = _merge_datasets( - in_df_sims, "sum", dataset_col=dataset_col, err_from_sumw2=err_from_sumw2, is_null_poissonian=kwargs['is_null_poissonian']) + in_df_sims, "sum", dataset_col=dataset_col, err_from_sumw2=err_from_sumw2, + is_null_poissonian=kwargs['is_null_poissonian']) if summary == "ratio-error-both": error = "both" elif summary == "ratio-error-markers": @@ -412,7 +415,8 @@ def plot_1d_many(df, prefix="", data="data", signal=None, dataset_col="dataset", return main_ax, summary_ax -def _merge_datasets(df, style, dataset_col, param_name="_merge_datasets", err_from_sumw2=False, is_null_poissonian=False): +def _merge_datasets(df, style, dataset_col, param_name="_merge_datasets", err_from_sumw2=False, + is_null_poissonian=False): if style == "stack": df = utils.stack_datasets(df, dataset_level=dataset_col) elif style == "sum": diff --git a/fast_plotter/utils.py b/fast_plotter/utils.py index ee3c53b..3b30e2a 100644 --- a/fast_plotter/utils.py +++ b/fast_plotter/utils.py @@ -102,10 +102,12 @@ def calculate_error(df, sumw2_label="sumw2", err_label="err", inplace=True, do_r errs = np.true_divide(df[column], root_n) errs.loc[~np.isfinite(errs)] = np.nan df[err_name] = errs - else: - #elif not do_rel_err and sumw2_label in column: + elif not do_rel_err and sumw2_label in column: err_name = column.replace(sumw2_label, err_label) df[err_name] = np.sqrt(df[column]) + else: + err_name = "" + continue if is_null_poissonian: df[err_name] = df[err_name].apply(lambda x: x if x > 1.15 else np.sqrt(1.15**2+x**2)) if not inplace: From 4e1dc4ad17ca73ce49939e5a7e17ddfd640b0c1f Mon Sep 17 00:00:00 2001 From: David Anthony Date: Fri, 9 Jul 2021 12:53:55 +0100 Subject: [PATCH 4/4] clean up --- fast_plotter/utils.py | 1 - 1 file changed, 1 deletion(-) diff --git a/fast_plotter/utils.py b/fast_plotter/utils.py index 3b30e2a..1cfbf57 100644 --- a/fast_plotter/utils.py +++ b/fast_plotter/utils.py @@ -106,7 +106,6 @@ def calculate_error(df, sumw2_label="sumw2", err_label="err", inplace=True, do_r err_name = column.replace(sumw2_label, err_label) df[err_name] = np.sqrt(df[column]) else: - err_name = "" continue if is_null_poissonian: df[err_name] = df[err_name].apply(lambda x: x if x > 1.15 else np.sqrt(1.15**2+x**2))