-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMAS.py
More file actions
153 lines (124 loc) · 4.21 KB
/
MAS.py
File metadata and controls
153 lines (124 loc) · 4.21 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
import json
import networkx as nx
import random
from gensim.models import Word2Vec
import pickle
class MAS:
def __init__(
self,
g,
k,
num_permutations,
thresh_len,
# threshold,
dimensions,
window_size,
workers,
iter,
min_thresh=1,
seed=1,
) -> None:
self.g = g
self.k = k
self.num_permutations = num_permutations
self.thresh_len = thresh_len
self.min_thresh = min_thresh
# for Word2Vec
self.dimensions = dimensions
self.window_size = window_size
self.workers = workers
self.iter = iter
self.to_store_sentences = {i: [] for i in g.nodes()}
self.SEED = seed
self.walks = self.get_sentences()
self.model = self.get_model()
def get_sentences(self):
g = self.g
k = self.k
permutations = self.num_permutations
nodes = list(g.nodes())
sentences = []
for p in range(permutations):
print(f"PERMUTATION: {p}")
for node in nodes:
neighbours = list(nx.all_neighbors(g, node))
random.shuffle(neighbours)
len_n = len(neighbours)
if len_n:
for i in range(0, len_n, k):
start = min(i, max(0, len_n - k))
this_sentence = [node] + neighbours[start : start + k]
this_sentence = self.get_MAS(this_sentence)
sentences.append(this_sentence)
self.to_store_sentences[node].append(this_sentence)
else:
sentences.append([node])
self.to_store_sentences[node].append([node])
return sentences
def get_MAS(self, sentence):
"""
Get the MAS sequence from a given set of activated node and its k neighbours
"""
thresh_len = self.thresh_len
idx = 0
count_dict = {} # store current adjacencies
cur_nodes = set(sentence)
g = self.g
res = sentence
# initialise count_dict
for node in sentence:
this_neighbours = list(nx.all_neighbors(g, node))
for n in this_neighbours:
if n not in cur_nodes:
if n not in count_dict:
count_dict[n] = 0
count_dict[n] += 1
if not count_dict:
return sentence
while idx < thresh_len:
this_max = max(count_dict, key=count_dict.get)
del count_dict[this_max]
res.append(this_max)
cur_nodes.add(this_max)
idx += 1
this_neighbours = list(nx.all_neighbors(g, this_max))
for neigh in this_neighbours:
if neigh not in cur_nodes:
if neigh not in count_dict:
count_dict[neigh] = 0
count_dict[neigh] += 1
return res
def get_model(self):
"""
Get Work2Vec model
"""
model = Word2Vec(
self.walks,
vector_size=self.dimensions,
window=self.window_size,
min_count=0,
sg=1,
workers=self.workers,
epochs=self.iter,
seed=self.SEED,
)
return model
def get_embedding(self, u):
idx = self.model.wv.key_to_index[u]
return self.model.wv[idx]
def store_embeddings(self, path):
d = self.model.wv.key_to_index
embeds = self.model.wv
with open(f"{path}_embeddings_mapping_MAS.json", "w", encoding="utf-8") as f:
json.dump(d, f, indent=4)
with open(f"{path}_embeddings_MAS.pickle", "wb") as handle:
pickle.dump(embeds, handle, protocol=pickle.HIGHEST_PROTOCOL)
def store_sequences(self, path):
"""
To store data about adjacency list to create MAS-graph
"""
s = self.to_store_sentences
with open(f"{path}_sequences.json", "w", encoding="utf-8") as f:
json.dump(s, f, indent=4)
print(f">> INFO: Stored sequences at {path}_sequences.json")
return 0