-
Notifications
You must be signed in to change notification settings - Fork 0
/
mnc.py
50 lines (38 loc) · 1.71 KB
/
mnc.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
import scipy.sparse as sps
import numpy as np
def get_counterpart(alignment_matrix):
counterpart_dict = {}
if not sps.issparse(alignment_matrix):
sorted_indices = np.argsort(alignment_matrix)
n_nodes = alignment_matrix.shape[0]
for node_index in range(n_nodes):
if sps.issparse(alignment_matrix):
row, possible_alignments, possible_values = sps.find(alignment_matrix[node_index])
node_sorted_indices = possible_alignments[possible_values.argsort()]
else:
node_sorted_indices = sorted_indices[node_index]
counterpart = node_sorted_indices[-1]
counterpart_dict[node_index] = counterpart
return counterpart_dict
def score_MNC(alignment_matrix, adj1, adj2):
mnc = 0
if sps.issparse(alignment_matrix): alignment_matrix = alignment_matrix.toarray()
if sps.issparse(adj1): adj1 = adj1.toarray()
if sps.issparse(adj2): adj2 = adj2.toarray()
counter_dict = get_counterpart(alignment_matrix)
node_num = alignment_matrix.shape[0]
for i in range(node_num):
a = np.array(adj1[i, :])
one_hop_neighbor = np.flatnonzero(a)
b = np.array(adj2[counter_dict[i], :])
# neighbor of counterpart
new_one_hop_neighbor = np.flatnonzero(b)
one_hop_neighbor_counter = []
for count in one_hop_neighbor:
one_hop_neighbor_counter.append(counter_dict[count])
num_stable_neighbor = np.intersect1d(new_one_hop_neighbor, np.array(one_hop_neighbor_counter)).shape[0]
union_align = np.union1d(new_one_hop_neighbor, np.array(one_hop_neighbor_counter)).shape[0]
sim = float(num_stable_neighbor) / union_align
mnc += sim
mnc /= node_num
return mnc