Repository navigation
Expand file tree
/
Copy path_report.py
More file actions
309 lines (282 loc) · 11.1 KB
/
Copy path_report.py
File metadata and controls
309 lines (282 loc) · 11.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
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
__all__ = ['prepare_coords', 'report']
import warnings
from typing import Optional, Sequence, List
from copy import deepcopy
from ipywidgets import widgets as wdg
from pandas import DataFrame
from ._distance import get_topics_dist, get_topics_scatter
from ._vis import plot_scatter_topics, plot_terms, plot_docs
from ._helpers import (
calc_terms_probs_ratio,
get_phi, get_theta,
get_top_docs)
warnings.filterwarnings("ignore", category=DeprecationWarning)
warnings.filterwarnings("ignore", category=FutureWarning)
def prepare_coords(
model: object,
labels: Optional[Sequence] = None,
dist_kws: dict = None,
scatter_kws: dict = None) -> DataFrame:
"""Prepare coordinates for topics scatter plot.
Parameters
----------
model : object
Topic model instance.
labels : Optional[Sequence]
Topics labels.
dist_kws : dict, optional
Keyword arguments passed to :py:meth:`tmplot.get_topics_dist()`.
scatter_kws : dict, optional
Keyword arguments passed to :py:meth:`tmplot.get_topics_scatter()`.
"""
if not dist_kws:
dist_kws = {}
if not scatter_kws:
scatter_kws = {}
phi = get_phi(model)
theta = get_theta(model)
topics_dists = get_topics_dist(phi, **dist_kws)
topics_coords = get_topics_scatter(topics_dists, theta, **scatter_kws)
topics_coords['label'] = labels or theta.index
return topics_coords
def report(
model: object,
docs: Sequence[str],
*,
topics_labels: Optional[Sequence[str]] = None,
corpus: Optional[List] = None,
layout: wdg.Layout = None,
show_headers: bool = True,
show_docs: bool = True,
show_words: bool = True,
show_topics: bool = True,
topics_kws: dict = None,
height: int = 500,
width: int = 300,
coords_kws: dict = None,
words_kws: dict = None,
docs_kws: dict = None,
top_docs_kws: dict = None) -> wdg.VBox:
"""Interactive report interface.
Parameters
----------
model : object
Topic model instance.
docs : Sequence[str]
Documents.
topics_labels : Optional[Sequence[str]], optional
Topics labels.
corpus : Optional[List[str]], optional
Gensim corpus.
layout : wdg.Layout, optional
Interface layout instance.
show_headers : bool, optional
Show headers.
show_docs : bool, optional
Show documents widget.
show_words : bool, optional
Show words widget.
show_topics : bool, optional
Show topics scatter plot widget.
topics_kws : dict, optional
Keyword arguments passed to :py:meth:`tmplot.plot_scatter_topics()`.
coords_kws : dict, optional
Keyword arguments passed to :py:meth:`tmplot.prepare_coords()`.
words_kws : dict, optional
Keyword arguments passed to :py:meth:`tmplot.plot_terms()`.
docs_kws : dict, optional
Keyword arguments passed to :py:meth:`tmplot.plot_docs()`.
top_docs_kws : dict, optional
Keyword arguments passed to :py:meth:`tmplot.get_top_docs()`.
Returns
-------
ipywidgets.widgets.widget_box.VBox
Report interface as a VBox instance.
"""
from IPython.display import display
_topics_kws = {
'chart_kws': {'height': height, 'width': width}}\
if not topics_kws else deepcopy(topics_kws)
_coords_kws = {} if not coords_kws else deepcopy(coords_kws)
_words_kws = {
'chart_kws': {'height': height, 'width': width}}\
if not words_kws else deepcopy(words_kws)
_top_docs_kws = {} if not docs_kws else deepcopy(top_docs_kws)
_docs_kws = {} if not docs_kws else deepcopy(docs_kws)
# Headers init
topics_header = wdg.HTML('<b>Intertopic distance plot</b>')\
if show_headers and show_topics else None
words_header = wdg.HTML('<b>Relevant words (terms)</b>')\
if show_headers and show_words else None
docs_header = wdg.HTML('<b>Top documents in a topic</b>')\
if show_headers and show_docs else None
# Layout init
grid_cols = " ".join(['1fr'] * sum([show_docs, show_words, show_topics]))
layout = wdg.Layout(
grid_template_columns=grid_cols,
# justify_items='center'
)\
if not layout else layout
# Children widgets list init
children = []
if 'topics_coords' not in _topics_kws:
topics_coords = prepare_coords(model, **_coords_kws)
_topics_kws.update({
'topics_coords': topics_coords,
'label_col': 'label',
'size_col': 'size',
'topic': 0
})
if 'terms_probs' not in _words_kws:
phi = get_phi(model)
terms_probs = calc_terms_probs_ratio(phi, topic=0)
_words_kws.update({'terms_probs': terms_probs})
if 'docs' not in _docs_kws:
theta = get_theta(model, corpus=corpus).values
_top_docs_kws.update({
'docs': docs, 'theta': theta,
'topics': [0], 'docs_num': 2})
top_docs = get_top_docs(**_top_docs_kws)
top_docs.columns = ['']
_docs_kws.update({'docs': top_docs})
# Topic selection
def _on_select_topic(sel):
topic = sel['new']
topics_plot_output.clear_output(wait=False)
words_plot_output.clear_output(wait=False)
docs_plot_output.clear_output(wait=False)
with words_plot_output:
terms_probs = calc_terms_probs_ratio(
phi, topic=topic, lambda_=lambda_slider.value)
_words_kws.update({'terms_probs': terms_probs})
display(plot_terms(**_words_kws))
with topics_plot_output:
_topics_kws.update({'topic': topic})
display(plot_scatter_topics(**_topics_kws))
with docs_plot_output:
_top_docs_kws.update({'topics': [sel['new']]})
top_docs = get_top_docs(**_top_docs_kws)
top_docs.columns = ['']
_docs_kws.update({'docs': top_docs})
display(plot_docs(**_docs_kws))
topics_ids = list(range(len(_topics_kws['topics_coords'])))
topics_labels = topics_labels or topics_ids
select_topic = wdg.Dropdown(
options=list(zip(topics_labels, topics_ids)), value=0)
select_topic.observe(_on_select_topic, names='value')
select_topic_header = wdg.HTML('<b>Select a topic</b>:')
select_topic_widget = wdg.HBox([select_topic_header, select_topic])
select_topic_wrapper = wdg.VBox(
[select_topic_widget], layout={'align_items': 'center'})
# Topics scatter
def _on_select_topics_method(names):
topics_plot_output.clear_output(wait=False)
with topics_plot_output:
_coords_kws.update({'scatter_kws': {'method': names['new']}})
topics_coords = prepare_coords(model, **_coords_kws)
_topics_kws.update({
'topics_coords': topics_coords,
'topic': select_topic.value
})
display(plot_scatter_topics(**_topics_kws))
if show_topics:
topics_plot_children = [topics_header] if show_headers else []
options_methods = [
('TSNE', 'tsne'),
('Spectral Embedding', 'sem'),
('MDS', 'mds'),
('Locally Linear Embedding (Standard)', 'lle'),
('Locally Linear Embedding (LTSA)', 'ltsa'),
('Isomap', 'isomap')
]
topics_method_header = wdg.HTML('Select a method:')
topics_method = wdg.Dropdown(
options=options_methods,
value='tsne',
layout=wdg.Layout(width=f'{width/1.25}px')
)
topics_method_widget = wdg.HBox([topics_method_header, topics_method])
topics_method.observe(_on_select_topics_method, names='value')
topics_plot_output = wdg.Output()
topics_plot = plot_scatter_topics(**_topics_kws)
topics_plot_output.append_display_data(topics_plot)
topics_plot_children.extend([topics_method_widget, topics_plot_output])
topics_widget = wdg.VBox(
topics_plot_children,
layout={'align_items': 'center'})
children.append(topics_widget)
# Words
if show_words:
def _on_select_lambda(sel):
topic = select_topic.value
lambda_ = lambda_slider.value
words_plot_output.clear_output(wait=False)
with words_plot_output:
terms_probs = calc_terms_probs_ratio(
phi, topic=topic, lambda_=lambda_)
_words_kws.update({'terms_probs': terms_probs})
display(plot_terms(**_words_kws))
lambda_slider = wdg.FloatSlider(
value=0.6,
min=0.0,
max=1.0,
step=0.01,
description='',
continuous_update=False,
orientation='horizontal',
readout=True,
readout_format='.2f',
layout=wdg.Layout(width=f'{width/1.25}px')
)
lambda_slider.observe(_on_select_lambda, names='value')
lambda_slider_header = wdg.HTML('Lambda value:')
lambda_slider_widget = wdg.HBox([lambda_slider_header, lambda_slider])
words_plot = plot_terms(**_words_kws)
words_plot_output = wdg.Output()
words_plot_output.append_display_data(words_plot)
words_plot_children = [words_header, lambda_slider_widget]\
if show_headers else [lambda_slider_widget]
words_plot_children.append(words_plot_output)
words_widget = wdg.VBox(
words_plot_children,
layout={'align_items': 'center'})
children.append(words_widget)
# Docs
if show_docs:
def _on_select_docs_num(_):
docs_num = docs_num_slider.value
docs_plot_output.clear_output(wait=False)
with docs_plot_output:
_top_docs_kws.update({'docs_num': docs_num})
top_docs = get_top_docs(**_top_docs_kws)
top_docs.columns = ['']
_docs_kws.update({'docs': top_docs})
display(plot_docs(**_docs_kws))
docs_num_slider = wdg.IntSlider(
value=2,
min=1,
max=100,
continuous_update=False,
orientation='horizontal',
readout=True,
readout_format='d',
layout=wdg.Layout(width=f'{width/1.25}px')
)
docs_num_slider.observe(_on_select_docs_num, names='value')
docs_num_slider_header = wdg.HTML('Documents number:')
docs_num_slider_widget = wdg.HBox(
[docs_num_slider_header, docs_num_slider])
docs_plot = plot_docs(**_docs_kws)
docs_plot_output = wdg.Output()
docs_plot_output.append_display_data(docs_plot)
docs_plot_children = [docs_header, docs_num_slider_widget]\
if show_headers else [docs_num_slider_widget]
docs_plot_children.append(docs_plot_output)
docs_widget = wdg.VBox(
docs_plot_children,
layout={'align_items': 'center'})
children.append(docs_widget)
grid_box = wdg.GridBox(children, layout=layout)
hr_line = wdg.HTML('<hr style="border: 0; border-bottom: 1px solid #aaa">')
app = wdg.VBox([select_topic_wrapper, hr_line, grid_box])
return app