-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvisualize.py
More file actions
67 lines (53 loc) · 1.66 KB
/
visualize.py
File metadata and controls
67 lines (53 loc) · 1.66 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
import numpy
import random
import networkx as nx
import matplotlib.pyplot as plt
def visualize(ker, threshold=0.3):
'''
L is the output kernel
labels come form caltech_101_classes.txt
singletons are omitted from the graph
'''
G = nx.Graph()
weight = {}
#initialize the weights
for i in range(len(ker)):
a = list(ker[i])
del a[i]
if numpy.max(a) > threshold:
weight[i] = {}
for j in range(i+1,len(ker)):
if ker[i][j] > threshold:
weight[i][j] = ker[i][j]
print(weight.keys())
#add nodes
G.add_nodes_from(list(weight.keys()))
#add edges if similarity > 0.5
for i in weight.keys():
for j in weight[i].keys():
G.add_edges_from([(i,j,{'weight':ker[i][j]})])
pos = nx.spring_layout(G)
#load labels
file = open('labels/caltech_101_classes.txt','r')
lines = file.readline().strip().split(',')
lines = [lines[i] for i in range(len(lines)) if i in weight.keys()]
label = dict(zip(weight.keys(),lines))
#drwa the graph
plt.figure(figsize=(20,20))
nx.draw_networkx_nodes(G,pos,node_color='grey',
node_size=500,alpha=0.8)
nx.draw_networkx_edges(G,pos,width=1.0,alpha=0.5)
nx.draw_networkx_labels(G,pos,label,font_size=16)
plt.axis('off')
plt.savefig('full.png')
#show the figure
#plt.show()
if __name__ == '__main__':
"""
L here is randomly intialized
"""
L = numpy.eye(30)
for i in range(len(L)):
for j in range(i+1,len(L)):
L[i][j] = random.random()
visualize(L)