-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvis.py
More file actions
executable file
·493 lines (372 loc) · 15.4 KB
/
vis.py
File metadata and controls
executable file
·493 lines (372 loc) · 15.4 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
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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
"""
visualisation
"""
import numpy as np
import pandas as pd
import seaborn as sns
import networkx as nx
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
from datetime import datetime
def plot_matrix(X, path="", labels=[], labels_sort=False, label_axis=0, label_ticks=False, title="", xlabel="", ylabel="", fontsize=12, cmap=plt.cm.Blues, figsize=None, dpi=None):
"""
plot matrix
- example:
>>> plot_matrix(X, labels=[range(X.shape[0])], label_axis=2, label_ticks=True, title="square matrix")
@param X: np.matrix, ndarray, or dataframe
@param path: path to save image file (default: matrix_timestamp.png)
@param labels: list of labels (optional)
@param labels_sort: sort matrix using a corresponding list of labels, ie. cluster labels. also looks at label_axis to sort quadratic matrices both ways (default: False)
@param label_axis: 0 = x axis, 1 = y axis, 2 = both (default: 0)
@param label_ticks: tick labels alongside specified axis (default: False)
@param title, xlabel, ylabel: plot title, axis label strings
@param fontsize: size for all labels, value in points (default: 12)
@param cmap: colormap to use (default: plt.cm.Blues)
@param figsize: tuple of integers, values in inches (default: None - uses pyplot default values)
@param dpi: resolution of the figure (default: None - uses pyplot default values)
"""
if isinstance(X, pd.DataFrame): X = X.values()
if not path: path = "matrix_"+datetime.now().strftime("%Y-%m-%d_%H-%M-%S")+".png"
# sort
if labels_sort is True:
X = X[np.argsort(labels)]
if label_axis == 2:
X = X[:, np.argsort(labels)]
# plot
plt.figure(figsize=figsize)
ax = plt.add_subplot(111)
m_ax = ax.matshow(X, cmap=cmap)
plt.colorbar(m_ax)
plt.title(title, fontsize=fontsize)
plt.xlabel(xlabel, fontsize=fontsize)
plt.ylabel(xlabel, fontsize=fontsize)
if labels and label_axis == 0: ax.set_xticklabels([''] + labels)
if labels and label_axis == 1: ax.set_yticklabels([''] + labels)
if labels and label_axis == 2:
ax.set_xticklabels([''] + labels)
ax.set_yticklabels([''] + labels)
ax.xaxis.set_major_locator(ticker.MultipleLocator(1))
ax.yaxis.set_major_locator(ticker.MultipleLocator(1))
plt.savefig(path, dpi=dpi)
def plot_graph(G, path="", labels=[], layout="spring", node_size=20, node_color="blue", alpha=0.5, width=0.5, font_size=10, font_color="k", figsize=None, dpi=None):
"""
plot graph
@param G: networkx graph
@param path: path to save image file (default: graph_timestamp.png)
@param labels: list of labels (optional)
@param layout: layout algorithm to use, "spring", "shell", or "circular" (default: "spring")
@param node_size: size of nodes (default: 20)
@param node_color: color of nodes (default: "blue")
@param alpha: transparency of nodes (default: 0.5)
@param width: line width of edges (default: 0.5)
@param font_size: font size (default: 10)
@param font_color: font color (default: "k" = black)
@param figsize: tuple of integers, values in inches (default: None - uses pyplot default values)
@param dpi: resolution of the figure (default: None - uses pyplot default values)
"""
if not path: path = "graph_"+datetime.now().strftime("%Y-%m-%d_%H-%M-%S")+".png"
plt.figure(figsize=figsize)
if layout is "spring": pos = nx.spring_layout(G, k=0.2, iterations=100) #, scale=10.0)
if layout is "shell": pos = nx.shell_layout(G)
if layout is "circular": pos = nx.circular_layout(G)
if labels:
nx.draw(G, pos=pos, node_size=node_size, node_color=node_color, alpha=alpha, width=width, with_labels=True)
nx.draw_networkx_labels(G, pos, labels=labels, font_size=font_size, font_color=font_color)
else:
nx.draw(G, pos=pos, node_size=node_size, node_color=node_color, alpha=alpha, width=width, with_labels=False)
plt.savefig(path, dpi=dpi)
def plot_bigraph(B, path="", labels=[], node_size=20, node_color="blue", alpha=0.5, width=0.5, font_size=10, font_color="k", figsize=None, dpi=None):
"""
plot bipartite graph
@param B: networkx bigraph
@param path: path to save image file (default: bigraph_timestamp.png)
@param labels: list of labels (optional)
@param node_size: size of nodes (default: 20)
@param node_color: color of nodes (default: "blue")
@param alpha: transparency of nodes (default: 0.5)
@param width: line width of edges (default: 0.5)
@param font_size: font size (default: 10)
@param font_color: font color (default: "k" = black)
@param figsize: tuple of integers, values in inches (default: None - uses pyplot default values)
@param dpi: resolution of the figure (default: None - uses pyplot default values)
"""
if not path: path = "bigraph_"+datetime.now().strftime("%Y-%m-%d_%H-%M-%S")+".png"
X, Y = nx.bipartite.sets(B)
plt.figure(figsize=figsize)
pos = dict()
pos.update( (n, (1, i+20)) for i, n in enumerate(X) ) # put nodes from X at x=1
pos.update( (n, (2, i+20)) for i, n in enumerate(Y) ) # put nodes from Y at x=2
nx.draw(B, pos=pos, node_size=node_size, node_color=node_color, alpha=alpha, width=width)
if labels: nx.draw_networkx_labels(B, pos, labels=labels, font_size=font_size, font_color=font_color)
plt.savefig(path, dpi=dpi)
def plot_categorical(data, path="", kind="strip", palette="bright", bin=False, num_bin=5, title="", xlabel="", ylabel="", fontsize=12, figsize=None, dpi=None):
"""
plot categorical data
- cf. https://seaborn.pydata.org/tutorial/categorical.html
@param data: list of values (will be counted automatically) or np.ndarray, np.matrix, dataframe
@param path: path to save image file (default: categorical_timestamp.png)
@param kind: "strip" (default), "swarm", "box", "violin", "boxen", "point", "bar", "count"
@param palette: "deep", "muted", "bright" (default), "pastel", "dark", "colorblind"
@param bin: bin data (default: False)
@param num_bin: number of bins (default: 5)
@param title, xlabel, ylabel: plot title, axis label strings
@param fontsize: size for all labels, value in points (default: 12)
@param figsize: tuple of integers, values in inches (default: None - uses pyplot default values)
@param dpi: resolution of the figure (default: None - uses pyplot default values)
"""
if not path: path = "categorical_"+datetime.now().strftime("%Y-%m-%d_%H-%M-%S")+".png"
if isinstance(data, list):
df = pd.DataFrame.from_dict(data=Counter(data), orient='index')
elif isinstance(data, np.ndarray) or isinstance(data, np.matrix):
df = pd.DataFrame(np.asarray(data), columns=range(data.shape[1]))
elif isinstance(data, pd.DataFrame):
df = data
df.sort_index(inplace=True)
# bin dataframe
if bin is True:
index_orig = df.index.tolist()
# TODO: list -> df column assignment checken
df['index'] = enumerate(index_orig, start=1)
bins = np.arange(0, len(index_orig), len(index_orig) / num_bin)
labels = [str(index_orig[i]) for i in bins]
# TODO: label für jeden bin als range: "orig_label1 - orig_label2"
df['bin'] = pd.cut(df['index'], bins=bins, labels=labels, include_lowest=True)
df['bin'] = df['bin'].astype(int)
grouped = df.groupby('bin')
#df = grouped.agg(np.sum) # TypeError: unorderable types: str() < int()
# alternativ zu agg()
size_per_bin = {}
for g, sizes in grouped: size_per_bin[g] = sizes[0].sum()
df = pd.DataFrame.from_dict(data=size_per_bin, orient='index')
df.sort_index(inplace=True)
# TODO: funktion catplot
# argumente: kind, palette + labels = kategorien
# plot
plt.figure()
ax = sns.barplot(data=df.T, orient='h', palette='PuBuGn_d') # df=df[:25].T
ax.set_title('Novels by creation date')
ax.set(xlabel=str(df[0].sum())+' works total\n'+format_number(tok_count)+' tokens', ylabel='')
plt.savefig('works-genre.png', dpi=80)
"""
fuer diskrete daten (no binning):
df = pd.DataFrame.from_dict(data=Counter(data), orient='index')
#df = df.sort_index()
df.sort_values(0, axis='index', ascending=False, inplace=True)
#df.columns = ['']
plt.figure()
plt.tick_params(axis='both', which='major', labelsize=15)
#df.plot.pie(figsize=(6, 6), subplots=True, colormap='Blues')
ax = sns.barplot(data=df.T, orient='h', palette='PuBuGn_d') # df=df[:25].T
ax.set_title('top features')
plt.savefig(result_path+'/'+result_file[:-4]+'_topfeat.png', dpi=80)
"""
def plot_continuous():
"""
plot continuous data
cf. https://seaborn.pydata.org/tutorial/distributions.html
"""
def plot_dendrogram(X, outfile):
"""
plot dendrogram
cf. met-cluster/cluster.py
"""
Z = scipy.cluster.hierarchy.ward(X)
tree = scipy.cluster.hierarchy.to_tree(Z) # for tree-based traversal of linkage matrix
level = 200
plt.figure(figsize=(240,240))
dendro = dendrogram(Z, level, orientation='left', labels=labels, show_leaf_counts=True, leaf_label_func=llf, leaf_rotation=90, leaf_font_size=1, truncate_mode='level', distance_sort='ascending', count_sort='ascending')
plt.xlim([0, 500]) # truncate to stay within size limit even for high levels
plt.savefig('out/dendrogram_lvl'+str(level)+'.png', dpi=90)
################
# DENDROGRAM LABELING
# cf. hgfc/cluster.py
# cf. http://rootslash.net/20808/scipy-hierarchical-cluster-dendrogram-labels-mixed-up
# when given a tree and id, it returns all the leave nodes
# (indices for nouns) to retrieve the nouns of that cluster
def search_tree(tree, id):
if tree.get_id() == id:
return tree.pre_order()
if tree.is_leaf():
return []
return search_tree(tree.get_left(), id) + search_tree(tree.get_right(), id)
# leaf label function
# maps the indices back to the original nouns
def llf(id):
if id < labels_len:
label = str(labels[id]) + '\n'
return label
else:
indices = search_tree(tree, id)
nouns = [labels[index] for index in indices][0:30] # display only the first 30 nouns
output = [str(len(nouns)) + ":"] + nouns
return ' '.join(output)
# same as llf() but returns a list instead of a label string
def label_lookup(id):
if id < labels_len:
label = [str(labels[id])]
return label
else:
indices = search_tree(tree, id)
nouns = [labels[index] for index in indices]
return nouns
################
def tsne_cluster(X, outfile):
"""
word embedding/doc2vec.py + lsa.py: model -> tsne, cluster
"""
"""
alternativ:
cf. met-cluster/cluster.py
# scipy agg / sklearn agg 3d
#projection = TSNE(n_components=3)
#projection.fit(X)
agg_labels = scipy.cluster.hierarchy.fcluster(Z, 800, criterion="maxclust")
#agg_labels = AgglomerativeClustering(100).fit_predict(X)
logging.debug('shape of labels array: ', agg_labels.shape)
fig = plt.figure(figsize=(40, 40), dpi=80)
ax = fig.add_subplot(111, projection='3d')
palette = sns.palettes.color_palette('spectral', 850)
for j in set(agg_labels):
ax.scatter(X_3d[agg_labels == j, 0], X_3d[agg_labels == j, 1], X_3d[agg_labels == j, 2],
s=50, color=palette[j], alpha=0.5, label=labels[j], zdir=u'y')
#plt.title('t-SNE embedding with AgglomerativeClustering labels')
#plt.legend()
#plt.savefig('out/scatter3d_aggscipy.png', dpi=80)
plt.show()
"""
def lda_heatmap(X, outfile):
"""
topic modeling/lda_heatmap.py: model -> sort -> img
"""
def lda_network(X, outfile):
"""
topic modeling/lda_network.py: model -> nx graph -> img
"""
def scatterplot_decision_surface(X, outfile):
"""
van halteren/vh_tutorial.py: clf decision surfaces -> img
"""
def plot_fuzzyclustering(X, outfile):
"""
cf. met-cluster/cluster.py
"""
"""
import skfuzzy as fuzz
ncenters = 80
palette = sns.palettes.color_palette('spectral', 85)
cntr, u, u0, d, jm, p, fpc = fuzz.cluster.cmeans(X, ncenters, 2, error=0.005, maxiter=1000, init=None)
# Plot assigned clusters, for each data point in training set
cluster_membership = np.argmax(u, axis=0)
#logging.debug(type(cluster_membership))
logging.debug(cluster_membership.shape)
for j in range(ncenters):
plt.plot(X_2d[cluster_membership == j, 0],
X_2d[cluster_membership == j, 1], '.', color=palette[j])
# Mark the center of each fuzzy cluster
for pt in cntr:
plt.plot(pt[0], pt[1], 'rs')
#plt.set_title('Centers = {0}; FPC = {1:.2f}'.format(ncenters, fpc))
#plt.axis('off')
plt.tight_layout()
plt.savefig('out/scatter_fuzzy.png', dpi=80)
"""
def scatterplot_hexbin(X, outfile):
"""
cf. met-cluster/cluster.py
"""
"""
# hexbin + scatterplot (tsne projection of original data)
logging.debug('plotting hexbin ..\n')
plt.figure()
plt.title("Density")
plt.hexbin(*X_2d.T)
plt.savefig('out/hexbin.png', dpi=160)
logging.debug('plotting scatterplot ..\n')
assignments = scipy.cluster.hierarchy.fcluster(Z, 4, criterion="maxclust")
#logging.debug(scipy.cluster.hierarchy.leaders(Z, assignments))
plt.figure()
plt.title("4 state ward clustering of data")
plt.plot(X_2d[assignments==1, 0], X_2d[assignments==1, 1], 'o', label="1")
plt.plot(X_2d[assignments==2, 0], X_2d[assignments==2, 1], 'o', label="2")
plt.plot(X_2d[assignments==3, 0], X_2d[assignments==3, 1], 'o', label="3")
plt.plot(X_2d[assignments==4, 0], X_2d[assignments==4, 1], 'o', label="4")
plt.legend()
plt.savefig('out/scatter.png', dpi=160)
"""
def scatterplot_hdbscan(X, outfile):
"""
cf. met-cluster/cluster.py
"""
"""
# hdbscan
hdbscan_labels = hdbscan.HDBSCAN().fit_predict(X)
palette = sns.palettes.color_palette('spectral', 100)
for digit in set(map(int,hdbscan_labels)):
if digit == -1:
plt.scatter(projection.embedding_.T[0][hdbscan_labels == digit],
projection.embedding_.T[1][hdbscan_labels == digit],
color='black', alpha=0.5, label='unclassified')
else:
plt.scatter(projection.embedding_.T[0][hdbscan_labels == digit],
projection.embedding_.T[1][hdbscan_labels == digit],
color=palette[digit], alpha=0.5, label=str(digit))
plt.title('t-SNE embedding with HDBSCAN labels')
plt.legend()
plt.savefig('out/scatter_hdb.png', dpi=160)
"""
def scatterplot_3d(X, outfile):
"""
cf. met-cluster/cluster.py
"""
"""
# scatterplot 3d (tsne projection of original data)
fig = plt.figure(num=1, figsize=(32, 24), dpi=80, facecolor="w", edgecolor="k")
ax = fig.add_subplot(111, projection='3d')
ax.scatter(X_3d[:, 0], X_3d[:, 1], X_3d[:, 2], zdir=u'y', s=50)
#plt.savefig('out/scatter3d.png', dpi=160)
plt.show()
"""
def matrix_dendrogram(X, outfile):
"""
cf. met-cluster/cluster.py
"""
"""
# scipy agg matrix plot
import scipy
import pylab
import scipy.cluster.hierarchy as sch
level = 100
# Generate features and distance matrix.
#x = scipy.rand(40)
#D = scipy.zeros([40,40])
#for i in range(40):
# for j in range(40):
# D[i,j] = abs(x[i] - x[j])
# Compute and plot dendrogram.
fig = pylab.figure(figsize=(80,80))
axdendro = fig.add_axes([0.09,0.1,0.2,0.8])
#Y = sch.linkage(D, method='centroid')
#dendro = sch.dendrogram(Z, level, orientation='right', truncate_mode='level', distance_sort='ascending', count_sort='ascending')
dendro = sch.dendrogram(Z, orientation='right', distance_sort='ascending', count_sort='ascending')
axdendro.set_xticks([])
axdendro.set_yticks([])
# Plot distance matrix.
axmatrix = fig.add_axes([0.3,0.1,0.6,0.8])
index = dendro['leaves']
#index = list(set(dendro['leaves']))
#logging.debug(type(index))
#logging.debug(index)
X = X[index,:]
X = X[:,index]
im = axmatrix.matshow(X, aspect='auto', origin='lower')
axmatrix.set_xticks([])
axmatrix.set_yticks([])
# Plot colorbar.
axcolor = fig.add_axes([0.91,0.1,0.02,0.8])
pylab.colorbar(im, cax=axcolor)
# Display and save figure.
fig.show()
fig.savefig('out/matrix_dendro.png', dpi=160)
"""
if __name__ == '__main__':
print("not specified")