-
Notifications
You must be signed in to change notification settings - Fork 48
/
metrics.py
205 lines (182 loc) · 7.7 KB
/
metrics.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
"""
Copright © 2023 Howard Hughes Medical Institute, Authored by Carsen Stringer and Marius Pachitariu.
"""
from sklearn.manifold import SpectralEmbedding, Isomap, LocallyLinearEmbedding
import time
from scipy.stats import zscore, spearmanr
from multiprocessing import Pool
from scipy.spatial.distance import pdist
import numpy as np
import scipy
from openTSNE import TSNE, affinity, TSNEEmbedding
from umap import UMAP
def emb_to_idx(emb):
if emb.ndim==2:
embs = emb[:,0]
else:
embs = emb
isort = embs.argsort()
idx = np.zeros_like(isort)
idx[isort] = np.arange(0, len(isort))
return idx
def triplet_order(gt, emb):
""" benchmarking triplet score for embedding with ground truth"""
if (gt<1).sum() == len(gt):
idx_gt = emb_to_idx(gt)
idx_emb = emb_to_idx(emb)
nn = len(idx_gt)
correct_triplets = 0
nrand = nn * 10
for i in range(nrand):
i_gt = np.random.choice(nn, size=3, replace=False)
i_gt = i_gt[i_gt.argsort()]
triplet_gt = np.array([np.nonzero(idx_gt==k)[0][0] for k in i_gt])
triplet_emb = idx_emb[triplet_gt]
if ((triplet_emb[0] < triplet_emb[1] and triplet_emb[1] < triplet_emb[2]) or
(triplet_emb[0] > triplet_emb[1] and triplet_emb[1] > triplet_emb[2])):
correct_triplets += 1
return correct_triplets / nrand
else:
n_modules = int(np.ceil(gt.max()))
triplet_scores = np.zeros(n_modules)
for k in range(n_modules):
inds = np.floor(gt) == k
triplet_scores[k] = triplet_order(gt[inds]-k, emb[inds])
return triplet_scores
def embedding_contamination(gt, emb):
""" benchmarking contamination score for embedding with ground truth"""
n_modules = int(np.ceil(gt.max()))
contamination_scores = np.zeros(n_modules)
isort = emb.flatten().argsort()
for k in range(n_modules):
imod = np.floor(gt) == k
in_mod = np.nonzero(imod)[0]
nn = len(in_mod)
nrand = nn * 10
nk = 0
for i in range(nrand):
i_gt = in_mod[np.random.choice(nn, size=2, replace=False)]
i0 = np.nonzero(isort == i_gt[0])[0][0]
i1 = np.nonzero(isort == i_gt[1])[0][0]
if np.abs(i1 - i0) > 5:
i0, i1 = min(i0, i1), max(i0, i1)
contamination_scores[k] += 1 - imod[isort[i0+1 : i1-1]].mean()
nk+=1
contamination_scores[k] /= nk
return contamination_scores
def benchmarks(xi_all, embs):
n_modules = int(np.ceil(xi_all.max()))
emb_rand = np.random.rand(len(embs[0]), 1).astype("float32")
contamination_scores = np.zeros((len(embs)+1, n_modules))
for k, emb in enumerate(embs):
contamination_scores[k] = embedding_contamination(xi_all, emb)
contamination_scores[k+1] = embedding_contamination(xi_all, emb_rand)
triplet_scores = np.zeros((len(embs)+1, n_modules))
for k, emb in enumerate(embs):
triplet_scores[k] = triplet_order(xi_all, emb)
triplet_scores[k+1] = triplet_order(xi_all, emb_rand)
return contamination_scores, triplet_scores
def embedding_quality_gt(gt, embs, knn=[10,50,100,200,500]):
""" benchmarking local and global scores for embedding with ground truth """
idx_gt = emb_to_idx(gt)[:,np.newaxis]
mnn = np.zeros((len(embs), len(knn)))
rho = np.zeros((len(embs),))
for k,emb in enumerate(embs):
idx_emb = emb_to_idx(emb)[:,np.newaxis]
mnn[k], rho[k] = embedding_quality(idx_gt, idx_emb, knn=knn)
return mnn, rho
def distance_matrix(Z, n_X=None, wrapping=False, correlation=False):
if wrapping:
#n_X = int(np.floor(Z.max() + 1))
dists = (Z - Z[:, np.newaxis, :]) % n_X
Zdist = (np.minimum(dists, n_X - dists)**2).sum(axis=-1)
else:
if correlation:
Zdist = Z @ Z.T
Z2 = 1e-10 + np.diag(Zdist)**.5
Zdist = 1 - Zdist / np.outer(Z2, Z2)
else:
#Zdist = ((Z - Z[:,np.newaxis,:])**2).sum(axis=-1)
Z2 = np.sum(Z**2, 1)
Zdist = Z2 + Z2[:, np.newaxis] - 2 * Z @ Z.T
Zdist = np.maximum(0, Zdist)
#import pdb; pdb.set_trace();
return Zdist
def embedding_quality(X, Z, knn=[10,50,100,200,500], subsetsize=2000,
wrapping=False, correlation=False, n_X=None):
""" changed correlation to False and n_X from 0 to None"""
from sklearn.neighbors import NearestNeighbors
np.random.seed(101)
if subsetsize < X.shape[0]:
subset = np.random.choice(X.shape[0], size=subsetsize, replace=False)
else:
subsetsize = X.shape[0]
subset = slice(0, X.shape[0])
Xdist = distance_matrix(X[subset], correlation=correlation)
Zdist = distance_matrix(Z[subset], n_X=n_X, wrapping=wrapping)
xd = Xdist[np.tril_indices(Xdist.shape[0], -1)]
zd = Zdist[np.tril_indices(Xdist.shape[0], -1)]
mnn = []
if not isinstance(knn, (np.ndarray, list)):
knn = [knn]
elif isinstance(knn, np.ndarray):
knn = list(knn)
for kni in knn:
nbrs1 = NearestNeighbors(n_neighbors=kni, metric="precomputed").fit(Xdist)
ind1 = nbrs1.kneighbors(return_distance=False)
nbrs2 = NearestNeighbors(n_neighbors=kni, metric="precomputed").fit(Zdist)
ind2 = nbrs2.kneighbors(return_distance=False)
intersections = 0.0
for i in range(subsetsize):
intersections += len(set(ind1[i]) & set(ind2[i]))
mnn.append(intersections / subsetsize / kni)
rho = spearmanr(xd, zd).correlation
return (mnn, rho)
def run_TSNE(U, perplexities=[30], metric="cosine", verbose=False):
if len(perplexities) > 1:
affinities_annealing = affinity.PerplexityBasedNN(
U,
perplexity=perplexities[1],
metric=metric,
n_jobs=16,
random_state=1,
verbose=verbose
)
embedding = TSNEEmbedding(
U[:,:1]*0.0001,
affinities_annealing,
negative_gradient_method="fft",
random_state=1,
n_jobs=16,
verbose=verbose
)
embedding1 = embedding.optimize(n_iter=250, exaggeration=12, momentum=0.5)
embedding2 = embedding1.optimize(n_iter=750, exaggeration=1, momentum=0.8)
affinities_annealing.set_perplexity(perplexities[0])
embeddingOPENTSNE = embedding2.optimize(n_iter=500, momentum=0.8)
else:
tsne = TSNE(
perplexity=perplexities[0],
metric=metric,
n_jobs=16,
random_state=1,
verbose=verbose,
n_components = 1,
initialization = .0001 * U[:,:1],
)
embeddingOPENTSNE = tsne.fit(U)
return embeddingOPENTSNE
def run_UMAP(U, n_neighbors=15, min_dist=0.1, metric="cosine"):
embeddingUMAP = UMAP(n_components=1, n_neighbors=n_neighbors, random_state=1,
min_dist=min_dist, init=U[:,:1], metric=metric).fit_transform(U)
return embeddingUMAP
def run_LE(U):
LE = SpectralEmbedding(n_components=1, n_jobs=16, random_state=1).fit(U)
return LE.embedding_
def run_LLE(U, n_neighbors=5):
LLE = LocallyLinearEmbedding(n_components=1, n_jobs=16, n_neighbors=n_neighbors, random_state=1).fit(U)
return LLE.embedding_
def run_isomap(U, n_neighbors=5, metric="cosine"):
IM = Isomap(n_components=1, n_jobs=16, n_neighbors=n_neighbors,
metric=metric).fit(U)
return IM.embedding_