Repository navigation
Expand file tree
/
Copy path_vis.py
More file actions
318 lines (281 loc) · 9.45 KB
/
Copy path_vis.py
File metadata and controls
318 lines (281 loc) · 9.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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
from __future__ import annotations
# TODO: heatmap of docs in topics
# TODO: topic dynamics in time
# TODO: word cloud
__all__ = ["plot_docs", "plot_scatter_topics", "plot_terms"]
from collections.abc import Sequence
from typing import Optional, Union
from altair import (
AxisConfig,
Chart,
Color,
LayerChart,
Legend,
Scale,
Size,
Text,
X,
Y,
value,
)
from IPython.display import HTML
from numpy import ndarray
from pandas import DataFrame, option_context
def plot_scatter_topics(
topics_coords: Union[ndarray, DataFrame],
x_col: str = "x",
y_col: str = "y",
topic: Optional[int] = None,
size_col: Optional[str] = None,
label_col: Optional[str] = None,
# Accepted but unused: the scatter colours by topic index. Kept because
# dropping it would break callers passing it positionally or by name.
color_col: Optional[str] = None, # noqa: ARG001
topic_col: Optional[str] = None,
font_size: int = 13,
x_kws: Optional[dict] = None,
y_kws: Optional[dict] = None,
chart_kws: Optional[dict] = None,
circle_kws: Optional[dict] = None,
circle_enc_kws: Optional[dict] = None,
text_kws: Optional[dict] = None,
text_enc_kws: Optional[dict] = None,
size_kws: Optional[dict] = None,
color_kws: Optional[dict] = None,
) -> LayerChart:
"""Topics scatter plot in 2D.
Parameters
----------
topics_coords : Union[ndarray, DataFrame]
Topics scatter coordinates.
x_col : str, optional
X column name.
y_col : str, optional
Y column name.
topic : int, optional
Topic index.
size_col : str, optional
Column with size values.
label_col : str, optional
Column with topic labels.
color_col : str, optional
Column with colors.
topic_col : str, optional
Column with topics texts.
font_size : int, optional
Font size.
x_kws : dict, optional
Keyword arguments passed to :py:meth:`altair.X()`.
y_kws : dict, optional
Keyword arguments passed to :py:meth:`altair.Y()`.
chart_kws : dict, optional
Keyword arguments passed to :py:meth:`altair.Chart()`.
circle_kws : dict, optional
Keyword arguments passed to :py:meth:`altair.Chart.mark_circle()`.
circle_enc_kws : dict, optional
Keyword arguments passed to :py:meth:`altair.Chart.encode()`
for circle elements.
text_kws : dict, optional
Keyword arguments passed to :py:meth:`altair.Chart.mark_text()`.
text_enc_kws : dict, optional
Keyword arguments passed to :py:meth:`altair.Chart.encode()`
for text elements.
size_kws : dict, optional
Keyword arguments passed to :py:meth:`altair.Size()` for
circle elements.
color_kws : dict, optional
Keyword arguments passed to :py:meth:`altair.Color()` for
circle elements.
Returns
-------
altair.Chart
Topics scatter plot.
"""
if not chart_kws:
chart_kws = {}
if not x_kws:
x_kws = {"shorthand": x_col, "axis": None}
if not y_kws:
y_kws = {"shorthand": y_col, "axis": None}
if not circle_kws:
circle_kws = {"opacity": 0.33, "stroke": "black", "strokeWidth": 1}
if not size_kws:
size_kws = {
"title": "Marginal topic distribution",
"scale": Scale(range=[0, 3000]),
}
if not text_kws:
text_kws = {"align": "center", "baseline": "middle"}
# Input validation
if isinstance(topics_coords, ndarray):
if topics_coords.size == 0:
raise ValueError("topics_coords cannot be empty")
data = DataFrame(topics_coords, columns=[x_col, y_col])
else:
if topics_coords.empty:
raise ValueError("topics_coords DataFrame cannot be empty")
data = topics_coords.copy()
if size_col and size_col not in data:
raise ValueError(f"size column {size_col!r} is missing")
if label_col and label_col not in data:
raise ValueError(f"label column {label_col!r} is missing")
if not circle_enc_kws:
circle_enc_kws = {
"x": X(**x_kws),
"y": Y(**y_kws),
"size": Size(size_col, **size_kws)
if size_col and not data[size_col].isna().any()
else value(500),
}
if not topic_col:
topic_col = "topic"
data = data.assign(**{topic_col: range(len(topics_coords))})
# Built after topic_col is resolved so the highlight targets the column
# actually present in the data.
if not color_kws:
color_kws = (
{}
if topic is None
else {
"condition": {
"test": f"datum[{topic_col!r}] == {topic}",
"value": "red",
}
}
)
if not text_enc_kws:
text_enc_kws = {
"x": X(**x_kws),
"y": Y(**y_kws),
"text": Text(topic_col),
"size": value(font_size),
}
# Tooltips initialization
tooltips = []
if label_col:
tooltips.append(label_col)
if size_col:
tooltips.append(size_col)
if tooltips:
circle_enc_kws.update({"tooltip": tooltips})
text_enc_kws.update({"tooltip": tooltips})
if color_kws:
circle_enc_kws.update({"color": Color(**color_kws)})
base = Chart(data, **chart_kws)
rule = base.mark_rule().encode(y="average(y)", color=value("gray"), size=value(0.2))
rule2 = base.mark_rule().encode(
x="average(x)", color=value("gray"), size=value(0.2)
)
points = base.mark_circle(**circle_kws).encode(**circle_enc_kws)
text = base.mark_text(**text_kws).encode(**text_enc_kws)
return (
(rule + rule2 + points + text)
.configure_axis(labelFontSize=font_size, titleFontSize=font_size, grid=False)
.configure(axis=AxisConfig(disable=True))
.configure_view(stroke="transparent", strokeWidth=0)
.configure_legend(
orient="bottom", labelFontSize=font_size, titleFontSize=font_size
)
)
def plot_terms(
terms_probs: DataFrame,
x_col: str = "Probability",
y_col: str = "Terms",
color_col: str = "Type",
font_size: int = 13,
chart_kws: Optional[dict] = None,
bar_kws: Optional[dict] = None,
x_kws: Optional[dict] = None,
y_kws: Optional[dict] = None,
color_kws: Optional[dict] = None,
) -> Chart:
"""Plot words conditional and marginal probabilities.
Parameters
----------
terms_probs : DataFrame
Words probabilities.
x_col : str, optional
X column name.
y_col : str, optional
Y column name.
color_col : str, optional
Column with values types (for coloring).
font_size : int, optional
Font size.
chart_kws : dict, optional
Keyword arguments passed to :py:meth:`altair.Chart()`.
bar_kws : dict, optional
Keyword arguments passed to :py:meth:`altair.Chart.mark_bar()`.
x_kws : dict, optional
Keyword arguments passed to :py:meth:`altair.X()`.
y_kws : dict, optional
Keyword arguments passed to :py:meth:`altair.Y()`.
color_kws : dict, optional
Keyword arguments passed to :py:meth:`altair.Color()`.
Returns
-------
altair.Chart
Terms probabilities chart.
"""
# Input validation
if terms_probs.empty:
raise ValueError("terms_probs DataFrame cannot be empty")
required_cols = [x_col, y_col, color_col]
missing_cols = [col for col in required_cols if col not in terms_probs.columns]
if missing_cols:
raise ValueError(f"Missing required columns: {missing_cols}")
if not x_kws:
x_kws = {"stack": None}
if not y_kws:
y_kws = {"sort": None, "title": None}
if not color_kws:
color_kws = {
"shorthand": color_col,
"legend": Legend(orient="bottom"),
"scale": Scale(scheme="category20"),
}
if not chart_kws:
chart_kws = {}
if not bar_kws:
bar_kws = {}
return (
Chart(data=terms_probs, **chart_kws)
.mark_bar(**bar_kws)
.encode(x=X(x_col, **x_kws), y=Y(y_col, **y_kws), color=Color(**color_kws))
.configure_axis(labelFontSize=font_size, titleFontSize=font_size)
.configure_legend(
labelFontSize=font_size, titleFontSize=font_size, columns=1, labelLimit=250
)
)
def plot_docs(
docs: Union[Sequence[str], DataFrame],
styles: Optional[str] = None,
html_kws: Optional[dict] = None,
) -> HTML:
"""Documents plotting functionality for report interface.
Parameters
----------
docs : Union[Sequence[str], DataFrame]
Documents.
styles : str, optional
Styles string for formatting the table with documents.
Concatenated with HTML.
html_kws : dict, optional
Keyword arguments passed to :py:meth:`pandas.DataFrame.to_html` method.
Returns
-------
ipywidgets.HTML
Topic documents.
"""
if styles is None:
styles = (
"<style>table td{text-align: left !important}"
+ "table th{text-align: center !important}</style>"
)
if html_kws is None:
# html_kws = {'classes': 'plot'}
html_kws = {}
df_docs = docs.copy() if isinstance(docs, DataFrame) else DataFrame({"docs": docs})
with option_context("display.max_colwidth", 0):
# df_docs.style.set_properties(**{'text-align': 'center'})
return HTML(styles + df_docs.to_html(**html_kws))