plot_parametrized.py
This file
Path
scripts/plot_parametrized.py
123 lines · 3.7 KiB · Python
Last changed by
Make plotting scripts runnable with uvDavid Peterbefore adoptionce49529
History of this fileTake it
The walk stopped after 200 commits: lines older than 0f91d0c point at it.
Bump serde from 1.0.174 to 1.0.179dependabot[bot]before adoption0f91d0c
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# ///Bump serde from 1.0.174 to 1.0.179dependabot[bot]before adoption0f91d0c
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 pltBump serde from 1.0.174 to 1.0.179dependabot[bot]before adoption0f91d0c
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 back19Bump serde from 1.0.174 to 1.0.179dependabot[bot]before adoption0f91d0c
line 20why, one step back20Make plotting scripts runnable with uvDavid Peterbefore adoptionce49529
lines 21–29why, one step back21def main():22 parser = argparse.ArgumentParser(description=__doc__)23 add_metric_argument(parser)24 parser.add_argument("file", help="JSON file with benchmark results", nargs="+")25 parser.add_argument(26 "--parameter-name",27 metavar="name",28 type=str,29 help="Deprecated; parameter names are now inferred from benchmark files",Bump serde from 1.0.174 to 1.0.179dependabot[bot]before adoption0f91d0c
line 30why, one step back30 )Make plotting scripts runnable with uvDavid Peterbefore adoptionce49529
lines 31–44why, one step back31 parser.add_argument(32 "--log-x", help="Use a logarithmic x (parameter) axis", action="store_true"33 )34 parser.add_argument(35 "--log-y",36 "--log-time",37 dest="log_y",38 help="Use a logarithmic metric axis",39 action="store_true",40 )41 parser.add_argument(42 "--titles", help="Comma-separated list of titles for the plot legend"43 )44 parser.add_argument("-o", "--output", help="Save image to the given filename.")Bump serde from 1.0.174 to 1.0.179dependabot[bot]before adoption0f91d0c
line 45why, one step back45Make plotting scripts runnable with uvDavid Peterbefore adoptionce49529
lines 46–53why, one step back46 args = parser.parse_args()47 metric, datasets = load_results(parser, args.file, args.metric)48 metric_info = METRICS[metric]49 if args.parameter_name is not None:50 sys.stderr.write(51 "warning: --parameter-name is deprecated; names are inferred from "52 "benchmark results\n"53 )Bump serde from 1.0.174 to 1.0.179dependabot[bot]before adoption0f91d0c
line 54why, one step back54Make plotting scripts runnable with uvDavid Peterbefore adoptionce49529
lines 55–57why, one step back55 def die(msg):56 sys.stderr.write(f"fatal: {msg}\n")57 sys.exit(1)Bump serde from 1.0.174 to 1.0.179dependabot[bot]before adoption0f91d0c
line 58why, one step back58Make plotting scripts runnable with uvDavid Peterbefore adoptionce49529
lines 59–69why, one step back59 def 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(67 f"benchmarks must all have the same parameter name, but found: {sorted(names)}"68 )69 return (next(iter(names)), list(values))Bump serde from 1.0.174 to 1.0.179dependabot[bot]before adoption0f91d0c
line 70why, one step back70Make plotting scripts runnable with uvDavid Peterbefore adoptionce49529
lines 71–81why, one step back71 def unique_parameter(benchmark):72 """Return the unique parameter `(name: str, value: float)`, or die."""73 params_dict = benchmark.get("parameters", {})74 if not params_dict:75 die("benchmarks must have exactly one parameter, but found none")76 if len(params_dict) > 1:77 die(78 f"benchmarks must have exactly one parameter, but found multiple: {sorted(params_dict)}"79 )80 [(name, value)] = params_dict.items()81 return (name, float(value["value"]))Bump serde from 1.0.174 to 1.0.179dependabot[bot]before adoption0f91d0c
line 82why, one step back82Make plotting scripts runnable with uvDavid Peterbefore adoptionce49529
line 83why, one step back83 parameter_name = NoneBump serde from 1.0.174 to 1.0.179dependabot[bot]before adoption0f91d0c
line 84why, one step back84Make plotting scripts runnable with uvDavid Peterbefore adoptionce49529
lines 85–91why, one step back85 for results in datasets:86 (this_parameter_name, parameter_values) = extract_parameters(results)87 if parameter_name is not None and this_parameter_name != parameter_name:88 die(89 f"files must all have the same parameter name, but found {parameter_name!r} vs. {this_parameter_name!r}"90 )91 parameter_name = this_parameter_nameBump serde from 1.0.174 to 1.0.179dependabot[bot]before adoption0f91d0c
line 92why, one step back92Make plotting scripts runnable with uvDavid Peterbefore adoptionce49529
lines 93–98why, one step back93 summaries = [b["summary"][metric] for b in results]94 means = [s["mean"] / metric_info.scale for s in summaries]95 stddevs = [96 s["stddev"] / metric_info.scale if s["stddev"] is not None else float("nan")97 for s in summaries98 ]Bump serde from 1.0.174 to 1.0.179dependabot[bot]before adoption0f91d0c
line 99why, one step back99Make plotting scripts runnable with uvDavid Peterbefore adoptionce49529
line 100why, one step back100 plt.errorbar(x=parameter_values, y=means, yerr=stddevs, capsize=2)Bump serde from 1.0.174 to 1.0.179dependabot[bot]before adoption0f91d0c
line 101why, one step back101Make plotting scripts runnable with uvDavid Peterbefore adoptionce49529
lines 102–103why, one step back102 plt.xlabel(parameter_name)103 plt.ylabel(metric_info.axis_label)Bump serde from 1.0.174 to 1.0.179dependabot[bot]before adoption0f91d0c
line 104why, one step back104Make plotting scripts runnable with uvDavid Peterbefore adoptionce49529
lines 105–108why, one step back105 if args.log_y:106 plt.yscale("log")107 else:108 plt.ylim(0, None)Bump serde from 1.0.174 to 1.0.179dependabot[bot]before adoption0f91d0c
line 109why, one step back109Make plotting scripts runnable with uvDavid Peterbefore adoptionce49529
lines 110–111why, one step back110 if args.log_x:111 plt.xscale("log")Bump serde from 1.0.174 to 1.0.179dependabot[bot]before adoption0f91d0c
line 112why, one step back112Make plotting scripts runnable with uvDavid Peterbefore adoptionce49529
lines 113–114why, one step back113 if args.titles:114 plt.legend(args.titles.split(","))Bump serde from 1.0.174 to 1.0.179dependabot[bot]before adoption0f91d0c
line 115why, one step back115Make plotting scripts runnable with uvDavid Peterbefore adoptionce49529
lines 116–119why, one step back116 if args.output:117 plt.savefig(args.output)118 else:119 plt.show()Bump serde from 1.0.174 to 1.0.179dependabot[bot]before adoption0f91d0c
lines 120–121why, one step back120121Make plotting scripts runnable with uvDavid Peterbefore adoptionce49529
lines 122–123why, one step back122if __name__ == "__main__":123 main()