plot_whisker.py
This file
Path
scripts/plot_whisker.py
73 lines · 2.3 KiB · Python
Last changed by
Support all exported metrics in plotting scripts (#981)David Peterbefore adoptionef86c83
History of this fileTake it
The walk stopped after 200 commits: lines older than 9859eca point at it.
Always compute relative speed comprison for export, closes #642David Peterbefore adoption9859eca
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# ///Always compute relative speed comprison for export, closes #642David Peterbefore adoption9859eca
lines 9–19why, one step back910"""This program shows `hyperfine` benchmark results as a box and whisker plot.1112Quoting from the matplotlib documentation:13 The box extends from the lower to upper quartile values of the data, with14 a line at the median. The whiskers extend from the box to show the range15 of the data. Flier points are those past the end of the whiskers.16"""1718import argparse19import jsonSort importsDavid Peterbefore adoptionbc37eff
line 20why, one step back20Always compute relative speed comprison for export, closes #642David Peterbefore adoption9859eca
lines 21–22why, one step back21import matplotlib.pyplot as plt22Support all exported metrics in plotting scripts (#981)David Peterbefore adoptionef86c83
lines 23–24why, one step back23from plot_utils import METRICS, add_metric_argument, validate_metric24Always compute relative speed comprison for export, closes #642David Peterbefore adoption9859eca
line 25why, one step back25parser = argparse.ArgumentParser(description=__doc__)Support all exported metrics in plotting scripts (#981)David Peterbefore adoptionef86c83
line 26why, one step back26add_metric_argument(parser)Always compute relative speed comprison for export, closes #642David Peterbefore adoption9859eca
lines 27–28why, one step back27parser.add_argument("file", help="JSON file with benchmark results")28parser.add_argument("--title", help="Plot Title")Update formatting, use f-stringsDavid Peterbefore adoption65dd84f
line 29why, one step back29parser.add_argument("--sort-by", choices=["median"], help="Sort method")Always compute relative speed comprison for export, closes #642David Peterbefore adoption9859eca
lines 30–32why, one step back30parser.add_argument(31 "--labels", help="Comma-separated list of entries for the plot legend"32)Update formatting, use f-stringsDavid Peterbefore adoption65dd84f
line 33why, one step back33parser.add_argument("-o", "--output", help="Save image to the given filename.")Always compute relative speed comprison for export, closes #642David Peterbefore adoption9859eca
lines 34–35why, one step back3435args = parser.parse_args()Support all exported metrics in plotting scripts (#981)David Peterbefore adoptionef86c83
line 36why, one step back36metric_label, metric_scale = METRICS[args.metric]Always compute relative speed comprison for export, closes #642David Peterbefore adoption9859eca
line 37why, one step back37Update formatting, use f-stringsDavid Peterbefore adoption65dd84f
line 38why, one step back38with open(args.file, encoding="utf-8") as f:Always compute relative speed comprison for export, closes #642David Peterbefore adoption9859eca
line 39why, one step back39 results = json.load(f)["results"]Support all exported metrics in plotting scripts (#981)David Peterbefore adoptionef86c83
line 40why, one step back40validate_metric(parser, results, args.metric)Always compute relative speed comprison for export, closes #642David Peterbefore adoption9859eca
lines 41–44why, one step back4142if args.labels:43 labels = args.labels.split(",")44else:Merge pull request #790 from sharkdp/new-metricsDavid Peterbefore adoptionf26c015
line 45why, one step back45 labels = [b.get("name", b["command"]) for b in results]Support all exported metrics in plotting scripts (#981)David Peterbefore adoptionef86c83
lines 46–48why, one step back46values = [47 [m[args.metric]["value"] / metric_scale for m in b["measurements"]] for b in results48]Always compute relative speed comprison for export, closes #642David Peterbefore adoption9859eca
line 49why, one step back49Update formatting, use f-stringsDavid Peterbefore adoption65dd84f
line 50why, one step back50if args.sort_by == "median":Support all exported metrics in plotting scripts (#981)David Peterbefore adoptionef86c83
line 51why, one step back51 medians = [b["summary"][args.metric]["median"] for b in results]Add --sort-by optionSerpent7776before adoption51d056a
lines 52–53why, one step back52 indices = sorted(range(len(labels)), key=lambda k: medians[k])53 labels = [labels[i] for i in indices]Support all exported metrics in plotting scripts (#981)David Peterbefore adoptionef86c83
line 54why, one step back54 values = [values[i] for i in indices]Nicer whiskers plotSerpent7776before adoption5c9d7a8
line 55why, one step back55Fix long labels being cut offSerpent7776before adoption836d173
line 56why, one step back56plt.figure(figsize=(10, 6), constrained_layout=True)Support all exported metrics in plotting scripts (#981)David Peterbefore adoptionef86c83
line 57why, one step back57boxplot = plt.boxplot(values, vert=True, patch_artist=True)Always compute relative speed comprison for export, closes #642David Peterbefore adoption9859eca
line 58why, one step back58cmap = plt.get_cmap("rainbow")Support all exported metrics in plotting scripts (#981)David Peterbefore adoptionef86c83
line 59why, one step back59colors = [cmap(val / len(values)) for val in range(len(values))]Always compute relative speed comprison for export, closes #642David Peterbefore adoption9859eca
lines 60–66why, one step back6061for patch, color in zip(boxplot["boxes"], colors):62 patch.set_facecolor(color)6364if args.title:65 plt.title(args.title)66plt.legend(handles=boxplot["boxes"], labels=labels, loc="best", fontsize="medium")Support all exported metrics in plotting scripts (#981)David Peterbefore adoptionef86c83
line 67why, one step back67plt.ylabel(metric_label)Always compute relative speed comprison for export, closes #642David Peterbefore adoption9859eca
line 68why, one step back68plt.ylim(0, None)Update formatting, use f-stringsDavid Peterbefore adoption65dd84f
line 69why, one step back69plt.xticks(list(range(1, len(labels) + 1)), labels, rotation=45)Always compute relative speed comprison for export, closes #642David Peterbefore adoption9859eca
lines 70–73why, one step back70if args.output:71 plt.savefig(args.output)72else:73 plt.show()