Skip to content
Myr

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 file

Take 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 back
1#!/usr/bin/env python

Add PEP-723 inline metadataDavid Peterbefore adoptioncfae9cb

lines 2–8why, one step back
2# /// script
3# 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 back
9
10"""This program shows parametrized `hyperfine` benchmark results as an11errorbar plot."""1213import argparse14import sys

Sort importsDavid Peterbefore adoptionbc37eff

lines 15–16why, one step back
15
16import matplotlib.pyplot as plt

Bump serde from 1.0.174 to 1.0.179dependabot[bot]before adoption0f91d0c

line 17why, one step back
17

Alternative performance metrics in the terminal (#986)David Peterbefore adoptionbdbfae9

line 18why, one step back
18from plot_utils import METRICS, add_metric_argument, load_results

Support all exported metrics in plotting scripts (#981)David Peterbefore adoptionef86c83

line 19why, one step back
19

Bump serde from 1.0.174 to 1.0.179dependabot[bot]before adoption0f91d0c

line 20why, one step back
20

Make plotting scripts runnable with uvDavid Peterbefore adoptionce49529

lines 21–29why, one step back
21def 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 back
30 )

Make plotting scripts runnable with uvDavid Peterbefore adoptionce49529

lines 31–44why, one step back
31 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 back
45

Make plotting scripts runnable with uvDavid Peterbefore adoptionce49529

lines 46–53why, one step back
46 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 back
54

Make plotting scripts runnable with uvDavid Peterbefore adoptionce49529

lines 55–57why, one step back
55 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 back
58

Make plotting scripts runnable with uvDavid Peterbefore adoptionce49529

lines 59–69why, one step back
59 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 back
70

Make plotting scripts runnable with uvDavid Peterbefore adoptionce49529

lines 71–81why, one step back
71 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 back
82

Make plotting scripts runnable with uvDavid Peterbefore adoptionce49529

line 83why, one step back
83 parameter_name = None

Bump serde from 1.0.174 to 1.0.179dependabot[bot]before adoption0f91d0c

line 84why, one step back
84

Make plotting scripts runnable with uvDavid Peterbefore adoptionce49529

lines 85–91why, one step back
85 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_name

Bump serde from 1.0.174 to 1.0.179dependabot[bot]before adoption0f91d0c

line 92why, one step back
92

Make plotting scripts runnable with uvDavid Peterbefore adoptionce49529

lines 93–98why, one step back
93 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 back
99

Make plotting scripts runnable with uvDavid Peterbefore adoptionce49529

line 100why, one step back
100 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 back
101

Make plotting scripts runnable with uvDavid Peterbefore adoptionce49529

lines 102–103why, one step back
102 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 back
104

Make plotting scripts runnable with uvDavid Peterbefore adoptionce49529

lines 105–108why, one step back
105 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 back
109

Make plotting scripts runnable with uvDavid Peterbefore adoptionce49529

lines 110–111why, one step back
110 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 back
112

Make plotting scripts runnable with uvDavid Peterbefore adoptionce49529

lines 113–114why, one step back
113 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 back
115

Make plotting scripts runnable with uvDavid Peterbefore adoptionce49529

lines 116–119why, one step back
116 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 back
120
121

Make plotting scripts runnable with uvDavid Peterbefore adoptionce49529

lines 122–123why, one step back
122if __name__ == "__main__":
123 main()