Skip to content
Myr

plot_parametrized.py

This file

Path

scripts/plot_parametrized.py

109 lines · 3.0 KiB · Python

Last changed by

Change parameter name in plotting scriptsBenjamin Rosemannbefore adoption49fcc4b

History of this file

Take it

The walk stopped after 200 commits: lines older than e58a7f1 point at it.

scripts: modify shebang to use "/usr/bin/env python"muxatorbefore adoption5a80a23

line 1why, one step back
1#!/usr/bin/env python

Bump serde_json from 1.0.53 to 1.0.55dependabot-preview[bot]before adoptione58a7f1

lines 2–8why, one step back
2
3"""This program shows parametrized `hyperfine` benchmark results as an4errorbar plot."""56import argparse7import json8import matplotlib.pyplot as plt

[update patch]William Charginbefore adoptiona8a75a3

line 9why, one step back
9import sys

Bump serde_json from 1.0.53 to 1.0.55dependabot-preview[bot]before adoptione58a7f1

lines 10–16why, one step back
10
11parser = argparse.ArgumentParser(description=__doc__)12parser.add_argument("file", help="JSON file with benchmark results", nargs="+")13parser.add_argument(14 "--parameter-name",15 metavar="name",16 type=str,

[update patch]William Charginbefore adoptiona8a75a3

line 17why, one step back
17 help="Deprecated; parameter names are now inferred from benchmark files",

Bump serde_json from 1.0.53 to 1.0.55dependabot-preview[bot]before adoptione58a7f1

lines 18–27why, one step back
18)
19parser.add_argument(20 "--log-x", help="Use a logarithmic x (parameter) axis", action="store_true"21)22parser.add_argument(23 "--log-time", help="Use a logarithmic time axis", action="store_true"24)25parser.add_argument(26 "--titles", help="Comma-separated list of titles for the plot legend"27)

Change parameter name in plotting scriptsBenjamin Rosemannbefore adoption49fcc4b

lines 28–30why, one step back
28parser.add_argument(
29 "-o", "--output", help="Save image to the given filename."30)

Bump serde_json from 1.0.53 to 1.0.55dependabot-preview[bot]before adoptione58a7f1

lines 31–32why, one step back
31
32args = parser.parse_args()

[update patch]William Charginbefore adoptiona8a75a3

lines 33–37why, one step back
33if args.parameter_name is not None:
34 sys.stderr.write(35 "warning: --parameter-name is deprecated; names are inferred from "36 "benchmark results\n"37 )

Bump serde_json from 1.0.53 to 1.0.55dependabot-preview[bot]before adoptione58a7f1

line 38why, one step back
38

[update patch]William Charginbefore adoptiona8a75a3

lines 39–55why, one step back
39
40def die(msg):41 sys.stderr.write("fatal: %s\n" % (msg,))42 sys.exit(1)434445def extract_parameters(results):46 """Return `(parameter_name: str, parameter_values: List[float])`."""47 if not results:48 die("no benchmark data to plot")49 (names, values) = zip(*(unique_parameter(b) for b in results))50 names = frozenset(names)51 if len(names) != 1:52 die(53 "benchmarks must all have the same parameter name, but found: %s"54 % sorted(names)55 )

[update patch]William Charginbefore adoption8973778

line 56why, one step back
56 return (next(iter(names)), list(values))

[update patch]William Charginbefore adoptiona8a75a3

lines 57–59why, one step back
57
5859def unique_parameter(benchmark):

[update patch]William Charginbefore adoption8973778

line 60why, one step back
60 """Return the unique parameter `(name: str, value: float)`, or die."""

[update patch]William Charginbefore adoptiona8a75a3

lines 61–68why, one step back
61 params_dict = benchmark.get("parameters", {})
62 if not params_dict:63 die("benchmarks must have exactly one parameter, but found none")64 if len(params_dict) > 1:65 die(66 "benchmarks must have exactly one parameter, but found multiple: %s"67 % sorted(params_dict)68 )

[update patch]William Charginbefore adoption8973778

lines 69–70why, one step back
69 [(name, value)] = params_dict.items()
70 return (name, float(value))

[update patch]William Charginbefore adoptiona8a75a3

lines 71–74why, one step back
71
7273parameter_name = None74

Bump serde_json from 1.0.53 to 1.0.55dependabot-preview[bot]before adoptione58a7f1

lines 75–78why, one step back
75for filename in args.file:
76 with open(filename) as f:77 results = json.load(f)["results"]78

[update patch]William Charginbefore adoptiona8a75a3

lines 79–86why, one step back
79 (this_parameter_name, parameter_values) = extract_parameters(results)
80 if parameter_name is not None and this_parameter_name != parameter_name:81 die(82 "files must all have the same parameter name, but found %r vs. %r"83 % (parameter_name, this_parameter_name)84 )85 parameter_name = this_parameter_name86

Bump serde_json from 1.0.53 to 1.0.55dependabot-preview[bot]before adoptione58a7f1

lines 87–91why, one step back
87 times_mean = [b["mean"] for b in results]
88 times_stddev = [b["stddev"] for b in results]8990 plt.errorbar(x=parameter_values, y=times_mean, yerr=times_stddev, capsize=2)91

[update patch]William Charginbefore adoptiona8a75a3

line 92why, one step back
92plt.xlabel(parameter_name)

Bump serde_json from 1.0.53 to 1.0.55dependabot-preview[bot]before adoptione58a7f1

lines 93–105why, one step back
93plt.ylabel("Time [s]")
9495if args.log_time:96 plt.yscale("log")97else:98 plt.ylim(0, None)99100if args.log_x:101 plt.xscale("log")102103if args.titles:104 plt.legend(args.titles.split(","))105

Change parameter name in plotting scriptsBenjamin Rosemannbefore adoption49fcc4b

lines 106–107why, one step back
106if args.output:
107 plt.savefig(args.output)

Add parameters to plotting scriptsBenjamin Rosemannbefore adoptionb04a9ec

lines 108–109why, one step back
108else:
109 plt.show()