forked from Lifeomics/DUSTED
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.py
316 lines (254 loc) · 10.9 KB
/
model.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
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch_geometric.nn import GCNConv, GATConv
from STAGATE_pyG.gat_conv import GATConv
class Flatten(nn.Module):
"""
Flatten layer to reshape the input tensor to a 2D tensor (batch_size, -1).
"""
def forward(self, x):
return x.view(x.size(0), -1)
def mean_act(x):
"""
Activation function for mean predictions.
Args:
x (torch.Tensor): Input tensor.
Returns:
torch.Tensor: Activated tensor with values clamped between 1e-5 and 1e6.
"""
return torch.clamp(torch.exp(x), 1e-5, 1e6)
def disp_act(x):
"""
Activation function for dispersion predictions.
Args:
x (torch.Tensor): Input tensor.
Returns:
torch.Tensor: Activated tensor with values clamped between 1e-4 and 1e4.
"""
return torch.clamp(F.softplus(x), 1e-4, 1e4)
def pi_act(x):
"""
Activation function for zero-inflation probability.
Args:
x (torch.Tensor): Input tensor.
Returns:
torch.Tensor: Activated tensor with values between 0 and 1.
"""
return torch.sigmoid(x)
class GCALayer(nn.Module):
"""
Graph Channel Attention (GCA) layer.
Args:
gate_channels (int): Number of channels for the gate.
reduction_ratio (int): Reduction ratio for the channel attention mechanism.
pool_types (list): Types of pooling operations to apply ('avg', 'max').
"""
def __init__(self, gate_channels, reduction_ratio=16, pool_types=['avg', 'max']):
super(GCALayer, self).__init__()
self.gate_channels = gate_channels
self.mlp = nn.Sequential(
Flatten(),
nn.Linear(gate_channels, gate_channels // reduction_ratio),
nn.ReLU(),
nn.Linear(gate_channels // reduction_ratio, gate_channels)
)
self.pool_types = pool_types
def forward(self, x):
channel_att_sum = None
for pool_type in self.pool_types:
if pool_type == 'avg':
pool = torch.mean(x, dim=0, keepdim=True)
elif pool_type == 'max':
pool = torch.max(x, dim=0, keepdim=True)[0]
channel_att_raw = self.mlp(pool)
if channel_att_sum is None:
channel_att_sum = channel_att_raw
else:
channel_att_sum += channel_att_raw
scale = torch.sigmoid(channel_att_sum).expand_as(x)
return x * scale
class DUSTED(nn.Module):
"""
DUSTED: A dual-attention spatial transcriptomics enhanced denoiser model.
Args:
hidden_dims (list): List of dimensions for input, hidden, and output layers.
"""
def __init__(self, hidden_dims,alpha=1.5):
super(DUSTED, self).__init__()
[in_dim, num_hidden, out_dim] = hidden_dims
self.conv1 = GATConv(in_dim, num_hidden, heads=1, concat=False,
dropout=0, add_self_loops=False, bias=False)
self.conv2 = GATConv(num_hidden, out_dim, heads=1, concat=False,
dropout=0, add_self_loops=False, bias=False)
self.conv3 = GATConv(out_dim, num_hidden, heads=1, concat=False,
dropout=0, add_self_loops=False, bias=False)
self.disp = GATConv(num_hidden, in_dim, heads=1, concat=False,
dropout=0, add_self_loops=False, bias=False)
self.mean = GATConv(num_hidden, in_dim, heads=1, concat=False,
dropout=0, add_self_loops=False, bias=False)
self.pi = GATConv(num_hidden, in_dim, heads=1, concat=False,
dropout=0, add_self_loops=False, bias=False)
self.gca1 = GCALayer(in_dim)
self.alpha = alpha
def forward(self, features, edge_index, scale_factor):
h1 = self.gca1(features)
h1 = alpha * h1 + features
h1 = F.elu(self.conv1(h1, edge_index))
h2 = self.conv2(h1, edge_index)
self.conv3.lin_src.data = self.conv2.lin_src.transpose(0, 1)
self.conv3.lin_dst.data = self.conv2.lin_dst.transpose(0, 1)
h3 = F.elu(self.conv3(h2, edge_index, attention=True,
tied_attention=self.conv1.attentions))
pi = pi_act(self.pi(h3, edge_index))
disp = disp_act(self.disp(h3, edge_index))
mean = mean_act(self.mean(h3, edge_index))
mean = (mean.T * scale_factor).T
return mean, disp, pi, h2
class AE(nn.Module):
"""
Autoencoder (AE) model for feature extraction and dimensionality reduction.
Args:
n_enc_1 (int): Number of units in the first encoder layer.
n_enc_2 (int): Number of units in the second encoder layer.
n_enc_3 (int): Number of units in the third encoder layer.
n_dec_1 (int): Number of units in the first decoder layer.
n_dec_2 (int): Number of units in the second decoder layer.
n_dec_3 (int): Number of units in the third decoder layer.
n_input (int): Number of input features.
n_z (int): Number of latent variables.
"""
def __init__(self, n_enc_1, n_enc_2, n_enc_3, n_dec_1, n_dec_2, n_dec_3, n_input, n_z):
super(AE, self).__init__()
# Encoder layers
self.enc_1 = nn.Linear(n_input, n_enc_1)
self.enc_2 = nn.Linear(n_enc_1, n_enc_2)
self.enc_3 = nn.Linear(n_enc_2, n_enc_3)
self.z_layer = nn.Linear(n_enc_3, n_z)
# Decoder layers
self.dec_1 = nn.Linear(n_z, n_dec_1)
self.dec_2 = nn.Linear(n_dec_1, n_dec_2)
self.dec_3 = nn.Linear(n_dec_2, n_dec_3)
self.x_bar_layer = nn.Linear(n_dec_3, n_input)
def forward(self, x):
# Encoding
enc_h1 = F.relu(self.enc_1(x))
enc_h2 = F.relu(self.enc_2(enc_h1))
enc_h3 = F.relu(self.enc_3(enc_h2))
z = self.z_layer(enc_h3)
# Decoding
dec_h1 = F.relu(self.dec_1(z))
dec_h2 = F.relu(self.dec_2(dec_h1))
dec_h3 = F.relu(self.dec_3(dec_h2))
x_bar = self.x_bar_layer(dec_h3)
return x_bar, enc_h1, enc_h2, enc_h3, z
class GAE(nn.Module):
"""
Graph Autoencoder (GAE) model for graph-based feature extraction.
Args:
hidden_dims (list): List of dimensions for input, hidden, and output layers.
"""
def __init__(self, hidden_dims):
super(GAE, self).__init__()
in_dim, num_hidden, out_dim = hidden_dims
# Graph convolutional layers
self.conv1 = GCNConv(in_dim, num_hidden, cached=True)
self.conv2 = GCNConv(num_hidden, out_dim, cached=True)
self.conv3 = GCNConv(out_dim, num_hidden, cached=True)
self.conv4 = GCNConv(num_hidden, in_dim, cached=True)
def forward(self, features, edge_index):
h1 = F.elu(self.conv1(features, edge_index))
h2 = self.conv2(h1, edge_index)
h3 = F.elu(self.conv3(h2, edge_index))
h4 = self.conv4(h3, edge_index)
return h2, h4
class DCA_ZINB(nn.Module):
"""
Denoising Autoencoder (DCA) with ZINB output layer.
Args:
n_input (int): Number of input features.
n_z (int): Number of latent variables.
n_enc_1 (int): Number of units in the first encoder layer.
n_enc_2 (int): Number of units in the second encoder layer.
n_enc_3 (int): Number of units in the third encoder layer.
n_dec_1 (int): Number of units in the first decoder layer.
n_dec_2 (int): Number of units in the second decoder layer.
n_dec_3 (int): Number of units in the third decoder layer.
enc_dropout (float, optional): Dropout rate for encoder layers. Defaults to 0.5.
dec_dropout (float, optional): Dropout rate for decoder layers. Defaults to 0.5.
"""
def __init__(self, n_input, n_z, n_enc_1, n_enc_2, n_enc_3, n_dec_1, n_dec_2, n_dec_3, enc_dropout=0.5, dec_dropout=0.5):
super(DCA_ZINB, self).__init__()
# Encoder layers
self.enc_1 = nn.Linear(n_input, n_enc_1)
self.enc_2 = nn.Linear(n_enc_1, n_enc_2)
self.enc_3 = nn.Linear(n_enc_2, n_enc_3)
self.z_layer = nn.Linear(n_enc_3, n_z)
# Decoder layers
self.dec_1 = nn.Linear(n_z, n_dec_1)
self.dec_2 = nn.Linear(n_dec_1, n_dec_2)
self.dec_3 = nn.Linear(n_dec_2, n_dec_3)
# ZINB output layers
self.pi = nn.Linear(n_dec_3, n_input)
self.disp = nn.Linear(n_dec_3, n_input)
self.mean = nn.Linear(n_dec_3, n_input)
def forward(self, x, scale_factor):
# Encoding
enc_h1 = F.relu(self.enc_1(x))
enc_h2 = F.relu(self.enc_2(enc_h1))
enc_h3 = F.relu(self.enc_3(enc_h2))
z = self.z_layer(enc_h3)
# Decoding
dec_h1 = F.relu(self.dec_1(z))
dec_h2 = F.relu(self.dec_2(dec_h1))
dec_h3 = F.relu(self.dec_3(dec_h2))
# ZINB parameters
pi = pi_act(self.pi(dec_h3))
disp = disp_act(self.disp(dec_h3))
mean = mean_act(self.mean(dec_h3))
mean = (mean.T * scale_factor).T
return enc_h1, enc_h2, enc_h3, z, mean, disp, pi
class DCA_NB(nn.Module):
"""
Denoising Autoencoder (DCA) with NB output layer.
Args:
n_input (int): Number of input features.
n_z (int): Number of latent variables.
n_enc_1 (int): Number of units in the first encoder layer.
n_enc_2 (int): Number of units in the second encoder layer.
n_enc_3 (int): Number of units in the third encoder layer.
n_dec_1 (int): Number of units in the first decoder layer.
n_dec_2 (int): Number of units in the second decoder layer.
n_dec_3 (int): Number of units in the third decoder layer.
enc_dropout (float, optional): Dropout rate for encoder layers. Defaults to 0.5.
dec_dropout (float, optional): Dropout rate for decoder layers. Defaults to 0.5.
"""
def __init__(self, n_input, n_z, n_enc_1, n_enc_2, n_enc_3, n_dec_1, n_dec_2, n_dec_3, enc_dropout=0.5, dec_dropout=0.5):
super(DCA_NB, self).__init__()
# Encoder layers
self.enc_1 = nn.Linear(n_input, n_enc_1)
self.enc_2 = nn.Linear(n_enc_1, n_enc_2)
self.enc_3 = nn.Linear(n_enc_2, n_enc_3)
self.z_layer = nn.Linear(n_enc_3, n_z)
# Decoder layers
self.dec_1 = nn.Linear(n_z, n_dec_1)
self.dec_2 = nn.Linear(n_dec_1, n_dec_2)
self.dec_3 = nn.Linear(n_dec_2, n_dec_3)
# NB output layers
self.disp = nn.Linear(n_dec_3, n_input)
self.mean = nn.Linear(n_dec_3, n_input)
def forward(self, x, scale_factor):
# Encoding
enc_h1 = F.relu(self.enc_1(x))
enc_h2 = F.relu(self.enc_2(enc_h1))
enc_h3 = F.relu(self.enc_3(enc_h2))
z = self.z_layer(enc_h3)
# Decoding
dec_h1 = F.relu(self.dec_1(z))
dec_h2 = F.relu(self.dec_2(dec_h1))
dec_h3 = F.relu(self.dec_3(dec_h2))
# NB parameters
disp = disp_act(self.disp(dec_h3))
mean = mean_act(self.mean(dec_h3))
mean = (mean.T * scale_factor).T
return enc_h1, enc_h2, enc_h3, z, mean, disp