forked from YXNan0110/RANA
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatcher.py
More file actions
57 lines (48 loc) · 1.49 KB
/
matcher.py
File metadata and controls
57 lines (48 loc) · 1.49 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
import numpy as np
def greedy_match(S):
"""
:param S: Scores matrix, shape MxN where M is the number of source nodes,
N is the number of target nodes.
:return: A dict, map from source to list of targets.
"""
S = S.T
m, n = S.shape
x = S.T.flatten()
min_size = min([m,n])
used_rows = np.zeros((m))
used_cols = np.zeros((n))
max_list = np.zeros((min_size))
row = np.zeros((min_size)) # target indexes
col = np.zeros((min_size)) # source indexes
ix = np.argsort(-x) + 1
matched = 1
index = 1
while(matched <= min_size):
ipos = ix[index-1]
jc = int(np.ceil(ipos/m))
ic = ipos - (jc-1)*m
if ic == 0: ic = 1
if (used_rows[ic-1] == 0 and used_cols[jc-1] == 0):
row[matched-1] = ic - 1
col[matched-1] = jc - 1
max_list[matched-1] = x[index-1]
used_rows[ic-1] = 1
used_cols[jc-1] = 1
matched += 1
index += 1
result = np.zeros(S.T.shape)
for i in range(len(row)):
result[int(col[i]), int(row[i])] = 1
return result
def top_k(S, k=1):
"""
S: scores, numpy array of shape (M, N) where M is the number of source nodes,
N is the number of target nodes
k: number of predicted elements to return
"""
top = np.argsort(-S)[:,:k]
result = np.zeros(S.shape)
for idx, target_elms in enumerate(top):
for elm in target_elms:
result[idx,elm] = 1
return result