-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmultimetapath2vec.py
60 lines (47 loc) · 1.92 KB
/
multimetapath2vec.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
import random
class Graph:
def __init__(self, nx_G):
self.G = nx_G
def multimetapath2vec_walk(self, walk_length, start_node, metapath):
"""
Simulate a random walk starting from start node.
:param walk_length: length of walk
:param start_node: starting node
:param metapath: list of types of nodes to walk through
:return:
"""
G = self.G
walk = [start_node]
for i in range(1, walk_length):
current_node = walk[-1]
next_type_index = i % (len(metapath)-1)
next_type = metapath[next_type_index]
allowed_neighbors = [neighbor for neighbor in G.neighbors(current_node) if
G.nodes[neighbor]['type'] == next_type]
if not allowed_neighbors:
break
next_node = random.choice(allowed_neighbors)
walk.append(next_node)
return walk
def simulate_walks(self, num_walks, walk_length, metapaths):
"""
Repeatedly simulate random walks from each node.
"""
G = self.G
walks = []
good = 0
for i, metapath in enumerate(metapaths, start=1):
print ("Metapath %d/%d" %(i, len(metapaths)))
start_type = metapath[0]
start_nodes = [node for node in G.nodes() if G.nodes[node]['type'] == start_type]
print('Walk iteration:')
for walk_iter in range(num_walks):
print(str(walk_iter + 1), '/', str(num_walks))
random.shuffle(start_nodes)
for start_node in start_nodes:
walks.append(self.multimetapath2vec_walk(walk_length=walk_length, start_node=start_node, metapath=metapath))
for walk in walks:
if len(walk) == walk_length:
good += 1
print("Correct length walks: %d/%d" % (good, len(walks)))
return walks