-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmodel.py
501 lines (408 loc) · 15.6 KB
/
model.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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
# -*- coding: utf-8 -*-
"""
model - the multiscale temporal aggregation implementation with the hierarchy
"""
# Author: Eren Cakmak <eren.cakmak@uni-konstanz.de>
#
# License: MIT
import pickle
import datetime
from collections import Counter
import math
import networkx as nx
import numpy as np
from sklearn.preprocessing import normalize
hierarchy = None
dataset_paths = [[
'data/synthetic_data.pkl', 'data/synthetic_data_embeddings_100.pkl'
], ['data/synthetic_data.pkl', 'data/synthetic_data_embeddings_1000.pkl'],
['data/reddit_graphs.pkl', 'data/reddit_embeddings.pkl']]
default_pixel_bars = 200
def load_data(index):
"""Load the graph data with the vectors.
Keyword arguments:
index -- index to the list of dataset paths
"""
global hierarchy
graph_file_path = dataset_paths[index][0]
graph_embeddings_path = dataset_paths[index][1]
with open(graph_file_path, 'rb') as f:
graphs = pickle.load(f)
with open(graph_embeddings_path, 'rb') as f:
embeddings = pickle.load(f)
hierarchy = Hierarchy(graphs, embeddings)
print('Data loading done.')
class Hierarchy:
def __init__(self, graphs, embeddings):
"""Initialize the hierarchy from a list of graphs.
Keyword arguments:
graphs -- list of networkX graphs
embeddings -- all the embeddings
"""
self.graphs = graphs
self.levels = {}
self.height = 1
keys = np.array(embeddings.pop('keys', None))
self.graph_embedding_names = []
# normalization of the embeddings
for key, value in embeddings.items():
self.graph_embedding_names.append(key)
# L2 normalization
embeddings[key] = normalize(value)
self.embeddings = embeddings
# zoom level and zoom data
self.selected_embedding_name = self.graph_embedding_names[0]
self.zoom_level = []
self.zoom_data = []
window = 1
while window < len(self.graphs):
# get the embeddings for the level
level_vectors = {}
for key, value in self.embeddings.items():
level_vectors[key] = value[np.flatnonzero(
np.core.defchararray.find(keys,
str(self.height) + '_') != -1)]
# window size
window = int(math.pow(2, (self.height - 1)))
self.levels[self.height] = Level(self.graphs, self.height,
level_vectors)
# set zoom levels and zoom data
# per default it is first time more default_pixel_bars time teps
if len(self.zoom_level) == 0:
if self.levels[self.height].get_number_snapshots(
) <= default_pixel_bars:
# get the number of embeddings in the initial level
level_len = len(self.levels[self.height].get_embeddings(
self.selected_embedding_name))
# zoom level is used for storing the zoom levels
self.zoom_level = np.full(level_len, self.height)
self.height = self.height + 1
self.update_data()
def __repr__(self):
return str(self.levels)
def __str__(self):
return str(self.levels)
def get_hierarchy_meta(self):
"""Return the hierarchy as a dict
"""
level_dict = {}
for key, l in self.levels.items():
level_dict[l.level] = {
'level': l.level,
'window_size': l.window_size
}
# time
time1 = datetime.datetime.combine(
self.graphs[0].graph['time'][0],
datetime.time(self.graphs[0].graph['time'][1]))
time2 = datetime.datetime.combine(
self.graphs[-1].graph['time'][0],
datetime.time(self.graphs[-1].graph['time'][1]))
return {
'height': self.height,
'time_steps': len(self.graphs),
'levels': level_dict,
'time_1': time1,
'time_2': time2
}
def get_snapshot(self, level, num):
"""Return the snapshot (level,num)
"""
if level > self.height:
print('Hierarchy height overflow')
return None
level = self.levels[level]
return level.get_snapshot(num)
def check_snapshot(
self,
level,
num,
):
"""Return bool if level contains an element at position num
"""
if level > self.height:
return False
level = self.levels[level]
return level.check_snapshot(num)
def get_embeddings(self):
"""Return the current embeddings
"""
return self.zoom_data.tolist()
def get_embedding_names(self):
"""Get the embeddings names
"""
return self.graph_embedding_names
def load_embedding(self, name):
"""Set the embedding
"""
self.selected_embedding_name = name
self.update_data()
def get_zoom_levels(self):
"""get the zoom levels
"""
time_intervals = []
time = datetime.datetime.combine(
self.graphs[0].graph['time'][0],
datetime.time(self.graphs[0].graph['time'][1]))
# iterate and find levels
for i, l in enumerate(self.zoom_level):
time1, time2 = self.levels[l].get_interval(time)
time_intervals.append([str(time1), str(time2)])
time = time2
return {'data': self.zoom_level.tolist(), 'time': time_intervals}
def update_data(self):
"""Update the zoom data
"""
self.zoom_data = []
time = datetime.datetime.combine(
self.graphs[0].graph['time'][0],
datetime.time(self.graphs[0].graph['time'][1]))
for l in self.zoom_level:
embedding, time = self.levels[l].get_one_embedding(
self.selected_embedding_name, time)
self.zoom_data.append(embedding)
self.zoom_data = np.array(self.zoom_data)
def zoom_in(self, indx):
"""Zoom in
"""
new_zoom = []
time = datetime.datetime.combine(
self.graphs[0].graph['time'][0],
datetime.time(self.graphs[0].graph['time'][1]))
# iterate and find levels
for i, l in enumerate(self.zoom_level):
time1, time = self.levels[l].get_interval(time)
if i in indx and l > 1:
new_level = l - 1
num_snaps = self.levels[new_level].num_snaps_in_interval(
time1, time)
new_zoom.extend([new_level] * num_snaps)
else:
new_zoom.append(l)
# update the zoom level and reload data
self.zoom_level = np.array(new_zoom)
self.update_data()
def zoom_out(self, indx):
"""zoom out
"""
new_zoom = []
time = datetime.datetime.combine(
self.graphs[0].graph['time'][0],
datetime.time(self.graphs[0].graph['time'][1]))
drill_up_intervals = []
# iterate and find levels
for i, l in enumerate(self.zoom_level):
time1, time = self.levels[l].get_interval(time)
if i in indx and l < self.height:
drill_up_intervals.append(self.levels[l + 1].is_in_interval(
time1, time))
# get first pop up interval
time3, time4 = drill_up_intervals.pop(0)
time = datetime.datetime.combine(
self.graphs[0].graph['time'][0],
datetime.time(self.graphs[0].graph['time'][1]))
skip = False
for i, l in enumerate(self.zoom_level):
time1, time2 = self.levels[l].get_interval(time)
# pop if the interval if larger then the end of the
# drill up interval
if time1 >= time4 and len(drill_up_intervals):
time3, time4 = drill_up_intervals.pop(0)
# zoom levels time is lower than the drill up interval
if time1 < time3 and time2 < time4:
new_zoom.append(l)
skip = False
# level is in the zoom level interval
elif time3 <= time1 <= time2 <= time4:
if not skip and self.height >= l + 1:
new_zoom.append(l + 1)
skip = True
# this is called if all drill up intervals are popped
elif time1 >= time4:
new_zoom.append(l)
time = time2
# update the zoom level and reload data
self.zoom_level = np.array(new_zoom)
self.update_data()
def get_graph(self, indx):
"""Get the summary graph for the indices
Keyword arguments:
indx -- positions of the graphs in the zoom level
"""
time = datetime.datetime.combine(
self.graphs[0].graph['time'][0],
datetime.time(self.graphs[0].graph['time'][1]))
graphs = []
# iterate and find levels
for i, l in enumerate(self.zoom_level):
time1, time2 = self.levels[l].get_interval(time)
if i in indx:
G = self.levels[l].get_graph(time1)
graphs.append(G)
time = time2
# union / intersection / disjoint for graphs
# occurences of nodes over time in a dict
nodes = []
for g in graphs:
nodes.append(g.nodes)
# get number of occurences
node_occ = Counter(x for xs in nodes for x in set(xs))
# Union graph
G = nx.Graph()
for graph in graphs:
G.add_nodes_from(graph.nodes(data=True))
G.add_edges_from(graph.edges(data=True))
# intersection
nodes_dict = {
x: node_occ[x]
for x in node_occ if node_occ[x] == len(graphs)
}
H = G.subgraph([*nodes_dict])
nx.set_node_attributes(H, 1, 'intersection')
nx.set_edge_attributes(H, 1, 'intersection')
G.add_nodes_from(H)
G.add_edges_from(H.edges)
# disjoint
nodes_dict = {
x: node_occ[x]
for x in node_occ if node_occ[x] < len(graphs)
}
I = G.subgraph([*nodes_dict])
nx.set_node_attributes(I, 1, 'disjoint')
nx.set_edge_attributes(I, 1, 'disjoint')
G.add_nodes_from(I)
G.add_edges_from(I.edges)
G.graph['time'] = [graphs[0].graph['time'], graphs[-1].graph['time']]
return G
class Level:
def __init__(self, graphs, level, embeddings):
"""Initialize a level from from a list of graphs.
Keyword arguments:
graphs -- list of networkX graphs
level -- number for the level used to create window size
embeddings -- embeddings of the level
"""
self.graphs = graphs
self.level = level
self.window_size = int(math.pow(2, (level - 1)))
self.embeddings = embeddings
# initialize the snapshots
if self.window_size < 1:
raise ValueError('Window size of level below 1')
if self.window_size > 0:
self.snapshots = []
indx = 0 # index for vectors
for i in range(0, len(self.graphs), self.window_size):
# get the vectors
self.snapshots.append(
Snapshot(self.graphs, i, i + self.window_size))
indx = indx + 1
def __repr__(self):
return 'Level: ' + str(self.level) + ' - ' + str(self.window_size)
def __str__(self):
return 'Level: ' + str(self.level) + ' - ' + str(self.window_size)
def get_snapshot(self, num):
"""Return the snapshot (num) of type of graph
"""
if num > len(self.snapshots):
print('Snapshot number is bigger than level')
return None
return self.snapshots[num].get_snapshot()
def check_snapshot(self, num):
"""Return true if the snapshot is in the level
"""
if num < 0 or num >= len(self.snapshots):
return False
return True
def get_embeddings(self, embedding_name):
"""Return the embeddings
"""
return self.embeddings[embedding_name].tolist()
def get_one_embedding(self, embedding_name, time):
"""Return the embeddings
"""
for index, snap in enumerate(self.snapshots):
time1, time2 = snap.get_time()
# the second return var is the next time interval which is input
if time < time1:
return self.embeddings[embedding_name][index], time2
return self.embeddings[embedding_name][-1], time2
def get_number_snapshots(self):
"""Return the embeddings
"""
return len(self.snapshots)
def num_snaps_in_interval(self, time1, time2):
""" Get the time steps
"""
num_snaps = 0
for snap in self.snapshots:
(time3, time4) = snap.get_time()
if time1 <= time3 and time4 <= time2:
num_snaps = num_snaps + 1
return num_snaps
def get_interval(self, time):
""" gets the interval which includes time1 and time2
"""
for snap in self.snapshots:
(time1, time2) = snap.get_time()
if time < time1:
# if time1 <= time <= time2:
return time1, time2
return time1, time2
def is_in_interval(self, time1, time2):
""" gets the interval which includes time1 and time2
"""
num_snaps = 0
for snap in self.snapshots:
(time3, time4) = snap.get_time()
if time3 <= time1 and time2 <= time4:
return time3, time4
def get_graph(self, time):
""" gets the interval which includes time1 and time2
"""
for snap in self.snapshots:
(time1, time2) = snap.get_time()
if time <= time1:
# if time1 <= time <= time2:
return snap.get_snapshot()
class Snapshot:
def __init__(self, graphs, indx1, indx2):
"""Initialize snapshot from a list of graphs.
Keyword arguments:
graphs -- list of networkX graphs
indx1 -- first index in the overall graph list
indx2 -- last index in the overall graph list
"""
self.graphs = graphs[indx1:indx2]
self.indx1 = indx1
self.indx2 = indx2
self.time1 = datetime.datetime.combine(
self.graphs[0].graph['time'][0],
datetime.time(self.graphs[0].graph['time'][1])) # .item()
self.time2 = datetime.datetime.combine(
self.graphs[-1].graph['time'][0],
datetime.time(self.graphs[-1].graph['time'][1])) # .item()
self.duration = self.time2 - self.time1
self.union_g = None
def __repr__(self):
return 'Snapshot: ' + str(self.time1) + ' - ' + str(self.time2)
def __str__(self):
return 'Snapshot: ' + str(self.time1) + ' - ' + str(self.time2)
def union_graph(self):
# if already computed just return
if not self.union_g:
G = nx.Graph()
for graph in self.graphs:
G.add_nodes_from(graph.nodes(data=True))
G.add_edges_from(graph.edges(data=True))
G.graph['time'] = [self.time1, self.time2]
self.union_g = G
return self.union_g
def get_snapshot(self):
"""Return the snapshot of type of graph.
"""
return self.union_graph()
def get_time(self):
"""The time duration of the snapshot
"""
return (self.time1, self.time2)