Repository navigation
Expand file tree
/
Copy pathplotly_utils.py
More file actions
234 lines (217 loc) · 12.1 KB
/
Copy pathplotly_utils.py
File metadata and controls
234 lines (217 loc) · 12.1 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
import torch as t
from typing import List, Union
import plotly.express as px
import plotly.graph_objects as go
from plotly.subplots import make_subplots
import numpy as np
import re
from transformer_lens.utils import to_numpy
from typing import List
# GENERIC PLOTTING FUNCTIONS
update_layout_set = {"xaxis_range", "yaxis_range", "hovermode", "xaxis_title", "yaxis_title", "colorbar", "colorscale", "coloraxis", "title_x", "bargap", "bargroupgap", "xaxis_tickformat", "yaxis_tickformat", "title_y", "legend_title_text", "xaxis_showgrid", "xaxis_gridwidth", "xaxis_gridcolor", "yaxis_showgrid", "yaxis_gridwidth", "yaxis_gridcolor", "showlegend", "xaxis_tickmode", "yaxis_tickmode", "margin", "xaxis_visible", "yaxis_visible", "bargap", "bargroupgap", "coloraxis_showscale", "xaxis_tickangle", "yaxis2_range"}
def imshow(tensor: t.Tensor, renderer=None, **kwargs):
kwargs_post = {k: v for k, v in kwargs.items() if k in update_layout_set}
kwargs_pre = {k: v for k, v in kwargs.items() if k not in update_layout_set}
facet_labels = kwargs_pre.pop("facet_labels", None)
border = kwargs_pre.pop("border", False)
return_fig = kwargs_pre.pop("return_fig", False)
text = kwargs_pre.pop("text", None)
xaxis_tickangle = kwargs_post.pop("xaxis_tickangle", None)
static = kwargs_pre.pop("static", False)
if "color_continuous_scale" not in kwargs_pre:
kwargs_pre["color_continuous_scale"] = "RdBu"
if "color_continuous_midpoint" not in kwargs_pre:
kwargs_pre["color_continuous_midpoint"] = 0.0
if "margin" in kwargs_post and isinstance(kwargs_post["margin"], int):
kwargs_post["margin"] = dict.fromkeys(list("tblr"), kwargs_post["margin"])
fig = px.imshow(to_numpy(tensor), **kwargs_pre).update_layout(**kwargs_post)
if facet_labels:
# Weird thing where facet col wrap means labels are in wrong order
if "facet_col_wrap" in kwargs_pre:
facet_labels = reorder_list_in_plotly_way(facet_labels, kwargs_pre["facet_col_wrap"])
for i, label in enumerate(facet_labels):
fig.layout.annotations[i]['text'] = label
if border:
fig.update_xaxes(showline=True, linewidth=1, linecolor='black', mirror=True)
fig.update_yaxes(showline=True, linewidth=1, linecolor='black', mirror=True)
if text:
if tensor.ndim == 2:
# if 2D, then we assume text is a list of lists of strings
assert isinstance(text[0], list)
assert isinstance(text[0][0], str)
text = [text]
else:
# if 3D, then text is either repeated for each facet, or different
assert isinstance(text[0], list)
if isinstance(text[0][0], str):
text = [text for _ in range(len(fig.data))]
for i, _text in enumerate(text):
fig.data[i].update(
text=_text,
texttemplate="%{text}",
textfont={"size": 12}
)
# Very hacky way of fixing the fact that updating layout with new tickangle only applies to first facet by default
if xaxis_tickangle is not None:
n_facets = 1 if tensor.ndim == 2 else tensor.shape[0]
for i in range(1, 1+n_facets):
xaxis_name = "xaxis" if i == 1 else f"xaxis{i}"
fig.layout[xaxis_name]["tickangle"] = xaxis_tickangle
return fig if return_fig else fig.show(renderer=renderer, cfg={"staticPlot": static})
def reorder_list_in_plotly_way(L: list, col_wrap: int):
'''
Helper function, because Plotly orders figures in an annoying way when there's column wrap.
'''
L_new = []
while len(L) > 0:
L_new.extend(L[-col_wrap:])
L = L[:-col_wrap]
return L_new
def line(y: Union[t.Tensor, List[t.Tensor]], renderer=None, **kwargs):
'''
Edit to this helper function, allowing it to take args in update_layout (e.g. yaxis_range).
'''
kwargs_post = {k: v for k, v in kwargs.items() if k in update_layout_set}
kwargs_pre = {k: v for k, v in kwargs.items() if k not in update_layout_set}
return_fig = kwargs_pre.pop("return_fig", False)
if "margin" in kwargs_post and isinstance(kwargs_post["margin"], int):
kwargs_post["margin"] = dict.fromkeys(list("tblr"), kwargs_post["margin"])
if "xaxis_tickvals" in kwargs_pre:
tickvals = kwargs_pre.pop("xaxis_tickvals")
kwargs_post["xaxis"] = dict(
tickmode = "array",
tickvals = kwargs_pre.get("x", np.arange(len(tickvals))),
ticktext = tickvals
)
if "hovermode" not in kwargs_post:
kwargs_post["hovermode"] = "x unified"
if "use_secondary_yaxis" in kwargs_pre and kwargs_pre["use_secondary_yaxis"]:
del kwargs_pre["use_secondary_yaxis"]
if "labels" in kwargs_pre:
labels: dict = kwargs_pre.pop("labels")
kwargs_post["yaxis_title_text"] = labels.get("y1", None)
kwargs_post["yaxis2_title_text"] = labels.get("y2", None)
kwargs_post["xaxis_title_text"] = labels.get("x", None)
for k in ["title", "template", "width", "height"]:
if k in kwargs_pre:
kwargs_post[k] = kwargs_pre.pop(k)
fig = make_subplots(specs=[[{"secondary_y": True}]]).update_layout(**kwargs_post)
y0 = to_numpy(y[0])
y1 = to_numpy(y[1])
x0, x1 = kwargs_pre.pop("x", [np.arange(len(y0)), np.arange(len(y1))])
name0, name1 = kwargs_pre.pop("names", ["yaxis1", "yaxis2"])
fig.add_trace(go.Scatter(y=y0, x=x0, name=name0), secondary_y=False)
fig.add_trace(go.Scatter(y=y1, x=x1, name=name1), secondary_y=True)
else:
y = list(map(to_numpy, y)) if isinstance(y, list) and not (isinstance(y[0], int) or isinstance(y[0], float)) else to_numpy(y)
names = kwargs_pre.pop("names", None)
fig = px.line(y=y, **kwargs_pre).update_layout(**kwargs_post)
if names is not None:
fig.for_each_trace(lambda trace: trace.update(name=names.pop(0)))
return fig if return_fig else fig.show(renderer=renderer)
def scatter(x, y, renderer=None, **kwargs):
x = to_numpy(x)
y = to_numpy(y)
add_line = None
if "add_line" in kwargs:
add_line = kwargs.pop("add_line")
kwargs_post = {k: v for k, v in kwargs.items() if k in update_layout_set}
kwargs_pre = {k: v for k, v in kwargs.items() if k not in update_layout_set}
return_fig = kwargs_pre.pop("return_fig", False)
facet_labels = kwargs_pre.pop("facet_labels", None)
if "margin" in kwargs_post and isinstance(kwargs_post["margin"], int):
kwargs_post["margin"] = dict.fromkeys(list("tblr"), kwargs_post["margin"])
fig = px.scatter(y=y, x=x, **kwargs_pre).update_layout(**kwargs_post)
if add_line is not None:
xrange = fig.layout.xaxis.range or [x.min(), x.max()]
yrange = fig.layout.yaxis.range or [y.min(), y.max()]
add_line = add_line.replace(" ", "")
if add_line in ["x=y", "y=x"]:
fig.add_trace(go.Scatter(mode='lines', x=xrange, y=xrange, showlegend=False))
elif re.match("(x|y)=", add_line):
try: c = float(add_line.split("=")[1])
except: raise ValueError(f"Unrecognized add_line: {add_line}. Please use either 'x=y' or 'x=c' or 'y=c' for some float c.")
x, y = ([c, c], yrange) if add_line[0] == "x" else (xrange, [c, c])
fig.add_trace(go.Scatter(mode='lines', x=x, y=y, showlegend=False))
else:
raise ValueError(f"Unrecognized add_line: {add_line}. Please use either 'x=y' or 'x=c' or 'y=c' for some float c.")
if facet_labels:
for i, label in enumerate(facet_labels):
fig.layout.annotations[i]['text'] = label
return fig if return_fig else fig.show(renderer=renderer)
def bar(tensor, renderer=None, **kwargs):
'''
'''
if isinstance(tensor, list):
if isinstance(tensor[0], t.Tensor): arr = [to_numpy(tn) for tn in tensor]
elif isinstance(tensor[0], list): arr = [np.array(tn) for tn in tensor]
else: arr = np.array(tensor)
else:
arr = to_numpy(tensor)
kwargs_post = {k: v for k, v in kwargs.items() if k in update_layout_set}
kwargs_pre = {k: v for k, v in kwargs.items() if k not in update_layout_set}
return_fig = kwargs_pre.pop("return_fig", False)
names = kwargs_pre.pop("names", None)
if "hovermode" not in kwargs_post:
kwargs_post["hovermode"] = "x unified"
if "margin" in kwargs_post and isinstance(kwargs_post["margin"], int):
kwargs_post["margin"] = dict.fromkeys(list("tblr"), kwargs_post["margin"])
fig = px.bar(y=arr, **kwargs_pre).update_layout(**kwargs_post)
if names is not None:
for i in range(len(fig.data)):
fig.data[i]["name"] = names[i // 2 if "marginal" in kwargs_pre else i]
return fig if return_fig else fig.show(renderer=renderer)
def hist(tensor, renderer=None, **kwargs):
kwargs_post = {k: v for k, v in kwargs.items() if k in update_layout_set}
kwargs_pre = {k: v for k, v in kwargs.items() if k not in update_layout_set}
draw = kwargs_pre.pop("draw", True)
static = kwargs_pre.pop("static", False)
return_fig = kwargs_pre.pop("return_fig", False)
if isinstance(tensor, list):
if isinstance(tensor[0], t.Tensor): arr = [to_numpy(tn) for tn in tensor]
elif isinstance(tensor[0], list): arr = [np.array(tn) for tn in tensor]
else: arr = np.array(tensor)
else:
arr = to_numpy(tensor)
if "modebar_add" not in kwargs_post:
kwargs_post["modebar_add"] = ['drawline', 'drawopenpath', 'drawclosedpath', 'drawcircle', 'drawrect', 'eraseshape']
add_mean_line = kwargs_pre.pop("add_mean_line", False)
names = kwargs_pre.pop("names", None)
if "barmode" not in kwargs_post:
kwargs_post["barmode"] = "overlay"
if "bargap" not in kwargs_post:
kwargs_post["bargap"] = 0.0
if "margin" in kwargs_post and isinstance(kwargs_post["margin"], int):
kwargs_post["margin"] = dict.fromkeys(list("tblr"), kwargs_post["margin"])
if "hovermode" not in kwargs_post:
kwargs_post["hovermode"] = "x unified"
if "autosize" not in kwargs_post:
kwargs_post["autosize"] = False
# If `arr` has a list of arrays, then just doing px.histogram doesn't work annoyingly enough
# This is janky, even for my functions!
if isinstance(arr, list) and isinstance(arr[0], np.ndarray):
assert "marginal" not in kwargs_pre, "Can't use `marginal` with a list of arrays"
for thing_to_move_from_pre_to_post in ["title", "template", "height", "width", "labels"]:
if thing_to_move_from_pre_to_post in kwargs_pre:
kwargs_post[thing_to_move_from_pre_to_post] = kwargs_pre.pop(thing_to_move_from_pre_to_post)
if "labels" in kwargs_post:
kwargs_post["xaxis_title_text"] = kwargs_post["labels"].get("x", "")
kwargs_post["yaxis_title_text"] = kwargs_post["labels"].get("y", "")
del kwargs_post["labels"]
fig = go.Figure(layout=go.Layout(**kwargs_post))
if "nbins" in kwargs_pre:
kwargs_pre["nbinsx"] = int(kwargs_pre.pop("nbins"))
for x in arr:
fig.add_trace(go.Histogram(x=x, name=names.pop(0) if names is not None else None, **kwargs_pre))
else:
fig = px.histogram(x=arr, **kwargs_pre).update_layout(**kwargs_post)
if names is not None:
for i in range(len(fig.data)):
fig.data[i]["name"] = names[i // 2 if "marginal" in kwargs_pre else i]
if add_mean_line:
if arr.ndim == 1:
fig.add_vline(x=arr.mean(), line_width=3, line_dash="dash", line_color="black", annotation_text=f"Mean = {arr.mean():.3f}", annotation_position="top")
elif arr.ndim == 2:
for i in range(arr.shape[0]):
fig.add_vline(x=arr[i].mean(), line_width=3, line_dash="dash", line_color="black", annotation_text=f"Mean = {arr.mean():.3f}", annotation_position="top")
return fig if return_fig else fig.show(renderer=renderer)