-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsnap_node2vec.py
139 lines (127 loc) · 5.31 KB
/
snap_node2vec.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
from gem.embedding.node2vec import node2vec
from subprocess import call
import tempfile
from gem.utils import graph_util
from time import time
import sys,os
import gzip
HOME = os.path.expanduser("~")
def loadWalks(file_name):
walks = []
with open(file_name, 'r') as f:
for line in f:
walk = list(map(int,line.strip().split()))
walks.append(walk)
return walks
class snap_node2vec(node2vec):
def learn_embedding(self, graph=None, edge_f=None,
is_weighted=False, no_python=False, directed=False):
args = [HOME+"/snap/examples/node2vec/node2vec"]
if directed == False:
graph_mod = graph.to_undirected()
elif directed ==True:
graph_mod = graph
with tempfile.TemporaryDirectory(dir = './') as dname:
original_graph = dname + '/node2vec_test.graph'
emb_result = dname + '/node2vec_test.emb'
graph_util.saveGraphToEdgeListTxtn2v(graph_mod, original_graph)
args.append("-i:%s" % original_graph)
args.append("-o:%s" % emb_result)
args.append("-d:%d" % self._d)
args.append("-l:%d" % self._walk_len)
args.append("-r:%d" % self._num_walks)
args.append("-k:%d" % self._con_size)
args.append("-e:%d" % self._max_iter)
args.append("-p:%f" % self._ret_p)
args.append("-q:%f" % self._inout_p)
args.append("-v")
if directed ==True:
args.append("-dr")
if is_weighted ==True:
args.append("-w")
t1 = time()
try:
call(args)
except Exception as e:
print(str(e))
raise Exception('node2vec not found. Please compile snap, place node2vec in the system path and grant executable permission')
self._X = graph_util.loadEmbedding(emb_result)
t2 = time()
return self._X, (t2 - t1)
def sample_random_walks(self, graph=None, edge_f=None,
is_weighted=False, no_python=False, directed=False):
args = [HOME+"/snap/examples/node2vec/node2vec"]
if directed == False:
graph_mod = graph.to_undirected()
elif directed ==True:
graph_mod = graph
with tempfile.TemporaryDirectory(dir = './') as dname:
original_graph = dname + '/node2vec_test.graph'
emb_result = dname + '/node2vec_test.walks'
graph_util.saveGraphToEdgeListTxtn2v(graph_mod, original_graph)
args.append("-i:%s" % original_graph)
args.append("-o:%s" % emb_result)
args.append("-d:%d" % self._d)
args.append("-l:%d" % self._walk_len)
args.append("-r:%d" % self._num_walks)
args.append("-k:%d" % self._con_size)
args.append("-e:%d" % self._max_iter)
args.append("-p:%f" % self._ret_p)
args.append("-q:%f" % self._inout_p)
args.append("-v")
if directed ==True:
args.append("-dr")
if is_weighted ==True:
args.append("-w")
args.append("-ow")
t1 = time()
try:
call(args)
except Exception as e:
print(str(e))
raise Exception('node2vec not found. Please compile snap, place node2vec in the system path and grant executable permission')
self._W = loadWalks(emb_result)
t2 = time()
return self._W, (t2 - t1)
def save_random_walks(self, graph=None, edge_f=None, is_weighted=False, no_python=False, directed=False,
save_directory=None, file_name=None, compress=False):
args = [HOME+"/snap/examples/node2vec/node2vec"]
if directed == False:
graph_mod = graph.to_undirected()
elif directed ==True:
graph_mod = graph
os.makedirs(save_directory, exist_ok=True)
original_graph = save_directory + '/node2vec_test.graph'
emb_result = save_directory + file_name
graph_util.saveGraphToEdgeListTxtn2v(graph_mod, original_graph)
args.append("-i:%s" % original_graph)
args.append("-o:%s" % emb_result)
args.append("-d:%d" % self._d)
args.append("-l:%d" % self._walk_len)
args.append("-r:%d" % self._num_walks)
args.append("-k:%d" % self._con_size)
args.append("-e:%d" % self._max_iter)
args.append("-p:%f" % self._ret_p)
args.append("-q:%f" % self._inout_p)
args.append("-v")
if directed ==True:
args.append("-dr")
if is_weighted ==True:
args.append("-w")
args.append("-ow")
t1 = time()
try:
call(args)
except Exception as e:
print(str(e))
raise Exception('node2vec not found. Please compile snap, place node2vec in the system path and grant executable permission')
call('rm ' + original_graph, shell=True)
if compress:
f_in = open(emb_result)
f_out = gzip.open(emb_result + '.gz', 'wt')
f_out.writelines(f_in)
f_out.close()
f_in.close()
call('rm ' + emb_result, shell=True)
t2 = time()
return (t2 - t1)