forked from facebookresearch/PoincareMaps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathembedding_quality_score.py
executable file
·188 lines (139 loc) · 4.56 KB
/
embedding_quality_score.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
import numpy as np
from sklearn.metrics import pairwise_distances
import matplotlib.pyplot as plt
from poincare_maps import *
import timeit
from sklearn.utils.graph_shortest_path import graph_shortest_path
from scipy.sparse import csgraph
from sklearn.neighbors import kneighbors_graph
from data import connect_knn
def get_scalars(qs):
lcmc = np.copy(qs)
N = len(qs)
for j in range(N):
lcmc[j] = lcmc[j] - j/N
K_max = np.argmax(lcmc) + 1
Qlocal = np.mean(qs[:K_max])
Qglobal = np.mean(qs[K_max:])
return Qlocal, Qglobal, K_max
def get_rank_high(data, k_neighbours = 15, knn_sym=True):
# computes ranking of the original dataset through geodesic distances
KNN = kneighbors_graph(data, k_neighbours,
mode='distance',
include_self=False).toarray()
if knn_sym:
KNN = np.maximum(KNN, KNN.T)
n_components, labels = csgraph.connected_components(KNN)
print(n_components)
D_high = graph_shortest_path(KNN)
if n_components:
max_dist = np.max(D_high)*10
for comp in np.unique(labels):
ix_comp = np.where(labels == comp)[0]
ix_not_comp = np.where(labels != comp)[0]
for i in ix_comp:
for j in ix_not_comp:
D_high[i, j] = max_dist
D_high[j, i] = max_dist
Rank_high = get_ranking(D_high)
return Rank_high
# def get_rank_high(data, k_neighbours = 15, knn_sym=True):
# # computes ranking of the original dataset through geodesic distances
# KNN = kneighbors_graph(data, k_neighbours,
# mode='distance',
# include_self=False).toarray()
# if knn_sym:
# KNN = np.maximum(KNN, KNN.T)
# n_components, labels = csgraph.connected_components(KNN)
# if (n_components > 1):
# print('Connecting', n_components)
# distances = pairwise_distances(data, metric='euclidean')
# KNN = connect_knn(KNN, distances, n_components, labels)
# D_high = graph_shortest_path(KNN)
# Rank_high = get_ranking(D_high)
# return Rank_high
def get_ranking(D):
start = timeit.default_timer()
n = len(D)
Rank = np.zeros([n, n])
for i in range(n):
# tmp = D[i, :10]
idx = np.array(list(range(n)))
sidx = np.argsort(D[i, :])
Rank[i, idx[sidx][1:]] = idx[1:]-np.ones(n-1)
print(f"Ranking: time = {(timeit.default_timer() - start):.1f} sec")
return Rank
# def get_ranking(D):
# start = timeit.default_timer()
# n = len(D)
# Rank = np.zeros([n, n])
# for i in range(n):
# # tmp = D[i, :10]
# idx = list(range(n))
# sidx = np.argsort(D[i, :])
# for c, j in enumerate(sidx):
# Rank[i, idx[j]] = c
# print(f"Ranking: time = {(timeit.default_timer() - start):.3f} sec")
# return Rank
def Tk(M, T = "lower"):
c = 0
for i in range(len(M)):
for j in range(i+1, len(M)):
if T == "lower":
c += M[j, i]
if T == "upper":
c += M[i, j]
return(c)
# def get_coRanking(Rank_high, Rank_low):
# n = len(Rank_high)
# coRank = np.zeros([n-1, n-1])
# for k in range(n-1):
# for l in range(n-1):
# coRank[k, l] = len(np.where((Rank_high == (k+1)) & (Rank_low == (l+1)))[0])
# return coRank
def get_coRanking(Rank_high, Rank_low):
start = timeit.default_timer()
n = len(Rank_high)
coRank = np.zeros([n-1, n-1])
for i in range(n):
for j in range(n):
k = int(Rank_high[i, j])
l = int(Rank_low[i, j])
if (k > 0) and (l > 0):
coRank[k-1][l-1] += 1
print(f"Co-ranking: time = {(timeit.default_timer() - start):.2f} sec")
return coRank
def get_score(Rank_high, Rank_low, fname=None):
coRank = get_coRanking(Rank_high, Rank_low)
start = timeit.default_timer()
n = len(Rank_high) + 1
df_score = pd.DataFrame(columns=['Qnx', 'Bnx'])
Qnx = 0
Bnx = 0
for K in range(1, n-1):
Fk = list(range(K))
Qnx += sum(coRank[:K, K-1]) + sum(coRank[K-1, :K]) - coRank[K-1, K-1]
Bnx += sum(coRank[:K, K-1]) - sum(coRank[K-1, :K])
df_score.loc[len(df_score)] = [Qnx /(K*n), Bnx/(K*n)]
if not (fname is None):
df_score.to_csv(fname, sep = ',', index=False)
# print(df_score.mean()[['Qnx', 'Bnx']])
Qlocal, Q_global, Kmax = get_scalars(df_score['Qnx'].values)
print(f"Qlocal = {Qlocal:.2f}, Q_global = {Q_global:.2f}, Kmax = {Kmax}")
print(f"Time = {(timeit.default_timer() - start):.2f} sec")
return df_score
def get_quality_metrics(coord_high, coord_low, distance='E', fname=None):
D_high = pairwise_distances(coord_high)
if distance == 'E':
D_low = pairwise_distances(coord_low)
if distance == 'P':
print('Poincaré space')
model = PoincareMaps(coord_low)
model.get_distances()
D_low = model.distances
Rank_high = get_ranking(D_high)
print('Rank high')
Rank_low = get_ranking(D_low)
print('Rank low')
df_score = get_score(Rank_high, Rank_low, fname=fname)
return df_score