-
Notifications
You must be signed in to change notification settings - Fork 3
/
GeneAE.py
executable file
·384 lines (284 loc) · 11.9 KB
/
GeneAE.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from load import save_h5ad, load_h5ad
#from loss import NB_loglikelihood
from temp import save_figure, plotTSNE
import numpy as np
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import QuantileTransformer, StandardScaler, MinMaxScaler
import tensorflow as tf
import keras.backend as K
from keras.utils import plot_model
from keras.layers import Input, Dense
from keras.models import Model
from keras import regularizers
from keras.layers.advanced_activations import LeakyReLU
from keras.layers import multiply
import scanpy as sc
# (Almost reproducible)
# np.random.seed(1337)
# tf.random.set_seed(1235)
# from tensorflow.python import debug as tf_debug
# sess = K.get_session()
# sess = tf_debug.LocalCLIDebugWrapperSession(sess)
# K.set_session(sess)
# tf.config.experimental_run_functions_eagerly(True)
from time import time
from keras.callbacks import TensorBoard
if int(tf.__version__[0]) < 2:
tf2_flag = False
else:
tf2_flag = True
# NEED TO PUT THIS IN DIFFERENT FILE with code from temp.py
# create directory 'models' if it doesn't exist
base_dir = '.'
plots_dir = base_dir + '/plots'
models_dir = plots_dir + '/models'
from pathlib import Path
for i in [plots_dir, models_dir]:
Path(i).mkdir(parents=True, exist_ok=True)
# =============================================================================
# Model parameters
# =============================================================================
# Size of encoded representation
encoding_dim = 256
# Fraction of data used in training
train_size = 0.7
epochs = 1
batch_size = 256
# =============================================================================
# Load data
# =============================================================================
adata = load_h5ad('preprocessed') # need to add code to ensure this exists
# Input shape
input_dim = adata.X.shape[1]
input_shape = (input_dim,)
# scaler = QuantileTransformer(n_quantiles=1000, output_distribution='normal')
# scaler = StandardScaler()
x = 1e-10
gene_scaler = MinMaxScaler(feature_range=(x, 1-x))
sf_scaler = MinMaxScaler(feature_range=(x, 1-x))
adata.X = gene_scaler.fit_transform(adata.X)
adata.obs['sf'].values[:] = sf_scaler.fit_transform(adata.obs['sf'].values.reshape(-1, 1)).reshape(1, -1)
# adata.obs['sf'].values[:] = 1
# scale = X.max(axis=0)
# X = np.divide(X, scale)
X_train, X_test = train_test_split(adata.X,
train_size=train_size, shuffle=False)
sf_train, sf_test = train_test_split(adata.obs['sf'].values,
train_size=train_size, shuffle=False)
# =============================================================================
# Build models
# =============================================================================
use_sf = True
calc_sf_count = True
calc_sf_obs = False
model = 'zinb'
# model = 'nb'
#model = 'gaussian'
# Encoder Model
count_input = Input(shape=input_shape, name='count_input')
x = Dense(1024, activation='relu')(count_input)
x = Dense(512, activation='relu')(x)
latent = Dense(encoding_dim, activation='relu', name='latent')(x)
encoder = Model(count_input, latent, name='encoder')
plot_model(encoder, to_file=models_dir + '/' + model + '_encoder.png',
show_shapes=True, show_layer_names=True)
# Size factors
# try learning sf from the input data, hopefully will prevent factors from being so large
#sf_input = Input(shape-input_shape, name='size_factor_input')
if use_sf:
if calc_sf_count:
sf = Dense(1, activation='relu')(count_input)
sf_model = Model(count_input, sf, name='sf_model')
elif calc_sf_obs:
sf_input = Input(shape=(1,), name='size_factor_input')
sf = Dense(1, activation='relu')(sf_input)
sf_model = Model(sf_input, sf, name='sf_model')
else:
sf = Input(shape=(1,), name='size_factor_input')
# Decoder Model
# Lossy reconstruction of the input
lat_input = Input(shape=(encoding_dim,))
x = Dense(512, activation='relu')(lat_input)
x = Dense(1024, activation='relu')(x)
if model == 'gaussian':
decoder_outputs = Dense(input_dim, activation='sigmoid')(x)
elif model == 'nb' or model == 'zinb':
MeanAct = lambda a: tf.clip_by_value(K.exp(a), 1e-5, 1e6)
DispAct = lambda a: tf.clip_by_value(tf.nn.softplus(a), 1e-4, 1e4)
mu = Dense(input_dim, activation = MeanAct, name='mu')(x)
disp = Dense(input_dim, activation = DispAct, name='disp')(x)
# Multiply mu by sf here, broadcasting not done >> need to use K.repeat_elements
# sf_reshape = tf.reshape(sf, [-1,1]) # Column vector reshaped to row vector
# sf = K.repeat_elements(sf, input_dim, 1)
# mu_sf = mu * sf_reshape # Multiply by sf here
if use_sf:
mu_sf = multiply([mu, sf])
decoder_outputs = [mu_sf, disp]
if calc_sf_count:
decoder_inputs = [lat_input, count_input]
elif calc_sf_obs:
decoder_inputs = [lat_input, sf_input]
else:
decoder_inputs = [lat_input, sf]
else:
decoder_inputs = lat_input
decoder_outputs = [mu, disp]
if model == 'zinb':
# Activation is sigmoid because values restricted to [0,1]
pi = Dense(input_dim, activation = 'sigmoid', name='pi')(x)
decoder_outputs.append(pi)
decoder = Model(decoder_inputs, decoder_outputs, name='decoder')
plot_model(decoder, to_file=models_dir + '/' + model + '_decoder.png',
show_shapes=True, show_layer_names=True)
# Autoencoder Model
if use_sf:
if calc_sf_count:
AE_inputs = count_input
AE_outputs = decoder([encoder(count_input), count_input])
elif calc_sf_obs:
AE_inputs = [count_input, sf_input]
AE_outputs = decoder([encoder(count_input), sf_input])
else:
AE_inputs = [count_input, sf]
AE_outputs = decoder([encoder(count_input), sf])
else:
AE_inputs = count_input
AE_outputs = decoder(encoder(count_input))
autoencoder = Model(AE_inputs, AE_outputs, name='autoencoder')
print (autoencoder.summary())
plot_model(autoencoder, to_file=models_dir + '/' + model + '_autoencoder.png',
show_shapes=True, show_layer_names=True)
# =============================================================================
# Define custom loss
# =============================================================================
def NB_loglikelihood(outputs):
def loss (y_true, y_pred):
eps = 1e-10 # Prevent NaN loss value?
y = y_true
mu = outputs[0]
r = outputs[1]
if tf2_flag:
l1 = tf.math.lgamma(y+r+eps) - tf.math.lgamma(r+eps) - tf.math.lgamma(y+1.0)
l2 = y * tf.math.log((mu+eps)/(r+mu+eps)) + r * tf.math.log((r+eps)/(r+mu+eps))
else:
l1 = tf.lgamma(y+r) - tf.lgamma(r) - tf.lgamma(y+1.0)
l2 = y * tf.log(mu/(r+mu)) + r * tf.log(r/(r+mu))
log_likelihood = l1 + l2
return -K.sum(log_likelihood, axis=-1)
return loss
def ZINB_loglikelihood(outputs):
# return scalar loss for each data point
def loss (y_true, y_pred):
eps = 1e-10 # Prevent NaN loss value?
y = y_true
mu = outputs[0]
r = outputs[1]
pi = outputs[2]
if tf2_flag:
l1 = tf.math.lgamma(y+r+eps) - tf.math.lgamma(r+eps) - tf.math.lgamma(y+1.0)
l2 = y * tf.math.log((mu+eps)/(r+mu+eps)) + r * tf.math.log((r+eps)/(r+mu+eps))
else:
l1 = tf.lgamma(y+r) - tf.lgamma(r) - tf.lgamma(y+1.0)
l2 = y * tf.log(mu/(r+mu)) + r * tf.log(r/(r+mu))
nb_log_likelihood = l1 + l2
if tf2_flag:
case_zero = tf.math.log(eps + pi + (1.0 - pi) * tf.math.pow((r/(r+mu+eps)), r))
case_nonzero = tf.math.log(1.0 - pi + eps) + nb_log_likelihood
else:
case_zero = tf.log(pi + (1.0-pi) * tf.pow((r/(r+mu)), r))
case_nonzero = tf.log(1.0-pi) + nb_log_likelihood
# whenever a count value < 1e-8, use case_zero for the log-likelihood
zinb_log_likelihood = tf.where(tf.less(y, 1e-8), case_zero, case_nonzero)
# return scalar for each cell;
# cell likelihood = product of likelihoods over genes
return -K.sum(zinb_log_likelihood, axis=1)
return loss
# Loss function run thrice (once for each output) but only one used
if model == 'zinb':
autoencoder.compile(optimizer='adam',
loss=ZINB_loglikelihood(AE_outputs),
loss_weights=[1., 0.0, 0.0])
if model == 'nb':
autoencoder.compile(optimizer='adam',
loss=NB_loglikelihood(AE_outputs),
loss_weights=[1., 0.0])
# alternative method: add_loss does not require you to restrict the parameters
# of the loss to y_pred and y_actual
# may change to this
'''
def NB_loglikelihood(y, mu, r):
if tf2_flag:
l1 = tf.math.lgamma(y+r) - tf.math.lgamma(r) - tf.math.lgamma(y+1.0)
l2 = y * tf.math.log(mu/(r+mu)) + r * tf.math.log(r/(r+mu))
else:
l1 = tf.lgamma(y+r) - tf.lgamma(r) - tf.lgamma(y+1.0)
l2 = y * tf.log(mu/(r+mu)) + r * tf.log(r/(r+mu))
log_likelihood = l1 + l2
return log_likelihood
if model == 'nb':
reconstruction_loss = - K.sum(NB_loglikelihood(input, outputs[0],
outputs[1]), axis=-1)
print (K.print_tensor(NB_loglikelihood(input, outputs[0], outputs[1])))
print (K.print_tensor(reconstruction_loss))
autoencoder.add_loss(K.mean(reconstruction_loss))
autoencoder.add_loss(reconstruction_loss)
autoencoder.compile(optimizer='adam', loss=None)
'''
if model == 'gaussian':
autoencoder.compile(optimizer='adam', loss='mse')
# from tb_callback import MyTensorBoard
tensorboard = TensorBoard(log_dir='logs/{}'.format(time()))
# =============================================================================
# Train model
# =============================================================================
if use_sf and not calc_sf_count:
fit_x = [X_train, sf_train]
else:
fit_x = X_train
if model == 'gaussian':
fit_y = X_train
elif model == 'nb':
fit_y = [X_train, X_train]
elif model == 'zinb':
fit_y = [X_train, X_train, X_train]
# Pass adata.obs['sf'] as an input. 2nd, 3rd elements of y not used
loss = autoencoder.fit(fit_x, fit_y, epochs=epochs, batch_size=batch_size,
shuffle=False, callbacks=[tensorboard])
autoencoder.save('AE.h5')
# =============================================================================
# Test model
# =============================================================================
if use_sf:
if calc_sf_count:
encoded_data = encoder.predict(adata.X)
decoded_data = decoder.predict([encoded_data, adata.X])
else:
encoded_data = encoder.predict(adata.X)
decoded_data = decoder.predict([encoded_data, adata.obs['sf'].values])
else:
encoded_data = encoder.predict(adata.X)
decoded_data = decoder.predict(encoded_data)
adata.X = decoded_data[0]
# adata.X = gene_scaler.inverse_transform(decoded_data[0])
save_h5ad(adata, 'denoised')
def test_sf():
if calc_sf_count:
sf = sf_model.predict(adata.X)
else:
sf = sf_model.predict(adata.obs['sf'].values)
return sf
def test_AE():
if use_sf:
if calc_sf_count:
encoded_data = encoder.predict(X_train[0:batch_size])
decoded_data = decoder.predict([encoded_data, X_train[0:batch_size]])
else:
encoded_data = encoder.predict(X_train[0:batch_size])
decoded_data = decoder.predict([encoded_data, adata.obs['sf'].values[0:batch_size]])
else:
encoded_data = encoder.predict(X_train[0:batch_size])
decoded_data = decoder.predict(encoded_data)
return decoded_data