-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot.py
More file actions
89 lines (69 loc) · 2.42 KB
/
plot.py
File metadata and controls
89 lines (69 loc) · 2.42 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
"""Pure rendering layer for the Kattis history graphs.
No Discord, no DB, no argument parsing — just `PlotRequest` + series -> PNG
bytes, so it is unit-testable offline. Uses the object-oriented Matplotlib
`Figure` API (NOT the global `pyplot` state), because the bot renders inside
`asyncio.to_thread` and global pyplot state is not thread-safe across
concurrent `/kattis` invocations.
"""
import enum
import io
from dataclasses import dataclass
from datetime import datetime
from matplotlib.figure import Figure
from matplotlib.ticker import MaxNLocator
# Above this many lines the legend clutters more than it helps, so it's hidden.
_LEGEND_MAX_LINES = 10
class Metric(enum.Enum):
# value doubles as the y-axis label.
SCORE = "Score"
RANK = "Rank"
NUM_USERS = "#users"
NUM_AFFILIATIONS = "#unis"
class Scope(enum.Enum):
GLOBAL = "global"
SWE = "swe"
CHALMERS = "chalmers"
ALL = "all"
@dataclass(frozen=True)
class PlotRequest:
metric: Metric
scope: Scope
days: int
log: bool
entity_kind: str # 'user' | 'uni' | 'country' — for context only
Series = list[tuple[str, list[tuple[datetime, float]]]]
def render(req: PlotRequest, series: Series) -> bytes:
"""Render the given (label, [(date, value), ...]) series to PNG bytes.
Series with no points are skipped.
"""
fig = Figure()
ax = fig.add_subplot(111)
n_lines = 0
for label, points in series:
if not points:
continue
xs = [p[0] for p in points]
ys = [p[1] for p in points]
ax.plot(xs, ys, label=label)
n_lines += 1
ax.set_ylabel(req.metric.value)
if req.metric is not Metric.SCORE:
ax.yaxis.set_major_locator(MaxNLocator(integer=True)) # only integer ticks
if req.metric is Metric.RANK:
ax.invert_yaxis() # rank 1 at the top
if req.log:
ax.set_yscale("log")
ax.tick_params(axis="x", labelrotation=20)
if 0 < n_lines <= _LEGEND_MAX_LINES:
handles, labels = ax.get_legend_handles_labels()
# Order the legend best-first: ascending rank, descending otherwise.
sign = 1 if req.metric is Metric.RANK else -1
labels, handles = zip(*sorted(
zip(labels, handles),
key=lambda t: sign * t[1].get_ydata()[-1],
))
ax.legend(handles, labels)
ax.grid(True)
buf = io.BytesIO()
fig.savefig(buf, format="png")
return buf.getvalue()