gnodevae 0.0.3__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- gnodevae-0.0.3/GNODEVAE/CLEAR.py +172 -0
- gnodevae-0.0.3/GNODEVAE/GODEVAE.py +413 -0
- gnodevae-0.0.3/GNODEVAE/GODEVAE_agent.py +385 -0
- gnodevae-0.0.3/GNODEVAE/GODEVAE_env.py +758 -0
- gnodevae-0.0.3/GNODEVAE/GODEVAE_trainer.py +309 -0
- gnodevae-0.0.3/GNODEVAE/SC3.py +198 -0
- gnodevae-0.0.3/GNODEVAE/__init__.py +6 -0
- gnodevae-0.0.3/GNODEVAE/agent.py +472 -0
- gnodevae-0.0.3/GNODEVAE/env.py +474 -0
- gnodevae-0.0.3/GNODEVAE/mixin.py +418 -0
- gnodevae-0.0.3/GNODEVAE/module.py +939 -0
- gnodevae-0.0.3/GNODEVAE/module_refined.py +561 -0
- gnodevae-0.0.3/GNODEVAE/partition_time.py +185 -0
- gnodevae-0.0.3/GNODEVAE/scGCC.py +184 -0
- gnodevae-0.0.3/GNODEVAE/scGNN.py +146 -0
- gnodevae-0.0.3/GNODEVAE/trainer.py +397 -0
- gnodevae-0.0.3/GNODEVAE/utils.py +460 -0
- gnodevae-0.0.3/GNODEVAE/utils_ODE.py +140 -0
- gnodevae-0.0.3/LICENSE +21 -0
- gnodevae-0.0.3/MANIFEST.in +6 -0
- gnodevae-0.0.3/PKG-INFO +330 -0
- gnodevae-0.0.3/README.md +275 -0
- gnodevae-0.0.3/gnodevae.egg-info/PKG-INFO +330 -0
- gnodevae-0.0.3/gnodevae.egg-info/SOURCES.txt +27 -0
- gnodevae-0.0.3/gnodevae.egg-info/dependency_links.txt +1 -0
- gnodevae-0.0.3/gnodevae.egg-info/requires.txt +10 -0
- gnodevae-0.0.3/gnodevae.egg-info/top_level.txt +1 -0
- gnodevae-0.0.3/pyproject.toml +52 -0
- gnodevae-0.0.3/setup.cfg +4 -0
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
import torch
|
|
2
|
+
import torch.nn as nn
|
|
3
|
+
import torch.nn.functional as F
|
|
4
|
+
from torch.utils.data import Dataset, DataLoader
|
|
5
|
+
import numpy as np
|
|
6
|
+
import scanpy as sc
|
|
7
|
+
from anndata import AnnData
|
|
8
|
+
from copy import deepcopy
|
|
9
|
+
from tqdm import tqdm
|
|
10
|
+
|
|
11
|
+
# --- Core Model Components ---
|
|
12
|
+
|
|
13
|
+
class _MLPEncoder(nn.Module):
|
|
14
|
+
"""Internal MLP encoder for single-cell data."""
|
|
15
|
+
def __init__(self, n_genes, hidden_dim=1024, output_dim=128, dropout=0.0):
|
|
16
|
+
super().__init__()
|
|
17
|
+
self.encoder = nn.Sequential(
|
|
18
|
+
nn.Linear(n_genes, hidden_dim),
|
|
19
|
+
nn.ReLU(),
|
|
20
|
+
nn.Dropout(dropout),
|
|
21
|
+
nn.Linear(hidden_dim, output_dim),
|
|
22
|
+
)
|
|
23
|
+
|
|
24
|
+
def forward(self, x):
|
|
25
|
+
return self.encoder(x)
|
|
26
|
+
|
|
27
|
+
class _MoCo(nn.Module):
|
|
28
|
+
"""Internal Momentum Contrast model."""
|
|
29
|
+
def __init__(self, n_genes, dim=128, queue_size=1024, momentum=0.999, temperature=0.2):
|
|
30
|
+
super().__init__()
|
|
31
|
+
self.dim = dim
|
|
32
|
+
self.queue_size = queue_size
|
|
33
|
+
self.momentum = momentum
|
|
34
|
+
self.temperature = temperature
|
|
35
|
+
|
|
36
|
+
self.encoder_q = _MLPEncoder(n_genes, output_dim=dim)
|
|
37
|
+
self.encoder_k = _MLPEncoder(n_genes, output_dim=dim)
|
|
38
|
+
|
|
39
|
+
for param_q, param_k in zip(self.encoder_q.parameters(), self.encoder_k.parameters()):
|
|
40
|
+
param_k.data.copy_(param_q.data)
|
|
41
|
+
param_k.requires_grad = False
|
|
42
|
+
|
|
43
|
+
self.register_buffer("queue", F.normalize(torch.randn(dim, queue_size), dim=0))
|
|
44
|
+
self.register_buffer("queue_ptr", torch.zeros(1, dtype=torch.long))
|
|
45
|
+
|
|
46
|
+
@torch.no_grad()
|
|
47
|
+
def _momentum_update_key_encoder(self):
|
|
48
|
+
for param_q, param_k in zip(self.encoder_q.parameters(), self.encoder_k.parameters()):
|
|
49
|
+
param_k.data = param_k.data * self.momentum + param_q.data * (1. - self.momentum)
|
|
50
|
+
|
|
51
|
+
@torch.no_grad()
|
|
52
|
+
def _dequeue_and_enqueue(self, keys):
|
|
53
|
+
batch_size = keys.shape[0]
|
|
54
|
+
ptr = int(self.queue_ptr)
|
|
55
|
+
if self.queue_size % batch_size == 0:
|
|
56
|
+
self.queue[:, ptr:ptr + batch_size] = keys.T
|
|
57
|
+
self.queue_ptr[0] = (ptr + batch_size) % self.queue_size
|
|
58
|
+
|
|
59
|
+
def forward(self, query, key):
|
|
60
|
+
q = F.normalize(self.encoder_q(query), dim=1)
|
|
61
|
+
|
|
62
|
+
with torch.no_grad():
|
|
63
|
+
self._momentum_update_key_encoder()
|
|
64
|
+
k = F.normalize(self.encoder_k(key), dim=1)
|
|
65
|
+
|
|
66
|
+
l_pos = torch.einsum('nc,nc->n', [q, k]).unsqueeze(-1)
|
|
67
|
+
l_neg = torch.einsum('nc,ck->nk', [q, self.queue.clone().detach()])
|
|
68
|
+
logits = torch.cat([l_pos, l_neg], dim=1) / self.temperature
|
|
69
|
+
labels = torch.zeros(logits.shape[0], dtype=torch.long, device=q.device)
|
|
70
|
+
self._dequeue_and_enqueue(k)
|
|
71
|
+
return logits, labels
|
|
72
|
+
|
|
73
|
+
@torch.no_grad()
|
|
74
|
+
def inference(self, x):
|
|
75
|
+
self.encoder_k.eval()
|
|
76
|
+
return F.normalize(self.encoder_k(x), dim=1)
|
|
77
|
+
|
|
78
|
+
# --- Dataset Class ---
|
|
79
|
+
|
|
80
|
+
class _CLEAR_Dataset(Dataset):
|
|
81
|
+
"""Internal Dataset for CLEAR with augmentations."""
|
|
82
|
+
def __init__(self, adata, aug_prob=0.5):
|
|
83
|
+
self.data = adata.X.toarray() if hasattr(adata.X, 'toarray') else adata.X
|
|
84
|
+
self.aug_prob = aug_prob
|
|
85
|
+
self.n_cells, self.n_genes = self.data.shape
|
|
86
|
+
|
|
87
|
+
def __len__(self):
|
|
88
|
+
return self.n_cells
|
|
89
|
+
|
|
90
|
+
def __getitem__(self, idx):
|
|
91
|
+
cell_profile = self.data[idx].copy()
|
|
92
|
+
view1 = self._augment(cell_profile)
|
|
93
|
+
view2 = self._augment(cell_profile)
|
|
94
|
+
return torch.from_numpy(view1).float(), torch.from_numpy(view2).float()
|
|
95
|
+
|
|
96
|
+
def _augment(self, profile):
|
|
97
|
+
if np.random.rand() < self.aug_prob:
|
|
98
|
+
# Masking
|
|
99
|
+
mask = np.random.choice([True, False], self.n_genes, p=[0.2, 0.8])
|
|
100
|
+
profile[mask] = 0
|
|
101
|
+
# Gaussian Noise
|
|
102
|
+
mask = np.random.choice([True, False], self.n_genes, p=[0.7, 0.3])
|
|
103
|
+
noise = np.random.normal(0, 0.2, np.sum(mask))
|
|
104
|
+
profile[mask] += noise
|
|
105
|
+
return profile
|
|
106
|
+
|
|
107
|
+
# --- Main CLEAR Class ---
|
|
108
|
+
|
|
109
|
+
class CLEAR:
|
|
110
|
+
def __init__(self, adata: AnnData, use_highly_variable: bool = True, layer: str = None, latent_dim: int = 128):
|
|
111
|
+
self.adata = adata.copy()
|
|
112
|
+
self.use_highly_variable = use_highly_variable
|
|
113
|
+
self.layer = layer
|
|
114
|
+
self.latent_dim = latent_dim
|
|
115
|
+
self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
|
116
|
+
self.model = None
|
|
117
|
+
self._prepare_data()
|
|
118
|
+
|
|
119
|
+
def _prepare_data(self):
|
|
120
|
+
if self.layer and self.layer in self.adata.layers:
|
|
121
|
+
self.adata.X = self.adata.layers[self.layer]
|
|
122
|
+
|
|
123
|
+
if self.use_highly_variable:
|
|
124
|
+
if 'highly_variable' not in self.adata.var.keys():
|
|
125
|
+
sc.pp.highly_variable_genes(self.adata)
|
|
126
|
+
self.adata = self.adata[:, self.adata.var['highly_variable']].copy()
|
|
127
|
+
|
|
128
|
+
def fit(self, epochs=100, lr=5e-3, batch_size=512, verbose=True):
|
|
129
|
+
dataset = _CLEAR_Dataset(self.adata)
|
|
130
|
+
if len(dataset) < batch_size:
|
|
131
|
+
batch_size = len(dataset)
|
|
132
|
+
|
|
133
|
+
train_loader = DataLoader(dataset, batch_size=batch_size, shuffle=True, drop_last=True)
|
|
134
|
+
|
|
135
|
+
self.model = _MoCo(self.adata.n_vars, dim=self.latent_dim, queue_size=batch_size).to(self.device)
|
|
136
|
+
optimizer = torch.optim.SGD(self.model.parameters(), lr, momentum=0.9, weight_decay=1e-6)
|
|
137
|
+
criterion = nn.CrossEntropyLoss()
|
|
138
|
+
|
|
139
|
+
for epoch in range(epochs):
|
|
140
|
+
self.model.train()
|
|
141
|
+
total_loss = 0
|
|
142
|
+
progress_bar = tqdm(train_loader, desc=f"Epoch {epoch+1}/{epochs}", disable=not verbose, miniters=10)
|
|
143
|
+
|
|
144
|
+
for view1, view2 in progress_bar:
|
|
145
|
+
view1, view2 = view1.to(self.device), view2.to(self.device)
|
|
146
|
+
logits, labels = self.model(view1, view2)
|
|
147
|
+
loss = criterion(logits, labels)
|
|
148
|
+
|
|
149
|
+
optimizer.zero_grad()
|
|
150
|
+
loss.backward()
|
|
151
|
+
optimizer.step()
|
|
152
|
+
total_loss += loss.item()
|
|
153
|
+
if verbose:
|
|
154
|
+
progress_bar.set_postfix(loss=loss.item())
|
|
155
|
+
|
|
156
|
+
if verbose:
|
|
157
|
+
print(f"Epoch {epoch+1}/{epochs}, Average Loss: {total_loss / len(train_loader):.4f}")
|
|
158
|
+
return self
|
|
159
|
+
|
|
160
|
+
def get_embedding(self) -> np.ndarray:
|
|
161
|
+
if not self.model:
|
|
162
|
+
raise RuntimeError("Model has not been trained. Please call .fit() first.")
|
|
163
|
+
|
|
164
|
+
self.model.eval()
|
|
165
|
+
full_data = self.adata.X.toarray() if hasattr(self.adata.X, 'toarray') else self.adata.X
|
|
166
|
+
data_tensor = torch.from_numpy(full_data).float().to(self.device)
|
|
167
|
+
|
|
168
|
+
with torch.no_grad():
|
|
169
|
+
embedding = self.model.inference(data_tensor).cpu().numpy()
|
|
170
|
+
|
|
171
|
+
self.adata.obsm['X_clear'] = embedding
|
|
172
|
+
return embedding
|
|
@@ -0,0 +1,413 @@
|
|
|
1
|
+
"""
|
|
2
|
+
GNODEVAE: Graph-based Neural ODE Variational Autoencoder
|
|
3
|
+
|
|
4
|
+
This module implements the core GNODEVAE model that combines:
|
|
5
|
+
- Graph neural networks for encoding cell-cell relationships
|
|
6
|
+
- Neural Ordinary Differential Equations (ODE) for modeling continuous dynamics
|
|
7
|
+
- Variational Autoencoders for learning latent representations
|
|
8
|
+
|
|
9
|
+
The model is designed for single-cell RNA sequencing data analysis, enabling:
|
|
10
|
+
- Clustering of cell types
|
|
11
|
+
- Trajectory inference
|
|
12
|
+
- Temporal dynamics modeling
|
|
13
|
+
"""
|
|
14
|
+
|
|
15
|
+
from .module_refined import BaseGraphNetwork, BaseLinearModel, GraphStructureDecoder, LinearDecoder
|
|
16
|
+
from .utils_ODE import get_step_size, LatentODEfunc
|
|
17
|
+
from torchdiffeq import odeint
|
|
18
|
+
import torch
|
|
19
|
+
import torch.nn as nn
|
|
20
|
+
import torch.nn.functional as F
|
|
21
|
+
from torch.distributions import Normal
|
|
22
|
+
from typing import Optional, Tuple
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
class GraphEncoder_t(BaseGraphNetwork):
|
|
26
|
+
"""
|
|
27
|
+
Graph-based encoder with temporal component for GNODEVAE.
|
|
28
|
+
|
|
29
|
+
This encoder extends the base graph network to include a time variable `t`
|
|
30
|
+
that represents the developmental or temporal state of each cell. The time
|
|
31
|
+
variable is learned during training and used by the ODE solver to model
|
|
32
|
+
continuous trajectories.
|
|
33
|
+
|
|
34
|
+
Parameters
|
|
35
|
+
----------
|
|
36
|
+
input_dim : int
|
|
37
|
+
Input feature dimension (number of genes)
|
|
38
|
+
hidden_dim : int
|
|
39
|
+
Hidden layer dimension
|
|
40
|
+
latent_dim : int
|
|
41
|
+
Latent space dimension for cell embeddings
|
|
42
|
+
conv_layer_type : str, default='GAT'
|
|
43
|
+
Type of graph convolutional layer ('GAT', 'GCN', 'SAGE', etc.)
|
|
44
|
+
hidden_layers : int, default=2
|
|
45
|
+
Number of hidden layers
|
|
46
|
+
dropout : float, default=0.05
|
|
47
|
+
Dropout rate for regularization
|
|
48
|
+
Cheb_k : int, default=1
|
|
49
|
+
Order of Chebyshev polynomials for ChebConv
|
|
50
|
+
alpha : float, default=0.5
|
|
51
|
+
Teleport probability for SSGConv
|
|
52
|
+
"""
|
|
53
|
+
|
|
54
|
+
def __init__(
|
|
55
|
+
self,
|
|
56
|
+
input_dim: int,
|
|
57
|
+
hidden_dim: int,
|
|
58
|
+
latent_dim: int,
|
|
59
|
+
conv_layer_type: str = 'GAT',
|
|
60
|
+
hidden_layers: int = 2,
|
|
61
|
+
dropout: float = 0.05,
|
|
62
|
+
Cheb_k: int = 1,
|
|
63
|
+
alpha: float = 0.5,
|
|
64
|
+
):
|
|
65
|
+
super().__init__(input_dim, hidden_dim, latent_dim, conv_layer_type, hidden_layers, dropout, Cheb_k, alpha)
|
|
66
|
+
# Additional layer to predict pseudo-time for each cell
|
|
67
|
+
self.fc_t = nn.Linear(hidden_dim, 1)
|
|
68
|
+
self.apply(self._init_weights)
|
|
69
|
+
|
|
70
|
+
def _build_output_layer(self, hidden_dim: int, latent_dim: int, Cheb_k: int, alpha: float) -> None:
|
|
71
|
+
"""Build output layers for mean and log variance of latent distribution."""
|
|
72
|
+
self.conv_mean = self._create_conv_layer(hidden_dim, latent_dim, Cheb_k, alpha)
|
|
73
|
+
self.conv_logvar = self._create_conv_layer(hidden_dim, latent_dim, Cheb_k, alpha)
|
|
74
|
+
|
|
75
|
+
def forward(
|
|
76
|
+
self,
|
|
77
|
+
x: torch.Tensor,
|
|
78
|
+
edge_index: torch.Tensor,
|
|
79
|
+
edge_weight: Optional[torch.Tensor] = None,
|
|
80
|
+
use_residual: bool = True,
|
|
81
|
+
) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor]:
|
|
82
|
+
"""
|
|
83
|
+
Forward pass through the graph encoder with temporal component.
|
|
84
|
+
|
|
85
|
+
Parameters
|
|
86
|
+
----------
|
|
87
|
+
x : torch.Tensor
|
|
88
|
+
Node features (gene expression), shape (num_cells, input_dim)
|
|
89
|
+
edge_index : torch.Tensor
|
|
90
|
+
Edge indices for cell-cell graph, shape (2, num_edges)
|
|
91
|
+
edge_weight : torch.Tensor, optional
|
|
92
|
+
Edge weights, shape (num_edges,)
|
|
93
|
+
use_residual : bool, default=True
|
|
94
|
+
Whether to use residual connections
|
|
95
|
+
|
|
96
|
+
Returns
|
|
97
|
+
-------
|
|
98
|
+
q_z : torch.Tensor
|
|
99
|
+
Sampled latent representation, shape (num_cells, latent_dim)
|
|
100
|
+
q_m : torch.Tensor
|
|
101
|
+
Mean of latent distribution, shape (num_cells, latent_dim)
|
|
102
|
+
q_s : torch.Tensor
|
|
103
|
+
Log variance of latent distribution, shape (num_cells, latent_dim)
|
|
104
|
+
t : torch.Tensor
|
|
105
|
+
Pseudo-time for each cell, shape (num_cells, 1), values in [0, 1]
|
|
106
|
+
"""
|
|
107
|
+
residual = None
|
|
108
|
+
|
|
109
|
+
# Pass through graph convolutional layers
|
|
110
|
+
for i, (conv, bn, dropout) in enumerate(zip(self.convs, self.bns, self.dropouts)):
|
|
111
|
+
x = self._process_layer(x, conv, edge_index, edge_weight)
|
|
112
|
+
x = bn(x)
|
|
113
|
+
x = self.relu(x)
|
|
114
|
+
x = dropout(x)
|
|
115
|
+
# Save first layer output for residual connection
|
|
116
|
+
if use_residual and i == 0:
|
|
117
|
+
residual = x
|
|
118
|
+
|
|
119
|
+
# Add residual connection if enabled
|
|
120
|
+
if use_residual and residual is not None:
|
|
121
|
+
x = x + residual
|
|
122
|
+
|
|
123
|
+
# Predict pseudo-time for each cell (sigmoid ensures values in [0, 1])
|
|
124
|
+
t = torch.sigmoid(self.fc_t(x))
|
|
125
|
+
|
|
126
|
+
# Compute parameters of latent distribution
|
|
127
|
+
q_m = self._process_layer(x, self.conv_mean, edge_index, edge_weight)
|
|
128
|
+
q_s = self._process_layer(x, self.conv_logvar, edge_index, edge_weight)
|
|
129
|
+
|
|
130
|
+
# Sample from latent distribution using reparameterization trick
|
|
131
|
+
std = F.softplus(q_s) + 1e-6 # Ensure positive standard deviation
|
|
132
|
+
dist = Normal(q_m, std)
|
|
133
|
+
q_z = dist.rsample() # Differentiable sampling
|
|
134
|
+
|
|
135
|
+
return q_z, q_m, q_s, t
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
class LinearEncoder_t(BaseLinearModel):
|
|
139
|
+
"""
|
|
140
|
+
Linear encoder with temporal component for GNODEVAE.
|
|
141
|
+
|
|
142
|
+
This encoder uses fully connected layers instead of graph convolutions,
|
|
143
|
+
making it suitable for scenarios where graph structure is not available
|
|
144
|
+
or when computational efficiency is critical.
|
|
145
|
+
|
|
146
|
+
Parameters
|
|
147
|
+
----------
|
|
148
|
+
input_dim : int
|
|
149
|
+
Input feature dimension (number of genes)
|
|
150
|
+
hidden_dim : int
|
|
151
|
+
Hidden layer dimension
|
|
152
|
+
latent_dim : int
|
|
153
|
+
Latent space dimension
|
|
154
|
+
hidden_layers : int, default=2
|
|
155
|
+
Number of hidden layers
|
|
156
|
+
dropout : float, default=0.0
|
|
157
|
+
Dropout rate
|
|
158
|
+
"""
|
|
159
|
+
|
|
160
|
+
def __init__(
|
|
161
|
+
self,
|
|
162
|
+
input_dim: int,
|
|
163
|
+
hidden_dim: int,
|
|
164
|
+
latent_dim: int,
|
|
165
|
+
hidden_layers: int = 2,
|
|
166
|
+
dropout: float = 0.0,
|
|
167
|
+
):
|
|
168
|
+
super().__init__(input_dim, hidden_dim, hidden_dim, hidden_layers, dropout)
|
|
169
|
+
# Output layers for variational distribution
|
|
170
|
+
self.mu_layer = nn.Linear(hidden_dim, latent_dim)
|
|
171
|
+
self.logvar_layer = nn.Linear(hidden_dim, latent_dim)
|
|
172
|
+
# Layer for pseudo-time prediction
|
|
173
|
+
self.fc_t = nn.Linear(hidden_dim, 1)
|
|
174
|
+
|
|
175
|
+
# Initialize weights with Xavier initialization
|
|
176
|
+
nn.init.xavier_uniform_(self.mu_layer.weight)
|
|
177
|
+
nn.init.zeros_(self.mu_layer.bias)
|
|
178
|
+
nn.init.xavier_uniform_(self.logvar_layer.weight)
|
|
179
|
+
nn.init.zeros_(self.logvar_layer.bias)
|
|
180
|
+
nn.init.xavier_uniform_(self.fc_t.weight)
|
|
181
|
+
nn.init.zeros_(self.fc_t.bias)
|
|
182
|
+
|
|
183
|
+
def forward(self, x: torch.Tensor) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor]:
|
|
184
|
+
"""
|
|
185
|
+
Forward pass through the linear encoder.
|
|
186
|
+
|
|
187
|
+
Parameters
|
|
188
|
+
----------
|
|
189
|
+
x : torch.Tensor
|
|
190
|
+
Input features, shape (num_cells, input_dim)
|
|
191
|
+
|
|
192
|
+
Returns
|
|
193
|
+
-------
|
|
194
|
+
q_z : torch.Tensor
|
|
195
|
+
Sampled latent representation, shape (num_cells, latent_dim)
|
|
196
|
+
q_m : torch.Tensor
|
|
197
|
+
Mean of latent distribution, shape (num_cells, latent_dim)
|
|
198
|
+
q_s : torch.Tensor
|
|
199
|
+
Log variance of latent distribution, shape (num_cells, latent_dim)
|
|
200
|
+
t : torch.Tensor
|
|
201
|
+
Pseudo-time for each cell, shape (num_cells, 1)
|
|
202
|
+
"""
|
|
203
|
+
# Pass through base network (fully connected layers)
|
|
204
|
+
h = self.network(x)
|
|
205
|
+
|
|
206
|
+
# Predict pseudo-time (values in [0, 1])
|
|
207
|
+
t = torch.sigmoid(self.fc_t(h))
|
|
208
|
+
|
|
209
|
+
# Compute latent distribution parameters
|
|
210
|
+
q_m = self.mu_layer(h)
|
|
211
|
+
q_s = self.logvar_layer(h)
|
|
212
|
+
|
|
213
|
+
# Sample from latent distribution
|
|
214
|
+
std = F.softplus(q_s) + 1e-6
|
|
215
|
+
dist = Normal(q_m, std)
|
|
216
|
+
q_z = dist.rsample()
|
|
217
|
+
|
|
218
|
+
return q_z, q_m, q_s, t
|
|
219
|
+
|
|
220
|
+
|
|
221
|
+
class GODEVAE_r(nn.Module):
|
|
222
|
+
"""
|
|
223
|
+
Graph-based Neural ODE Variational Autoencoder (GNODEVAE).
|
|
224
|
+
|
|
225
|
+
This is the main model class that combines graph neural networks, neural ODEs,
|
|
226
|
+
and variational autoencoders to model single-cell data with temporal dynamics.
|
|
227
|
+
|
|
228
|
+
The model workflow:
|
|
229
|
+
1. Encode: Graph encoder maps gene expression to latent space with pseudo-time
|
|
230
|
+
2. ODE: Neural ODE models continuous trajectories in latent space
|
|
231
|
+
3. Decode: Decoders reconstruct both graph structure and gene expression
|
|
232
|
+
|
|
233
|
+
Parameters
|
|
234
|
+
----------
|
|
235
|
+
input_dim : int
|
|
236
|
+
Input feature dimension (number of genes)
|
|
237
|
+
hidden_dim : int
|
|
238
|
+
Hidden layer dimension
|
|
239
|
+
latent_dim : int
|
|
240
|
+
Latent space dimension
|
|
241
|
+
n_ode_hidden : int
|
|
242
|
+
Number of hidden units in ODE function network
|
|
243
|
+
encoder_type : str, default='graph'
|
|
244
|
+
Type of encoder ('graph' or 'linear')
|
|
245
|
+
graph_type : str, default='GAT'
|
|
246
|
+
Type of graph convolution ('GAT', 'GCN', 'SAGE', etc.)
|
|
247
|
+
structure_decoder_type : str, default='mlp'
|
|
248
|
+
Type of structure decoder ('mlp', 'bilinear', 'inner_product')
|
|
249
|
+
feature_decoder_type : str, default='linear'
|
|
250
|
+
Type of feature decoder ('linear' or 'graph')
|
|
251
|
+
hidden_layers : int, default=2
|
|
252
|
+
Number of hidden layers
|
|
253
|
+
decoder_hidden_dim : int, default=128
|
|
254
|
+
Hidden dimension for structure decoder
|
|
255
|
+
dropout : float, default=0.05
|
|
256
|
+
Dropout rate
|
|
257
|
+
use_residual : bool, default=True
|
|
258
|
+
Whether to use residual connections
|
|
259
|
+
Cheb_k : int, default=1
|
|
260
|
+
Order of Chebyshev polynomials
|
|
261
|
+
alpha : float, default=0.5
|
|
262
|
+
Teleport probability for SSGConv
|
|
263
|
+
threshold : float, default=0
|
|
264
|
+
Threshold for edge probability in structure decoder
|
|
265
|
+
sparse_threshold : int, optional
|
|
266
|
+
Maximum number of edges per node
|
|
267
|
+
|
|
268
|
+
Attributes
|
|
269
|
+
----------
|
|
270
|
+
encoder : GraphEncoder_t or LinearEncoder_t
|
|
271
|
+
Encoder that maps input to latent space with time
|
|
272
|
+
structure_decoder : GraphStructureDecoder
|
|
273
|
+
Decoder for reconstructing graph structure
|
|
274
|
+
feature_decoder : LinearDecoder
|
|
275
|
+
Decoder for reconstructing gene expression
|
|
276
|
+
lode_func : LatentODEfunc
|
|
277
|
+
Neural ODE function for modeling dynamics
|
|
278
|
+
"""
|
|
279
|
+
|
|
280
|
+
def __init__(
|
|
281
|
+
self,
|
|
282
|
+
input_dim: int,
|
|
283
|
+
hidden_dim: int,
|
|
284
|
+
latent_dim: int,
|
|
285
|
+
n_ode_hidden: int,
|
|
286
|
+
encoder_type: str = 'graph',
|
|
287
|
+
graph_type: str = 'GAT',
|
|
288
|
+
structure_decoder_type: str = 'mlp',
|
|
289
|
+
feature_decoder_type: str = 'linear',
|
|
290
|
+
hidden_layers: int = 2,
|
|
291
|
+
decoder_hidden_dim: int = 128,
|
|
292
|
+
dropout: float = 0.05,
|
|
293
|
+
use_residual: bool = True,
|
|
294
|
+
Cheb_k: int = 1,
|
|
295
|
+
alpha: float = 0.5,
|
|
296
|
+
threshold: float = 0,
|
|
297
|
+
sparse_threshold: Optional[int] = None,
|
|
298
|
+
):
|
|
299
|
+
super().__init__()
|
|
300
|
+
|
|
301
|
+
# Validate encoder type
|
|
302
|
+
if encoder_type not in ['linear', 'graph']:
|
|
303
|
+
raise ValueError("encoder_type must be 'linear' or 'graph'")
|
|
304
|
+
|
|
305
|
+
# Initialize encoder based on type
|
|
306
|
+
if encoder_type == 'linear':
|
|
307
|
+
self.encoder = LinearEncoder_t(input_dim, hidden_dim, latent_dim, hidden_layers, dropout)
|
|
308
|
+
else:
|
|
309
|
+
self.encoder = GraphEncoder_t(
|
|
310
|
+
input_dim, hidden_dim, latent_dim, graph_type, hidden_layers, dropout, Cheb_k, alpha
|
|
311
|
+
)
|
|
312
|
+
|
|
313
|
+
# Initialize structure decoder for graph reconstruction
|
|
314
|
+
self.structure_decoder = GraphStructureDecoder(
|
|
315
|
+
structure_decoder=structure_decoder_type,
|
|
316
|
+
latent_dim=latent_dim,
|
|
317
|
+
hidden_dim=decoder_hidden_dim,
|
|
318
|
+
threshold=threshold,
|
|
319
|
+
sparse_threshold=sparse_threshold,
|
|
320
|
+
symmetric=True,
|
|
321
|
+
add_self_loops=False,
|
|
322
|
+
)
|
|
323
|
+
|
|
324
|
+
# Validate and initialize feature decoder
|
|
325
|
+
if feature_decoder_type not in ['linear', 'graph']:
|
|
326
|
+
raise ValueError("feature_decoder_type must be 'linear' or 'graph'")
|
|
327
|
+
|
|
328
|
+
if feature_decoder_type == 'linear':
|
|
329
|
+
self.feature_decoder = LinearDecoder(input_dim, hidden_dim, latent_dim, hidden_layers, dropout)
|
|
330
|
+
else:
|
|
331
|
+
raise NotImplementedError("Graph-based feature decoder is not supported for NODE.")
|
|
332
|
+
|
|
333
|
+
self.encoder_type = encoder_type
|
|
334
|
+
self.feature_decoder_type = feature_decoder_type
|
|
335
|
+
self.use_residual = use_residual
|
|
336
|
+
|
|
337
|
+
# Neural ODE function for modeling continuous dynamics
|
|
338
|
+
self.lode_func = LatentODEfunc(latent_dim, n_ode_hidden)
|
|
339
|
+
|
|
340
|
+
def forward(
|
|
341
|
+
self,
|
|
342
|
+
x: torch.Tensor,
|
|
343
|
+
edge_index: Optional[torch.Tensor] = None,
|
|
344
|
+
edge_weight: Optional[torch.Tensor] = None,
|
|
345
|
+
) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor]:
|
|
346
|
+
"""
|
|
347
|
+
Forward pass through the GNODEVAE model.
|
|
348
|
+
|
|
349
|
+
Parameters
|
|
350
|
+
----------
|
|
351
|
+
x : torch.Tensor
|
|
352
|
+
Input features (gene expression), shape (num_cells, input_dim)
|
|
353
|
+
edge_index : torch.Tensor, optional
|
|
354
|
+
Edge indices for graph encoder, shape (2, num_edges)
|
|
355
|
+
edge_weight : torch.Tensor, optional
|
|
356
|
+
Edge weights, shape (num_edges,)
|
|
357
|
+
|
|
358
|
+
Returns
|
|
359
|
+
-------
|
|
360
|
+
q_z : torch.Tensor
|
|
361
|
+
Initial latent representation, shape (num_cells, latent_dim)
|
|
362
|
+
q_m : torch.Tensor
|
|
363
|
+
Mean of latent distribution, shape (num_cells, latent_dim)
|
|
364
|
+
q_s : torch.Tensor
|
|
365
|
+
Log variance of latent distribution, shape (num_cells, latent_dim)
|
|
366
|
+
pred_a : torch.Tensor
|
|
367
|
+
Predicted adjacency matrix or edge probabilities
|
|
368
|
+
pred_x : torch.Tensor
|
|
369
|
+
Reconstructed features from initial latent, shape (num_cells, input_dim)
|
|
370
|
+
q_z_ode : torch.Tensor
|
|
371
|
+
Latent trajectories from ODE solver, shape (num_timepoints, latent_dim)
|
|
372
|
+
pred_x_ode : torch.Tensor
|
|
373
|
+
Reconstructed features from ODE trajectories
|
|
374
|
+
"""
|
|
375
|
+
# Step 1: Encode input to latent space with pseudo-time
|
|
376
|
+
if self.encoder_type == 'linear':
|
|
377
|
+
q_z, q_m, q_s, t = self.encoder(x)
|
|
378
|
+
else:
|
|
379
|
+
if edge_index is None:
|
|
380
|
+
raise ValueError("edge_index is required for graph encoder")
|
|
381
|
+
q_z, q_m, q_s, t = self.encoder(x, edge_index, edge_weight, self.use_residual)
|
|
382
|
+
|
|
383
|
+
# Step 2: Sort cells by pseudo-time and remove duplicates
|
|
384
|
+
t = t.ravel()
|
|
385
|
+
idx1 = torch.argsort(t)
|
|
386
|
+
t_ordered, q_z_ordered = t[idx1], q_z[idx1]
|
|
387
|
+
|
|
388
|
+
# Remove duplicate time points for ODE solver
|
|
389
|
+
idx2 = (t_ordered[:-1] != t_ordered[1:])
|
|
390
|
+
idx2 = torch.cat((idx2, torch.tensor([True], device=idx2.device)))
|
|
391
|
+
t_ordered, q_z_ordered = t_ordered[idx2], q_z_ordered[idx2]
|
|
392
|
+
|
|
393
|
+
# Store indices for later reordering if needed
|
|
394
|
+
self.idx1, self.idx2 = idx1, idx2
|
|
395
|
+
|
|
396
|
+
# Step 3: Solve ODE to get continuous trajectories in latent space
|
|
397
|
+
# Uses Euler method to integrate from initial state to all time points
|
|
398
|
+
q_z_ode = odeint(
|
|
399
|
+
self.lode_func,
|
|
400
|
+
q_z_ordered[0], # Initial state
|
|
401
|
+
t_ordered, # Time points
|
|
402
|
+
method='euler',
|
|
403
|
+
options=get_step_size(None, t_ordered[0], t_ordered[-1], len(t_ordered)),
|
|
404
|
+
).to(q_z.device)
|
|
405
|
+
|
|
406
|
+
# Step 4: Decode structure (reconstruct cell-cell graph)
|
|
407
|
+
pred_a, pred_edge_index, pred_edge_weight = self.structure_decoder(q_z, edge_index)
|
|
408
|
+
|
|
409
|
+
# Step 5: Decode features (reconstruct gene expression)
|
|
410
|
+
pred_x = self.feature_decoder(q_z) # From initial latent
|
|
411
|
+
pred_x_ode = self.feature_decoder(q_z_ode) # From ODE trajectories
|
|
412
|
+
|
|
413
|
+
return q_z, q_m, q_s, pred_a, pred_x, q_z_ode, pred_x_ode
|