-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmetapath2vec.py
54 lines (42 loc) · 1.63 KB
/
metapath2vec.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
import random
class Graph:
def __init__(self, nx_G):
self.G = nx_G
def metapath2vec_walk(self, walk_length, start_node, metapath):
"""
Simulate a random walk starting from start node.
"""
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, metapath):
"""
Repeatedly simulate random walks from each node.
"""
G = self.G
walks = []
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.metapath2vec_walk(walk_length=walk_length, start_node=start_node, metapath=metapath))
good = 0
for walk in walks:
print(len(walk))
if len(walk) == walk_length:
good += 1
print("Correct length walks: %d/%d" % (good, len(walks)))
return walks