Skip to content

Commit

Permalink
Support log-based "graph_show_values"
Browse files Browse the repository at this point in the history
  • Loading branch information
tbarbette committed Nov 18, 2024
1 parent 1a84ddd commit 08d6b32
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 19 deletions.
2 changes: 1 addition & 1 deletion npf/cmdline.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
7 changes: 3 additions & 4 deletions npf/output/graph/plots/barplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion npf/output/graph/plots/heatmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
30 changes: 17 additions & 13 deletions npf/output/grapher.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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.
Expand All @@ -1580,15 +1580,19 @@ 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):
continue
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)
Expand All @@ -1614,23 +1618,23 @@ 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:
gcolor = range(len(graphcolorseries))
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))
return True, ndata

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=[]
Expand Down Expand Up @@ -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
Expand All @@ -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):
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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

0 comments on commit 08d6b32

Please sign in to comment.