-
Notifications
You must be signed in to change notification settings - Fork 0
/
languagePlot.py
412 lines (355 loc) · 14.9 KB
/
languagePlot.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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
### Language Plotter
## Alex John Quijano
## Created: 1/26/2018
## Import packages
import os
import re
import sys
import math
import numpy as np
import pandas as pd
import scipy as sc
import matplotlib.font_manager as mfm
import matplotlib.pyplot as plt
import matplotlib.colors as cl
import matplotlib.cm as cm
from sklearn.preprocessing import normalize
import languageCompute as lc
from adjustText import adjust_text
#plt.switch_backend('agg')
fontP = mfm.FontProperties(fname='unifont.ttf',size=12)
### Global variables
colors_hex = { 'b':'#0000FF','blue':'#0000FF',
'g':'#008000','green':'#008000',
'r':'#FF0000', 'red':'#FF0000',
'c':'#00FFFF', 'cyan':'#00FFFF',
'm':'#FF00FF', 'magenta':'#FF00FF',
'y':'#FFFF00', 'yellow':'#FFFF00',
'k':'#000000', 'black':'#000000',
'w':'#FFFFFF', 'white':'#FFFFFF'}
### Plotting utilities
# the function below takes a color as a input (hex-value) and returns its complementary color (hex-value).
def get_complementary(color):
# strip the # from the beginning
color = color[1:]
# convert the string into hex
color = int(color, 16)
# invert the three bytes
# as good as substracting each of RGB component by 255(FF)
comp_color = 0xFFFFFF ^ color
# convert the color back to hex by prefixing a #
comp_color = "#%06X" % comp_color
return comp_color
## To create custom colormaps
def make_colormap(seq):
seq = [(None,) * 3, 0.0] + list(seq) + [1.0, (None,) * 3]
cdict = {'red': [], 'green': [], 'blue': []}
for i, item in enumerate(seq):
if isinstance(item, float):
r1, g1, b1 = seq[i - 1]
r2, g2, b2 = seq[i + 1]
cdict['red'].append([item, r1, r2])
cdict['green'].append([item, g1, g2])
cdict['blue'].append([item, b1, b2])
return cl.LinearSegmentedColormap('CustomMap', cdict)
## To shift a colomap
def shiftedColorMap(cmap, start=0, midpoint=0.5, stop=1.0, name='shiftedcmap'):
cdict = {'red': [],'green': [],'blue': [],'alpha': []}
# regular index to compute the colors
reg_index = np.linspace(start, stop, 257)
# shifted index to match the data
shift_index = np.hstack([
np.linspace(0.0, midpoint, 128, endpoint=False),
np.linspace(midpoint, 1.0, 129, endpoint=True)
])
for ri, si in zip(reg_index, shift_index):
r, g, b, a = cmap(ri)
cdict['red'].append((si, r, r))
cdict['green'].append((si, g, g))
cdict['blue'].append((si, b, b))
cdict['alpha'].append((si, a, a))
newcmap = cl.LinearSegmentedColormap(name, cdict)
plt.register_cmap(cmap=newcmap)
return newcmap
### Plotting functions
## time series plot
def time_series_plot(data,xlabel=' ',ylabel=' ',title=' ',color='red',linestyles=['-','--','-.',':','-','--','-','--','-.',':','-','--'],legend=True,annotation=False,annotation_parameters=[],ax=None):
# keys for the ngrams
ws = data.keys()
if ws.shape[0] > 12:
print('Error: Too much word inputs. You can only plot at most 12 time-series on the same figure. \n Automatically plotting only the first 12 words...')
ws = ws[0:12]
y_range = data.index
# define colormap given a color
match_hex = re.search(r'^#(?:[0-9a-fA-F]{3}){1,2}$', color)
if not match_hex:
try:
color = colors_hex[color]
except:
print('Color '+str(color)+' not available. Try converting it to hexadecimal. Using default color...')
color = colors_hex['red']
color_complement = get_complementary(color)
c = cl.ColorConverter().to_rgb
c_cmap = make_colormap([c(color), c('black'), 0.50, c('black'), c(color_complement)])
index_color = c_cmap(np.linspace(0,1,len(ws)))
if not ax:
fig, ax = plt.subplots(figsize=(7.5,3))
for i, j in enumerate(ws):
ax.plot(y_range,data[j],color=index_color[i],label=j,linewidth=2,linestyle=linestyles[i])
if annotation == True:
params = annotation_parameters
time_points = params[0]
time_labels = params[1]
for tp_index, tp in enumerate(time_points):
y_min = np.min(np.matrix(data))
if len(tp) == 1:
ax.axvline(tp[0],color='gray',alpha=0.50)
ax.annotate(time_labels[tp_index],xy=(tp[0],y_min),horizontalalignment='center',verticalalignment='center',fontweight='bold',color='black',bbox=dict(boxstyle='square',facecolor='white'),fontsize=12)
elif len(tp) == 2:
ax.axvspan(tp[0],tp[1],facecolor='gray',alpha=0.50)
ax.annotate(time_labels[tp_index],xy=(np.mean(tp),y_min),horizontalalignment='center',verticalalignment='center',fontweight='bold',color='black',bbox=dict(boxstyle='square',facecolor='white'),fontsize=12)
ax.set_xlabel(xlabel)
ax.set_ylabel(ylabel)
ax.set_title(title)
if legend == True:
ax.legend(loc=0,prop=fontP)
else:
pass
return ax
## plot the pairwise cosine similarity matrix
def pairwise_cosine_similarity_plot(data,data_selected=[],color='red',color_selected='yellow',data_linestyles=['-','--','-.',':'],title=' ',ax=None):
# all data: define colormap given a color
match_hex = re.search(r'^#(?:[0-9a-fA-F]{3}){1,2}$', color)
if not match_hex:
try:
color = colors_hex[color]
except:
print('Color '+str(color)+' not available. Try converting it to hexadecimal. Using default color...')
color = colors_hex['red']
color_complement = get_complementary(color)
c1 = cl.ColorConverter().to_rgb
c1_cmap = make_colormap([c1(color), c1('white'), 0.50, c1('white'), c1(color_complement)])
# selected data: define colormap given a color
match_hex = re.search(r'^#(?:[0-9a-fA-F]{3}){1,2}$', color_selected)
if not match_hex:
try:
color_selected = colors_hex[color_selected]
except:
print('Color '+str(color)+' not available. Try converting it to hexadecimal. Using default color...')
color_selected = colors_hex['red']
color_complement_selected = get_complementary(color_selected)
c2 = cl.ColorConverter().to_rgb
c2_cmap = make_colormap([c2(color_selected), c2('black'), 0.50, c2('black'), c2(color_complement_selected)])
index_color = c2_cmap(np.linspace(0,1,len(data_selected)))
data_index = {j:i for i,j in enumerate(data.keys())}
# draw the matrix as an image
if not ax:
fig, ax = plt.subplots(1,1,figsize=(8,6))
opacity = 1
if data_selected != []:
opacity = 0.90
img = ax.imshow(data,cmap=c1_cmap,vmin=-1,vmax=1,alpha=opacity)
for i, j in enumerate(data_selected):
ax.axhline(data_index[j], linewidth=4,color=index_color[i],label=j,linestyle=data_linestyles[i])
ax.set_xlabel(r'$w_i$',fontsize=14)
ax.set_ylabel(r'$w_j$',fontsize=14)
ax.set_xticks([])
ax.set_yticks([])
ax.set_title(title)
if data_selected != []:
ax.legend(loc=0,prop=fontP)
cbar = plt.colorbar(img,orientation='vertical',ax=ax)
cbar.set_label('cosine similarity')
return ax
## pairwise cosine distribution plot
def cosine_distribution_plot(data,linestyles=['-','--','-.',':'],color='red',title=' ',bins=50,ax=None):
# keys for the ngrams
ws = data.keys()
if ws.shape[0] > 4:
print('Error: Too much word inputs. You can only plot at most three cosine distributions on the same figure. \n Automatically plotting only the first four words...')
ws = ws[0:3]
# define colormap given a color
match_hex = re.search(r'^#(?:[0-9a-fA-F]{3}){1,2}$', color)
if not match_hex:
try:
color = colors_hex[color]
except:
print('Color '+str(color)+' not available. Try converting it to hexadecimal. Using default color...')
color = colors_hex['red']
color_complement = get_complementary(color)
c = cl.ColorConverter().to_rgb
c_cmap = make_colormap([c(color), c('black'), 0.50, c('black'), c(color_complement)])
index_color = c_cmap(np.linspace(0,1,len(ws)))
if not ax:
fig, ax = plt.subplots(figsize=(7.5,3))
for i, j in enumerate(ws):
ax.hist(data[j],bins=bins,color=index_color[i],label=j,histtype='step',linewidth=2,linestyle=linestyles[i])
ax.hist(data[j],bins=bins,color=index_color[i],alpha=0.10)
ax.set_xlabel('cosine similarity')
ax.set_ylabel('density')
ax.set_title(title)
ax.legend(loc=0,prop=fontP)
return ax
## plot bimodality coefficient and data
def bimodality_coefficient_plot(data={},data_selected=[],domain=[-2,-2,2,2],grid_size=5000,title=' ',color='red',color_selected='yellow',markerstyle='.',markerstyle_selected=['o','s','^','x'],markersize=5,markersize_selected=6,cmap_selected=True,annotation_selected=False,linewidth=2,legend=True,colorbar=True,ax=None):
# check if data inputs are valid
if data_selected != [] and data == {}:
print('Error: Input data is not valid! the \'data\' parameter needs to be defined if \'data_selected\' is nonzero.')
data_selected = []
if len(data_selected) > 4:
print('Error: Too much word inputs. You can only plot at most three words on the same figure. \n Automatically plotting only the first four words...')
data_selected = data_selected[0:4]
# define default domains
m4_b = [domain[0],domain[2]]
m3_b = [domain[1],domain[3]]
# define critical line
crit = 5.0/9.0 # critical line 5/9
m3_crit = np.linspace(m3_b[0],m3_b[1],grid_size)
m4_crit = lc.bc_curve_m3(m3_crit,crit,n=grid_size)
crit_label = r'$BC_{critical} = \frac{5}{9}$'
# define normal line
norm = 1.0/3.0 # normal line 1/3
m3_norm = np.linspace(m3_b[0],m3_b[1],grid_size)
m4_norm = lc.bc_curve_m3(m3_norm,norm,n=grid_size)
norm_label = r'$BC_{normal} = \frac{1}{3}$'
# define bc space gradient
m4 = np.linspace(m4_b[0],m4_b[1],grid_size) # kurtosi0
m3 = np.linspace(m3_b[0],m3_b[1],grid_size) # skewness
m4_X, m3_Y = np.meshgrid(m4,m3)
BC_vals = lc.sample_bimodality_coefficient(m4_X,m3_Y)
dx = (m4[1]-m4[0])/2 # x-axis is kurtosis
dy = (m3[1]-m3[0])/2 # y-axis is skewness
extent = [m4[0]-dx, m4[-1]+dx, m3[0]-dy, m3[-1]+dy]
# define colormaps and lines
midpoint = crit/np.max(BC_vals) - 0.03 # colormap midpoint
m4_lines = [m4_crit,m4_norm]
m3_lines = [m3_crit,m3_norm]
lines_label = [crit_label,norm_label]
lines_color = ['blue','orange']
orig_cmap = cm.Greys
shifted_cmap = shiftedColorMap(orig_cmap, midpoint=midpoint, name='shifted')
# selected data: define colormap given a color
if cmap_selected == True:
match_hex = re.search(r'^#(?:[0-9a-fA-F]{3}){1,2}$', color_selected)
if not match_hex:
try:
color_selected = colors_hex[color_selected]
except:
print('Color '+str(color)+' not available. Try converting it to hexadecimal. Using default color...')
color_selected = colors_hex['red']
color_complement_selected = get_complementary(color_selected)
c2 = cl.ColorConverter().to_rgb
c2_cmap = make_colormap([c2(color_selected), c2('black'), 0.50, c2('black'), c2(color_complement_selected)])
index_color = c2_cmap(np.linspace(0,1,len(data_selected)))
elif cmap_selected == False:
index_color = color_selected
# set-up dataset to plot
if data != {}:
m3_data = data['m3']
m4_data = data['m4']
# draw gradients, lines, and datapoints
if not ax:
fig, ax = plt.subplots(1,1,figsize=(7,4.666))
img = ax.imshow(BC_vals, extent=extent, origin='lower', cmap=shifted_cmap)
for k in range(len(m4_lines)):
ax.plot(m4_lines[k],m3_lines[k],'--',label=lines_label[k],color=lines_color[k],linewidth=linewidth)
if data != {}:
for i, j in enumerate(m3_data):
ax.plot(m4_data[i],j,color=color,marker=markerstyle,markersize=markersize,linestyle='None')
if data_selected != []:
for i, j in enumerate(data_selected):
if annotation_selected == False:
ax.plot(m4_data[j],m3_data[j],marker=markerstyle_selected[i],markersize=markersize_selected,color=index_color[i],label=j,linestyle='None')
elif annotation_selected == True:
ax.plot(m4_data[j],m3_data[j],markersize=markersize_selected)
ax.annotate(j,xy=(m4_data[j],m3_data[j]),xytext=(m4_data[j]+0.05,m3_data[j]+0.05))
ax.set_xlim(m4_b)
ax.set_ylim(m3_b)
ax.set_xlabel('kurtosis')
ax.set_ylabel('skewness')
ax.set_title(title)
if legend == True:
ax.legend(loc='upper center',prop=fontP,ncol=3)
if colorbar == True:
cbar = plt.colorbar(img,orientation='vertical',ax=ax)
cbar.set_label('bimodality coefficient (BC)')
return ax
## plot the word relations based on a relation matrix and plot the time-series
def word_relation_time_series_plot(w,df,ts,top=3,bottom=3,ax=None,title=''):
# get related words
t_vect = list(reversed(ts.columns))
related_w_top = pd.DataFrame(df[w]).sort_values(by=[w],ascending=False)[0:top+1]
related_w_bottom = pd.DataFrame(df[w]).sort_values(by=[w],ascending=True)[0:bottom].sort_values(by=[w],ascending=False)
related_w = pd.concat([related_w_top,related_w_bottom]).T
c = []
h = []
col = []
for i in list(related_w.keys()):
weight_val = round(related_w[i],4).values[0]
h.append(weight_val)
if weight_val == 1:
col.append('black')
elif weight_val >= 0:
col.append('blue')
else:
col.append('red')
c.append(i)
if not ax:
fig, ax = plt.subplots(figsize=(5,5))
axs = ts.loc[c].T.plot(subplots=True,legend=False,yticks=[],xticks=[],color=col,grid=True,sharex=True,ax=ax)
for j, i in enumerate(axs):
if j == 0:
i.set_title(title)
i.annotate(c[j],xy=(t_vect[0],0),xytext=(t_vect[0]+2,0),color=col[j],verticalalignment='center',fontweight='bold')
else:
i.annotate(c[j],xy=(t_vect[0],0),xytext=(t_vect[0]+2,0),color=col[j],verticalalignment='center')
i.plot(t_vect,[0]*len(t_vect),'--',color='gray',alpha=0.5)
i.axis('off')
return axs
## plot the word relations based on a relation matrix
def word_relation_network_plot(w,df,top=3,bottom=3,ax=None,title=''):
# get related words
related_w_top = pd.DataFrame(df[w]).sort_values(by=[w],ascending=False)[0:top+1]
related_w_bottom = pd.DataFrame(df[w]).sort_values(by=[w],ascending=True)[0:bottom]
related_w = pd.concat([related_w_top,related_w_bottom]).T
edges = {}
edges_col = []
for i in related_w.keys():
weight_val = round(related_w[i],4).values[0]
edges[(w,i)] = weight_val
if weight_val >= 0:
edges_col.append('black')
else:
edges_col.append('red')
# create circle for drawing
r = 0.5
xy = []
for i in edges.keys():
theta = np.arccos(edges[i])
r = 1
x = r*np.cos(theta)
y = r*np.sin(theta)
xy.append((x,y,i[1]))
if not ax:
fig, ax = plt.subplots(figsize=(6.5,5))
text = [w for (x,y,w) in xy]
eucs = [x for (x,y,w) in xy]
covers = [y for (x,y,w) in xy]
# plotting the lines
ax.plot(eucs,covers,alpha=0)
texts = []
for x, y, s in zip(eucs, covers, text):
if x == 1.0 and y == 0.0:
ax.plot((0,x),(0,y),'-',color='black',alpha=1,linewidth=3)
texts.append(ax.annotate(s,(x,y),color='black'))
elif x >= 0.0 and x < 1.0:
ax.plot((0,x),(0,y),'-',color='blue',alpha=0.25)
texts.append(ax.annotate(s,(x,y),color='blue'))
elif x >= -1 and x < 0.0 :
ax.plot((0,x),(0,y),'-',color='red',alpha=0.25)
texts.append(ax.annotate(s,(x,y),color='red'))
#texts.append(ax.annotate(s,(x,y)))
adjust_text(texts, only_move={'points':'y', 'texts':'y'}, arrowprops=dict(arrowstyle="->", color='k', lw=1), ax=ax)
ax.set_title(title)
ax.axis('off')
return ax