From d44b381686e243a806a126c97580a7831b6254f7 Mon Sep 17 00:00:00 2001 From: pabloitu_home Date: Thu, 22 Jul 2021 19:00:13 +0200 Subject: [PATCH 1/3] Modified comparison plot, to plot simultaneously T and W tests --- csep/utils/plots.py | 51 ++++++++++++++++++++++++++++++++++++--------- 1 file changed, 41 insertions(+), 10 deletions(-) diff --git a/csep/utils/plots.py b/csep/utils/plots.py index 1f8a9d7c..8980e5cc 100644 --- a/csep/utils/plots.py +++ b/csep/utils/plots.py @@ -1232,29 +1232,60 @@ def _get_marker_style(obs_stat, p, one_sided_lower): fmt = 'gs' return fmt -def plot_comparison_test(results, plot_args=None): - """Plots list of T-Test or W-Test Results""" +def plot_comparison_test(results_t, results_w=None, plot_args=None): + """Plots list of T-Test (and W-Test) Results""" + if plot_args is None: plot_args = {} - title = plot_args.get('title', 'CSEP1 Consistency Test') + title = plot_args.get('title', 'CSEP1 Comparison Test') xlabel = plot_args.get('xlabel', 'X') ylabel = plot_args.get('ylabel', 'Y') + ylims = plot_args.get('ylims', (None, None)) + capsize = plot_args.get('capsize', 2) + linewidth = plot_args.get('linewidth', 1) + markersize = plot_args.get('markersize', 2) fig, ax = pyplot.subplots() + ax.axhline(y=0, linestyle='--', color='black') - for index, result in enumerate(results): - ylow = result.observed_statistic - result.test_distribution[0] - yhigh = result.test_distribution[1] - result.observed_statistic - ax.errorbar(index, result.observed_statistic, yerr=numpy.array([[ylow, yhigh]]).T, color='black', capsize=4) - ax.plot(index, result.observed_statistic, 'ok') - ax.set_xticklabels([res.sim_name[0] for res in results]) - ax.set_xticks(numpy.arange(len(results))) + + Results = zip(results_t, results_w) if results_w else zip(results_t) + for index, result in enumerate(Results): + result_t = result[0] + result_w = result[1] if results_w else None + + ylow = result_t.observed_statistic - result_t.test_distribution[0] + yhigh = result_t.test_distribution[1] - result_t.observed_statistic + color = _get_marker_t_color( result_t.test_distribution) + ax.errorbar(index, result_t.observed_statistic, + yerr=numpy.array([[ylow, yhigh]]).T , + color = color, + linewidth=linewidth, capsize=capsize) + + if _get_marker_w_color( result_w.quantile): + facecolor = _get_marker_t_color(result_t.test_distribution) + else: + facecolor = 'white' + ax.plot(index, result_t.observed_statistic, marker='o', markerfacecolor=facecolor, markeredgecolor=color, markersize=markersize) + + ax.set_xticklabels([res.sim_name[0] for res in results_t], rotation=90) + ax.set_xticks(numpy.arange(len(results_t))) ax.set_xlabel(xlabel) ax.set_ylabel(ylabel) ax.set_title(title) + ax.yaxis.grid() + xTickPos, _ = pyplot.xticks() + ax.yaxis.set_major_locator(matplotlib.ticker.MaxNLocator(integer=True)) + ax.set_ylim([ylims[0], ylims[1]]) + ax.set_xlim([ax.get_xlim()[0] + 0.5, ax.get_xlim()[1] - 0.5]) + ax.bar(xTickPos, numpy.array([9999] * len(xTickPos)), bottom=-2000, + width=(xTickPos[1] - xTickPos[0]), color=['gray', 'w'], alpha=0.2) + + fig.tight_layout() return ax + def plot_poisson_consistency_test(eval_results, normalize=False, one_sided_lower=False, plot_args=None): """ Plots results from CSEP1 tests following the CSEP1 convention. From 26e6d3a26a03a27e9701d6b2d6f22a8983de8bba Mon Sep 17 00:00:00 2001 From: pabloitu_home Date: Thu, 22 Jul 2021 19:20:25 +0200 Subject: [PATCH 2/3] Made sure, a comparison plot is possible if no results_w are provided as arg --- csep/utils/plots.py | 34 +++++++++++++++++++++++++++++----- 1 file changed, 29 insertions(+), 5 deletions(-) diff --git a/csep/utils/plots.py b/csep/utils/plots.py index 8980e5cc..1d7e37d8 100644 --- a/csep/utils/plots.py +++ b/csep/utils/plots.py @@ -1232,6 +1232,27 @@ def _get_marker_style(obs_stat, p, one_sided_lower): fmt = 'gs' return fmt +def _get_marker_t_color(distribution): + """Returns matplotlib marker style as fmt string""" + if distribution[0] > 0. and distribution[1] > 0.: + fmt = 'green' + elif distribution[0] < 0. and distribution[1] < 0.: + fmt = 'red' + else: + fmt = 'grey' + + return fmt + +def _get_marker_w_color(distribution): + """Returns matplotlib marker style as fmt string""" + + if distribution < 0.05: + fmt = True + else: + fmt = False + + return fmt + def plot_comparison_test(results_t, results_w=None, plot_args=None): """Plots list of T-Test (and W-Test) Results""" @@ -1246,10 +1267,10 @@ def plot_comparison_test(results_t, results_w=None, plot_args=None): markersize = plot_args.get('markersize', 2) fig, ax = pyplot.subplots() - ax.axhline(y=0, linestyle='--', color='black') Results = zip(results_t, results_w) if results_w else zip(results_t) + for index, result in enumerate(Results): result_t = result[0] result_w = result[1] if results_w else None @@ -1262,10 +1283,14 @@ def plot_comparison_test(results_t, results_w=None, plot_args=None): color = color, linewidth=linewidth, capsize=capsize) - if _get_marker_w_color( result_w.quantile): - facecolor = _get_marker_t_color(result_t.test_distribution) + if result_w is not None: + if _get_marker_w_color(result_w.quantile): + facecolor = _get_marker_t_color(result_t.test_distribution) + else: + facecolor = 'white' else: facecolor = 'white' + ax.plot(index, result_t.observed_statistic, marker='o', markerfacecolor=facecolor, markeredgecolor=color, markersize=markersize) ax.set_xticklabels([res.sim_name[0] for res in results_t], rotation=90) @@ -1280,9 +1305,8 @@ def plot_comparison_test(results_t, results_w=None, plot_args=None): ax.set_xlim([ax.get_xlim()[0] + 0.5, ax.get_xlim()[1] - 0.5]) ax.bar(xTickPos, numpy.array([9999] * len(xTickPos)), bottom=-2000, width=(xTickPos[1] - xTickPos[0]), color=['gray', 'w'], alpha=0.2) - - fig.tight_layout() + return ax From b8ce9842dc4fcdb7fe93b8ef6669c78fd03fd423 Mon Sep 17 00:00:00 2001 From: pabloitu_home Date: Thu, 22 Jul 2021 19:42:22 +0200 Subject: [PATCH 3/3] Added option to create the figure from previously defined matplotlib.pyplot.ax() object and percentile for W-test confidence interval --- csep/utils/plots.py | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/csep/utils/plots.py b/csep/utils/plots.py index 1d7e37d8..b09517e7 100644 --- a/csep/utils/plots.py +++ b/csep/utils/plots.py @@ -1243,21 +1243,23 @@ def _get_marker_t_color(distribution): return fmt -def _get_marker_w_color(distribution): +def _get_marker_w_color(distribution, percentile): """Returns matplotlib marker style as fmt string""" - if distribution < 0.05: + if distribution < (1 - percentile/100): fmt = True else: fmt = False return fmt -def plot_comparison_test(results_t, results_w=None, plot_args=None): +def plot_comparison_test(results_t, results_w=None, axes=None, plot_args=None): """Plots list of T-Test (and W-Test) Results""" if plot_args is None: plot_args = {} + + figsize = plot_args.get('figsize', None) title = plot_args.get('title', 'CSEP1 Comparison Test') xlabel = plot_args.get('xlabel', 'X') ylabel = plot_args.get('ylabel', 'Y') @@ -1265,8 +1267,14 @@ def plot_comparison_test(results_t, results_w=None, plot_args=None): capsize = plot_args.get('capsize', 2) linewidth = plot_args.get('linewidth', 1) markersize = plot_args.get('markersize', 2) + percentile = plot_args.get('percentile', 95) + + if axes is None: + fig, ax = pyplot.subplots(figsize=figsize) + else: + ax = axes + fig = ax.get_figure() - fig, ax = pyplot.subplots() ax.axhline(y=0, linestyle='--', color='black') Results = zip(results_t, results_w) if results_w else zip(results_t) @@ -1284,7 +1292,7 @@ def plot_comparison_test(results_t, results_w=None, plot_args=None): linewidth=linewidth, capsize=capsize) if result_w is not None: - if _get_marker_w_color(result_w.quantile): + if _get_marker_w_color(result_w.quantile, percentile): facecolor = _get_marker_t_color(result_t.test_distribution) else: facecolor = 'white'