comflow 0.0.3__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- comflow/__init__.py +10 -0
- comflow/_optimal_transport/__init__.py +13 -0
- comflow/_optimal_transport/_cot.py +850 -0
- comflow/_optimal_transport/_cot_preRevise.py +850 -0
- comflow/_optimal_transport/_unot.py +485 -0
- comflow/_optimal_transport/_usot.py +117 -0
- comflow/_utils/__init__.py +20 -0
- comflow/_utils/_binarization.py +43 -0
- comflow/_utils/_clustering.py +57 -0
- comflow/_utils/_plotting.py +398 -0
- comflow/_utils/_similarity.py +479 -0
- comflow/plotting/__init__.py +5 -0
- comflow/plotting/_plotting.py +794 -0
- comflow/preprocessing/__init__.py +3 -0
- comflow/preprocessing/_infer_spatial.py +161 -0
- comflow/preprocessing/_ligand_receptor_database.py +153 -0
- comflow/tools/__init__.py +12 -0
- comflow/tools/_downstream_analysis.py +870 -0
- comflow/tools/_spatial_communication.py +1250 -0
- comflow-0.0.3.dist-info/LICENSE.md +21 -0
- comflow-0.0.3.dist-info/METADATA +113 -0
- comflow-0.0.3.dist-info/RECORD +24 -0
- comflow-0.0.3.dist-info/WHEEL +5 -0
- comflow-0.0.3.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,479 @@
|
|
|
1
|
+
import numpy as np
|
|
2
|
+
from scipy import sparse
|
|
3
|
+
from sys import getsizeof
|
|
4
|
+
import libpysal
|
|
5
|
+
from scipy.stats import spearmanr, pearsonr
|
|
6
|
+
from sklearn.ensemble import RandomForestRegressor, GradientBoostingRegressor
|
|
7
|
+
from sklearn.preprocessing import normalize
|
|
8
|
+
from sklearn.neighbors import NearestNeighbors
|
|
9
|
+
import igraph
|
|
10
|
+
import networkx as nx
|
|
11
|
+
|
|
12
|
+
def pairwise_scc(X1, X2):
|
|
13
|
+
X1 = X1.argsort(axis=1).argsort(axis=1)
|
|
14
|
+
X2 = X2.argsort(axis=1).argsort(axis=1)
|
|
15
|
+
X1 = (X1-X1.mean(axis=1, keepdims=True))/X1.std(axis=1, keepdims=True)
|
|
16
|
+
X2 = (X2-X2.mean(axis=1, keepdims=True))/X2.std(axis=1, keepdims=True)
|
|
17
|
+
sccmat = np.empty([X1.shape[0], X2.shape[0]], float)
|
|
18
|
+
for i in range(X1.shape[0]):
|
|
19
|
+
for j in range(X2.shape[0]):
|
|
20
|
+
c = np.dot( X1[i,:], X2[j,:]) / float(X1.shape[1])
|
|
21
|
+
sccmat[i,j] = c
|
|
22
|
+
return sccmat
|
|
23
|
+
|
|
24
|
+
def standardize(X):
|
|
25
|
+
X_mean = X.mean(axis=0)
|
|
26
|
+
X_std = X.std(axis=0)
|
|
27
|
+
X_standard = np.empty_like(X)
|
|
28
|
+
for i in range(X.shape[1]):
|
|
29
|
+
if X_std[i] > 0:
|
|
30
|
+
X_standard[:,i] = (X[:,i] - X_mean[i]) / X_std[i]
|
|
31
|
+
else:
|
|
32
|
+
X_standard[:,i] = 0.0
|
|
33
|
+
return X_standard
|
|
34
|
+
|
|
35
|
+
def partial_corr(x, y, cov, method="spearman", aggregate=True):
|
|
36
|
+
# Standardize data
|
|
37
|
+
x = standardize(x)
|
|
38
|
+
y = standardize(y)
|
|
39
|
+
cov = standardize(cov)
|
|
40
|
+
if aggregate:
|
|
41
|
+
rs = []
|
|
42
|
+
for i in range(cov.shape[1]):
|
|
43
|
+
beta_x = np.linalg.lstsq(cov[:,i].reshape(-1,1), x, rcond=None)[0]
|
|
44
|
+
beta_y = np.linalg.lstsq(cov[:,i].reshape(-1,1), y, rcond=None)[0]
|
|
45
|
+
res_x = x - cov[:,i].reshape(-1,1) @ beta_x
|
|
46
|
+
res_y = y - cov[:,i].reshape(-1,1) @ beta_y
|
|
47
|
+
if method == "spearman":
|
|
48
|
+
r,p = spearmanr(res_x, res_y)
|
|
49
|
+
elif method == "pearson":
|
|
50
|
+
r,p = pearsonr(res_x, res_y)
|
|
51
|
+
rs.append(r)
|
|
52
|
+
r = np.mean(rs); p = -1
|
|
53
|
+
else:
|
|
54
|
+
beta_x = np.linalg.lstsq(cov, x, rcond=None)[0]
|
|
55
|
+
beta_y = np.linalg.lstsq(cov, y, rcond=None)[0]
|
|
56
|
+
res_x = x - cov @ beta_x
|
|
57
|
+
res_y = y - cov @ beta_y
|
|
58
|
+
if method == "spearman":
|
|
59
|
+
r,p = spearmanr(res_x, res_y)
|
|
60
|
+
elif method == "pearson":
|
|
61
|
+
r,p = pearsonr(res_x, res_y)
|
|
62
|
+
return r, p
|
|
63
|
+
|
|
64
|
+
def semipartial_corr(x, y, xcov=None, ycov=None, method="spearman"):
|
|
65
|
+
# Standardize data
|
|
66
|
+
x = standardize(x)
|
|
67
|
+
y = standardize(y)
|
|
68
|
+
if not xcov is None:
|
|
69
|
+
xcov = standardize(xcov)
|
|
70
|
+
if not ycov is None:
|
|
71
|
+
ycov = standardize(ycov)
|
|
72
|
+
res_x = np.copy(x); res_y = np.copy(y)
|
|
73
|
+
if not xcov is None:
|
|
74
|
+
beta_x = np.linalg.lstsq(xcov, x, rcond=None)[0]
|
|
75
|
+
res_x = x - xcov @ beta_x
|
|
76
|
+
if not ycov is None:
|
|
77
|
+
beta_y = np.linalg.lstsq(ycov, y, rcond=None)[0]
|
|
78
|
+
res_y = y - ycov @ beta_y
|
|
79
|
+
if method == "spearman":
|
|
80
|
+
r,p = spearmanr(res_x, res_y)
|
|
81
|
+
elif method == "pearson":
|
|
82
|
+
r,p = pearsonr(res_x, res_y)
|
|
83
|
+
return r, p
|
|
84
|
+
|
|
85
|
+
def treebased_score(x,
|
|
86
|
+
y,
|
|
87
|
+
cov,
|
|
88
|
+
method="rf",
|
|
89
|
+
n_trees=100,
|
|
90
|
+
n_repeat=10,
|
|
91
|
+
max_depth=5,
|
|
92
|
+
max_features="sqrt",
|
|
93
|
+
learning_rate=0.1,
|
|
94
|
+
subsample=1.0
|
|
95
|
+
):
|
|
96
|
+
random_seeds = np.random.randint(100000, size=n_repeat)
|
|
97
|
+
X_train = np.concatenate((x.reshape(-1,1), cov), axis=1)
|
|
98
|
+
ranks = []
|
|
99
|
+
for i in range(n_repeat):
|
|
100
|
+
if method == "rf":
|
|
101
|
+
model = RandomForestRegressor(n_estimators=n_trees,
|
|
102
|
+
max_depth=max_depth,
|
|
103
|
+
max_features=max_features,
|
|
104
|
+
random_state=random_seeds[i],
|
|
105
|
+
n_jobs=-1
|
|
106
|
+
)
|
|
107
|
+
elif method == "gbt":
|
|
108
|
+
model = GradientBoostingRegressor(n_estimators=n_trees,
|
|
109
|
+
max_depth=max_depth,
|
|
110
|
+
max_features=max_features,
|
|
111
|
+
learning_rate=learning_rate,
|
|
112
|
+
subsample=subsample,
|
|
113
|
+
random_state=random_seeds[i]
|
|
114
|
+
)
|
|
115
|
+
model.fit(X_train, y)
|
|
116
|
+
importance = model.feature_importances_
|
|
117
|
+
rank = np.where(np.argsort(-importance)==0)[0][0]
|
|
118
|
+
ranks.append(float(cov.shape[1]-rank)/float(cov.shape[1]))
|
|
119
|
+
return np.mean(ranks)
|
|
120
|
+
|
|
121
|
+
def treebased_score_multifeature(
|
|
122
|
+
X,
|
|
123
|
+
y,
|
|
124
|
+
cov,
|
|
125
|
+
method='rf',
|
|
126
|
+
n_trees=100,
|
|
127
|
+
n_repeat=10,
|
|
128
|
+
max_depth=5,
|
|
129
|
+
max_features='sqrt',
|
|
130
|
+
learning_rate=0.1,
|
|
131
|
+
subsample=1.0
|
|
132
|
+
):
|
|
133
|
+
random_seeds = np.random.randint(100000, size=n_repeat)
|
|
134
|
+
X_train = np.concatenate((X, cov), axis=1)
|
|
135
|
+
ranks = np.empty([X.shape[1], n_repeat], float)
|
|
136
|
+
for i in range(n_repeat):
|
|
137
|
+
if method == 'rf':
|
|
138
|
+
model = RandomForestRegressor(n_estimators=n_trees,
|
|
139
|
+
max_depth=max_depth,
|
|
140
|
+
max_features=max_features,
|
|
141
|
+
random_state=random_seeds[i],
|
|
142
|
+
n_jobs=-1
|
|
143
|
+
)
|
|
144
|
+
elif method == 'gbt':
|
|
145
|
+
model = GradientBoostingRegressor(n_estimators=n_trees,
|
|
146
|
+
max_depth=max_depth,
|
|
147
|
+
max_features=max_features,
|
|
148
|
+
learning_rate=learning_rate,
|
|
149
|
+
subsample=subsample,
|
|
150
|
+
random_state=random_seeds[i]
|
|
151
|
+
)
|
|
152
|
+
model.fit(X_train, y)
|
|
153
|
+
importance = model.feature_importances_
|
|
154
|
+
sorted_idx = np.argsort(-importance)
|
|
155
|
+
for j in range(X.shape[1]):
|
|
156
|
+
rank = np.where(sorted_idx==j)[0][0]
|
|
157
|
+
ranks[j,i] = float(X_train.shape[1] - rank - 1) / float(X_train.shape[1]-1)
|
|
158
|
+
return np.mean(ranks, axis=1)
|
|
159
|
+
|
|
160
|
+
|
|
161
|
+
def vf_diff(vf_1, vf_2):
|
|
162
|
+
norm_vf_1 = np.linalg.norm(vf_1, axis=1)
|
|
163
|
+
norm_vf_2 = np.linalg.norm(vf_2, axis=1)
|
|
164
|
+
dot_vf = (vf_1 * vf_2).sum(axis=1)
|
|
165
|
+
|
|
166
|
+
s_cosine = dot_vf / (norm_vf_1 * norm_vf_2)
|
|
167
|
+
s_cosine[np.isnan(s_cosine)] = 0.0
|
|
168
|
+
|
|
169
|
+
s_coop = dot_vf
|
|
170
|
+
|
|
171
|
+
return s_cosine, s_coop
|
|
172
|
+
|
|
173
|
+
def d_graph_local_jaccard(A1, A2):
|
|
174
|
+
a = float( np.sum( (A1 != 0) * (A2 != 0) ) )
|
|
175
|
+
b = float( np.sum( (A1 != 0) + (A2 != 0) ) )
|
|
176
|
+
if b == 0:
|
|
177
|
+
d = 1
|
|
178
|
+
else:
|
|
179
|
+
d = a / b
|
|
180
|
+
return 1 - d
|
|
181
|
+
|
|
182
|
+
def d_graph_local_jaccard_weighted(A1, A2):
|
|
183
|
+
a = float( np.sum( np.minimum(A1, A2) ) )
|
|
184
|
+
b = float( np.sum( np.maximum(A1, A2) ) )
|
|
185
|
+
if b == 0:
|
|
186
|
+
d = 1
|
|
187
|
+
else:
|
|
188
|
+
d = a / b
|
|
189
|
+
return 1 - d
|
|
190
|
+
|
|
191
|
+
# %%
|
|
192
|
+
def d_graph_mesoscale_heat(A1, A2, tau, p=2):
|
|
193
|
+
A1_lap = sparse.csgraph.laplacian(A1, normed = True)
|
|
194
|
+
w,v = np.linalg.eigh(A1_lap.todense())
|
|
195
|
+
# tmp_idx = np.argsort(w)
|
|
196
|
+
# w = w[tmp_idx]
|
|
197
|
+
# v = v[:,tmp_idx]
|
|
198
|
+
W = np.diag(w)
|
|
199
|
+
R1 = v.real @ np.exp( - tau * W ) @ v.real.T
|
|
200
|
+
|
|
201
|
+
A2_lap = sparse.csgraph.laplacian(A2, normed = True)
|
|
202
|
+
w,v = np.linalg.eigh(A2_lap.todense())
|
|
203
|
+
# tmp_idx = np.argsort(w)
|
|
204
|
+
# w = w[tmp_idx]
|
|
205
|
+
# v = v[:,tmp_idx]
|
|
206
|
+
W = np.diag(w)
|
|
207
|
+
R2 = v.real @ np.exp( - tau * W ) @ v.real.T
|
|
208
|
+
|
|
209
|
+
delta = R1 - R2
|
|
210
|
+
|
|
211
|
+
N = A1.shape[0]
|
|
212
|
+
|
|
213
|
+
d = np.trace(delta.T @ delta) / float(N)
|
|
214
|
+
|
|
215
|
+
# d = np.linalg.norm(R1-R2) ** 2 / float(N)
|
|
216
|
+
|
|
217
|
+
return d
|
|
218
|
+
|
|
219
|
+
# %%
|
|
220
|
+
def d_graph_global_structure(A1, A2, w1=0.45, w2=0.45, w3=0.1):
|
|
221
|
+
# Ref: Schieber, Tiago A., et al. "Quantification of network
|
|
222
|
+
# structural dissimilarities." Nature communications 8.1 (2017): 1-10.
|
|
223
|
+
A1 = A1.astype(bool).astype(int)
|
|
224
|
+
A2 = A2.astype(bool).astype(int)
|
|
225
|
+
g1 = igraph.Graph(directed=True).Adjacency( (A1>0).tolist() )
|
|
226
|
+
g2 = igraph.Graph(directed=True).Adjacency( (A2>0).tolist() )
|
|
227
|
+
n = g1.vcount()
|
|
228
|
+
m = g2.vcount()
|
|
229
|
+
pg1 = nnd(g1)
|
|
230
|
+
pg2 = nnd(g2)
|
|
231
|
+
pm = np.zeros([max(m,n)], float)
|
|
232
|
+
pm[:n-1] = pg1[:n-1]
|
|
233
|
+
pm[-1] = pg1[n-1]
|
|
234
|
+
pm[:m-1] = pm[:m-1] + pg2[:m-1]
|
|
235
|
+
pm[-1] = pm[-1]+pg2[m-1]
|
|
236
|
+
pm = pm / 2
|
|
237
|
+
first = np.sqrt( max( (entropia(pm)-(entropia(pg1[:n])+entropia(pg2[:m]))/2)/np.log(2), 0) )
|
|
238
|
+
second = np.abs(np.sqrt(pg1[n]) - np.sqrt(pg2[m]))
|
|
239
|
+
|
|
240
|
+
pg1 = alpha(A1)
|
|
241
|
+
pg2 = alpha(A2)
|
|
242
|
+
m = max(len(pg1), len(pg2))
|
|
243
|
+
Pg1 = np.zeros([m], float)
|
|
244
|
+
Pg2 = np.zeros([m], float)
|
|
245
|
+
Pg1[m-len(pg1):] = pg1[:]
|
|
246
|
+
Pg2[m-len(pg2):] = pg2[:]
|
|
247
|
+
third = np.sqrt( (entropia((Pg1+Pg2)/2)-(entropia(pg1)+entropia(pg2))/2 )/np.log(2))/2
|
|
248
|
+
A1_compl = np.ones_like(A1) - A1
|
|
249
|
+
for i in range(A1.shape[0]): A1_compl[i,i] = 1
|
|
250
|
+
A2_compl = np.ones_like(A2) - A2
|
|
251
|
+
for i in range(A2.shape[0]): A2_compl[i,i] = 1
|
|
252
|
+
pg1 = alpha(A1_compl)
|
|
253
|
+
pg2 = alpha(A2_compl)
|
|
254
|
+
m = max(len(pg1), len(pg2))
|
|
255
|
+
Pg1 = np.zeros([m], float)
|
|
256
|
+
Pg2 = np.zeros([m], float)
|
|
257
|
+
Pg1[m-len(pg1):] = pg1[:]
|
|
258
|
+
Pg2[m-len(pg2):] = pg2[:]
|
|
259
|
+
third = third + np.sqrt( (entropia((Pg1+Pg2)/2)-(entropia(pg1)+entropia(pg2))/2 )/np.log(2))/2
|
|
260
|
+
|
|
261
|
+
return w1*first + w2*second + w3*third
|
|
262
|
+
|
|
263
|
+
|
|
264
|
+
def alpha(A):
|
|
265
|
+
n = A.shape[0]
|
|
266
|
+
g_nx = nx.from_numpy_array(A, create_using=nx.DiGraph)
|
|
267
|
+
g_degree = dict( g_nx.degree() )
|
|
268
|
+
for key in g_degree.keys():
|
|
269
|
+
g_degree[key] = g_degree[key] / (n-1)
|
|
270
|
+
g_nx.remove_edges_from(nx.selfloop_edges(g_nx))
|
|
271
|
+
centrality = nx.katz_centrality(g_nx, beta=g_degree, alpha=1/float(n), normalized=False)
|
|
272
|
+
centrality_vec = []
|
|
273
|
+
for i in range(n):
|
|
274
|
+
centrality_vec.append(centrality[i])
|
|
275
|
+
centrality_vec = np.array(centrality_vec)
|
|
276
|
+
r = np.sort(centrality_vec) / n**2
|
|
277
|
+
rr = np.zeros([n+1],float)
|
|
278
|
+
rr[:n] = r[:]
|
|
279
|
+
rr[-1] = max(0,1-r.sum())
|
|
280
|
+
return rr
|
|
281
|
+
|
|
282
|
+
def entropia(a):
|
|
283
|
+
a = a[np.where(a>0)]
|
|
284
|
+
return -np.sum(a*np.log(a))
|
|
285
|
+
|
|
286
|
+
def node_distance(g):
|
|
287
|
+
n = g.vcount()
|
|
288
|
+
a = np.zeros([n,n], float)
|
|
289
|
+
m = np.array( g.shortest_paths(mode="ALL"), float )
|
|
290
|
+
m[np.isinf(m)] = n
|
|
291
|
+
quem = np.unique(m)
|
|
292
|
+
quem = quem[np.where(quem!=0)]
|
|
293
|
+
for mm in quem:
|
|
294
|
+
tmp_mask = (m == mm)
|
|
295
|
+
a[:,int(mm)-1] = tmp_mask.astype(float).sum(axis=1)
|
|
296
|
+
return a / (n-1)
|
|
297
|
+
|
|
298
|
+
def nnd(g):
|
|
299
|
+
n = g.vcount()
|
|
300
|
+
nd = node_distance(g)
|
|
301
|
+
pdfm = nd.mean(axis=0)
|
|
302
|
+
norm = np.log( max(2, len(np.where(pdfm[:-1]>0)[0])+1) )
|
|
303
|
+
y = max(0, entropia(pdfm)-entropia(nd)/n)/norm
|
|
304
|
+
out = np.zeros([len(pdfm)+1],float)
|
|
305
|
+
out[:n] = pdfm[:]; out[n] = y
|
|
306
|
+
return out
|
|
307
|
+
|
|
308
|
+
# %%
|
|
309
|
+
def spatial_weight(
|
|
310
|
+
X,
|
|
311
|
+
bandwidth = None,
|
|
312
|
+
k = 5,
|
|
313
|
+
function = 'gaussian',
|
|
314
|
+
row_standardize = False,
|
|
315
|
+
zero_diagonal = False
|
|
316
|
+
):
|
|
317
|
+
kw = libpysal.weights.Kernel(X,
|
|
318
|
+
bandwidth = bandwidth, k = k, function = function)
|
|
319
|
+
if row_standardize:
|
|
320
|
+
kw.transform = 'r'
|
|
321
|
+
I = []; J = []; W = []
|
|
322
|
+
for i in range(X.shape[0]):
|
|
323
|
+
nnb = len(kw.neighbors[i])
|
|
324
|
+
for j in range(nnb):
|
|
325
|
+
if zero_diagonal and i == kw.neighbors[i][j]:
|
|
326
|
+
continue
|
|
327
|
+
I.append(i)
|
|
328
|
+
J.append(kw.neighbors[i][j])
|
|
329
|
+
W.append(kw.weights[i][j])
|
|
330
|
+
I = np.array(I, int)
|
|
331
|
+
J = np.array(J, int)
|
|
332
|
+
W = np.array(W, float)
|
|
333
|
+
|
|
334
|
+
return I, J, W
|
|
335
|
+
|
|
336
|
+
# %%
|
|
337
|
+
def tmp_moranI_vector_global(
|
|
338
|
+
u,
|
|
339
|
+
v,
|
|
340
|
+
I,
|
|
341
|
+
J,
|
|
342
|
+
W
|
|
343
|
+
):
|
|
344
|
+
"""
|
|
345
|
+
Liu, Yu, Daoqin Tong, and Xi Liu. "Measuring spatial autocorrelation
|
|
346
|
+
of vectors." Geographical Analysis 47.3 (2015): 300-319.
|
|
347
|
+
"""
|
|
348
|
+
n = float( len(u) )
|
|
349
|
+
a = np.sum( W * (u[I]*u[J]+v[I]*v[J]) )
|
|
350
|
+
b = np.sum( u**2 + v**2 )
|
|
351
|
+
c = np.sum( W )
|
|
352
|
+
moranI = ( n * a ) / ( b * c )
|
|
353
|
+
return moranI
|
|
354
|
+
|
|
355
|
+
def tmp_moranI_vector_global_3d(
|
|
356
|
+
u,
|
|
357
|
+
v,
|
|
358
|
+
w,
|
|
359
|
+
I,
|
|
360
|
+
J,
|
|
361
|
+
W
|
|
362
|
+
):
|
|
363
|
+
"""
|
|
364
|
+
Liu, Yu, Daoqin Tong, and Xi Liu. "Measuring spatial autocorrelation
|
|
365
|
+
of vectors." Geographical Analysis 47.3 (2015): 300-319.
|
|
366
|
+
"""
|
|
367
|
+
n = float( len(u) )
|
|
368
|
+
a = np.sum( W * (u[I]*u[J]+v[I]*v[J] + w[I]*w[J]) )
|
|
369
|
+
b = np.sum( u**2 + v**2 + w**2)
|
|
370
|
+
c = np.sum( W )
|
|
371
|
+
moranI = ( n * a ) / ( b * c )
|
|
372
|
+
return moranI
|
|
373
|
+
|
|
374
|
+
def tmp_moranI_vector_local(
|
|
375
|
+
u,
|
|
376
|
+
v,
|
|
377
|
+
I,
|
|
378
|
+
J,
|
|
379
|
+
W
|
|
380
|
+
):
|
|
381
|
+
n = float( len(u) )
|
|
382
|
+
m2 = np.sum( u**2 + v**2 ) / n
|
|
383
|
+
print(m2)
|
|
384
|
+
moranI = np.zeros([len(u)])
|
|
385
|
+
for i in range(len(I)):
|
|
386
|
+
moranI[I[i]] += u[I[i]] * W[i] * u[J[i]] + v[I[i]] * W[i] * v[J[i]]
|
|
387
|
+
moranI = moranI / m2
|
|
388
|
+
return moranI
|
|
389
|
+
|
|
390
|
+
def moranI_vector_global(
|
|
391
|
+
X,
|
|
392
|
+
V,
|
|
393
|
+
weight_bandwidth = None,
|
|
394
|
+
weight_k = 5,
|
|
395
|
+
weight_function = 'triangular',
|
|
396
|
+
weight_row_standardize = False,
|
|
397
|
+
n_permutations = 999
|
|
398
|
+
):
|
|
399
|
+
z_V = V - V.mean(axis=0)
|
|
400
|
+
I, J, W = spatial_weight(X,
|
|
401
|
+
bandwidth = weight_bandwidth, k = weight_k,
|
|
402
|
+
function = weight_function, row_standardize = weight_row_standardize,
|
|
403
|
+
zero_diagonal = True)
|
|
404
|
+
if V.shape[1] == 2:
|
|
405
|
+
moranI = tmp_moranI_vector_global(z_V[:,0], z_V[:,1], I, J, W)
|
|
406
|
+
elif V.shape[1] == 3:
|
|
407
|
+
moranI = tmp_moranI_vector_global_3d(z_V[:,0], z_V[:,1], z_V[:,2], I, J, W)
|
|
408
|
+
permute_moranI = []
|
|
409
|
+
idx = np.arange(X.shape[0])
|
|
410
|
+
for i in range(n_permutations):
|
|
411
|
+
permute_idx = np.random.permutation(idx)
|
|
412
|
+
if V.shape[1] == 2:
|
|
413
|
+
tmp_I = tmp_moranI_vector_global(z_V[permute_idx,0], z_V[permute_idx,1], I, J, W)
|
|
414
|
+
elif V.shape[1] == 3:
|
|
415
|
+
tmp_I = tmp_moranI_vector_global_3d(z_V[permute_idx,0], z_V[permute_idx,1], z_V[permute_idx,2], I, J, W)
|
|
416
|
+
permute_moranI.append(tmp_I)
|
|
417
|
+
permute_moranI = np.abs( np.array(permute_moranI, float) )
|
|
418
|
+
p_value = ( permute_moranI > np.abs(moranI) ).astype(float).sum() / float(n_permutations)
|
|
419
|
+
|
|
420
|
+
return moranI, p_value
|
|
421
|
+
|
|
422
|
+
def moranI_vector_local(
|
|
423
|
+
X,
|
|
424
|
+
V,
|
|
425
|
+
weight_bandwidth = None,
|
|
426
|
+
weight_k = 5,
|
|
427
|
+
weight_function = 'triangular',
|
|
428
|
+
weight_row_standardize = False
|
|
429
|
+
):
|
|
430
|
+
z_V = V - V.mean(axis=0)
|
|
431
|
+
I, J, W = spatial_weight(X,
|
|
432
|
+
bandwidth = weight_bandwidth, k = weight_k,
|
|
433
|
+
function = weight_function, row_standardize = weight_row_standardize,
|
|
434
|
+
zero_diagonal = True)
|
|
435
|
+
moranI = tmp_moranI_vector_local(z_V[:,0], z_V[:,1], I, J, W)
|
|
436
|
+
|
|
437
|
+
return moranI
|
|
438
|
+
|
|
439
|
+
|
|
440
|
+
|
|
441
|
+
|
|
442
|
+
# %%
|
|
443
|
+
|
|
444
|
+
def preprocess_vector_field(
|
|
445
|
+
X,
|
|
446
|
+
V,
|
|
447
|
+
knn_smoothing = -1,
|
|
448
|
+
normalize_vf = 'quantile',
|
|
449
|
+
quantile = 0.99
|
|
450
|
+
):
|
|
451
|
+
if knn_smoothing >= 2:
|
|
452
|
+
nbrs = NearestNeighbors(n_neighbors=knn_smoothing, algorithm='ball_tree').fit(X)
|
|
453
|
+
_, idx = nbrs.kneighbors(X)
|
|
454
|
+
V_smoothed = np.zeros_like(V)
|
|
455
|
+
for i in range(knn_smoothing):
|
|
456
|
+
V_smoothed = V_smoothed + V[idx[:,i],:]
|
|
457
|
+
V_smoothed = V_smoothed / float(knn_smoothing)
|
|
458
|
+
V = V_smoothed
|
|
459
|
+
V_norm = np.linalg.norm(V, axis=1)
|
|
460
|
+
if normalize_vf == 'quantile':
|
|
461
|
+
scale = np.quantile(V_norm, quantile)
|
|
462
|
+
if scale > 0:
|
|
463
|
+
V = V / scale
|
|
464
|
+
elif normalize_vf == 'unit_norm':
|
|
465
|
+
V = normalize(V)
|
|
466
|
+
return V
|
|
467
|
+
|
|
468
|
+
# def d_vf_cosine_similarity(
|
|
469
|
+
# X,
|
|
470
|
+
# V1,
|
|
471
|
+
# V2,
|
|
472
|
+
# knn_smoothing = -1,
|
|
473
|
+
# normalize = "quantile",
|
|
474
|
+
# quantile = 0.99
|
|
475
|
+
# ):
|
|
476
|
+
# if knn_smoothing >= 2:
|
|
477
|
+
# nbrs = NearestNeighbors(n_neighbors=knn_smoothing, algorithm='ball_tree').fit(X)
|
|
478
|
+
# _, idx = nbrs.kneighbors(X)
|
|
479
|
+
# V_smoothed = np.zero_like(V)
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
from ._plotting import plot_cell_communication
|
|
2
|
+
from ._plotting import plot_cluster_communication_network
|
|
3
|
+
from ._plotting import plot_cluster_communication_dotplot
|
|
4
|
+
from ._plotting import plot_communication_dependent_genes
|
|
5
|
+
from ._plotting import plot_communication_impact
|