Debian Codegraph @

autoray/lazy/draw.py

raw
1 """Visualizations for ``LazyArray`` computational graphs."""
2
3 import itertools
4 import functools
5 import importlib.util
6
7
8 COLORING_SEED = 1 # 8, 10
9
10
11 def set_coloring_seed(seed):
12 """Set the seed for the random color generator.
13
14 Parameters
15 ----------
16 seed : int
17 The seed to use.
18 """
19 global COLORING_SEED
20 COLORING_SEED = seed
21
22
23 def hash_to_nvalues(s, nval, seed=None):
24 import hashlib
25
26 if seed is None:
27 seed = COLORING_SEED
28
29 m = hashlib.sha256()
30 m.update(f"{seed}".encode())
31 m.update(s.encode())
32 hsh = m.hexdigest()
33
34 b = len(hsh) // nval
35 if b == 0:
36 raise ValueError(
37 f"Can't extract {nval} values from hash of length {len(hsh)}"
38 )
39 return tuple(
40 int(hsh[i * b : (i + 1) * b], 16) / 16**b for i in range(nval)
41 )
42
43
44 def hash_to_color(
45 s,
46 hmin=0.0,
47 hmax=1.0,
48 smin=0.3,
49 smax=0.8,
50 vmin=0.8,
51 vmax=1.0,
52 ):
53 """Generate a random color for a string ``s``.
54
55 Parameters
56 ----------
57 s : str
58 The string to generate a color for.
59 hmin : float, optional
60 The minimum hue value.
61 hmax : float, optional
62 The maximum hue value.
63 smin : float, optional
64 The minimum saturation value.
65 smax : float, optional
66 The maximum saturation value.
67 vmin : float, optional
68 The minimum value value.
69 vmax : float, optional
70 The maximum value value.
71
72 Returns
73 -------
74 color : tuple
75 A tuple of floats in the range [0, 1] representing the RGB color.
76 """
77 from matplotlib.colors import hsv_to_rgb
78
79 h, s, v = hash_to_nvalues(s, 3)
80 h = hmin + h * (hmax - hmin)
81 s = smin + s * (smax - smin)
82 v = vmin + v * (vmax - vmin)
83
84 return hsv_to_rgb((h, s, v))
85
86
87 def rotated_house_shape(xy, r=0.4):
88 x, y = xy
89 return [
90 [x - r, y - r],
91 [x - r, y + r],
92 [x, y + r],
93 [x + r, y],
94 [x, y - r],
95 ]
96
97
98 def count_around(c, layout):
99 if layout == "wide":
100 # just count upwards
101 yield from itertools.count(c)
102 elif layout == "compact":
103 # count backwards, then forwards after reaching zero
104 yield from range(c, -1, -1)
105 yield from itertools.count(c + 1)
106 else: # 'balanced'
107 # count backwards, then forwards, alternating
108 step = 0
109 # start by stepping to side closer to zero
110 sgn = (-1) ** (c <= 0)
111 while True:
112 cm = c - sgn * step
113 if step != 0: # and (cm >= 0):
114 yield cm
115 yield c + sgn * step
116 step += 1
117
118
119 def get_default_colors_dict(colors):
120 import numpy as np
121
122 colors = dict() if colors is None else dict(colors)
123 colors.setdefault("None", np.array([0.5, 0.5, 0.5]))
124 colors.setdefault("getitem", np.array([0.5, 0.5, 0.5]))
125 return colors
126
127
128 def rotate(xy, theta):
129 """Return a rotated set of points."""
130 import numpy as np
131
132 s = np.sin(theta)
133 c = np.cos(theta)
134
135 xyr = np.empty_like(xy)
136 xyr[:, 0] = c * xy[:, 0] - s * xy[:, 1]
137 xyr[:, 1] = s * xy[:, 0] + c * xy[:, 1]
138
139 return xyr
140
141
142 def span(xy):
143 """Return the vertical span of the points."""
144 return xy[:, 1].max() - xy[:, 1].min()
145
146
147 def massage_pos(pos, nangles=180, flatten=False):
148 """Rotate a position dict's points to cover a small vertical span"""
149 import numpy as np
150
151 xy = np.empty((len(pos), 2))
152 for i, (x, y) in enumerate(pos.values()):
153 xy[i, 0] = x
154 xy[i, 1] = y
155
156 thetas = np.linspace(0, 2 * np.pi, nangles, endpoint=False)
157 rxys = (rotate(xy, theta) for theta in thetas)
158 rxy0 = min(rxys, key=lambda rxy: span(rxy))
159
160 if flatten is True:
161 flatten = 2
162 if flatten:
163 rxy0[:, 1] /= flatten
164
165 return dict(zip(pos, rxy0))
166
167
168 def layout_pygraphviz(
169 G,
170 prog="neato",
171 dim=2,
172 **kwargs,
173 ):
174 # TODO: fix nodes with pin attribute
175 # TODO: initial positions
176 # TODO: max iters
177 # TODO: spring parameter
178 import pygraphviz as pgv
179
180 aG = pgv.AGraph(directed=G.is_directed())
181 mapping = {}
182 for nodea, nodeb in G.edges():
183 s_nodea = str(nodea)
184 s_nodeb = str(nodeb)
185 mapping[s_nodea] = nodea
186 mapping[s_nodeb] = nodeb
187 aG.add_edge(s_nodea, s_nodeb)
188
189 kwargs = {}
190
191 if dim == 2.5:
192 kwargs["dim"] = 3
193 kwargs["dimen"] = 2
194 else:
195 kwargs["dim"] = kwargs["dimen"] = dim
196 args = " ".join(f"-G{k}={v}" for k, v in kwargs.items())
197
198 # run layout algorithm
199 aG.layout(prog=prog, args=args)
200
201 # extract layout
202 pos = {}
203 for snode, node in mapping.items():
204 spos = aG.get_node(snode).attr["pos"]
205 pos[node] = tuple(map(float, spos.split(",")))
206
207 # normalize to unit square
208 xmin = ymin = zmin = float("inf")
209 xmax = ymax = zmaz = float("-inf")
210 for x, y, *maybe_z in pos.values():
211 xmin = min(xmin, x)
212 xmax = max(xmax, x)
213 ymin = min(ymin, y)
214 ymax = max(ymax, y)
215 for z in maybe_z:
216 zmin = min(zmin, z)
217 zmaz = max(zmaz, z)
218
219 for node, (x, y, *maybe_z) in pos.items():
220 pos[node] = (
221 2 * (x - xmin) / (xmax - xmin) - 1,
222 2 * (y - ymin) / (ymax - ymin) - 1,
223 *(2 * (z - zmin) / (zmaz - zmin) - 1 for z in maybe_z),
224 )
225
226 return pos
227
228
229 HAS_FA2 = importlib.util.find_spec("fa2") is not None
230 HAS_PYGRAPHVIZ = importlib.util.find_spec("pygraphviz") is not None
231
232
233 def get_nice_pos(
234 G,
235 *,
236 dim=2,
237 layout="auto",
238 initial_layout="auto",
239 iterations="auto",
240 k=None,
241 use_forceatlas2=False,
242 flatten=False,
243 **layout_opts,
244 ):
245 if (layout == "auto") and HAS_PYGRAPHVIZ:
246 layout = "neato"
247
248 if layout in ("dot", "neato", "fdp", "sfdp"):
249 pos = layout_pygraphviz(G, prog=layout, dim=dim)
250 if layout != "dot":
251 pos = massage_pos(pos, flatten=flatten)
252 return pos
253
254 import networkx as nx
255
256 if layout != "auto":
257 initial_layout = layout
258 iterations = 0
259
260 if initial_layout == "auto":
261 # automatically select
262 if len(G) <= 100:
263 # usually nicest
264 initial_layout = "kamada_kawai"
265 else:
266 # faster, but not as nice
267 initial_layout = "spectral"
268
269 if iterations == "auto":
270 # the smaller the graph, the more iterations we can afford
271 iterations = max(200, 1000 - len(G))
272
273 if dim == 2.5:
274 dim = 3
275 project_back_to_2d = True
276 else:
277 project_back_to_2d = False
278
279 # use spectral or other layout as starting point
280
281 if dim != 2:
282 layout_opts["dim"] = dim
283
284 pos0 = getattr(nx, initial_layout + "_layout")(G, **layout_opts)
285
286 # and then relax remaining using spring layout
287 if iterations:
288 if use_forceatlas2 is True:
289 # turn on for more than 1 node
290 use_forceatlas2 = 1
291 elif use_forceatlas2 in (0, False):
292 # never turn on
293 use_forceatlas2 = float("inf")
294
295 should_use_fa2 = HAS_FA2 and (len(G) > use_forceatlas2) and (dim == 2)
296
297 if should_use_fa2:
298 from fa2 import ForceAtlas2
299
300 # NB: some versions of fa2 don't support the `weight_attr` option
301 pos = ForceAtlas2(verbose=False).forceatlas2_networkx_layout(
302 G, pos=pos0, iterations=iterations
303 )
304 else:
305 pos = nx.spring_layout(
306 G,
307 pos=pos0,
308 k=k,
309 dim=dim,
310 iterations=iterations,
311 )
312 else:
313 pos = pos0
314
315 if project_back_to_2d:
316 # project back to 2d
317 pos = {k: v[:2] for k, v in pos.items()}
318 dim = 2
319
320 if dim == 2:
321 # finally rotate them to cover a small vertical span
322 pos = massage_pos(pos)
323
324 return pos
325
326
327 # def get_nice_pos():
328 # """Get a nice layout for a graph.
329 # """
330
331 # # compute a layout for the graph
332 # if initial_layout == "layers":
333 # for layer, nodes in enumerate(nx.topological_generations(G)):
334 # for node in nodes:
335 # G.nodes[node]["layer"] = layer
336
337 # layout_opts.setdefault("subset_key", "layer")
338 # layout_opts.setdefault("align", "vertical")
339 # pos = nx.multipartite_layout(G, **layout_opts)
340
341 # if layout_opts["align"] == "horizontal":
342 # dag_spread = 1 / dag_spread
343 # for k, (x, y) in pos.items():
344 # pos[k] = (x, dag_spread * y)
345
346 # else:
347 # if initial_layout == "spiral":
348 # layout_opts.setdefault("equidistant", True)
349
350 # pos = getattr(nx, initial_layout + "_layout")(G, **layout_opts)
351
352 # # further spring based refinement
353 # if iterations:
354 # pos = nx.layout.spring_layout(G, pos=pos, k=k, iterations=iterations)
355
356
357 def plot_graph(
358 self,
359 variables=None,
360 dim=2,
361 layout="auto",
362 initial_layout="auto",
363 iterations="auto",
364 k=None,
365 use_forceatlas2=False,
366 color_by="function",
367 colors=None,
368 connectionstyle="arc3,rad=-0.05",
369 arrowsize=6,
370 edge_color=(0.5, 0.5, 0.5),
371 edge_alpha=0.3,
372 var_color=(0, 0.5, 0.25),
373 const_color=(0, 0.5, 1.0),
374 root_color=(1, 0, 0.5),
375 node_shape="s",
376 node_scale=1.0,
377 node_alpha=1.0,
378 show_labels=True,
379 label_color=(0.5, 0.5, 0.5),
380 label_alpha=1.0,
381 font_size=8,
382 label_rotation=45,
383 figsize=None,
384 ax=None,
385 show_and_close=True,
386 **layout_opts,
387 ):
388 """Plot the computational graph of this ``LazyArray``."""
389 import numpy as np
390 import networkx as nx
391 import matplotlib.pyplot as plt
392
393 if color_by not in ("id", "function", "variables"):
394 raise ValueError("color_by must be 'id', 'function' or 'variables'")
395
396 colors = get_default_colors_dict(colors)
397 G = self.to_nx_digraph(variables=variables)
398
399 created_fig = ax is None
400 if created_fig:
401 if figsize is None:
402 w = h = (G.number_of_nodes() + 1) ** 0.5
403 figsize = (w, h)
404 fig, ax = plt.subplots(figsize=figsize, constrained_layout=True)
405 fig.set_facecolor((0, 0, 0, 0))
406 ax.axis("off")
407 ax.set_aspect("equal")
408
409 node_colors = {}
410 node_sizes = {}
411 node_labels = {}
412 node_markers = {}
413 for i, data in G.nodes(data=True):
414 # set node color
415 if data["array"] is self:
416 node_markers[i] = "X"
417
418 if color_by == "variables":
419 if data["array"] is self:
420 node_colors[i] = root_color
421 elif data["variable"]:
422 node_colors[i] = var_color
423 else:
424 node_colors[i] = const_color
425
426 elif color_by == "function":
427 if data["array"].fn_name in colors:
428 node_colors[i] = colors[data["array"].fn_name]
429 else:
430 node_colors[i] = hash_to_color(data["array"].fn_name)
431
432 elif color_by == "id":
433 node_colors[i] = hash_to_color(str(id(data["array"])))
434
435 # set node size
436 node_sizes[i] = 6 * node_scale * (np.log2(data["array"].size) + 1)
437
438 # set node label and marker
439 if data["array"].fn_name == "None":
440 node_markers.setdefault(i, "o")
441 node_labels[i] = ""
442 if data["array"].fn_name == "getitem":
443 node_markers.setdefault(i, ".")
444 node_labels[i] = ""
445 else:
446 node_labels[i] = data["array"].fn_name
447
448 node_markers.setdefault(i, node_shape)
449
450 if initial_layout == "layers":
451 for layer, nodes in enumerate(nx.topological_generations(G)):
452 for node in nodes:
453 G.nodes[node]["layer"] = layer
454
455 layout_opts.setdefault("subset_key", "layer")
456 layout_opts.setdefault("align", "vertical")
457
458 if layout_opts["align"] == "horizontal":
459 layout_opts.setdefault("flatten", 2)
460 else:
461 layout_opts.setdefault("flatten", 0.5)
462 layout = "multipartite"
463
464 elif initial_layout == "spiral":
465 layout_opts.setdefault("equidistant", True)
466
467 pos = get_nice_pos(
468 G,
469 dim=dim,
470 layout=layout,
471 initial_layout=initial_layout,
472 iterations=iterations,
473 k=k,
474 use_forceatlas2=use_forceatlas2,
475 **layout_opts,
476 )
477
478 # draw edges!
479 nx.draw_networkx_edges(
480 G,
481 pos=pos,
482 ax=ax,
483 edge_color=edge_color,
484 alpha=edge_alpha,
485 connectionstyle=connectionstyle,
486 arrowsize=arrowsize,
487 arrows=True,
488 )
489 # draw nodes!
490 for node in G.nodes:
491 ax.scatter(
492 *pos[node],
493 s=node_sizes[node],
494 facecolor=node_colors[node],
495 alpha=node_alpha,
496 marker=node_markers[node],
497 )
498 if show_labels:
499 # draw labels!
500 text = nx.draw_networkx_labels(
501 G,
502 pos=pos,
503 ax=ax,
504 labels=node_labels,
505 font_color=label_color,
506 font_size=font_size,
507 alpha=label_alpha,
508 bbox={"color": (0, 0, 0, 0)},
509 )
510 for _, t in text.items():
511 t.set_rotation(label_rotation)
512
513 if (fig is not None) and show_and_close:
514 plt.show()
515 plt.close(fig)
516
517 return fig, ax
518
519
520 def plot_circuit(
521 self,
522 color_by="function",
523 colors=None,
524 layout="balanced",
525 linewidth=None,
526 linewidth_scale=1,
527 linealpha=1.0,
528 fontsize=None,
529 fontsize_scale=1,
530 figsize=None,
531 ax=None,
532 show_and_close=True,
533 ):
534 import matplotlib as mpl
535 import matplotlib.pyplot as plt
536
537 if color_by not in ("id", "function"):
538 raise ValueError("color_by must be 'id' or 'function'")
539 if layout not in ("balanced", "compact", "wide"):
540 raise ValueError("layout must be 'balanced', 'compact', or 'wide'")
541
542 colors = get_default_colors_dict(colors)
543
544 nodes = list(self.ascend())
545 steps = {node: i for i, node in enumerate(nodes)}
546 rails = {self: 0}
547 edges = []
548 active = {0}
549
550 for node in reversed(nodes):
551 if color_by == "function":
552 if node.fn_name in colors:
553 c = colors[node.fn_name]
554 else:
555 c = hash_to_color(node.fn_name)
556 else:
557 c = hash_to_color(str(id(node)))
558 colors[node] = c
559
560 # free up the column
561 active.remove(rails[node])
562
563 # want to plot in same order the computational graph was created
564 deps = sorted(node.deps, key=lambda x: -x.depth)
565
566 # get the 'nearest columns' that are available for children
567 close_rails = (
568 c for c in count_around(rails[node], layout) if c not in active
569 )
570 child_rails = (next(close_rails) for c in deps if c not in rails)
571
572 for child in deps:
573 if child not in rails:
574 # place the node
575 rails[child] = next(child_rails)
576 active.add(rails[child])
577 # add connector
578 edges.append((node, child))
579
580 created_fig = ax is None
581 if created_fig:
582 if figsize is None:
583 w = h = (len(nodes) + 1) ** (2 / 3)
584 figsize = (w, h)
585 fig, ax = plt.subplots(figsize=figsize)
586 fig.set_facecolor((0, 0, 0, 0))
587 ax.axis("off")
588 ax.set_aspect("equal")
589
590 if linewidth is None:
591 linewidth = linewidth_scale * 8 * (figsize[1] / len(nodes))
592 if fontsize is None:
593 fontsize = fontsize_scale * 40 * (figsize[1] / len(nodes))
594
595 # draw the edges
596 for a, b in edges:
597 xya = steps[a], rails[a]
598 xyb = steps[b], rails[b]
599
600 if b.fn_name == "getitem":
601 color = colors[b.deps[0]]
602 else:
603 color = colors[b]
604
605 path_opts = dict(
606 edgecolor=color,
607 linewidth=linewidth,
608 alpha=linealpha,
609 facecolor="none",
610 zorder=9,
611 )
612
613 if xya[1] == xyb[1]:
614 # straight line
615 xy = (xya[0], xyb[0])
616 patch = mpl.patches.PathPatch(
617 mpl.path.Path(
618 [xya, xyb], [mpl.path.Path.MOVETO, mpl.path.Path.LINETO]
619 ),
620 **path_opts,
621 )
622 else:
623 # right angle line
624 patch = mpl.patches.PathPatch(
625 mpl.path.Path(
626 [
627 xya,
628 (xya[0], xyb[1] + 0.25 * (-1) ** (xya[1] < xyb[1])),
629 (xya[0] - 0.25, xyb[1]),
630 xyb,
631 ],
632 [mpl.path.Path.MOVETO] + [mpl.path.Path.LINETO] * 3,
633 ),
634 **path_opts,
635 )
636 ax.add_patch(patch)
637
638 # draw the nodes, and figure out plot range
639 xmin = ymin = float("inf")
640 xmax = ymax = float("-inf")
641 for node in nodes:
642 xy = steps[node], rails[node]
643 xmin, xmax = min(xmin, xy[0]), max(xmax, xy[0])
644 ymin, ymax = min(ymin, xy[1]), max(ymax, xy[1])
645 if not node.deps:
646 # make a square patch centered at xy with radius 0.4
647 patch = mpl.patches.Circle(
648 xy=xy, radius=0.4, color=colors[node], zorder=10
649 )
650 elif node.fn_name == "getitem":
651 # make a small circle for getitem (since not really a node)
652 patch = mpl.patches.Circle(
653 xy=xy, radius=0.15, color=colors[node.deps[0]], zorder=10
654 )
655 else:
656 # make a 'rotated house' shape
657 patch = mpl.patches.Polygon(
658 rotated_house_shape(xy, r=0.3), color=colors[node], zorder=10
659 )
660 ax.add_patch(patch)
661
662 # draw the labels
663 for node in nodes:
664 name = "←" if node.fn_name == "None" else node.fn_name
665 color = colors[node]
666 ax.text(
667 steps[node] - 0.25,
668 ymax + 1.0,
669 f"{name}{list(node.shape)}",
670 ha="left",
671 va="bottom",
672 color=color,
673 fontsize=fontsize,
674 rotation=45,
675 )
676 ax.plot(
677 [steps[node], steps[node]],
678 [ymax + 1, rails[node]],
679 color=color,
680 linewidth=linewidth / 2,
681 alpha=0.25,
682 linestyle=":",
683 clip_on=False,
684 )
685
686 # set plot limits
687 ax.set_xlim(xmin - 0.5, xmax + 0.5)
688 ax.set_ylim(ymin - 0.5, ymax + 0.5)
689
690 if (fig is not None) and show_and_close:
691 plt.show()
692 plt.close(fig)
693
694 return fig, ax
695
696
697 # a style to use for matplotlib that works with light and dark backgrounds
698 NEUTRAL_STYLE = {
699 "axes.edgecolor": (0.5, 0.5, 0.5),
700 "axes.facecolor": (0, 0, 0, 0),
701 "axes.grid": True,
702 "axes.labelcolor": (0.5, 0.5, 0.5),
703 "axes.spines.right": False,
704 "axes.spines.top": False,
705 "figure.facecolor": (0, 0, 0, 0),
706 "grid.alpha": 0.1,
707 "grid.color": (0.5, 0.5, 0.5),
708 "legend.frameon": False,
709 "text.color": (0.5, 0.5, 0.5),
710 "xtick.color": (0.5, 0.5, 0.5),
711 "xtick.minor.visible": True,
712 "ytick.color": (0.5, 0.5, 0.5),
713 "ytick.minor.visible": True,
714 }
715
716
717 def default_to_neutral_style(fn):
718 """Wrap a function or method to use the neutral style by default."""
719
720 @functools.wraps(fn)
721 def wrapper(*args, style="neutral", **kwargs):
722 import matplotlib.pyplot as plt
723
724 if style == "neutral":
725 style = NEUTRAL_STYLE
726 elif not style:
727 style = {}
728
729 with plt.style.context(style):
730 return fn(*args, **kwargs)
731
732 return wrapper
733
734
735 @default_to_neutral_style
736 def plot_history_size_footprint(
737 self,
738 log=None,
739 figsize=(8, 2),
740 color="purple",
741 alpha=0.5,
742 rasterize=4096,
743 rasterize_dpi=300,
744 ax=None,
745 show_and_close=True,
746 ):
747 """Plot the memory footprint throughout this computation.
748
749 Parameters
750 ----------
751 log : None or int, optional
752 If not None, display the sizes in base ``log``.
753 figsize : tuple, optional
754 Size of the figure.
755 color : str, optional
756 Color of the line.
757 alpha : float, optional
758 Alpha of the line.
759 ax : matplotlib.axes.Axes, optional
760 Axes to plot on, will be created if not provided.
761 return_fig : bool, optional
762 If True, return the figure object, else just show and close it.
763 """
764 import numpy as np
765 import matplotlib.pyplot as plt
766
767 y = np.array(self.history_size_footprint())
768 if log:
769 y = np.log2(y) / np.log2(log)
770 ylabel = f"$\\log_{log}[total size]$"
771 else:
772 ylabel = "total size"
773
774 x = np.arange(y.size)
775
776 if ax is None:
777 fig, ax = plt.subplots(figsize=figsize)
778 fig.set_dpi(rasterize_dpi)
779 else:
780 fig = None
781
782 if isinstance(rasterize, (float, int)):
783 # only turn on above a certain size
784 rasterize = y.size > rasterize
785
786 if rasterize:
787 ax.set_rasterization_zorder(0)
788
789 ax.fill_between(x, 0, y, alpha=alpha, color=color, zorder=-1)
790
791 if fig is not None:
792 ax.grid(True, c=(0.95, 0.95, 0.95), which="both")
793 ax.set_axisbelow(True)
794 ax.set_xlim(0, np.max(x))
795 ax.set_ylim(0, np.max(y))
796 ax.set_ylabel(ylabel)
797
798 if (fig is not None) and show_and_close:
799 plt.show()
800 plt.close(fig)
801
802 return fig, ax
803
804
805 @default_to_neutral_style
806 def plot_history_functions(
807 self,
808 *,
809 fn=None,
810 log=None,
811 colors=None,
812 kind="scatter",
813 scatter_size=5,
814 scatter_marker="s",
815 lines_width=5,
816 image_alpha_pow=2 / 3,
817 image_aspect=4,
818 legend=True,
819 legend_ncol=None,
820 legend_bbox_to_anchor=None,
821 legend_loc=None,
822 rasterize=4096,
823 rasterize_dpi=300,
824 ax=None,
825 figsize=(8, 2),
826 show_and_close=True,
827 ):
828 """Plot the functions used throughout this computation, color coded, as
829 either a scatter plot or an image, showing the size of the that individual
830 intermediate as well.
831 """
832 import numpy as np
833 import matplotlib as mpl
834 import matplotlib.pyplot as plt
835
836 if fn is not None:
837 ylabel = "custom"
838 else:
839 ylabel = "node size"
840
841 def fn(node):
842 return node.size
843
844 if log:
845 # wrap the function to take log of values
846 ylabel = f"$\\log_{{{log}}}[{ylabel}]$"
847 orig_fn = fn
848
849 def fn(node):
850 return np.log2(orig_fn(node)) / np.log2(log)
851
852 colors = get_default_colors_dict(colors)
853
854 xs = []
855 ys = []
856 cs = []
857 ymax = 0
858 for i, node in enumerate(self.ascend()):
859 xs.append(i)
860 y = fn(node)
861 ymax = max(ymax, y)
862 ys.append(y)
863 try:
864 c = colors[node.fn_name]
865 except KeyError:
866 c = colors[node.fn_name] = hash_to_color(node.fn_name)
867 cs.append(c)
868
869 if ax is None:
870 fig, ax = plt.subplots(figsize=figsize)
871 fig.set_dpi(rasterize_dpi)
872 ax.set_ylabel(ylabel)
873 else:
874 fig = None
875
876 if isinstance(rasterize, (float, int)):
877 # only turn on above a certain size
878 rasterize = len(xs) > rasterize
879
880 if rasterize:
881 ax.set_rasterization_zorder(0)
882
883 if kind == "scatter":
884 ax.scatter(
885 xs,
886 ys,
887 c=cs,
888 s=scatter_size,
889 marker=scatter_marker,
890 rasterized=rasterize,
891 )
892
893 elif kind == "lines":
894 lns = [((x, 0.0), (x, y)) for x, y in zip(xs, ys)]
895 ax.add_collection(
896 mpl.collections.LineCollection(
897 lns,
898 colors=cs,
899 zorder=-1,
900 lw=lines_width,
901 )
902 )
903 ax.set_xlim(-0.5, len(lns) + 0.5)
904 ax.set_ylim(0, 1.05 * ymax)
905
906 elif kind == "image":
907 ax.axis("off")
908 ys = np.array(ys)
909 ys = (ys / ys.max()).reshape(-1, 1) ** image_alpha_pow
910 N = len(cs)
911 da = round((N / image_aspect) ** 0.5)
912 db = N // da
913 while da * db < N:
914 db += 1
915 Ns = da * db
916 img = np.concatenate([cs, ys], axis=1)
917 img = np.concatenate([img, np.tile(0.0, (Ns - N, 4))], axis=0)
918 img = img.reshape(da, db, 4)
919 ax.imshow(img, zorder=-1)
920
921 if legend:
922 legend_items = [
923 mpl.patches.Patch(facecolor=c, label=fn_name)
924 for fn_name, c in colors.items()
925 ]
926
927 if legend_ncol is None:
928 legend_ncol = max(1, round(len(legend_items) / 6))
929 if legend_bbox_to_anchor is None:
930 legend_bbox_to_anchor = (1.0, 1.0)
931 if legend_loc is None:
932 legend_loc = "upper left"
933
934 ax.legend(
935 handles=legend_items,
936 ncol=legend_ncol,
937 bbox_to_anchor=legend_bbox_to_anchor,
938 loc=legend_loc,
939 )
940
941 if (fig is not None) and show_and_close:
942 plt.show()
943 plt.close(fig)
944
945 return fig, ax
946
947
948 @default_to_neutral_style
949 def plot_history_stats(
950 self,
951 *,
952 fn="count",
953 colors=None,
954 rasterize_dpi=300,
955 ax=None,
956 figsize=(2, 2),
957 show_and_close=True,
958 ):
959 from matplotlib import pyplot as plt
960
961 stats = self.history_stats(fn)
962
963 colors = get_default_colors_dict(colors)
964
965 if ax is None:
966 fig, ax = plt.subplots(figsize=figsize)
967 fig.set_dpi(rasterize_dpi)
968 else:
969 fig = None
970
971 xs, labels, clrs = [], [], []
972
973 for fn_name, cnt in sorted(stats.items(), key=lambda x: -x[1]):
974 xs.append(cnt)
975 labels.append(f"{fn_name}: {cnt}")
976 try:
977 color = colors[fn_name]
978 except KeyError:
979 color = colors[fn_name] = hash_to_color(fn_name)
980 clrs.append(color)
981
982 ax.pie(x=xs, labels=labels, colors=clrs)
983
984 if (fig is not None) and show_and_close:
985 plt.show()
986 plt.close(fig)
987
988 return fig, ax