-
Notifications
You must be signed in to change notification settings - Fork 0
/
sparseRRR.py
483 lines (411 loc) · 18.5 KB
/
sparseRRR.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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
import numpy as np
import time
import warnings
import seaborn as sns
import pylab as plt
import glmnet_python
from glmnet import glmnet
from scipy import sparse
###################################################
# Elastic net reduced-rank regression
def elastic_rrr(X, Y, rank=2, lambdau=1, alpha=0.5, max_iter = 100, verbose=0,
sparsity='row-wise'):
# in the pure ridge case, analytic solution is available:
if alpha == 0:
U,s,V = np.linalg.svd(X, full_matrices=False)
B = V.T @ np.diag(s/(s**2 + lambdau*X.shape[0])) @ U.T @ Y
U,s,V = np.linalg.svd(X@B, full_matrices=False)
w = B @ V.T[:,:rank]
v = V.T[:,:rank]
pos = np.argmax(np.abs(v), axis=0)
flips = np.sign(v[pos, range(v.shape[1])])
v = v * flips
w = w * flips
return (w,v)
# initialize with PLS direction
_,_,v = np.linalg.svd(X.T @ Y, full_matrices=False)
v = v[:rank,:].T
loss = np.zeros(max_iter)
for iter in range(max_iter):
if rank == 1:
w = glmnet(x = X.copy(), y = (Y @ v).copy(), alpha = alpha, lambdau = np.array([lambdau]),
standardize = False, intr = False)['beta']
else:
if sparsity=='row-wise':
w = glmnet(x = X.copy(), y = (Y @ v).copy(), alpha = alpha, lambdau = np.array([lambdau]),
family = "mgaussian", standardize = False, intr = False,
standardize_resp = False)['beta']
else:
w = []
for i in range(rank):
w.append(glmnet(x = X.copy(), y = (Y @ v[:,i]).copy(), alpha = alpha, lambdau = np.array([lambdau]),
standardize = False, intr = False, standardize_resp = False)['beta'])
w = np.concatenate(w, axis=1)
if np.all(w==0):
v = v * 0
return (w, v)
A = Y.T @ X @ w
a,c,b = np.linalg.svd(A, full_matrices = False)
v = a @ b
pos = np.argmax(np.abs(v), axis=0)
flips = np.sign(v[pos, range(v.shape[1])])
v = v * flips
w = w * flips
loss[iter] = np.sum((Y - X @ w @ v.T)**2)/np.sum(Y**2);
if iter > 0 and np.abs(loss[iter]-loss[iter-1]) < 1e-6:
if verbose > 0:
print('Converged in {} iteration(s)'.format(iter))
break
if (iter == max_iter-1) and (verbose > 0):
print('Did not converge. Losses: ', loss)
return (w, v)
def relaxed_elastic_rrr(X, Y, rank=2, lambdau=1, alpha=0.5, max_iter = 100,
sparsity='row-wise'):
w,v = elastic_rrr(X, Y, rank=rank, lambdau=lambdau, alpha=alpha,
sparsity=sparsity, max_iter=max_iter)
if alpha==0: # pure ridge: no need to re-fit
return (w,v)
nz = np.sum(np.abs(w), axis=1) != 0
wr,vr = elastic_rrr(X[:,nz], Y, rank=rank, lambdau=lambdau, alpha=0,
sparsity=sparsity, max_iter=max_iter)
if np.sum(nz)>=np.shape(w)[1]:
w[nz,:] = wr
v = vr
else:
w[nz,:][:,:np.sum(nz)] = wr
w[nz,:][:,np.sum(nz):] = 0
v[:,:np.sum(nz)] = vr
v[:,np.sum(nz):] = 0
return (w,v)
###################################################
# Double biplot function
def bibiplot(X, Y, w, v,
YdimsNames=np.array([]), YdimsToShow=None,
XdimsNames=np.array([]), XdimsToShow=None,
titles=[], xylim = 3,
cellTypes=np.array([]), cellTypeColors={}, cellTypeLabels={},
figsize=(9,4), axes=None):
if XdimsToShow is None:
nz = np.sum(np.abs(w), axis=1) != 0
XdimsToShow = np.where(nz)[0]
if YdimsToShow is None:
nz = np.sum(np.abs(v), axis=1) != 0
YdimsToShow = np.where(nz)[0]
# Project and standardize
Zx = X @ w[:,:2]
Zy = Y @ v[:,:2]
Zx = Zx / np.std(Zx, axis=0)
Zy = Zy / np.std(Zy, axis=0)
if not axes:
plt.figure(figsize=figsize)
plt.subplot(121, aspect='equal')
else:
plt.sca(axes[0])
if cellTypes.size == 0:
plt.scatter(Zx[:,0], Zx[:,1])
else:
for u in np.unique(cellTypes):
if not cellTypeLabels:
plt.scatter(Zx[cellTypes==u,0], Zx[cellTypes==u,1], color=cellTypeColors[u])
else:
plt.scatter(Zx[cellTypes==u,0], Zx[cellTypes==u,1], color=cellTypeColors[u], label=cellTypeLabels[u])
plt.xlim([-xylim,xylim])
plt.ylim([-xylim,xylim])
plt.gca().set_xticklabels([])
plt.gca().set_yticklabels([])
plt.xlabel('Component 1')
plt.ylabel('Component 2')
if titles:
plt.title(titles[0])
if cellTypeLabels:
plt.legend(bbox_to_anchor=(1.35, 1.0))
if XdimsToShow.size > 0:
scaleFactor = 2
L = np.corrcoef(np.concatenate((Zx[:,:2], X), axis=1), rowvar=False)[2:,:2]
for i in XdimsToShow:
plt.plot([0, scaleFactor*L[i,0]], [0, scaleFactor*L[i,1]], linewidth=1, color=[.4, .4, .4])
plt.text(scaleFactor*L[i,0]*1.2, scaleFactor*L[i,1]*1.2, XdimsNames[i],
ha='center', va='center', color=[.4, .4, .4], fontsize=10)
circ = plt.Circle((0,0), radius=scaleFactor, color=[.4, .4, .4], fill=False, linewidth=1)
plt.gca().add_patch(circ)
if not axes:
plt.subplot(122, aspect='equal')
else:
if not axes[1]:
return
plt.sca(axes[1])
if cellTypes.size == 0:
plt.scatter(Zy[:,0], Zy[:,1])
else:
for u in np.unique(cellTypes):
plt.scatter(Zy[cellTypes==u,0], Zy[cellTypes==u,1], color=cellTypeColors[u])
plt.xlim([-xylim,xylim])
plt.ylim([-xylim,xylim])
plt.gca().set_xticklabels([])
plt.gca().set_yticklabels([])
plt.xlabel('Component 1')
plt.ylabel('Component 2')
if titles:
plt.title(titles[1])
plt.tight_layout()
if YdimsToShow.size > 0:
scaleFactor = 2
L = np.corrcoef(np.concatenate((Zy[:,:2], Y), axis=1), rowvar=False)[2:,:2]
for i in YdimsToShow:
plt.plot([0, scaleFactor*L[i,0]], [0, scaleFactor*L[i,1]], linewidth=1, color=[.4, .4, .4])
plt.text(scaleFactor*L[i,0]*1.2, scaleFactor*L[i,1]*1.2, YdimsNames[i],
ha='center', va='center', color=[.4, .4, .4], fontsize=10)
circ = plt.Circle((0,0), radius=scaleFactor, color=[.4, .4, .4], fill=False, linewidth=1)
plt.gca().add_patch(circ)
###################################################
# Permutation procedures to estimate dimensionality
def dimensionality(X, Y, nrep = 100, seed = 42, axes=None, figsize=(7,2)):
np.random.seed(seed)
_,spectrum,_ = np.linalg.svd(X, full_matrices=False)
spectra = np.zeros((nrep, spectrum.size))
for rep in range(nrep):
Xperm = X.copy()
for i in range(Xperm.shape[1]):
Xperm[:,i] = Xperm[:,i][np.random.permutation(Xperm.shape[0])]
_, spectra[rep,:], _ = np.linalg.svd(Xperm, full_matrices=False)
if not axes:
plt.figure(figsize=figsize)
plt.subplot(131)
else:
plt.sca(axes[0])
plt.plot(np.arange(1, spectrum.size), spectra[:,:-1].T**2/np.sum(spectrum**2), 'k', linewidth=1)
plt.plot(np.arange(1, spectrum.size), spectrum[:-1]**2/np.sum(spectrum**2), '.-')
dimX = np.where(spectrum < np.percentile(spectra, 95, axis=0))[0][0]
plt.text(plt.xlim()[1]*.2, plt.ylim()[1]*.8, 'X dimensionality: ' + str(dimX), fontsize=8)
_,spectrum,_ = np.linalg.svd(Y, full_matrices=False)
spectra = np.zeros((nrep, spectrum.size))
for rep in range(nrep):
Xperm = Y.copy()
for i in range(Xperm.shape[1]):
Xperm[:,i] = Xperm[:,i][np.random.permutation(Xperm.shape[0])]
_, spectra[rep,:], _ = np.linalg.svd(Xperm, full_matrices=False)
showy = True
if not axes:
plt.subplot(132)
else:
if axes[1]:
plt.sca(axes[1])
else:
showy = False
if showy:
plt.plot(np.arange(1, spectrum.size), spectra[:,:-1].T**2/np.sum(spectrum**2), 'k', linewidth=1)
plt.plot(np.arange(1, spectrum.size), spectrum[:-1]**2/np.sum(spectrum**2), '.-')
dimY = np.where(spectrum < np.percentile(spectra, 95, axis=0))[0][0]
plt.text(plt.xlim()[1]*.2, plt.ylim()[1]*.8, 'Y dimensionality: ' + str(dimY), fontsize=8)
Xz,_,_ = np.linalg.svd(X, full_matrices=False)
Xz = Xz[:,:dimX]
yhat = Xz @ Xz.T @ Y
_,spectrum,_ = np.linalg.svd(yhat, full_matrices=False)
spectra = np.zeros((nrep, spectrum.size))
for rep in range(nrep):
Xz = Xz[np.random.permutation(Xz.shape[0]),:]
yhat = Xz @ Xz.T @ Y
_, spectra[rep,:], _ = np.linalg.svd(yhat, full_matrices=False)
if not axes:
plt.subplot(133)
else:
plt.sca(axes[2])
p = min(dimX, Y.shape[1])
plt.plot(np.arange(1, p+1), spectra[:,:p].T**2/np.sum(spectrum**2), 'k', linewidth=1)
plt.plot(np.arange(1, p+1), spectrum[:p]**2/np.sum(spectrum**2), '.-')
dimRRR = np.where(spectrum > np.percentile(spectra, 95, axis=0))[0][-1]
plt.text(plt.xlim()[1]*.2, plt.ylim()[1]*.8, 'RRR dimensionality: ' + str(dimRRR), fontsize=8)
plt.tight_layout()
###################################################
# Cross-validation for elastic net reduced-rank regression
def elastic_rrr_cv(X, Y, alphas = np.array([.2, .5, .9]), lambdas = np.array([.01, .1, 1]),
reps=10, folds=10, rank=1, seed=42, sparsity='row-wise'):
n = X.shape[0]
r2 = np.zeros((folds, reps, len(lambdas), len(alphas))) * np.nan
r2_relaxed = np.zeros((folds, reps, len(lambdas), len(alphas))) * np.nan
corrs = np.zeros((folds, reps, len(lambdas), len(alphas), rank)) * np.nan
corrs_relaxed = np.zeros((folds, reps, len(lambdas), len(alphas), rank)) * np.nan
nonzero = np.zeros((folds, reps, len(lambdas), len(alphas))) * np.nan
# CV repetitions
np.random.seed(seed)
t = time.time()
for rep in range(reps):
print(rep+1, end='')
ind = np.random.permutation(n)
X = X[ind,:]
Y = Y[ind,:]
# CV folds
for cvfold in range(folds):
print('.', end='')
indtest = np.arange(cvfold*int(n/folds), (cvfold+1)*int(n/folds))
indtrain = np.setdiff1d(np.arange(n), indtest)
Xtrain = np.copy(X[indtrain,:])
Ytrain = np.copy(Y[indtrain,:])
Xtest = np.copy(X[indtest,:])
Ytest = np.copy(Y[indtest,:])
# mean centering
X_mean = np.mean(Xtrain, axis=0)
Xtrain -= X_mean
Xtest -= X_mean
Y_mean = np.mean(Ytrain, axis=0)
Ytrain -= Y_mean
Ytest -= Y_mean
# loop over regularization parameters
for i,a in enumerate(lambdas):
for j,b in enumerate(alphas):
vx,vy = elastic_rrr(Xtrain, Ytrain, lambdau=a, alpha=b, rank=rank, sparsity=sparsity)
nz = np.sum(np.abs(vx), axis=1) != 0
if np.sum(nz) < rank:
continue
if np.allclose(np.std(Xtest @ vx, axis=0), 0):
continue
nonzero[cvfold, rep, i, j] = np.sum(nz)
r2[cvfold, rep, i, j] = 1 - np.sum((Ytest - Xtest @ vx @ vy.T)**2) / np.sum(Ytest**2)
for r in range(rank):
corrs[cvfold, rep, i, j, r] = np.corrcoef(Xtest @ vx[:,r], Ytest @ vy[:,r], rowvar=False)[0,1]
# Relaxation
vxr,vyr = elastic_rrr(Xtrain[:,nz], Ytrain, lambdau=a, alpha=0, rank=rank, sparsity=sparsity)
if np.sum(nz)>=np.shape(vy)[1]:
vx[nz,:] = vxr
vy = vyr
else:
vx[nz,:][:,:np.sum(nz)] = vxr
vx[nz,:][:,np.sum(nz):] = 0
vy[:,:np.sum(nz)] = vyr
vy[:,np.sum(nz):] = 0
if np.allclose(np.std(Xtest @ vx, axis=0), 0):
continue
r2_relaxed[cvfold, rep, i, j] = 1 - np.sum((Ytest - Xtest @ vx @ vy.T)**2) / np.sum(Ytest**2)
for r in range(rank):
corrs_relaxed[cvfold, rep, i, j, r] = np.corrcoef(Xtest @ vx[:,r], Ytest @ vy[:,r], rowvar=False)[0,1]
print(' ', end='')
t = time.time() - t
m,s = divmod(t, 60)
h,m = divmod(m, 60)
print('Time: {}h {:2.0f}m {:2.0f}s'.format(h,m,s))
return r2, r2_relaxed, nonzero, corrs, corrs_relaxed
###################################################
# Bootstrap selection for elastic net reduced-rank regression
def elastic_rrr_bootstrap(X, Y, rank=1, lambdau = 1.5, alpha = .5, nrep = 100, seed=42):
np.random.seed(seed)
ww = np.zeros((X.shape[1], nrep))
for rep in range(nrep):
print('.', end='')
n = np.random.choice(X.shape[0], size = X.shape[0])
w,v = elastic_rrr(X[n,:], Y[n,:], rank = rank, lambdau = lambdau, alpha = alpha)
ww[:,rep] = w[:,0]
print(' ')
bootCounts = np.sum(ww!=0, axis=1)/nrep
return bootCounts
####################################################
# Plot CV results
def plot_cv_results(r2=None, r2_relaxed=None, nonzeros=None, corrs=None, corrs_relaxed=None, alphas=None):
# suppressing "mean of empty slice" warnings
with warnings.catch_warnings():
warnings.simplefilter("ignore", category=RuntimeWarning)
n = np.nanmean(nonzeros, axis=(0,1))
cr = np.nanmean(r2_relaxed, axis=(0,1))
c = np.nanmean(r2, axis=(0,1))
c1 = np.nanmean(corrs_relaxed, axis=(0,1))[:,:,0]
if corrs_relaxed.shape[4]>1:
c2 = np.nanmean(corrs_relaxed, axis=(0,1))
plt.figure(figsize=(9,4))
plt.subplot(121)
plt.plot(n, cr, '.-', linewidth=1)
plt.gca().set_prop_cycle(None)
plt.plot(n, c, '.--', linewidth=1, alpha=.5)
plt.xscale('log')
plt.xlabel('Number of non-zero genes')
plt.ylabel('Test R2')
plt.legend(['$\\alpha='+str(a)+'$' for a in alphas])
plt.subplot(122)
plt.plot(n, c1, '.-', linewidth=1)
if corrs_relaxed.shape[4]>1:
for k in range(1, corrs_relaxed.shape[4]):
plt.gca().set_prop_cycle(None)
plt.plot(n, c2[:,:,k], '.--', linewidth=1)
plt.xscale('log')
plt.xlabel('Number of non-zero genes')
plt.ylabel('Correlations')
plt.legend(alphas)
plt.legend(['$\\alpha='+str(a)+'$' for a in alphas])
plt.tight_layout()
def geneSelection(data, threshold=0, atleast=10,
yoffset=.02, xoffset=5, decay=1.5, n=None,
plot=True, markers=None, genes=None, figsize=(6,3.5),
markeroffsets=None, labelsize=10, alpha=1):
if sparse.issparse(data):
zeroRate = 1 - np.squeeze(np.array((data>threshold).mean(axis=0)))
A = data.multiply(data>threshold)
A.data = np.log2(A.data)
meanExpr = np.zeros_like(zeroRate) * np.nan
detected = zeroRate < 1
meanExpr[detected] = np.squeeze(np.array(A[:,detected].mean(axis=0))) / (1-zeroRate[detected])
else:
zeroRate = 1 - np.mean(data>threshold, axis=0)
meanExpr = np.zeros_like(zeroRate) * np.nan
detected = zeroRate < 1
mask = data[:,detected]>threshold
logs = np.zeros_like(data[:,detected]) * np.nan
logs[mask] = np.log2(data[:,detected][mask])
meanExpr[detected] = np.nanmean(logs, axis=0)
lowDetection = np.array(np.sum(data>threshold, axis=0)).squeeze() < atleast
zeroRate[lowDetection] = np.nan
meanExpr[lowDetection] = np.nan
if n is not None:
up = 10
low = 0
for t in range(100):
nonan = ~np.isnan(zeroRate)
selected = np.zeros_like(zeroRate).astype(bool)
selected[nonan] = zeroRate[nonan] > np.exp(-decay*(meanExpr[nonan] - xoffset)) + yoffset
if np.sum(selected) == n:
break
elif np.sum(selected) < n:
up = xoffset
xoffset = (xoffset + low)/2
else:
low = xoffset
xoffset = (xoffset + up)/2
print('Chosen offset: {:.2f}'.format(xoffset))
else:
nonan = ~np.isnan(zeroRate)
selected = np.zeros_like(zeroRate).astype(bool)
selected[nonan] = zeroRate[nonan] > np.exp(-decay*(meanExpr[nonan] - xoffset)) + yoffset
if plot:
if figsize is not None:
plt.figure(figsize=figsize)
plt.ylim([0, 1])
if threshold>0:
plt.xlim([np.log2(threshold), np.ceil(np.nanmax(meanExpr))])
else:
plt.xlim([0, np.ceil(np.nanmax(meanExpr))])
x = np.arange(plt.xlim()[0], plt.xlim()[1]+.1,.1)
y = np.exp(-decay*(x - xoffset)) + yoffset
if decay==1:
plt.text(.4, 0.2, '{} genes selected\ny = exp(-x+{:.2f})+{:.2f}'.format(np.sum(selected),xoffset, yoffset),
color='k', fontsize=labelsize, transform=plt.gca().transAxes)
else:
plt.text(.4, 0.2, '{} genes selected\ny = exp(-{:.1f}*(x-{:.2f}))+{:.2f}'.format(np.sum(selected),decay,xoffset, yoffset),
color='k', fontsize=labelsize, transform=plt.gca().transAxes)
plt.plot(x, y, color=sns.color_palette()[1], linewidth=2)
xy = np.concatenate((np.concatenate((x[:,None],y[:,None]),axis=1), np.array([[plt.xlim()[1], 1]])))
t = plt.matplotlib.patches.Polygon(xy, color=sns.color_palette()[1], alpha=.4)
plt.gca().add_patch(t)
plt.scatter(meanExpr, zeroRate, s=1, alpha=alpha, rasterized=True)
if threshold==0:
plt.xlabel('Mean log2 nonzero expression')
plt.ylabel('Frequency of zero expression')
else:
plt.xlabel('Mean log2 nonzero expression')
plt.ylabel('Frequency of near-zero expression')
plt.tight_layout()
if markers is not None and genes is not None:
if markeroffsets is None:
markeroffsets = [(0, 0) for g in markers]
for num,g in enumerate(markers):
i = np.where(genes==g)[0]
plt.scatter(meanExpr[i], zeroRate[i], s=10, color='k')
dx, dy = markeroffsets[num]
plt.text(meanExpr[i]+dx+.1, zeroRate[i]+dy, g, color='k', fontsize=labelsize)
return selected