From 5279f423d25171ff3660786bde6d2e7bfc31d434 Mon Sep 17 00:00:00 2001 From: siranipour Date: Thu, 2 Apr 2020 15:18:01 +0100 Subject: [PATCH 1/9] Adding action to print chi2 dist for aggregate of datasets --- validphys2/src/validphys/dataplots.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/validphys2/src/validphys/dataplots.py b/validphys2/src/validphys/dataplots.py index f5479d00b6..76bcd08979 100644 --- a/validphys2/src/validphys/dataplots.py +++ b/validphys2/src/validphys/dataplots.py @@ -28,6 +28,22 @@ log = logging.getLogger(__name__) +@figure +def plot_chi2dist_experiments(total_experiments_chi2data, pdf): + fig, ax = plt.subplots() + label = pdf.name + alldata, central, npoints = total_experiments_chi2data + if not isinstance(alldata, MCStats): + ax.set_facecolor("#ffcccc") + log.warning("Chi² distribution plots have a " + "different meaning for non MC sets.") + label += " (%s!)" % pdf.ErrorType + + ax.hist(alldata.data.squeeze(), label=label, zorder=100) + l = ax.legend() + l.set_zorder(1000) + return fig + @figure def plot_chi2dist(results, dataset, abs_chi2_data, chi2_stats, pdf): """Plot the distribution of chi²s of the members of the pdfset.""" From ca7c9d5f87f7e5c2ef433c3b14ca2bc2aed77357 Mon Sep 17 00:00:00 2001 From: siranipour Date: Thu, 2 Apr 2020 16:07:36 +0100 Subject: [PATCH 2/9] Correcting legend for histogram plot --- validphys2/src/validphys/dataplots.py | 5 ++++- validphys2/src/validphys/results.py | 27 ++++++++++++++++++++++++++- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/validphys2/src/validphys/dataplots.py b/validphys2/src/validphys/dataplots.py index 76bcd08979..ebe11abe06 100644 --- a/validphys2/src/validphys/dataplots.py +++ b/validphys2/src/validphys/dataplots.py @@ -29,7 +29,8 @@ log = logging.getLogger(__name__) @figure -def plot_chi2dist_experiments(total_experiments_chi2data, pdf): +def plot_chi2dist_experiments(total_experiments_chi2data, experiments_chi2_stats, pdf): + """Plot the distribution of chi²s of the members of the pdfset.""" fig, ax = plt.subplots() label = pdf.name alldata, central, npoints = total_experiments_chi2data @@ -39,6 +40,8 @@ def plot_chi2dist_experiments(total_experiments_chi2data, pdf): "different meaning for non MC sets.") label += " (%s!)" % pdf.ErrorType + label += '\n'+ '\n'.join(str(chi2_stat_labels[k])+(' %.2f' % v) for (k,v) in experiments_chi2_stats.items()) + ax.hist(alldata.data.squeeze(), label=label, zorder=100) l = ax.legend() l.set_zorder(1000) diff --git a/validphys2/src/validphys/results.py b/validphys2/src/validphys/results.py index 3a9c3b3fb0..8e3d346ea5 100644 --- a/validphys2/src/validphys/results.py +++ b/validphys2/src/validphys/results.py @@ -819,8 +819,33 @@ def count_negative_points(possets_predictions): 'chi2_per_data': r'$\frac{\chi^2}{N_{data}}$' } +def experiments_chi2_stats(total_experiments_chi2data): + """Compute several estimators from the chi² for an + aggregate of experiments: + + - central_mean + + - npoints + + - perreplica_mean + + - perreplica_std + + - chi2_per_data + """ + rep_data, central_result, npoints = total_experiments_chi2data + m = central_result.mean() + rep_mean = rep_data.central_value().mean() + return OrderedDict([ + ('central_mean' , m), + ('npoints' , npoints), + ('chi2_per_data', m/npoints), + ('perreplica_mean', rep_mean), + ('perreplica_std', rep_data.std_error().mean()), + ]) + def chi2_stats(abs_chi2_data): - """Compute severa estimators from the chi²: + """Compute several estimators from the chi²: - central_mean From 90be0e868ff381a204abe8947f3d4e06c5af6028 Mon Sep 17 00:00:00 2001 From: siranipour Date: Mon, 13 Apr 2020 14:50:43 +0100 Subject: [PATCH 3/9] Removing results provider from plot_chi2dist (unused) --- validphys2/src/validphys/dataplots.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/validphys2/src/validphys/dataplots.py b/validphys2/src/validphys/dataplots.py index ebe11abe06..f820e56649 100644 --- a/validphys2/src/validphys/dataplots.py +++ b/validphys2/src/validphys/dataplots.py @@ -48,7 +48,7 @@ def plot_chi2dist_experiments(total_experiments_chi2data, experiments_chi2_stats return fig @figure -def plot_chi2dist(results, dataset, abs_chi2_data, chi2_stats, pdf): +def plot_chi2dist(dataset, abs_chi2_data, chi2_stats, pdf): """Plot the distribution of chi²s of the members of the pdfset.""" setlabel = dataset.name fig, ax = plt.subplots() From f0b49a4f9ca3135ca2b9d68d40e1e672d014bba7 Mon Sep 17 00:00:00 2001 From: siranipour Date: Mon, 13 Apr 2020 15:57:12 +0100 Subject: [PATCH 4/9] Correcting shape of numpy broadcasting --- validphys2/src/validphys/dataplots.py | 2 +- validphys2/src/validphys/results.py | 5 ++--- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/validphys2/src/validphys/dataplots.py b/validphys2/src/validphys/dataplots.py index f820e56649..e2043da754 100644 --- a/validphys2/src/validphys/dataplots.py +++ b/validphys2/src/validphys/dataplots.py @@ -42,7 +42,7 @@ def plot_chi2dist_experiments(total_experiments_chi2data, experiments_chi2_stats label += '\n'+ '\n'.join(str(chi2_stat_labels[k])+(' %.2f' % v) for (k,v) in experiments_chi2_stats.items()) - ax.hist(alldata.data.squeeze(), label=label, zorder=100) + ax.hist(alldata.data, label=label, zorder=100) l = ax.legend() l.set_zorder(1000) return fig diff --git a/validphys2/src/validphys/results.py b/validphys2/src/validphys/results.py index 8e3d346ea5..a49dc1a6fa 100644 --- a/validphys2/src/validphys/results.py +++ b/validphys2/src/validphys/results.py @@ -1096,11 +1096,10 @@ def total_experiments_chi2data(pdf: PDF, experiments_chi2): ndata = 0 central_chi2 = 0 nmembers = len(experiments_chi2[0].replica_result.error_members()) # ugh - member_chi2 = np.zeros(nmembers) + member_chi2 = np.zeros((nmembers, 1)) for cd in experiments_chi2: - # not sure why the transpose or [0] are needed here - member_chi2 += cd.replica_result.error_members().T[0] + member_chi2 += cd.replica_result.error_members() central_chi2 += cd.central_result ndata += cd.ndata return Chi2Data(pdf.stats_class(member_chi2), central_chi2, ndata) From 1f22441b42c0b892ea34c9c94cc989b6d312e312 Mon Sep 17 00:00:00 2001 From: siranipour Date: Mon, 13 Apr 2020 15:57:52 +0100 Subject: [PATCH 5/9] Adding title to plot --- validphys2/src/validphys/dataplots.py | 1 + 1 file changed, 1 insertion(+) diff --git a/validphys2/src/validphys/dataplots.py b/validphys2/src/validphys/dataplots.py index e2043da754..d51e7dd6b9 100644 --- a/validphys2/src/validphys/dataplots.py +++ b/validphys2/src/validphys/dataplots.py @@ -41,6 +41,7 @@ def plot_chi2dist_experiments(total_experiments_chi2data, experiments_chi2_stats label += " (%s!)" % pdf.ErrorType label += '\n'+ '\n'.join(str(chi2_stat_labels[k])+(' %.2f' % v) for (k,v) in experiments_chi2_stats.items()) + ax.set_title(r"Experiments $\chi^2$ distribution") ax.hist(alldata.data, label=label, zorder=100) l = ax.legend() From 8800deb8271801553d2fc54e67f753478866bf59 Mon Sep 17 00:00:00 2001 From: siranipour Date: Mon, 13 Apr 2020 16:40:47 +0100 Subject: [PATCH 6/9] Adding kde plots for chi2 distributions --- validphys2/src/validphys/dataplots.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/validphys2/src/validphys/dataplots.py b/validphys2/src/validphys/dataplots.py index d51e7dd6b9..380e31c7e5 100644 --- a/validphys2/src/validphys/dataplots.py +++ b/validphys2/src/validphys/dataplots.py @@ -48,6 +48,14 @@ def plot_chi2dist_experiments(total_experiments_chi2data, experiments_chi2_stats l.set_zorder(1000) return fig +@figure +def kde_chi2dist_experiments(total_experiments_chi2data): + alldata, central, npoints = total_experiments_chi2data + fig, ax = plt.subplots() + # We need the squeeze here to change shape from (x, 1) to (x,) + ax = plotutils.kde_plot(alldata.data.squeeze()) + return fig + @figure def plot_chi2dist(dataset, abs_chi2_data, chi2_stats, pdf): """Plot the distribution of chi²s of the members of the pdfset.""" From 462425328c99c96647c572dd4b16ea58d7bb538b Mon Sep 17 00:00:00 2001 From: siranipour Date: Tue, 14 Apr 2020 11:28:49 +0100 Subject: [PATCH 7/9] Adding title, axis labels, and legend to KDE plot --- validphys2/src/validphys/dataplots.py | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/validphys2/src/validphys/dataplots.py b/validphys2/src/validphys/dataplots.py index 380e31c7e5..2b17214b1a 100644 --- a/validphys2/src/validphys/dataplots.py +++ b/validphys2/src/validphys/dataplots.py @@ -49,11 +49,27 @@ def plot_chi2dist_experiments(total_experiments_chi2data, experiments_chi2_stats return fig @figure -def kde_chi2dist_experiments(total_experiments_chi2data): - alldata, central, npoints = total_experiments_chi2data +def kde_chi2dist_experiments(total_experiments_chi2data, experiments_chi2_stats, pdf): + """KDE plot for experiments chi2.""" fig, ax = plt.subplots() + label = pdf.name + alldata, central, npoints = total_experiments_chi2data + + if not isinstance(alldata, MCStats): + ax.set_facecolor("#ffcccc") + log.warning("Chi² distribution plots have a " + "different meaning for non MC sets.") + label += " (%s!)" % pdf.ErrorType + + label += '\n'+ '\n'.join(str(chi2_stat_labels[k])+(' %.2f' % v) for (k,v) in experiments_chi2_stats.items()) + ax.set_title(r"Experiments $\chi^2$ KDE plot") + ax.set_xlabel(r"Replica $\chi^2$") + ax.set_ylabel(r"Density") + # We need the squeeze here to change shape from (x, 1) to (x,) - ax = plotutils.kde_plot(alldata.data.squeeze()) + ax = plotutils.kde_plot(alldata.data.squeeze(), label=label) + l = ax.legend() + l.set_zorder(1000) return fig @figure From 3a4b2ab80a3a3546cb82da2a362e6f93344dc8b3 Mon Sep 17 00:00:00 2001 From: siranipour Date: Tue, 14 Apr 2020 11:29:03 +0100 Subject: [PATCH 8/9] Adding x axis labels to histogram plots --- validphys2/src/validphys/dataplots.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/validphys2/src/validphys/dataplots.py b/validphys2/src/validphys/dataplots.py index 2b17214b1a..e7c74c8821 100644 --- a/validphys2/src/validphys/dataplots.py +++ b/validphys2/src/validphys/dataplots.py @@ -42,6 +42,7 @@ def plot_chi2dist_experiments(total_experiments_chi2data, experiments_chi2_stats label += '\n'+ '\n'.join(str(chi2_stat_labels[k])+(' %.2f' % v) for (k,v) in experiments_chi2_stats.items()) ax.set_title(r"Experiments $\chi^2$ distribution") + ax.set_xlabel(r"Replica $\chi^2$") ax.hist(alldata.data, label=label, zorder=100) l = ax.legend() @@ -86,6 +87,7 @@ def plot_chi2dist(dataset, abs_chi2_data, chi2_stats, pdf): label += " (%s!)" % pdf.ErrorType label += '\n'+ '\n'.join(str(chi2_stat_labels[k])+(' %.2f' % v) for (k,v) in chi2_stats.items()) ax.set_title(r"$\chi^2$ distribution for %s" % setlabel) + ax.set_xlabel(r"Replica $\chi^2$") ax.hist(alldata.data, label=label, zorder=100) l = ax.legend() From 3c2435be176cb5273655493e621720ade659ab43 Mon Sep 17 00:00:00 2001 From: siranipour Date: Tue, 14 Apr 2020 13:00:40 +0100 Subject: [PATCH 9/9] Refactoring such that we get rid of boilerplate --- validphys2/src/validphys/dataplots.py | 61 ++++++++++----------------- 1 file changed, 23 insertions(+), 38 deletions(-) diff --git a/validphys2/src/validphys/dataplots.py b/validphys2/src/validphys/dataplots.py index e7c74c8821..fc9cf0822c 100644 --- a/validphys2/src/validphys/dataplots.py +++ b/validphys2/src/validphys/dataplots.py @@ -31,68 +31,53 @@ @figure def plot_chi2dist_experiments(total_experiments_chi2data, experiments_chi2_stats, pdf): """Plot the distribution of chi²s of the members of the pdfset.""" - fig, ax = plt.subplots() - label = pdf.name - alldata, central, npoints = total_experiments_chi2data - if not isinstance(alldata, MCStats): - ax.set_facecolor("#ffcccc") - log.warning("Chi² distribution plots have a " - "different meaning for non MC sets.") - label += " (%s!)" % pdf.ErrorType - - label += '\n'+ '\n'.join(str(chi2_stat_labels[k])+(' %.2f' % v) for (k,v) in experiments_chi2_stats.items()) + fig, ax = _chi2_distribution_plots(total_experiments_chi2data, experiments_chi2_stats, pdf, "hist") ax.set_title(r"Experiments $\chi^2$ distribution") - ax.set_xlabel(r"Replica $\chi^2$") - - ax.hist(alldata.data, label=label, zorder=100) - l = ax.legend() - l.set_zorder(1000) return fig + @figure def kde_chi2dist_experiments(total_experiments_chi2data, experiments_chi2_stats, pdf): """KDE plot for experiments chi2.""" - fig, ax = plt.subplots() - label = pdf.name - alldata, central, npoints = total_experiments_chi2data - - if not isinstance(alldata, MCStats): - ax.set_facecolor("#ffcccc") - log.warning("Chi² distribution plots have a " - "different meaning for non MC sets.") - label += " (%s!)" % pdf.ErrorType - - label += '\n'+ '\n'.join(str(chi2_stat_labels[k])+(' %.2f' % v) for (k,v) in experiments_chi2_stats.items()) - ax.set_title(r"Experiments $\chi^2$ KDE plot") - ax.set_xlabel(r"Replica $\chi^2$") + fig, ax = _chi2_distribution_plots(total_experiments_chi2data, experiments_chi2_stats, pdf, "kde") ax.set_ylabel(r"Density") - - # We need the squeeze here to change shape from (x, 1) to (x,) - ax = plotutils.kde_plot(alldata.data.squeeze(), label=label) - l = ax.legend() - l.set_zorder(1000) + ax.set_title(r"Experiments $\chi^2 KDE plot$") return fig + @figure def plot_chi2dist(dataset, abs_chi2_data, chi2_stats, pdf): """Plot the distribution of chi²s of the members of the pdfset.""" setlabel = dataset.name + fig, ax = _chi2_distribution_plots(abs_chi2_data, chi2_stats, pdf, "hist") + ax.set_title(r"$\chi^2$ distribution for %s" % setlabel) + return fig + + +def _chi2_distribution_plots(chi2_data, stats, pdf, plot_type): fig, ax = plt.subplots() label = pdf.name - alldata, central, npoints = abs_chi2_data + alldata, central, npoints = chi2_data if not isinstance(alldata, MCStats): ax.set_facecolor("#ffcccc") log.warning("Chi² distribution plots have a " "different meaning for non MC sets.") label += " (%s!)" % pdf.ErrorType - label += '\n'+ '\n'.join(str(chi2_stat_labels[k])+(' %.2f' % v) for (k,v) in chi2_stats.items()) - ax.set_title(r"$\chi^2$ distribution for %s" % setlabel) + label += '\n'+ '\n'.join(str(chi2_stat_labels[k])+(' %.2f' % v) for (k,v) in stats.items()) ax.set_xlabel(r"Replica $\chi^2$") - ax.hist(alldata.data, label=label, zorder=100) + if plot_type == "hist": + ax.hist(alldata.data, label=label, zorder=100) + elif plot_type == "kde": + # We need the squeeze here to change shape from (x, 1) to (x,) + ax = plotutils.kde_plot(alldata.data.squeeze(), label=label) + else: + raise ValueError(f"plot_type must either be hist or kde, not {plot_type}") + l = ax.legend() l.set_zorder(1000) - return fig + return fig, ax + @figure def plot_phi(experiments, experiments_phi):