-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrun_visualization.py
More file actions
169 lines (156 loc) · 5.05 KB
/
run_visualization.py
File metadata and controls
169 lines (156 loc) · 5.05 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
from sklearn.multiclass import OneVsRestClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import f1_score
from scipy.io import loadmat
from sklearn.utils import shuffle as skshuffle
from sklearn.preprocessing import MultiLabelBinarizer
from scipy.io import mmread,mminfo
from scipy.sparse import csr_matrix
import sys
import matplotlib.pyplot as plt
import networkx as nx
from networkx.algorithms import community
import random, math
import numpy as np
from sklearn.manifold import TSNE
from sklearn.manifold._t_sne import trustworthiness
from networkx.algorithms.community import greedy_modularity_communities
from matplotlib.pyplot import cm
from matplotlib import collections as mc
import warnings
warnings.filterwarnings("ignore")
def readEmbeddings(filename, nodes, dim):
embfile = open(filename, "r")
firstline = embfile.readline()
N = int(firstline.strip().split()[0])
X = [[0]*dim for i in range(nodes)]
for line in embfile.readlines():
tokens = line.strip().split()
nodeid = int(tokens[0])-1
x = []
for j in range(1, len(tokens)):
t = float(tokens[j])
x.append(t)
X[nodeid] = x
embfile.close()
print("Size of X:", len(X))
return np.array(X)
def readEmbeddingsFA2(filename, nodes, dim):
embfile = open(filename, "r")
X = [[0]*dim for i in range(nodes)]
i = 0
for line in embfile.readlines():
tokens = line.strip().split()
x = []
for j in range(0, len(tokens)):
t = float(tokens[j])
x.append(t)
X[i] = x
i += 1
embfile.close()
print("Size of X:", len(X))
return np.array(X)
def readEmbeddingsHARP(filename, nodes, dim):
dX = np.load(filename)
print("Size of X:", len(dX))
return dX
def readBinEmbeddings(filename, dim):
embfile = open(filename, "r")
D = np.fromfile(filename, np.float32)
length = D.shape[0]
embd_shape = [int(length / dim), dim]
D = D.reshape(embd_shape)
X = []
for line in D:
x = []
for v in line:
x.append(v)
X.append(x)
embfile.close()
print("Size of X:", len(X))
return np.array(X)
def readgroundtruth(truthlabelsfile, N):
Yd = dict()
distinctlabels = set()
lfile = open(truthlabelsfile)
arrY = [-1 for i in range(N)]
for line in lfile.readlines():
tokens = line.strip().split()
node = int(tokens[0])-1
label = int(tokens[1])
arrY[node] = label
if label in Yd:
tempy = Yd[label]
tempy.append(node)
Yd[label] = tempy
else:
Yd[label] = [node]
distinctlabels.add(label)
lfile.close()
return Yd, len(distinctlabels), np.array(arrY)
import community as commm
def drawGraphc(G, X, comm, nl, algo1="Graph"):
gridsize = (1, 1)
fig = plt.figure(figsize=(8, 5))
axIN = plt.subplot2grid(gridsize, (0, 0))
plt.axis('off')
axIN.set_xlim(min(X[:,0]), max(X[:,0]))
axIN.set_ylim(min(X[:,1]), max(X[:,1]))
linesIN = []
e = 0
print("Cluster:",len(comm))
mycolors = cm.rainbow(np.linspace(0,1,nl+2))
gd = dict()
for com in comm:
for node in list(comm[com]):
gd[node] = com
plt.scatter(X[node][0], X[node][1], s=2, color = mycolors[com])
plt.axis('off')
plt.savefig(algo1+'_vis.pdf')
def drawGraph(G, X, algo1="Graph"):
g = nx.Graph(G)
comm = community.greedy_modularity_communities(g)
gridsize = (1, 1)
fig = plt.figure(figsize=(8, 5))
axIN = plt.subplot2grid(gridsize, (0, 0))
plt.axis('off')
axIN.set_xlim(min(X[:,0]), max(X[:,0]))
axIN.set_ylim(min(X[:,1]), max(X[:,1]))
linesIN = []
e = 0
print("Cluster:",len(comm))
mycolors = cm.rainbow(np.linspace(0,1,len(comm)))
gd = dict()
for com in range(len(comm)):
for node in list(comm[com]):
gd[node] = com
plt.scatter(X[node][0], X[node][1], s=2, color = mycolors[com])
plt.axis('off')
modularity = commm.community_louvain.modularity(gd, g)
print("Modularity:", algo1, "=", modularity)
plt.savefig(algo1+'_vis.pdf')
filename = sys.argv[1]
G = mmread(filename)
graph = nx.Graph(G)
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score
from sklearn.metrics import f1_score
from sklearn import metrics
if sys.argv[2] == "1":
print("Running native...")
X = readEmbeddings(sys.argv[3], mminfo(filename)[0], int(sys.argv[4]))
elif sys.argv[2] == "5":
X = readEmbeddingsHARP(sys.argv[3], mminfo(filename)[0], int(sys.argv[4]))
elif sys.argv[2] == "2":
X = readEmbeddingsFA2(sys.argv[3], mminfo(filename)[0], int(sys.argv[4]))
else:
X = readBinEmbeddings(sys.argv[3], int(sys.argv[4]))
labs, l, gy = readgroundtruth(sys.argv[5], mminfo(filename)[0])
algoname = sys.argv[6]
print("Running TSNE")
X_f = X
drawGraphc(G, X_f, labs, l, algoname)
shil = metrics.silhouette_score(X_f, gy)
davd = metrics.davies_bouldin_score(X_f, gy)
print("silhouette:", shil, "davies_bouldin:", davd)
print("Visualization complete!")