forked from Graph-COM/Neural_Higher-order_Pattern_Prediction
-
Notifications
You must be signed in to change notification settings - Fork 0
/
position.py
63 lines (51 loc) · 1.92 KB
/
position.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
# from numba import jit
import numpy as np
import logging
numba_logger = logging.getLogger('numba')
numba_logger.setLevel(logging.WARNING)
# @jit(nopython=True)
def nodets2key(batch: int, node: int, ts: float):
key = '-'.join([str(batch), str(node), float2str(ts)])
return key
# @jit(nopython=True)
def float2str(ts):
return str(int(round(ts)))
def make_batched_keys(node_record, t_record):
batch = node_record.shape[0]
support = node_record.shape[1]
batched_keys = make_batched_keys_l(node_record, t_record, batch, support)
batched_keys = np.array(batched_keys).reshape((batch, support))
# batched_keys = np.array([nodets2key(b, n, t) for b, n, t in zip(batch_matrix.ravel(), node_record.ravel(), t_record.ravel())]).reshape(batch, support)
return batched_keys
# @jit(nopython=True)
def make_batched_keys_l(node_record, t_record, batch, support):
batch_matrix = np.arange(batch).repeat(support).reshape((-1, support))
# batch_matrix = np.tile(np.expand_dims(np.arange(batch), 1), (1, support))
batched_keys = []
for i in range(batch):
for j in range(support):
b = batch_matrix[i, j]
n = node_record[i, j]
t = t_record[i, j]
batched_keys.append(nodets2key(b, n, t))
return batched_keys
# @jit(nopython=True)
def anonymize(node_records, batch, M, walk_len):
new_node_records = np.zeros_like(node_records)
for i in range(batch):
for j in range(M):
seen_nodes = []
for w in range(walk_len):
index = list_index(seen_nodes, node_records[i, j, w])
if index == len(seen_nodes):
seen_nodes.append(node_records[i, j, w])
new_node_records[i, j, w] = index
return new_node_records
# @jit(nopython=True)
def list_index(arr, item):
count = 0
for e in arr:
if e == item:
return count
count += 1
return count