-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbigcn.py
156 lines (122 loc) · 4.3 KB
/
bigcn.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
import random
import numpy as np
import pandas as pd
import torch
from sklearn.metrics.pairwise import pairwise_kernels
from torch.nn import BatchNorm1d, Dropout, Linear, Module, MSELoss
from torch.nn.functional import relu
from torch_geometric.data import Data
from torch_geometric.nn import GCNConv, GraphNorm
from torch_sparse import SparseTensor
from tqdm import tqdm
def get_topX(X):
"""Get top X% of the values in the matrix"""
return X * np.array(X > np.percentile(X, 85), dtype=int)
def get_adj(X):
"""Get adjacency matrix from the matrix"""
adj = SparseTensor(
row=torch.tensor(np.array(X.nonzero()))[0],
col=torch.tensor(np.array(X.nonzero()))[1],
sparse_sizes=(X.shape[0], X.shape[0]),
)
return adj
def get_data(X, metric="linear"):
"""Get data and adjacency matrix from the matrix"""
dist = pairwise_kernels(X, metric=metric)
dist_x = get_topX(dist)
return torch.tensor(X.values, dtype=torch.float), get_adj(dist_x)
class AE_GCN(Module):
"""Autoencoder with GCN layers"""
def __init__(
self,
params,
):
super(AE_GCN, self).__init__()
self.gcn1 = GCNConv(params["hidden1"], params["hidden2"])
self.gcn2 = GCNConv(params["hidden2"], params["hidden1"])
self.dropout1 = Dropout(params["dropout1"])
self.dropout2 = Dropout(params["dropout2"])
self.graph_norm1 = GraphNorm(params["hidden2"])
self.graph_norm2 = GraphNorm(params["hidden1"])
self.gcn3 = GCNConv(params["hidden0"], params["hidden3"])
self.gcn4 = GCNConv(params["hidden3"], params["hidden0"])
self.graph_norm3 = GraphNorm(params["hidden3"])
self.graph_norm4 = GraphNorm(params["hidden0"])
self.batch_norm1 = BatchNorm1d(params["hidden1"])
self.batch_norm2 = BatchNorm1d(params["hidden0"])
def forward(self, data, x, adj, x_t, adj_t, clustering):
"""
data: gene expression matrix
x: gene expression matrix
adj: cell-cell similarity matrix
x_t: transposed gene expression matrix
adj_t: gene-gene similarity matrix
"""
# For Cell similarity
x = self.dropout1(relu(self.graph_norm1(self.gcn1(x, adj.t()))))
x = relu(self.graph_norm2(self.gcn2(x, adj.t())))
# For Gene similarity
x_t = self.dropout2(relu(self.graph_norm3(self.gcn3(x_t, adj_t.t()))))
x_t = relu(self.graph_norm4(self.gcn4(x_t, adj_t.t())))
res = x + x_t.T
if clustering:
res = self.batch_norm1(data) + self.batch_norm2(data.T).T
return res
def run_model(input_data, params=None, clustering=False, verbose=False, device=None):
"""Run model
input_data: gene expression matrix
params: hyperparameters
clustering: whether to add batch normalized data
"""
if device is None:
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
params = {
"dropout1": 0.3,
"dropout2": 0.1,
"epochs": 1500,
"hidden2": 128,
"hidden3": 1024,
"lr": 0.0001,
"optimizer": "Adam",
"clustering": False,
}
if clustering:
params = {
"dropout1": 0.4,
"dropout2": 0.3,
"epochs": 100,
"hidden2": 256,
"hidden3": 512,
"lr": 0.01,
"optimizer": "Adam",
"clustering": True,
}
x, adj = get_data(input_data)
x_t, adj_t = get_data(input_data.T)
x = x.to(device)
adj = adj.to(device)
x_t = x_t.to(device)
adj_t = adj_t.to(device)
params["hidden0"] = input_data.shape[0]
params["hidden1"] = input_data.shape[1]
model = AE_GCN(params).to(device)
loss_function = MSELoss().to(device)
optimizer = getattr(torch.optim, params["optimizer"])(
model.parameters(),
lr=params["lr"],
)
losses = []
res = pd.DataFrame()
if verbose:
epochs = tqdm(range(params["epochs"]))
else:
epochs = range(params["epochs"])
for epoch in epochs:
reconstructed = model(x, x, adj, x_t, adj_t, clustering)
loss = loss_function(reconstructed, x)
optimizer.zero_grad()
loss.backward()
optimizer.step()
losses.append(loss.item())
pred = reconstructed.cpu().detach().numpy()
return pred