-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraph_embading.py
More file actions
141 lines (117 loc) · 4.5 KB
/
Copy pathGraph_embading.py
File metadata and controls
141 lines (117 loc) · 4.5 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
import random
from sklearn.decomposition import PCA
from gensim.models import Word2Vec
import warnings
from matplotlib import pyplot as plt
import networkx as nx
import numpy as np
warnings.filterwarnings('ignore')
class Graph_embading:
def __init__(self,graph):
self.G = graph
def save_graph(self,file_name):
#initialze Figure
graph = self.G
G = graph
plt.figure(num=None, figsize=(20, 20))
plt.axis('off')
fig = plt.figure(1)
pos = nx.spring_layout(graph)
color_map = []
for node in G:
layer0 = "Layer_0"
layer1 = "Layer_1"
layer2 = "Layer_2"
if layer0 in node:
color_map.append('blue')
elif layer1 in node:
color_map.append('green')
else:
color_map.append('red')
nx.draw_networkx_nodes(graph,pos,node_color=color_map)
nx.draw_networkx_edges(graph,pos)
nx.draw_networkx_labels(graph,pos)
cut = 0.05
xmax = cut * max(xx for xx, yy in pos.values())
ymax = cut * max(yy for xx, yy in pos.values())
plt.xlim(0, xmax)
plt.ylim(0, ymax)
plt.savefig(file_name,bbox_inches="tight")
plt.show()
del fig
#Assuming that the graph g has nodes and edges entered
#save_graph(G,"my_graph.pdf")
def drwa_graph(self):
# use one of the edge properties to control line thickness
edgewidth = [ d['weight'] for (u,v,d) in self.G.edges(data=True)]
# layout
pos = nx.spring_layout(self.G)
color_map = []
for node in self.G:
layer0 = "layer0"
layer1 = "layer1"
layer2 = "layer2"
if layer0 in node:
color_map.append('blue')
elif layer1 in node:
color_map.append('green')
elif layer2 in node:
color_map.append('red')
else:
color_map.append('black')
nx.draw_networkx_nodes(self.G, pos,node_size=100,node_color=color_map)
nx.draw_networkx_edges(self.G, pos,)
#nx.draw_networkx_edges(self.G, pos, width=edgewidth,edge_color=edgewidth)
def draw_layer_graph(self,graph):
G = graph
# use one of the edge properties to control line thickness
edgewidth = [ d['weight'] for (u,v,d) in G.edges(data=True)]
# layout
pos = nx.spring_layout(G)
nx.draw_networkx_nodes(G, pos,node_size=100)
nx.draw_networkx_edges(G, pos,)
#nx.draw_networkx_edges(G, pos, width=edgewidth,edge_color=edgewidth)
def get_node_list(self):
nodes_list = np.array(list(self.G.nodes()))
node_name = nodes_list[:]
return node_name
def get_rando_mwalk_node(self,node, path_length):
random_walk = [node]
for i in range(path_length-1):
temp = list(self.G.neighbors(node))
temp = list(set(temp) - set(random_walk))
if len(temp) == 0:
break
random_node = random.choice(temp)
random_walk.append(random_node)
node = random_node
return random_walk
def randome_walk_nodes(self,node_name):
# get list of all nodes from the graph
all_nodes = list(self.G.nodes())
random_walks = []
for n in (node_name):
for i in range(5):
random_walks.append(self.get_rando_mwalk_node(n,15))
# count of sequences
return random_walks
def embed_graph(self,random_walks,embedding_size):
# train skip-gram (word2vec) model
model_w2v = Word2Vec(window = 4, sg = 1, hs = 0,
negative = 10, # for negative sampling
alpha=0.03, min_alpha=0.0007,
seed = 14)
model_w2v.build_vocab(random_walks, progress_per=2)
model_w2v.train(random_walks, total_examples = model_w2v.corpus_count, epochs=20, report_delay=1)
return model_w2v
def plot_embaded_graph(self,model,word_list):
X = model.wv[word_list]
# reduce dimensions to 2
pca = PCA(n_components=2)
result = pca.fit_transform(X)
plt.figure(figsize=(12,9))
# create a scatter plot of the projection
plt.scatter(result[:, 0], result[:, 1])
for i, word in enumerate(word_list):
plt.annotate(word, xy=(result[i, 0], result[i, 1]))
plt.show()