From 08d6b32281bbce843bb63f175f4b3ca2a4e9d1bc Mon Sep 17 00:00:00 2001 From: Tom Barbette Date: Mon, 18 Nov 2024 10:17:25 +0100 Subject: [PATCH] Support log-based "graph_show_values" --- npf/cmdline.py | 2 +- npf/output/graph/plots/barplot.py | 7 +++---- npf/output/graph/plots/heatmap.py | 2 +- npf/output/grapher.py | 30 +++++++++++++++++------------- 4 files changed, 22 insertions(+), 19 deletions(-) diff --git a/npf/cmdline.py b/npf/cmdline.py index a4dc5a8..5671515 100644 --- a/npf/cmdline.py +++ b/npf/cmdline.py @@ -121,7 +121,7 @@ def __call__(self, parser, namespace, values, option_string=None): def add_testing_options(parser: ArgumentParser, regression: bool = False): t = parser.add_argument_group('Testing options') tf = t.add_mutually_exclusive_group() - tf.add_argument('--no-test', + t.add_argument('--no-test', help='Do not run any tests, use previous results', dest='do_test', action='store_false', default=True) t.add_argument('--no-supplementary-test', diff --git a/npf/output/graph/plots/barplot.py b/npf/output/graph/plots/barplot.py index 26d606f..b8e7ecf 100644 --- a/npf/output/graph/plots/barplot.py +++ b/npf/output/graph/plots/barplot.py @@ -7,10 +7,10 @@ import numpy as np -def do_barplot(graph, axis, vars_all, dyns, result_type, data, shift, show_vals, horizontal=False, data_types=None): +def do_barplot(graph, axis, vars_all, dyns, result_type, data, shift, show_values=False, horizontal=False, data_types=None): nseries = len(data) - graph.format_figure(axis,result_type,shift) + isLog = graph.format_figure(axis,result_type,shift) # If more than 20 bars, do not print bar edges maxlen = max([len(serie_data[0]) for serie_data in data]) @@ -103,8 +103,7 @@ def nohatch_args(build): **common_args(build), **(hatch_args(build) if do_hatch else nohatch_args(build)) ) - if show_vals: - graph.write_labels(rects, plt, build._color) + graph.write_labels(show_values, rects, plt, build._color, isLog=isLog) ss = combine_variables_late(graph, vars_all, dyns) diff --git a/npf/output/graph/plots/heatmap.py b/npf/output/graph/plots/heatmap.py index 8922f10..b952998 100644 --- a/npf/output/graph/plots/heatmap.py +++ b/npf/output/graph/plots/heatmap.py @@ -7,7 +7,7 @@ def do_heatmap(graph, axis, key, result_type, data : XYEB, xdata : XYEB, vars_values: dict, shift=0, idx=0, sparse=False,show_values=False): - graph.format_figure(axis, result_type, shift, key=key) + isLog = graph.format_figure(axis, result_type, shift, key=key) nseries = 0 yvals = [] for x,y,e,build in data: diff --git a/npf/output/grapher.py b/npf/output/grapher.py index 51260f2..e9c8d7e 100644 --- a/npf/output/grapher.py +++ b/npf/output/grapher.py @@ -1205,12 +1205,12 @@ def generate_plot_for_graph(self, i, i_subplot, figure, n_cols, n_lines, vars_va barplot = True elif graph_type == "barh" or graph_type=="horizontal_bar": - r, ndata= do_barplot(self, axis, vars_all, dyns, result_type, data, shift, ibrokenY==0, horizontal=True, data_types=data_types) + r, ndata= do_barplot(self, axis, vars_all, dyns, result_type, data, shift, show_values=self.get_show_values() if ibrokenY==0 else False, horizontal=True, data_types=data_types) barplot = True horizontal = True else: """Barplot. X is all seen variables combination, series are version""" - r, ndata= do_barplot(self, axis, vars_all, dyns, result_type, data, shift, ibrokenY==0, data_types=data_types) + r, ndata= do_barplot(self, axis, vars_all, dyns, result_type, data, shift, show_values=self.get_show_values() if ibrokenY==0 else False, data_types=data_types) barplot = True except Exception as e: print("ERROR : could not graph %s" % result_type) @@ -1564,14 +1564,14 @@ def get_show_values(self): prec = 2 return prec - def write_labels(self, prec, rects, plt, color, idx = 0, each=False): + def write_labels(self, prec, rects, plt, color, idx = 0, each=False,isLog=False): if prec: def autolabel(rects, ax): for rect in rects: if hasattr(rect, 'get_ydata'): heights = rect.get_ydata() xs = rect.get_xdata() - m=1.1 + m=1.10 else: heights = rect.get_height() xs = rect.get_x() + rect.get_width()/2. @@ -1580,7 +1580,11 @@ def autolabel(rects, ax): if not each: xs = [np.mean(xs)] heights = [np.mean(heights)] + if not isLog: + m = 0.05*np.max(heights) + for x,height in zip(xs,heights): + try: if np.isnan(height): @@ -1588,7 +1592,7 @@ def autolabel(rects, ax): except Exception as e: print("exception", e) continue - ax.text(x, m*height, + ax.text(x, m*height if isLog else (m + height), ('%0.'+str(prec - 1)+'f') % height, color=color, fontweight='bold', ha='center', va='bottom') autolabel(rects, plt) @@ -1614,7 +1618,7 @@ def do_simple_barplot(self,axis, result_type, data,shift=0,isubplot=0): ticks = np.arange(ndata) + 0.5 - self.format_figure(axis,result_type,shift) + isLog = self.format_figure(axis,result_type,shift) gcolor = self.configlist('graph_color') if not gcolor: @@ -1622,7 +1626,7 @@ def do_simple_barplot(self,axis, result_type, data,shift=0,isubplot=0): c = graphcolorseries[gcolor[isubplot % len(gcolor)]][0] rects = plt.bar(ticks, y, label=x, color=c, width=width, yerr=( y - mean + std, mean - y + std)) - self.write_labels(self.get_show_values(), rects, plt,c) + self.write_labels(self.get_show_values(), rects, plt,c,isLog=isLog) plt.xticks(ticks, x) plt.gca().set_xlim(0, len(x)) @@ -1630,7 +1634,7 @@ def do_simple_barplot(self,axis, result_type, data,shift=0,isubplot=0): def do_box_plot(self, axis, key, result_type, data : XYEB, xdata : XYEB,shift=0,idx=0): - self.format_figure(axis, result_type, shift, key=key) + isLog = self.format_figure(axis, result_type, shift, key=key) nseries = max([len(y) for y in [y for x,y,e,build in data]]) labels=[] @@ -1697,7 +1701,7 @@ def do_box_plot(self, axis, key, result_type, data : XYEB, xdata : XYEB,shift=0, plt.setp(rects['fliers'], color = build._color) plt.setp(rects['medians'], color = lighter(build._color,0.50,0)) - self.write_labels(rects['boxes'], plt, build._color) + self.write_labels(rects['boxes'], plt, build._color, isLog=isLog) if nseries > 1: m = len(data)*nseries + 1 @@ -1719,7 +1723,7 @@ def do_box_plot(self, axis, key, result_type, data : XYEB, xdata : XYEB,shift=0, def do_cdf(self, axis, key, result_type, data : XYEB, xdata : XYEB,shift=0,idx=0): - self.format_figure(axis, result_type, shift, key=key, default_format="%d") + isLog = self.format_figure(axis, result_type, shift, key=key, default_format="%d") nseries = max([len(y) for y in [y for x,y,e,build in data]]) for i, (x, ys, e, build) in enumerate(data): @@ -1758,7 +1762,7 @@ def do_line_plot(self, axis, key, result_type, data : XYEB, data_types, shift,id #For each series... for i, (x, y, e, build) in enumerate(data): - self.format_figure(axis, result_type, shift, key=key) + isLog = self.format_figure(axis, result_type, shift, key=key) c = build._color if xdata: @@ -1861,7 +1865,7 @@ def do_line_plot(self, axis, key, result_type, data : XYEB, data_types, shift,id allmin = min(allmin, np.min(ax)) allmax = max(allmax, np.max(ax)) - self.write_labels(self.get_show_values(), rects, plt, build._color, idx, True) + self.write_labels(self.get_show_values(), rects, plt, build._color, idx, True,isLog=isLog) if xmin == float('inf'): return False, len(data) @@ -1959,4 +1963,4 @@ def format_figure(self, axis, result_type, shift, key = None, default_format = N if yticks: ticks = [npf.types.units.get_numeric(parseUnit(y)) for y in yticks.split('+')] plt.yticks(ticks) - + return isLog