-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlee_vec.py
311 lines (238 loc) · 10.9 KB
/
lee_vec.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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
import numpy
from scipy import sparse
from sklearn.base import BaseEstimator
from sklearn import preprocessing
from sklearn import utils
import random
from sys import getsizeof
import scipy.stats
import psutil
def get_memory():
# Get the virtual memory usage details
memory = psutil.virtual_memory()
# Calculate the free memory in MB
free_memory_mb = (memory.available / 1024.0) / 1024.0
return free_memory_mb
'''
def get_memory():
with open('/proc/meminfo', 'r') as mem:
free_memory = 0
for i in mem:
sline = i.split()
if str(sline[0]) in ('MemFree:', 'Buffers:', 'Cached:'):
free_memory += int(sline[1])
return free_memory / 1024
'''
class Spatial_Pearson(BaseEstimator):
"""
Global Spatial Pearson Correlation Statistic;
Mean of single-cell gene-peak correlation strength index.
Adapted and vectorized from https://github.com/pysal/esda
"""
def __init__(self, connectivity=None, permutations=999):
"""
Initialize a spatial pearson correlation estimator
Parameters
------------
connectivity: scipy.sparse matrix object
the connectivity structure describing the relationships
between observed units. Will be row-standardized.
permutations: int
the number of permutations to conduct for inference.
if < 1, no permutational inference will be conducted.
Attributes
------------
association_: numpy.ndarray (p,)
array containg the estimated Lee spatial pearson correlation
coefficients for all gene-peak pairs
reference_distribution_: numpy.ndarray (n_permutations, p)
distribution of correlation matrices for randomly-shuffled
maps.
significance_: numpy.ndarray (p,)
permutation-based z-scores (p-values) for the probability that
observed correlation was more extreme than the simulated
correlations.
"""
self.connectivity = connectivity
self.permutations = permutations
if self.connectivity is None:
self.connectivity = sparse.eye(Z.shape[0])
self.standard_connectivity = sparse.csc_matrix(
self.connectivity / (self.connectivity.sum(axis=1)[:, numpy.newaxis])
)
self.ctc = self.connectivity.T @ self.connectivity
ones = numpy.ones(self.ctc.shape[0])
self.denom = (ones.T @ self.ctc @ ones)
def fit(self, X, Y, percent=0.1, max_RAM=16, seed=1):
"""
bivariate spatial pearson's R based on Eq. 18 of :cite:`Lee2001`.
Parameters
------------
X: numpy.ndarray [n x p]
array containing continuous data
Y: numpy.ndarray [n x p]
array containing continuous data
percent: float
percentage of cells to shuffle during permutation.
For most of the time, default 0.1 is alread a good choice.
seed: int
random seed to make the results reproducible
max_RAM: int, float
maximum limitation of memory (Gb)
Returns
------------
the fitted estimator.
Notes
------------
Technical details and derivations can be found in :cite:`Lee2001`.
"""
random.seed(seed)
numpy.random.seed(seed)
X = utils.check_array(X)
Y = utils.check_array(Y)
X = preprocessing.StandardScaler().fit_transform(X)
Y = preprocessing.StandardScaler().fit_transform(Y)
X[X>10] = 10.0
X[X<-10] = -10.0
Y[Y>10] = 10.0
Y[Y<-10] = -10.0
self.association_ = self._statistic(X, Y, self.ctc, self.denom, max_RAM)
if self.permutations is None:
return self
elif self.permutations < 1:
return self
if self.permutations:
self.reference_distribution_ = numpy.zeros((self.permutations, X.shape[1]))
for i in range(self.permutations):
itp = numpy.random.choice(numpy.arange(X.shape[0]), int(X.shape[0]*percent), replace=False)
rnd_index = numpy.arange(X.shape[0])
rnd_index[itp] = numpy.random.permutation(itp)
self.reference_distribution_[i] = self._statistic(Y[rnd_index], X[rnd_index], self.ctc, self.denom, max_RAM)
self.z_sim = (self.association_ - numpy.mean(self.reference_distribution_, axis=0)) / numpy.std(self.reference_distribution_, axis=0)
#above = self.reference_distribution_ >= numpy.repeat(self.association_[numpy.newaxis,:],self.permutations,axis=0) #self.association_
#larger = above.sum(axis=0)
#extreme = numpy.minimum(self.permutations - larger, larger)
#self.significance_ = (extreme + 1.0) / (self.permutations + 1.0)
self.significance_ = scipy.stats.norm.sf(numpy.abs(self.z_sim))
return self
@staticmethod
def _statistic(X, Y, ctc, denom, max_RAM):
'''
Memory taken (Mb): 0.000008 * (2 * p * n + p)
'''
free_memory = min(get_memory(), max_RAM*1024)
if (free_memory*0.8) > (0.000008 * (2 * X.shape[1] * X.shape[0] + X.shape[1])):
out = ((Y.T @ ctc) * X.T).sum(-1) / denom
else:
nt = (0.000008 * (2 * X.shape[1] * X.shape[0] + X.shape[1])) / (free_memory*0.8) + 1
nt = int(nt)
nf = X.shape[1] // nt
out = numpy.zeros(X.shape[1])
for i in range(nt-1):
out[(nf*i):(nf*(i+1))] = ((Y[:,(nf*i):(nf*(i+1))].T @ ctc) * X[:,(nf*i):(nf*(i+1))].T).sum(-1) / denom
out[(nf*(nt-1)):] = ((Y[:,(nf*(nt-1)):].T @ ctc) * X[:,(nf*(nt-1)):].T).sum(-1) / denom
return out
class Spatial_Pearson_Local(BaseEstimator):
"""
Single-cell gene-peak correlation strength index.
Adapted and vectorized from esda library
"""
def __init__(self, connectivity=None, permutations=999):
"""
Initialize a spatial local pearson estimator
Parameters
------------
connectivity: scipy.sparse matrix object
the connectivity structure describing the relationships
between observed units. Will be row-standardized.
permutations: int
the number of permutations to conduct for inference.
if < 1, no permutational inference will be conducted.
Attributes
------------
associations_: numpy.ndarray (n_samples,)
array containg the estimated Lee spatial pearson correlation
coefficients, where element [0,1] is the spatial correlation
coefficient, and elements [0,0] and [1,1] are the "spatial
smoothing factor"
significance_: numpy.ndarray (n,p)
permutation-based z-scores (p-values) for the probability that
observed correlation was more extreme than the simulated
correlations.
Notes
-----
Technical details and derivations can be found in :cite:`Lee2001`.
"""
self.connectivity = connectivity
self.permutations = permutations
self.standard_connectivity = sparse.csc_matrix(
self.connectivity / (self.connectivity.sum(axis=1)[:, numpy.newaxis])
)
def fit(self, X, Y, max_RAM=16, seed=1):
"""
bivariate local pearson's R based on Eq. 22 in Lee (2001)
Parameters
------------
X: numpy.ndarray [n x p]
array containing continuous data
Y: numpy.ndarray [n x p]
array containing continuous data
seed: int
random seed to make the results reproducible
max_RAM: int, float
maximum limitation of memory (Gb)
Returns
-------
the fitted estimator.
"""
random.seed(seed)
numpy.random.seed(seed)
X = utils.check_array(X)
X = preprocessing.StandardScaler().fit_transform(X)
Y = utils.check_array(Y)
Y = preprocessing.StandardScaler().fit_transform(Y)
X[X>10] = 10.0
X[X<-10] = -10.0
Y[Y>10] = 10.0
Y[Y<-10] = -10.0
#Z = numpy.column_stack((x, y))
n, _ = X.shape
self.associations_ = self._statistic(X, Y, self.standard_connectivity, max_RAM)
if self.permutations:
self.significance_ = numpy.empty(X.shape)
#perm_X = numpy.empty((self.permutations, X.shape[0], X.shape[1]))
#perm_Y = numpy.empty((self.permutations, Y.shape[1], Y.shape[0]))
for i in range(X.shape[1]):
perm_X = numpy.empty((self.permutations, X.shape[0]))
perm_Y = numpy.empty((self.permutations, Y.shape[0]))
for j in range(self.permutations):
rnd_index = numpy.random.permutation(numpy.arange(X.shape[0]))
perm_X[j] = X[rnd_index,i]
perm_Y[j] = Y[rnd_index,i]
reference_distribution = numpy.transpose(
(numpy.transpose(perm_Y[:,:,numpy.newaxis], (0,2,1)) @ self.standard_connectivity.T), (0, 2, 1)
) * (self.standard_connectivity @ perm_X[:,:,numpy.newaxis])
above = reference_distribution.squeeze(-1) >= numpy.repeat(self.associations_[:,i][numpy.newaxis,:],self.permutations,axis=0)
larger = above.sum(axis=0)
extreme = numpy.minimum(larger, self.permutations - larger)
self.significance_[:,i] = (extreme + 1.0) / (self.permutations + 1.0)
else:
self.reference_distribution_ = None
return self
@staticmethod
def _statistic(X, Y, W, max_RAM):
'''
Memory taken (Mb): 0.000008 * (3 * p * n)
'''
free_memory = min(get_memory(), max_RAM*1024)
if (free_memory*0.8) > (0.000008 * (3 * X.shape[1] * X.shape[0])):
return (Y.T @ W.T).T * (W @ X)
else:
nt = (0.000008 * (3 * X.shape[1] * X.shape[0])) / (free_memory*0.8) + 1
nt = int(nt)
nf = X.shape[1] // nt
out = numpy.zeros(X.shape)
for i in range(nt-1):
out[:,(nf*i):(nf*(i+1))] = (Y[:,(nf*i):(nf*(i+1))].T @ W.T).T * (W @ X[:,(nf*i):(nf*(i+1))])
out[:,(nf*(nt-1)):] = (Y[:,(nf*(nt-1)):].T @ W.T).T * (W @ X[:,(nf*(nt-1)):])
return out