-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathETNgen.py
361 lines (279 loc) · 10.2 KB
/
ETNgen.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
import construction as cs
from ETN import *
# given:
# "0b001001001" return ["001","001","001"]
def split_etns(etns,k):
splitted_etns = []
for i in range(0,len(etns),k):
splitted_etns.append(etns[i:i+k])
return(splitted_etns)
# given
# ETNS, return
# dict 10x _--> 101 100001001001 etc
def get_dict(ETNS,k,return_statistics=False):
'''It generates the dictionary. It takes as input k and ETNS, which is a
dictionary (referring for instance to one hour of the day, or better to
local_split) with keys the signatures of the original graph and for values
their nb of occurrences. The dictionary that it creates puts together the
ETNSs that have the same key. Then it transforms frequencies into
probabilities.
New dictionary example:
key: 11x
value: [[110,111],[0.7,0.3]]
'''
# new_node == 001,01,0001,00001
new_node = "0"*k+"1"
ETNS_list = list(ETNS.keys())
diz = dict()
for etns in ETNS_list:
etns = etns[2:]
if len(etns) % (k+1) == 0:
splitted_etns = split_etns(etns,k+1)
key = ""
for etn in splitted_etns:
if not etn == new_node: # per il caso 001 non serve creare la key, che è semplicemente ""
key = key + etn[0:k]+"x"
if key in diz: # append to diz the new pruned signature with its nb. of occurrences
diz[key][0].append("0b"+etns)
diz[key][1].append(ETNS["0b"+etns])
else:
diz[key] = [["0b"+etns],[ETNS["0b"+etns]]]
statistics = 0
for key,value in diz.items():
summ = sum(diz[key][1]) # from frequences to probabilities
statistics += summ
c = 0
for val in value[1]:
diz[key][1][c] = diz[key][1][c]/summ
c = c + 1
if return_statistics:
return diz,statistics
else:
return diz
# from etns to key
# input: 101-011 --> 01x-11x
def create_key(etns,k):
if len(etns) == 0:
return ""
else:
return "x".join(split_etns(etns,k))+"x"
# counts etns only for a given node
def count_ETN_given_node(graphs,k,node):
'''It returns all the etns (at each timestamp) of a given node'''
v = node
etn = build_ETN(graphs[:k+1],v) # etn is a nx.graph representing the motif with node v as ego
if not etn == None:
etns,node_encoding = get_ETNS_with_encoding(etn) # etns is the corresponding signature and node_encoding tells the node identity
return (etns[2:],node_encoding)
# get ETNS and node generating etns
def get_ETNS_with_encoding(ETN):
'''It translates the ETN (a nx.graph) in signature. It return the signature etns and
node_encoding, which tells at which node the signature is referring.
For instance:
ETNS,node_encoding = 1011 {'65': '10', '56': '11'}
means that node n has had an interaction 10 with node 65 and
an interaction 11 with node 56.
'''
nodes = list(ETN.nodes())
nodes_no_ego = []
ids_no_ego = []
lenght_ETNS = 0
for n in nodes:
if not ("*" in n):
nodes_no_ego.append(n)
if not(n.split("_")[0] in ids_no_ego):
ids_no_ego.append(n.split("_")[0])
else:
ego = int(n.split("*")[0])
lenght_ETNS = lenght_ETNS + 1
node_encoding = get_node_encoding(ids_no_ego,nodes_no_ego,lenght_ETNS)
for k in node_encoding.keys():
node_encoding[k] = ''.join(str(e) for e in node_encoding[k])
binary_node_encodings = list(node_encoding.values())
binary_node_encodings.sort()
ETNS = '0b'+''.join(e for e in binary_node_encodings)
return(ETNS,node_encoding)
import random
# givend diz and key:
# return a key according to the probablity.
def get_random_etns(diz,key,k):
'''It returns a new etns (like 101), given a key (like 10x), according to the probability stored in the dictionary.'''
if (key in diz):
etns = diz[key][0]
prob = diz[key][1]
cumuative = [sum(prob[0:i+1]) for i in range(len(prob))]
cumuative = [0]+cumuative
r = random.random()
for i in range(len(cumuative)-1):
if r >= cumuative[i] and r < cumuative[i+1]:
return etns[i]
elif (key[:-(k+1)] in diz): # approximate key if the original key is not found
key = key[:-(k+1)]
etns = diz[key][0]
prob = diz[key][1]
cumuative = [sum(prob[0:i+1]) for i in range(len(prob))]
cumuative = [0]+cumuative
r = random.random()
for i in range(len(cumuative)-1):
if r >= cumuative[i] and r < cumuative[i+1]:
return etns[i]
else:
return None
# create edge_list_g2:
# given etns2 and etns3 merge them to create edge_list in g2
def create_edge_g2(n,etns3,node_encoding,k):
'''
It returns the list of edges to add at the (k+1)-th layer, given node n and the
egocentric neighborhood etns3.
Example:
n = 36
etns3 = '0b100100100111'
node_enc = {'99': '10', '56': '10', '65': '11', '32': '10'}
Returns [(36, 65)]
'''
new_node = k*"0"+"1"
edges = []
for split in split_etns(etns3[2:],k+1):
if split == new_node:
edges.append((n,"x"))
elif split[-1] == "1":
# search node correspondensy in node_embedding and remove:
for key,value in node_encoding.items():
if value == split[:k]:
edges.append((n,int(key)))
node_encoding[key] = "USED"
return edges
# split ("x","a") and ("a","b")
def split_stub(e):
'''Given a list of edges, it discerns between directed edges and stubs'''
edges = []
stubs = []
for i,j in e:
if i == "x" or j == "x":
stubs.append((i,j))
else:
edges.append((i,j))
return edges,stubs
########### originale
'''
def get_edges_to_keep(edges,alpha=0.5):
nb_edges = int(len(edges)*alpha)
edges_to_keep = []
# check double directions
for i,j in edges:
if (j,i) in edges:
if (i,j) not in edges_to_keep and (j,i) not in edges_to_keep:
edges_to_keep.append((i,j))
old = nb_edges
nb_edges = nb_edges - len(edges_to_keep)
print(old,nb_edges)
# remove taken edges:
for i,j in edges_to_keep:
edges.remove((i,j))
edges.remove((j,i))
np.random.shuffle(edges)
edges_to_keep.extend(edges[0:nb_edges])
# take only 0.5 edges
np.random.shuffle(edges_to_keep)
edges_to_keep = edges_to_keep[0:nb_edges]
return edges_to_keep
'''
################ TEST
def get_edges_to_keep(edges,alpha=0.5):
'''Given the list of directed edges, it returns only the bidirectional ones
+ a fraction alpha of the others.
'''
edges_to_keep = []
#print('Initial nb of edges:', len(edges))
for i,j in edges:
if (j,i) in edges:
if (i,j) not in edges_to_keep and (j,i) not in edges_to_keep:
edges_to_keep.append((i,j))
edges.remove((i,j))
edges.remove((j,i))
nb_edges = len(edges)
#print('Bidirectional edges to keep:',len(edges_to_keep))
#print('Nb of remaining edges:', len(edges))
nb_edges = int(len(edges)*alpha) # si approssima per difetto, che succede se si approssima per eccesso?
#print('Nb of remaining edges we want to keep:', nb_edges)
np.random.shuffle(edges)
edges_to_keep.extend(edges[0:nb_edges])
#print('Total nb of edges to keep:',len(edges_to_keep))
return edges_to_keep
# merge stubs
# take in input [(91, 'x'), (42, 'x'), (60, 'x'), (78, 'x'), (77, 'x'), (21, 'x')]
# return (91,42),(60,21) etc
def merge_stubs(stubs):
np.random.shuffle(stubs)
edges = []
node_seq = []
for i in range(len(stubs)):
u = stubs[i][0]
node_seq.append(u)
flag = True
while flag:
if len(node_seq) >= 2:
u = np.random.choice(node_seq)
node_seq.remove(u)
v = np.random.choice(node_seq)
node_seq.remove(v)
if not u == v:
edges.append((u,v))
else:
node_seq.append(u)
node_seq.append(v)
if len(np.unique(node_seq))==1:
flag = False
else:
flag = False
return edges
# merge edges and stubs
def get_edges_g2(edges,alpha):
edges,stubs = split_stub(edges)
edges_g2 = get_edges_to_keep(edges,alpha)
edges_g2.extend(merge_stubs(stubs))
return edges_g2
# given node ed edges build graph g3
def build_graph_g2(edges,nodes):
g2 = nx.Graph()
g2.add_nodes_from(nodes)
g2.add_edges_from(edges)
return g2
# given g0,g1,diz and nodes retrung a nx graph
def generate_graph_g2(nodes,graphs,diz,k,alpha):
'''It generates a new temporal layer, given the previous k, the dictionary and the nodes list'''
edges = []
for n in graphs[0].nodes(): #build provisional layer
etns2,node_enc = count_ETN_given_node(graphs,k,n) # etns of the last k layers for node n
key = create_key(etns2,k)
etns3 = get_random_etns(diz,key,k) #i.e. etns3 = '0b100'
if not etns3 == None:
edges.extend(create_edge_g2(n,etns3,node_enc,k))
edges_g2 = get_edges_g2(edges,alpha)
g2 =build_graph_g2(edges_g2,nodes)
return g2
#given seed and keys generate temporal graph
def generate_temporal_graph(nb_graphs, graph_seed,diz,k,alpha):
nodes = list(graph_seed[0].nodes())
tg = graph_seed
for i in range(nb_graphs-1):
graphs_in = tg[i:i+k]
g_new = generate_graph_g2(nodes,graphs_in,diz,k,alpha)
tg.append(g_new)
return tg
# generate a single graph_seed given the previous graphs
def generate_seed_graph(graphs,graphs_seed,k,alpha):
nodes = list(graphs_seed[0].nodes())
ETNS = count_ETN(graphs,k)
ETNS = {k: v for k, v in sorted(ETNS.items(),reverse=True, key=lambda item: item[1])}
ETNS_list = list(ETNS.keys())
diz = get_dict(ETNS,k)
new_g = generate_graph_g2(nodes,graphs_seed,diz,k,alpha)
graphs_seed.append(new_g)
return graphs_seed
# generate k graph seed given g0
def generate_seed_graphs(g0,graphs,k,alpha):
graphs_seed = [g0]
for i in range(k-1):
graphs_seed = generate_seed_graph(graphs,graphs_seed,i+1,alpha)
return graphs_seed