plot_parametrized.py
This file
Path
scripts/plot_parametrized.py
121 lines · 3.3 KiB · Python
Last changed by
Alternative performance metrics in the terminal (#986)David Peterbefore adoptionbdbfae9
History of this fileTake it
The walk stopped after 200 commits: lines older than 0ce6578 point at it.
Update man pageDavid Peterbefore adoption0ce6578
line 1why, one step back1#!/usr/bin/env pythonAdd PEP-723 inline metadataDavid Peterbefore adoptioncfae9cb
lines 2–8why, one step back2# /// script3# requires-python = ">=3.10"4# dependencies = [5# "matplotlib",6# "pyqt6",7# ]8# ///Update man pageDavid Peterbefore adoption0ce6578
lines 9–14why, one step back910"""This program shows parametrized `hyperfine` benchmark results as an11errorbar plot."""1213import argparse14import sysSort importsDavid Peterbefore adoptionbc37eff
lines 15–16why, one step back1516import matplotlib.pyplot as pltUpdate man pageDavid Peterbefore adoption0ce6578
line 17why, one step back17Alternative performance metrics in the terminal (#986)David Peterbefore adoptionbdbfae9
line 18why, one step back18from plot_utils import METRICS, add_metric_argument, load_resultsSupport all exported metrics in plotting scripts (#981)David Peterbefore adoptionef86c83
line 19why, one step back19Update man pageDavid Peterbefore adoption0ce6578
line 20why, one step back20parser = argparse.ArgumentParser(description=__doc__)Support all exported metrics in plotting scripts (#981)David Peterbefore adoptionef86c83
line 21why, one step back21add_metric_argument(parser)Update man pageDavid Peterbefore adoption0ce6578
lines 22–32why, one step back22parser.add_argument("file", help="JSON file with benchmark results", nargs="+")23parser.add_argument(24 "--parameter-name",25 metavar="name",26 type=str,27 help="Deprecated; parameter names are now inferred from benchmark files",28)29parser.add_argument(30 "--log-x", help="Use a logarithmic x (parameter) axis", action="store_true"31)32parser.add_argument(Support all exported metrics in plotting scripts (#981)David Peterbefore adoptionef86c83
lines 33–37why, one step back33 "--log-y",34 "--log-time",35 dest="log_y",36 help="Use a logarithmic metric axis",37 action="store_true",Update man pageDavid Peterbefore adoption0ce6578
lines 38–41why, one step back38)39parser.add_argument(40 "--titles", help="Comma-separated list of titles for the plot legend"41)Update formatting, use f-stringsDavid Peterbefore adoption65dd84f
line 42why, one step back42parser.add_argument("-o", "--output", help="Save image to the given filename.")Update man pageDavid Peterbefore adoption0ce6578
lines 43–44why, one step back4344args = parser.parse_args()Alternative performance metrics in the terminal (#986)David Peterbefore adoptionbdbfae9
lines 45–46why, one step back45metric, datasets = load_results(parser, args.file, args.metric)46metric_info = METRICS[metric]Update man pageDavid Peterbefore adoption0ce6578
lines 47–54why, one step back47if args.parameter_name is not None:48 sys.stderr.write(49 "warning: --parameter-name is deprecated; names are inferred from "50 "benchmark results\n"51 )525354def die(msg):Update formatting, use f-stringsDavid Peterbefore adoption65dd84f
line 55why, one step back55 sys.stderr.write(f"fatal: {msg}\n")Update man pageDavid Peterbefore adoption0ce6578
lines 56–66why, one step back56 sys.exit(1)575859def extract_parameters(results):60 """Return `(parameter_name: str, parameter_values: List[float])`."""61 if not results:62 die("no benchmark data to plot")63 (names, values) = zip(*(unique_parameter(b) for b in results))64 names = frozenset(names)65 if len(names) != 1:66 die(Update formatting, use f-stringsDavid Peterbefore adoption65dd84f
line 67why, one step back67 f"benchmarks must all have the same parameter name, but found: {sorted(names)}"Update man pageDavid Peterbefore adoption0ce6578
lines 68–78why, one step back68 )69 return (next(iter(names)), list(values))707172def unique_parameter(benchmark):73 """Return the unique parameter `(name: str, value: float)`, or die."""74 params_dict = benchmark.get("parameters", {})75 if not params_dict:76 die("benchmarks must have exactly one parameter, but found none")77 if len(params_dict) > 1:78 die(Update formatting, use f-stringsDavid Peterbefore adoption65dd84f
line 79why, one step back79 f"benchmarks must have exactly one parameter, but found multiple: {sorted(params_dict)}"Update man pageDavid Peterbefore adoption0ce6578
lines 80–81why, one step back80 )81 [(name, value)] = params_dict.items()Merge pull request #790 from sharkdp/new-metricsDavid Peterbefore adoptionf26c015
line 82why, one step back82 return (name, float(value["value"]))Update man pageDavid Peterbefore adoption0ce6578
lines 83–86why, one step back838485parameter_name = None86Alternative performance metrics in the terminal (#986)David Peterbefore adoptionbdbfae9
line 87why, one step back87for results in datasets:Update man pageDavid Peterbefore adoption0ce6578
lines 88–90why, one step back88 (this_parameter_name, parameter_values) = extract_parameters(results)89 if parameter_name is not None and this_parameter_name != parameter_name:90 die(Update formatting, use f-stringsDavid Peterbefore adoption65dd84f
line 91why, one step back91 f"files must all have the same parameter name, but found {parameter_name!r} vs. {this_parameter_name!r}"Update man pageDavid Peterbefore adoption0ce6578
lines 92–94why, one step back92 )93 parameter_name = this_parameter_name94Alternative performance metrics in the terminal (#986)David Peterbefore adoptionbdbfae9
lines 95–96why, one step back95 summaries = [b["summary"][metric] for b in results]96 means = [s["mean"] / metric_info.scale for s in summaries]Support all exported metrics in plotting scripts (#981)David Peterbefore adoptionef86c83
line 97why, one step back97 stddevs = [Alternative performance metrics in the terminal (#986)David Peterbefore adoptionbdbfae9
line 98why, one step back98 s["stddev"] / metric_info.scale if s["stddev"] is not None else float("nan")Support all exported metrics in plotting scripts (#981)David Peterbefore adoptionef86c83
line 99why, one step back99 for s in summariesMerge pull request #790 from sharkdp/new-metricsDavid Peterbefore adoptionf26c015
line 100why, one step back100 ]Update man pageDavid Peterbefore adoption0ce6578
line 101why, one step back101Support all exported metrics in plotting scripts (#981)David Peterbefore adoptionef86c83
line 102why, one step back102 plt.errorbar(x=parameter_values, y=means, yerr=stddevs, capsize=2)Update man pageDavid Peterbefore adoption0ce6578
lines 103–104why, one step back103104plt.xlabel(parameter_name)Alternative performance metrics in the terminal (#986)David Peterbefore adoptionbdbfae9
line 105why, one step back105plt.ylabel(metric_info.axis_label)Update man pageDavid Peterbefore adoption0ce6578
line 106why, one step back106Support all exported metrics in plotting scripts (#981)David Peterbefore adoptionef86c83
line 107why, one step back107if args.log_y:Update man pageDavid Peterbefore adoption0ce6578
lines 108–121why, one step back108 plt.yscale("log")109else:110 plt.ylim(0, None)111112if args.log_x:113 plt.xscale("log")114115if args.titles:116 plt.legend(args.titles.split(","))117118if args.output:119 plt.savefig(args.output)120else:121 plt.show()