-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfringiness.py
371 lines (312 loc) · 11.3 KB
/
fringiness.py
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
from __future__ import division, print_function
import numpy as np
from scipy.spatial.distance import pdist, squareform
from sklearn.decomposition import PCA
import json
#import matplotlib.pyplot as plt
from bokeh.plotting import figure, show
from bokeh.palettes import viridis
from bokeh.models import Span, HoverTool
from bokeh.models.sources import ColumnDataSource
from bokeh.models.callbacks import CustomJS
from bokeh.models.tools import TapTool
from bokeh.layouts import widgetbox, gridplot
from bokeh.models.widgets import Button
import pandas as pd
from IPython.core.debugger import set_trace
import data_getter
import webbrowser
from urllib.parse import quote
from data_getter import *
def fringiness(data, distance_metric='cosine'):
"""Calculates the fringiness of news articles.
The fringiness is a number between 0 and 1 that rates each news article
based on how well it is embedded in the context.
Parameters
----------
data : ndarray
Matrix with every row representing a news article and every column a
feature.
distance_metric : str
Distance metric to use to compute the differences between the news
articles. Supports all distance metrics in sklearn [1]
Returns
-------
array_like
x coordinate of the embedding
array_like
y coordinate of the embedding
array_like
Fringiness score for each news article.
References
----------
.. [1] https://docs.scipy.org/doc/scipy/reference/spatial.distance.html#module-scipy.spatial.distance
"""
normalised_data = (data.T / np.linalg.norm(data, axis=1)).T
pairwise_distances = squareform(pdist(normalised_data, distance_metric))
#s = pairwise_distances[0] < np.mean(pairwise_distances[0])
pca = PCA()
loadings = pca.fit_transform(pairwise_distances)#[s,:][:,s])
sum_sq_loadings = np.sum(loadings**2 * pca.explained_variance_ratio_,
axis=1)
sum_sq_loadings -= sum_sq_loadings.min()
sum_sq_loadings /= sum_sq_loadings.max()
return loadings.T[0], loadings.T[1], sum_sq_loadings
def to_json(x, y, s, filename=None):
d = [{'x':xx,'y':yy,'s':ss} for xx, yy, ss in zip(x, y, s)]
if filename is not None:
with open(filename,'w') as f:
json.dump(d, f)
return d
def embedding_plot_mpl(x, y, s):
fig = plt.figure()
ax = fig.add_subplot(111)
ax.scatter(x, y, c=s, vmin=0, vmax=1)
ax.axis('off')
return fig
def hex_color(color_float):
# Returns Hex color code
return str(hex(int(color_float * 255))).lstrip('0x').zfill(2).upper()
def vertohex(x, y):
'''
Computes distance from centroid for list of x and y coordinates and gives hex value for coloring by distance from center.
'''
centroid = (np.mean(x), np.mean(y))
#X, Y = np.meshgrid(x, y)
R = np.sqrt((x - centroid[0])**2 + (y - centroid[1])**2)
maxR = np.amax(R)
cmap_cust = (plt.cm.viridis(R / maxR))
hex_color_codes = []
for (v,r,d,_) in cmap_cust:
hex_color_codes.append(''.join(['#',
hex_color(v),
hex_color(r),
hex_color(d)]))
dists = ["{:0.2f}".format(x/maxR) for x in R]
return dists, hex_color_codes
def embedding_plot_bokeh(x, y, s, res):
"""
Draws a Bokeh scatter plot with the color indicating the fringiness. The
first row is indicated in red.
Parameters
----------
x : array_like
x coordinates of the points.
y : array_like
y coordinates of the points.
s : array_like
fringiness of the points.
Returns
-------
bokeh.plotting.figure.Figure
"""
# Produce annotations
_, idxer = res_to_matrix(res)
(ents, tit) = res_to_annot(res, idxer)
# Generate colors and some annotations
if not any(s):
centroid , colors = vertohex(x, y)
else:
col_ind = np.digitize(s, np.linspace(s.min(), s.max(), 20))
colormap = viridis(21)
colors = [colormap[int(ind)] for ind in col_ind]
colors[0] = 'red'
# Define tools
toolbox = "tap, pan, wheel_zoom, box_zoom, reset, box_select, lasso_select"
# Define hover window
# create a new plot with the tools, explicit ranges and some custom design
p = figure(plot_width= 640, plot_height= 480, tools= [toolbox], title="Mouse over the dots")
p.axis.visible = False
p.ygrid.grid_line_color = None
p.xgrid.grid_line_color = None
# p.circle(x, y, fill_color=colors, fill_alpha=0.6, line_color = None)
p = plot_with_hover(p, x, y, s, colors, ents, tit)
return p
#######################################################
# Get me something from the other side of spectra
def recommendopposite(R, title, plot):
'''
recommendoppostie- Take vector of distances from current point and vector of titles and append 'recommend alternative' hyperlink button to the ploting canavas.
Returns
-------
grid: bokeh.layouts.grid_plot
Spatial arrangement of glyphs on canvas.
'''
# Select title of the most distant datapoint
maxR_id = np.argmax(R)
title_opp = title[maxR_id]
recomm = "Try reading something allternative:\n {}".format(title_opp[0])
# Create Search URL
base_url = "http://www.google.com/?#q="
final_url = base_url + quote(title_opp[0])
button = Button(label = recomm, button_type = "primary")
# available button_types = default, primary, success, warning, danger, link
# Define event on callback
code = """
url = source.data['url']
window.open(url, "_self")
"""
source = ColumnDataSource(data=dict(url = [final_url]))
callback = CustomJS(args=dict(source=source), code=code)
button.callback = callback
wdgtbox = widgetbox(button)
# grid = gridplot([[plot],[wdgtbox]],toolbar_location='above', responsive = True, lnt = 1)
grid = gridplot([[plot],[wdgtbox]])
return grid
###########################################
def plot_with_hover(plot, x, y, f, colors, entities, title):
'''
Add hover over individual datapoints.
We use dirty trick of plotting invisible scatter points so that we can use exisitng plot.
doc, TBA
'''
# Compute distance from current article in terms of first two principal components
centroid = (x[0], y[0])
R = np.sqrt((x - centroid[0])**2 + (y - centroid[1])**2)
maxR = np.amax(R)
dists = ["{:0.2f}".format(x/maxR) for x in R]
# Get number of entities from for each article = datapoint
numEnts = [len(en) for en in entities]
# Create dataframe holding all the data that we want to appear on the final plot, including hover
d = {'x': x,
'y': y,
'f': f,
'ents': entities,
'numEnts': numEnts,
'dists': dists,
'tit': title}
# works also for series of different length
plot_df = dict([ (k, pd.Series(v)) for k,v in d.items() ])
# Plot empty circles and specify source. This enables us to add hover.
plot.circle('x',
'y',
fill_color= colors,
fill_alpha=0.6,
radius = 0.025,
line_color = None,
source = ColumnDataSource(data = plot_df))
# Add Hover tooltips
hover = gimmeHover()
plot.add_tools(hover)
plot = recommendopposite(R, title, plot)
return plot
def gimmeHover():
cols = ['f', 'ents', 'numEnts', 'dists', 'tit']
names = ['F', 'Entities', '# of Entities', 'Dsitance from current', 'Tile']
ttips_pairs = [(n, '@' + v) for (n, v) in zip(names, cols)]
hover = HoverTool(tooltips = """
<div style = "max-width: 750px">
<div>
<span style="font-size: 28px; font-weight: bold;">F = </span>
<span style="font-size: 28px; font-weight: bold;">@f</span>
</div>
<div>
<span style="font-size: 18px; font-weight: bold;">Title:<br/></span>
<span style="font-size: 18px;">@tit</span>
</div>
<div>
<span style="font-size: 18px; font-weight: bold;">Entities:<br/></span>
<span style="font-size: 18px;">@ents</span>
</div>
<div>
<span style="font-size: 18px; font-weight: bold;">No. of Entities = </span>
<span style="font-size: 18px;">@numEnts</span>
</div>
<!--
<div>
<span style="font-size: 12px; font-weight: bold;">Distance= </span>
<span style="font-size: 10px;">@dists</span>
</div>
<div>
<span style="font-size: 12px; font-weight: bold;">(x,y) </span>
<span style="font-size: 10px;">($x, $y) </span>
</div>
-->
</div>
""")
hover = HoverTool(tooltips = """
<div style = "max-width: 200px">
<div>
<span style="font-size: 18px;">@tit</span>
</div>
<div>
<span style="font-size: 11px;">@ents</span>
</div>
</div>
""")
return hover
def histogram_bokeh(s):
p=figure(title='Histogram')
bins=20
h, e = np.histogram(s, density=True, bins=bins)
colors = viridis(bins)
p.quad(top=h, bottom=0, left=e[:-1], right=e[1:], fill_color=colors)
vline = Span(location=s[0], dimension='height', line_color='red',
line_width=3)
p.add_layout(vline)
return p
def keys(env):
keys = set()
keys |= env['entities'].keys()
keys |= env.get('tags', {}).keys()
keys |= env.get('topics', {}).keys()
return keys
def res_to_matrix(res):
all_keys = set()
l = [res['point']] + res['environs']
for env in l:
all_keys |= keys(env)
reference = np.array(list(all_keys))
idxer = np.ones(len(l), dtype = bool)
vs = []
for it, env in enumerate(l):
v = np.zeros(len(reference))
try:
s = np.hstack([np.where(reference==key)
for key in keys(env)])
if np.sum(s) >= 1:
v[s] = 1
vs.append(v)
except ValueError:
idxer[it] = False;
pass
vs = np.vstack(vs)
return vs, idxer
def text_to_matrix(text):
return res_to_matrix(data_getter.run(text))
def text_to_fringiness(text):
return fringiness(res_to_matrix(run(text)))
def random_data(n, m, sparsity=0.8, mean=2, distribution='poisson'):
"""
Parameters
----------
n : int
number of samples
m : int
number of features
sparsity : float between 0 and 1
sets the ratio of zero values in the resulting matrix.
distribution : str
supported are 'poisson' and 'normal'
"""
if distribution == 'poisson':
r = np.random.poisson(mean, (n, m))
elif distribution == 'normal':
r = np.random.randn(n*m).reshape((n,m)) + mean
p = np.random.rand(n*m).reshape((n,m))
r[p<sparsity] = 0
return r
def res_to_annot(res, idxer):
# Take resources .json and extract topics and entites
# res is a result of data_getter.run(text)
ents = []
tit = []
l = [res['point']] + res['environs']
l = [e for (id, e) in zip(idxer, l) if id]
for env in l:
ents.append(list(env['entities'].keys()))
try:
tit.append([env['title']])
except:
tit.append([])
return (ents, tit)