-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathutils.py
248 lines (223 loc) · 9.37 KB
/
utils.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
import os
import sys
import time
import math
import numpy as np
import random
import torch
import torch.nn as nn
import torch.nn.init as init
import torch.utils.data as data
from scipy.linalg import norm
from sklearn.cluster import KMeans
from sklearn.metrics.pairwise import euclidean_distances
def cluster_acc(y_true, y_pred):
"""
Calculate clustering accuracy. Require scikit-learn installed
# Arguments
y: true labels, numpy.array with shape `(n_samples,)`
y_pred: predicted labels, numpy.array with shape `(n_samples,)`
# Return
accuracy, in [0,1]
"""
y_true = y_true.astype(np.int64)
assert y_pred.size == y_true.size
D = max(y_pred.max(), y_true.max()) + 1
w = np.zeros((D, D), dtype=np.int64)
for i in range(y_pred.size):
w[y_pred[i], y_true[i]] += 1
from sklearn.utils.linear_assignment_ import linear_assignment
ind = linear_assignment(w.max() - w)
return sum([w[i, j] for i, j in ind]) * 1.0 / y_pred.size
def generate_random_pair(y, label_cell_indx, num, error_rate=0):
"""
Generate random pairwise constraints.
"""
ml_ind1, ml_ind2 = [], []
cl_ind1, cl_ind2 = [], []
y = np.array(y)
def check_ind(ind1, ind2, ind_list1, ind_list2):
for (l1, l2) in zip(ind_list1, ind_list2):
if ind1 == l1 and ind2 == l2:
return True
return False
error_num = 0
num0 = num
while num > 0:
tmp1 = random.choice(label_cell_indx)
tmp2 = random.choice(label_cell_indx)
if tmp1 == tmp2:
continue
if check_ind(tmp1, tmp2, ml_ind1, ml_ind2):
continue
if y[tmp1] == y[tmp2]:
if error_num >= error_rate*num0:
ml_ind1.append(tmp1)
ml_ind2.append(tmp2)
else:
cl_ind1.append(tmp1)
cl_ind2.append(tmp2)
error_num += 1
else:
if error_num >= error_rate*num0:
cl_ind1.append(tmp1)
cl_ind2.append(tmp2)
else:
ml_ind1.append(tmp1)
ml_ind2.append(tmp2)
error_num += 1
num -= 1
ml_ind1, ml_ind2, cl_ind1, cl_ind2 = np.array(ml_ind1), np.array(ml_ind2), np.array(cl_ind1), np.array(cl_ind2)
ml_index = np.random.permutation(ml_ind1.shape[0])
cl_index = np.random.permutation(cl_ind1.shape[0])
ml_ind1 = ml_ind1[ml_index]
ml_ind2 = ml_ind2[ml_index]
cl_ind1 = cl_ind1[cl_index]
cl_ind2 = cl_ind2[cl_index]
return ml_ind1, ml_ind2, cl_ind1, cl_ind2, error_num
def generate_random_pair_from_proteins(latent_embedding, num, ML=0.1, CL=0.9):
"""
Generate random pairwise constraints.
"""
ml_ind1, ml_ind2 = [], []
cl_ind1, cl_ind2 = [], []
def check_ind(ind1, ind2, ind_list1, ind_list2):
for (l1, l2) in zip(ind_list1, ind_list2):
if ind1 == l1 and ind2 == l2:
return True
return False
latent_dist = euclidean_distances(latent_embedding, latent_embedding)
latent_dist_tril = np.tril(latent_dist, -1)
latent_dist_vec = latent_dist_tril.flatten()
latent_dist_vec = latent_dist_vec[latent_dist_vec>0]
cutoff_ML = np.quantile(latent_dist_vec, ML)
cutoff_CL = np.quantile(latent_dist_vec, CL)
while num > 0:
tmp1 = random.randint(0, latent_embedding.shape[0] - 1)
tmp2 = random.randint(0, latent_embedding.shape[0] - 1)
if tmp1 == tmp2:
continue
if check_ind(tmp1, tmp2, ml_ind1, ml_ind2):
continue
if norm(latent_embedding[tmp1] - latent_embedding[tmp2], 2) < cutoff_ML:
ml_ind1.append(tmp1)
ml_ind2.append(tmp2)
elif norm(latent_embedding[tmp1] - latent_embedding[tmp2], 2) > cutoff_CL:
cl_ind1.append(tmp1)
cl_ind2.append(tmp2)
else:
continue
num -= 1
ml_ind1, ml_ind2, cl_ind1, cl_ind2 = np.array(ml_ind1), np.array(ml_ind2), np.array(cl_ind1), np.array(cl_ind2)
ml_index = np.random.permutation(ml_ind1.shape[0])
cl_index = np.random.permutation(cl_ind1.shape[0])
ml_ind1 = ml_ind1[ml_index]
ml_ind2 = ml_ind2[ml_index]
cl_ind1 = cl_ind1[cl_index]
cl_ind2 = cl_ind2[cl_index]
return ml_ind1, ml_ind2, cl_ind1, cl_ind2
def generate_random_pair_from_CD_markers(markers, num, low1=0.4, high1=0.6, low2=0.2, high2=0.8):
"""
Generate random pairwise constraints.
"""
ml_ind1, ml_ind2 = [], []
cl_ind1, cl_ind2 = [], []
def check_ind(ind1, ind2, ind_list1, ind_list2):
for (l1, l2) in zip(ind_list1, ind_list2):
if ind1 == l1 and ind2 == l2:
return True
return False
gene_low1 = np.quantile(markers[0], low1)
gene_high1 = np.quantile(markers[0], high1)
gene_low2 = np.quantile(markers[1], low1)
gene_high2 = np.quantile(markers[1], high1)
gene_low1_ml = np.quantile(markers[0], low2)
gene_high1_ml = np.quantile(markers[0], high2)
gene_low2_ml = np.quantile(markers[1], low2)
gene_high2_ml = np.quantile(markers[1], high2)
gene_low3 = np.quantile(markers[2], low2)
gene_high3 = np.quantile(markers[2], high2)
gene_low4 = np.quantile(markers[3], low2)
gene_high4 = np.quantile(markers[3], high2)
while num > 0:
tmp1 = random.randint(0, markers.shape[1] - 1)
tmp2 = random.randint(0, markers.shape[1] - 1)
if tmp1 == tmp2:
continue
if check_ind(tmp1, tmp2, ml_ind1, ml_ind2):
continue
if markers[0, tmp1] < gene_low1 and markers[1, tmp1] > gene_high2 and markers[0, tmp2] > gene_high1 and markers[1, tmp2] < gene_low2:
cl_ind1.append(tmp1)
cl_ind2.append(tmp2)
elif markers[0, tmp2] < gene_low1 and markers[1, tmp2] > gene_high2 and markers[0, tmp1] > gene_high1 and markers[1, tmp1] < gene_low2:
cl_ind1.append(tmp1)
cl_ind2.append(tmp2)
elif markers[1, tmp1] > gene_high2_ml and markers[2, tmp1] > gene_high3 and markers[1, tmp2] > gene_high2_ml and markers[2, tmp2] > gene_high3:
ml_ind1.append(tmp1)
ml_ind2.append(tmp2)
elif markers[1, tmp1] > gene_high2_ml and markers[2, tmp1] < gene_low3 and markers[1, tmp2] > gene_high2_ml and markers[2, tmp2] < gene_low3:
ml_ind1.append(tmp1)
ml_ind2.append(tmp2)
elif markers[0, tmp1] > gene_high1_ml and markers[2, tmp1] > gene_high3 and markers[1, tmp2] > gene_high1_ml and markers[2, tmp2] > gene_high3:
ml_ind1.append(tmp1)
ml_ind2.append(tmp2)
elif markers[0, tmp1] > gene_high1_ml and markers[2, tmp1] < gene_low3 and markers[3, tmp1] > gene_high4 and markers[1, tmp2] > gene_high1_ml and markers[2, tmp2] < gene_low3 and markers[3, tmp2] > gene_high4:
ml_ind1.append(tmp1)
ml_ind2.append(tmp2)
elif markers[0, tmp1] > gene_high1_ml and markers[2, tmp1] < gene_low3 and markers[3, tmp1] < gene_low4 and markers[1, tmp2] > gene_high1_ml and markers[2, tmp2] < gene_low3 and markers[3, tmp2] < gene_low4:
ml_ind1.append(tmp1)
ml_ind2.append(tmp2)
else:
continue
num -= 1
ml_ind1, ml_ind2, cl_ind1, cl_ind2 = np.array(ml_ind1), np.array(ml_ind2), np.array(cl_ind1), np.array(cl_ind2)
ml_index = np.random.permutation(ml_ind1.shape[0])
cl_index = np.random.permutation(cl_ind1.shape[0])
ml_ind1 = ml_ind1[ml_index]
ml_ind2 = ml_ind2[ml_index]
cl_ind1 = cl_ind1[cl_index]
cl_ind2 = cl_ind2[cl_index]
return ml_ind1, ml_ind2, cl_ind1, cl_ind2
def generate_random_pair_from_embedding_clustering(latent_embedding, num, n_clusters, ML=0.005, CL=0.8):
"""
Generate random pairwise constraints.
"""
ml_ind1, ml_ind2 = [], []
cl_ind1, cl_ind2 = [], []
def check_ind(ind1, ind2, ind_list1, ind_list2):
for (l1, l2) in zip(ind_list1, ind_list2):
if ind1 == l1 and ind2 == l2:
return True
return False
kmeans = KMeans(n_clusters=n_clusters, n_init=20)
y_pred = kmeans.fit(latent_embedding).labels_
latent_dist = euclidean_distances(latent_embedding, latent_embedding)
latent_dist_tril = np.tril(latent_dist, -1)
latent_dist_vec = latent_dist_tril.flatten()
latent_dist_vec = latent_dist_vec[latent_dist_vec>0]
cutoff_ML = np.quantile(latent_dist_vec, ML)
cutoff_CL = np.quantile(latent_dist_vec, CL)
while num > 0:
tmp1 = random.randint(0, latent_embedding.shape[0] - 1)
tmp2 = random.randint(0, latent_embedding.shape[0] - 1)
if tmp1 == tmp2:
continue
if check_ind(tmp1, tmp2, ml_ind1, ml_ind2):
continue
if y_pred[tmp1]==y_pred[tmp2]:
ml_ind1.append(tmp1)
ml_ind2.append(tmp2)
elif y_pred[tmp1]!=y_pred[tmp2] and norm(latent_embedding[tmp1] - latent_embedding[tmp2], 2) > cutoff_CL:
cl_ind1.append(tmp1)
cl_ind2.append(tmp2)
else:
continue
num -= 1
ml_ind1, ml_ind2, cl_ind1, cl_ind2 = np.array(ml_ind1), np.array(ml_ind2), np.array(cl_ind1), np.array(cl_ind2)
ml_index = np.random.permutation(ml_ind1.shape[0])
cl_index = np.random.permutation(cl_ind1.shape[0])
ml_ind1 = ml_ind1[ml_index]
ml_ind2 = ml_ind2[ml_index]
cl_ind1 = cl_ind1[cl_index]
cl_ind2 = cl_ind2[cl_index]
return ml_ind1, ml_ind2, cl_ind1, cl_ind2