diff --git a/scripts/combine/pullsAndImpacts.py b/scripts/combine/pullsAndImpacts.py index 572b5bda3..5b9512784 100644 --- a/scripts/combine/pullsAndImpacts.py +++ b/scripts/combine/pullsAndImpacts.py @@ -32,9 +32,7 @@ def writeOutput(fig, outfile, extensions=[], postfix=None, args=None, meta_info=None): - name, ext = os.path.splitext(outfile) - if ext not in extensions: - extensions.append(ext) + name, _ = os.path.splitext(outfile) if postfix: name += f"_{postfix}" @@ -159,8 +157,8 @@ def plotImpacts( tickangle=0, side="top", ) - textargs = dict() + text_on_bars = False labels = df["label"] if impacts and show_numbers: if include_ref: @@ -205,80 +203,62 @@ def plotImpacts( nval = nval + nspace_ref + nval_ref labels = labels + nspace + nval else: - textargs = dict( - texttemplate="%{x:0.2f}", - textposition="outside", - textangle=0, - ) + text_on_bars = True if impacts: - fig.add_trace( - go.Bar( - x=( - np.where(df["impact"] < 0, None, df["impact"]) - if oneSidedImpacts - else df["impact"] - ), - y=labels, + + def make_bar( + key="impact", sign=1, color="#377eb8", name="+1σ impact", text_on_bars=False + ): + x = ( + np.where(sign * df[key] < 0, np.nan, sign * df[key]) + if oneSidedImpacts + else sign * df[key] + ) + + if text_on_bars: + text = np.where(np.isnan(x), None, [f"{value:.2f}" for value in x]) + else: + text = None + + return go.Bar( orientation="h", + x=x, + y=labels, + text=text, + textposition="outside", **get_marker( - filled=True, color="#377eb8", opacity=0.5 if include_ref else 1 + filled=True, color=color, opacity=0.5 if include_ref else 1 ), - name="+1σ impact", - **textargs, - ), + name=name, + ) + + fig.add_trace( + make_bar(text_on_bars=text_on_bars), row=1, col=1, ) if include_ref: fig.add_trace( - go.Bar( - x=( - np.where(df["impact_ref"] < 0, None, df["impact_ref"]) - if oneSidedImpacts - else df["impact_ref"] - ), - y=labels, - # width=0.2, - orientation="h", - **get_marker(filled=False, color="#377eb8"), - name=f"+1σ impact ({ref_name})", - ), + make_bar(key="impact_ref", name=f"+1σ impact ({ref_name})"), row=1, col=1, ) fig.add_trace( - go.Bar( - x=( - np.where(df["impact"] > 0, None, -df["impact"]) - if oneSidedImpacts - else -df["impact"] - ), - y=labels, - orientation="h", - **get_marker( - filled=True, color="#e41a1c", opacity=0.5 if include_ref else 1 - ), - name="-1σ impact", - **textargs, + make_bar( + name="-1σ impact", sign=-1, color="#e41a1c", text_on_bars=text_on_bars ), row=1, col=1, ) if include_ref: fig.add_trace( - go.Bar( - x=( - np.where(df["impact_ref"] > 0, None, -df["impact_ref"]) - if oneSidedImpacts - else -df["impact_ref"] - ), - y=labels, - # width=0.2, - orientation="h", - **get_marker(filled=False, color="#e41a1c"), + make_bar( + key="impact_ref", name=f"-1σ impact ({ref_name})", + sign=-1, + color="#e41a1c", ), row=1, col=1, @@ -881,7 +861,7 @@ def producePlots( if "html" in extensions and len(extensions) > 1: fig = plotImpacts(df, legend_pos="right", **kwargs) outfile_html = outfile.replace(outfile.split(".")[-1], "html") - writeOutput(fig, outfile_html, postfix=postfix) + writeOutput(fig, outfile_html, [".html"], postfix=postfix) extensions = [e for e in extensions if e != "html"] outfile = outfile.replace(outfile.split(".")[-1], extensions[0]) df = df[-args.num :] @@ -889,7 +869,7 @@ def producePlots( fig = plotImpacts(df, **kwargs) writeOutput( - fig, outfile, extensions[0:], postfix=postfix, args=args, meta_info=meta + fig, outfile, extensions, postfix=postfix, args=args, meta_info=meta ) if args.eoscp and output_tools.is_eosuser_path(args.outFolder): output_tools.copy_to_eos(outdir, args.outFolder, "")