GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — ppqueue/src/ppqueue/plot.py at main · fresh2dev/ppqueue Expand file tree/ Copy pathplot.pyMore file actions 271 lines (233 loc) · 7.45 KB / Copy pathplot.pyFile metadata and controls271 lines (233 loc) · 7.45 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 from __future__ import annotations import logging as log from enum import Enum, auto import pandas as pd import plotnine as gg from .job import Job from .utils import dedupe_list __all__ = [ "plot_jobs", "PlotColorBy", "PlotFacetBy", "PlotFacetScale", "PlotGridLines", "PlotTheme", ] class PlotColorBy(Enum): """Enum used to define color groups using this job property.""" QID = auto() PRIORITY = auto() GROUP = auto() CANCELLED = auto() EXITCODE = auto() NAME = auto() RESULT = auto() CALLBACK_RESULT = auto() class PlotFacetBy(Enum): """Enum used to facet the plot by this job property.""" QID = auto() PRIORITY = auto() GROUP = auto() CANCELLED = auto() EXITCODE = auto() NAME = auto() RESULT = auto() CALLBACK_RESULT = auto() class PlotTheme(Enum): """Enum used to expose supported plot themes.""" BW = auto() CLASSIC = auto() GRAY = auto() GREY = auto() SEABORN = auto() DARK = auto() MATPLOTLIB = auto() MINIMAL = auto() XKCD = auto() LIGHT = auto() class PlotFacetScale(Enum): FIXED = auto() FREE = auto() FREE_X = auto() FREE_Y = auto() class PlotGridLines(Enum): NONE = auto() MAJOR = auto() MINOR = auto() BOTH = auto() class Plot: def __init__(self, *args: list[Job]): df = pd.DataFrame([j.__dict__ for jobs in args for j in jobs]) df.group.fillna(value="", inplace=True) min_time = df["submit_timestamp"].min() df["submitted_offset"] = df["submit_timestamp"] - min_time df["started_offset"] = df["start_timestamp"] - min_time df["ended_offset"] = df["finish_timestamp"] - min_time df["processed_offset"] = df["process_timestamp"] - min_time self.jobs_df = df @staticmethod def _plot_theme( theme: PlotTheme, grid_lines: PlotGridLines, grid_axis: str | None, ) -> gg.theme: # Used to provide a consistent theme across plots. drop_grid = set() if grid_axis is None or grid_lines == PlotGridLines.NONE: drop_grid.update(["panel_grid_major", "panel_grid_minor"]) elif grid_axis == "x": drop_grid.update(["panel_grid_major_y", "panel_grid_minor_y"]) if grid_lines == PlotGridLines.MAJOR: drop_grid.add("panel_grid_minor_y") elif grid_lines == PlotGridLines.MINOR: drop_grid.add("panel_grid_major_y") elif grid_axis == "y": drop_grid.update(["panel_grid_major_x", "panel_grid_minor_x"]) if grid_lines == PlotGridLines.MAJOR: drop_grid.add("panel_grid_minor_x") elif grid_lines == PlotGridLines.MINOR: drop_grid.add("panel_grid_major_x") grid_opt = {} for x in drop_grid: grid_opt[x] = gg.element_blank() return getattr(gg, "theme_" + theme.name.lower())() + gg.theme( panel_border=gg.element_blank(), axis_line=gg.element_line(color="black"), **grid_opt, ) def build( self, title: str | None, color_by: PlotColorBy, facet_by: PlotFacetBy, facet_scale: PlotFacetScale, theme: PlotTheme, no_legend: bool, bar_width: int, color_pal: list[str] | None, ) -> gg.ggplot: df2 = self.jobs_df.loc[ :, dedupe_list( [ "qid", "idx", color_by.name.lower(), facet_by.name.lower(), "submitted_offset", "started_offset", "ended_offset", "processed_offset", ], ), ].melt( id_vars=dedupe_list( ["qid", "idx", color_by.name.lower(), facet_by.name.lower()], ), ) df2 = df2[df2["value"].notnull()] df_submit_start = df2[ (df2["variable"] == "submitted_offset") | (df2["variable"] == "started_offset") ] df_start_end = df2[ (df2["variable"] == "started_offset") | (df2["variable"] == "ended_offset") ] df_end_processed = df2[ (df2["variable"] == "ended_offset") | (df2["variable"] == "processed_offset") ] labs = {"x": "duration", "y": "job index"} if title is not None: labs["title"] = title gg_obj = ( gg.ggplot(gg.aes(x="value", y="idx", group="factor(idx)")) + gg.geom_line(df_submit_start, color="gray", size=bar_width, alpha=0.2) + gg.geom_line( df_start_end, gg.aes(color=f"factor({color_by.name.lower()})"), size=bar_width, show_legend=not bool(no_legend), ) + gg.geom_line(df_end_processed, color="gray", size=bar_width, alpha=0.2) + gg.labs(**labs) + gg.labs(color=color_by.name.lower()) + Plot._plot_theme( theme=theme, grid_lines=PlotGridLines.BOTH, grid_axis="x", ) + gg.facet_grid( facets=facet_by.name.lower() + "~", labeller="label_both", scales=facet_scale.name.lower(), as_table=True, ) ) if not color_pal: gg_obj += gg.scale_color_hue(h=0.65) else: n_colors = self.jobs_df[color_by.name.lower()].unique().size if len(color_pal) < n_colors: log.warning("Insufficient number of colors; need at least %d", n_colors) gg_obj += gg.scale_color_hue(h=0.65) else: gg_obj += gg.scale_color_manual(color_pal[:n_colors]) return gg_obj def plot_jobs( *args: list[Job], title: str | None = None, color_by: PlotColorBy = PlotColorBy.QID, facet_by: PlotFacetBy = PlotFacetBy.QID, facet_scale: PlotFacetScale = PlotFacetScale.FIXED, theme: PlotTheme = PlotTheme.BW, no_legend: bool = False, bar_width: int = 1, color_pal: list[str] | None = None, ) -> gg.ggplot: """ Args: *args: Sequences of `Job` instances to plot. title: ... color_by: ... facet_by: ... facet_scale: ... theme: ... no_legend: ... bar_width: ... color_pal: a sequence of colors used to color each group of `color_by`. Returns: ... Examples: >>> from ppqueue import Queue >>> from ppqueue.plot import plot_jobs >>> from time import sleep ... >>> with Queue() as queue: ... q1_jobs = queue.map(sleep, [1, 2, 3, 4, 5]) ... >>> with Queue() as queue: ... q2_jobs = queue.map(sleep, [1, 2, 3, 4, 5]) ... >>> plot = plot_jobs( ... q1_jobs, q2_jobs, ... title='Demo', ... color_by=PlotColorBy.QID, ... facet_by=PlotFacetBy.QID, ... theme=PlotTheme.DARK, ... bar_width=2, ... color_pal=['red', 'blue'] ... ) ... >>> print(plot) # doctest: +SKIP """ return Plot(*args).build( color_by=color_by, facet_by=facet_by, facet_scale=facet_scale, no_legend=no_legend, bar_width=bar_width, title=title, color_pal=color_pal, theme=theme, ) |
| You are visitor 01402775 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |