iaode 0.2.4__tar.gz → 0.2.7__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.
- {iaode-0.2.4/iaode.egg-info → iaode-0.2.7}/PKG-INFO +1 -1
- {iaode-0.2.4 → iaode-0.2.7}/iaode/__init__.py +1 -1
- {iaode-0.2.4 → iaode-0.2.7}/iaode/environment.py +28 -16
- {iaode-0.2.4 → iaode-0.2.7}/iaode/model.py +74 -42
- {iaode-0.2.4 → iaode-0.2.7}/iaode/module.py +21 -12
- {iaode-0.2.4 → iaode-0.2.7/iaode.egg-info}/PKG-INFO +1 -1
- {iaode-0.2.4 → iaode-0.2.7}/pyproject.toml +1 -1
- {iaode-0.2.4 → iaode-0.2.7}/LICENSE +0 -0
- {iaode-0.2.4 → iaode-0.2.7}/MANIFEST.in +0 -0
- {iaode-0.2.4 → iaode-0.2.7}/README.md +0 -0
- {iaode-0.2.4 → iaode-0.2.7}/iaode/BEN.py +0 -0
- {iaode-0.2.4 → iaode-0.2.7}/iaode/DRE.py +0 -0
- {iaode-0.2.4 → iaode-0.2.7}/iaode/LSE.py +0 -0
- {iaode-0.2.4 → iaode-0.2.7}/iaode/agent.py +0 -0
- {iaode-0.2.4 → iaode-0.2.7}/iaode/annotation.py +0 -0
- {iaode-0.2.4 → iaode-0.2.7}/iaode/datasets.py +0 -0
- {iaode-0.2.4 → iaode-0.2.7}/iaode/mixin.py +0 -0
- {iaode-0.2.4 → iaode-0.2.7}/iaode/utils.py +0 -0
- {iaode-0.2.4 → iaode-0.2.7}/iaode.egg-info/SOURCES.txt +0 -0
- {iaode-0.2.4 → iaode-0.2.7}/iaode.egg-info/dependency_links.txt +0 -0
- {iaode-0.2.4 → iaode-0.2.7}/iaode.egg-info/requires.txt +0 -0
- {iaode-0.2.4 → iaode-0.2.7}/iaode.egg-info/top_level.txt +0 -0
- {iaode-0.2.4 → iaode-0.2.7}/requirements.txt +0 -0
- {iaode-0.2.4 → iaode-0.2.7}/setup.cfg +0 -0
- {iaode-0.2.4 → iaode-0.2.7}/setup.py +0 -0
- {iaode-0.2.4 → iaode-0.2.7}/tests/test_smoke.py +0 -0
|
@@ -117,11 +117,13 @@ class Env(iaodeModel, envMixin):
|
|
|
117
117
|
def _register_anndata(self, adata, layer: str, latent_dim: int):
|
|
118
118
|
"""Load data and create train/val/test splits"""
|
|
119
119
|
|
|
120
|
-
# Load and log-
|
|
120
|
+
# Load raw counts and create log-transformed version
|
|
121
121
|
if hasattr(adata.layers[layer], 'toarray'):
|
|
122
|
-
self.
|
|
122
|
+
self.X_raw = adata.layers[layer].toarray()
|
|
123
|
+
self.X = np.log1p(self.X_raw)
|
|
123
124
|
else:
|
|
124
|
-
self.
|
|
125
|
+
self.X_raw = adata.layers[layer].copy()
|
|
126
|
+
self.X = np.log1p(self.X_raw)
|
|
125
127
|
|
|
126
128
|
self.n_obs = adata.shape[0]
|
|
127
129
|
self.n_var = adata.shape[1]
|
|
@@ -146,10 +148,14 @@ class Env(iaodeModel, envMixin):
|
|
|
146
148
|
self.val_idx = indices[n_train:n_train + n_val]
|
|
147
149
|
self.test_idx = indices[n_train + n_val:]
|
|
148
150
|
|
|
149
|
-
# Split data
|
|
151
|
+
# Split data (both raw and log-transformed)
|
|
150
152
|
self.X_train = self.X[self.train_idx]
|
|
151
153
|
self.X_val = self.X[self.val_idx]
|
|
152
154
|
self.X_test = self.X[self.test_idx]
|
|
155
|
+
|
|
156
|
+
self.X_raw_train = self.X_raw[self.train_idx]
|
|
157
|
+
self.X_raw_val = self.X_raw[self.val_idx]
|
|
158
|
+
self.X_raw_test = self.X_raw[self.test_idx]
|
|
153
159
|
|
|
154
160
|
# Indexing warnings suppressed – runtime types are ndarray
|
|
155
161
|
self.labels_train = self.labels[self.train_idx] # type: ignore[index]
|
|
@@ -169,15 +175,19 @@ class Env(iaodeModel, envMixin):
|
|
|
169
175
|
def _create_dataloaders(self):
|
|
170
176
|
"""Create PyTorch DataLoaders for training, validation, and testing"""
|
|
171
177
|
|
|
172
|
-
# Convert to tensors
|
|
178
|
+
# Convert to tensors (log-transformed for encoder, raw for loss)
|
|
173
179
|
X_train_tensor = torch.FloatTensor(self.X_train)
|
|
174
180
|
X_val_tensor = torch.FloatTensor(self.X_val)
|
|
175
181
|
X_test_tensor = torch.FloatTensor(self.X_test)
|
|
182
|
+
|
|
183
|
+
X_raw_train_tensor = torch.FloatTensor(self.X_raw_train)
|
|
184
|
+
X_raw_val_tensor = torch.FloatTensor(self.X_raw_val)
|
|
185
|
+
X_raw_test_tensor = torch.FloatTensor(self.X_raw_test)
|
|
176
186
|
|
|
177
|
-
# Create datasets
|
|
178
|
-
train_dataset = TensorDataset(X_train_tensor)
|
|
179
|
-
val_dataset = TensorDataset(X_val_tensor)
|
|
180
|
-
test_dataset = TensorDataset(X_test_tensor)
|
|
187
|
+
# Create datasets with both log-transformed and raw counts
|
|
188
|
+
train_dataset = TensorDataset(X_train_tensor, X_raw_train_tensor)
|
|
189
|
+
val_dataset = TensorDataset(X_val_tensor, X_raw_val_tensor)
|
|
190
|
+
test_dataset = TensorDataset(X_test_tensor, X_raw_test_tensor)
|
|
181
191
|
|
|
182
192
|
# Create dataloaders
|
|
183
193
|
self.train_loader = DataLoader(
|
|
@@ -210,9 +220,10 @@ class Env(iaodeModel, envMixin):
|
|
|
210
220
|
self.train()
|
|
211
221
|
epoch_losses = []
|
|
212
222
|
|
|
213
|
-
for
|
|
214
|
-
|
|
215
|
-
self.
|
|
223
|
+
for batch_data_log, batch_data_raw in self.train_loader:
|
|
224
|
+
batch_data_log = batch_data_log.to(self.device)
|
|
225
|
+
batch_data_raw = batch_data_raw.to(self.device)
|
|
226
|
+
self.update(batch_data_log, batch_data_raw)
|
|
216
227
|
epoch_losses.append(self.loss[-1][0])
|
|
217
228
|
|
|
218
229
|
avg_train_loss = np.mean(epoch_losses)
|
|
@@ -228,15 +239,16 @@ class Env(iaodeModel, envMixin):
|
|
|
228
239
|
all_latents = []
|
|
229
240
|
|
|
230
241
|
with torch.no_grad():
|
|
231
|
-
for
|
|
232
|
-
|
|
242
|
+
for batch_data_log, batch_data_raw in self.val_loader:
|
|
243
|
+
batch_data_log = batch_data_log.to(self.device)
|
|
244
|
+
batch_data_raw = batch_data_raw.to(self.device)
|
|
233
245
|
|
|
234
246
|
# Compute loss
|
|
235
|
-
loss_value = self._compute_loss_only(
|
|
247
|
+
loss_value = self._compute_loss_only(batch_data_log, batch_data_raw)
|
|
236
248
|
val_losses.append(loss_value)
|
|
237
249
|
|
|
238
250
|
# Extract latent representations
|
|
239
|
-
latent = self.take_latent(
|
|
251
|
+
latent = self.take_latent(batch_data_log)
|
|
240
252
|
all_latents.append(latent)
|
|
241
253
|
|
|
242
254
|
# Average validation loss
|
|
@@ -124,9 +124,18 @@ class iaodeModel(nn.Module, scviMixin, dipMixin, betatcMixin, infoMixin):
|
|
|
124
124
|
self.nn_optimizer = optim.Adam(self.nn.parameters(), lr=lr)
|
|
125
125
|
self.loss = []
|
|
126
126
|
|
|
127
|
-
def _compute_loss_only(self,
|
|
128
|
-
"""Compute loss without gradient update (for validation)
|
|
129
|
-
|
|
127
|
+
def _compute_loss_only(self, states_log, states_raw):
|
|
128
|
+
"""Compute loss without gradient update (for validation)
|
|
129
|
+
|
|
130
|
+
Parameters
|
|
131
|
+
----------
|
|
132
|
+
states_log : torch.Tensor
|
|
133
|
+
Log-transformed data for encoder stability
|
|
134
|
+
states_raw : torch.Tensor
|
|
135
|
+
Raw count data for NB/ZINB loss calculation
|
|
136
|
+
"""
|
|
137
|
+
states_log = states_log.to(self.device)
|
|
138
|
+
states_raw = states_raw.to(self.device)
|
|
130
139
|
|
|
131
140
|
with torch.no_grad():
|
|
132
141
|
if self.use_ode:
|
|
@@ -139,7 +148,7 @@ class iaodeModel(nn.Module, scviMixin, dipMixin, betatcMixin, infoMixin):
|
|
|
139
148
|
q_z_ode,
|
|
140
149
|
pred_x_ode, dropout_logits_ode,
|
|
141
150
|
pred_xl_ode, dropout_logitsl_ode,
|
|
142
|
-
) = self.nn(
|
|
151
|
+
) = self.nn(states_log, states_raw) # Pass both log and raw
|
|
143
152
|
|
|
144
153
|
L = x.sum(-1).view(-1, 1)
|
|
145
154
|
pred_x = pred_x * L
|
|
@@ -166,7 +175,7 @@ class iaodeModel(nn.Module, scviMixin, dipMixin, betatcMixin, infoMixin):
|
|
|
166
175
|
q_z_ode,
|
|
167
176
|
pred_x_ode,
|
|
168
177
|
pred_xl_ode,
|
|
169
|
-
) = self.nn(
|
|
178
|
+
) = self.nn(states_log, states_raw) # Pass both log and raw
|
|
170
179
|
|
|
171
180
|
if self.loss_mode == "nb":
|
|
172
181
|
L = x.sum(-1).view(-1, 1)
|
|
@@ -185,45 +194,47 @@ class iaodeModel(nn.Module, scviMixin, dipMixin, betatcMixin, infoMixin):
|
|
|
185
194
|
else:
|
|
186
195
|
irecon_loss = torch.zeros(1).to(self.device)
|
|
187
196
|
else:
|
|
188
|
-
|
|
189
|
-
recon_loss
|
|
190
|
-
|
|
191
|
-
irecon_loss
|
|
197
|
+
# MSE mode - use log-transformed for consistency
|
|
198
|
+
recon_loss = F.mse_loss(states_log, pred_x, reduction="none").sum(-1).mean()
|
|
199
|
+
recon_loss += F.mse_loss(states_log, pred_x_ode, reduction="none").sum(-1).mean()
|
|
200
|
+
irecon_loss = F.mse_loss(states_log, pred_xl, reduction="none").sum(-1).mean()
|
|
201
|
+
irecon_loss += F.mse_loss(states_log, pred_xl_ode, reduction="none").sum(-1).mean()
|
|
192
202
|
|
|
193
203
|
qz_div = F.mse_loss(q_z, q_z_ode, reduction="none").sum(-1).mean()
|
|
194
204
|
|
|
195
205
|
else:
|
|
196
206
|
if self.loss_mode == "zinb":
|
|
197
|
-
q_z, q_m, q_s, pred_x, dropout_logits, le, pred_xl, dropout_logitsl = self.nn(
|
|
207
|
+
q_z, q_m, q_s, x, pred_x, dropout_logits, le, pred_xl, dropout_logitsl = self.nn(states_log, states_raw)
|
|
198
208
|
|
|
199
|
-
L =
|
|
209
|
+
L = x.sum(-1).view(-1, 1) # Use raw counts for library size
|
|
200
210
|
pred_x = pred_x * L
|
|
201
211
|
disp = torch.exp(self.nn.decoder.disp)
|
|
202
|
-
recon_loss = -self._log_zinb(
|
|
212
|
+
recon_loss = -self._log_zinb(x, pred_x, disp, dropout_logits).sum(-1).mean() # Use raw counts
|
|
203
213
|
|
|
204
214
|
if self.irecon:
|
|
205
215
|
pred_xl = pred_xl * L
|
|
206
|
-
irecon_loss = -self.irecon * self._log_zinb(x, pred_xl, disp, dropout_logitsl).sum(-1).mean()
|
|
216
|
+
irecon_loss = -self.irecon * self._log_zinb(x, pred_xl, disp, dropout_logitsl).sum(-1).mean() # Use raw counts
|
|
207
217
|
else:
|
|
208
218
|
irecon_loss = torch.zeros(1).to(self.device)
|
|
209
219
|
|
|
210
220
|
else:
|
|
211
|
-
q_z, q_m, q_s, pred_x, le, pred_xl = self.nn(
|
|
221
|
+
q_z, q_m, q_s, x, pred_x, le, pred_xl = self.nn(states_log, states_raw)
|
|
212
222
|
|
|
213
223
|
if self.loss_mode == "nb":
|
|
214
|
-
L =
|
|
224
|
+
L = x.sum(-1).view(-1, 1) # Use raw counts for library size
|
|
215
225
|
pred_x = pred_x * L
|
|
216
226
|
disp = torch.exp(self.nn.decoder.disp)
|
|
217
|
-
recon_loss = -self._log_nb(
|
|
227
|
+
recon_loss = -self._log_nb(x, pred_x, disp).sum(-1).mean() # Use raw counts
|
|
218
228
|
|
|
219
229
|
if self.irecon:
|
|
220
230
|
pred_xl = pred_xl * L
|
|
221
|
-
irecon_loss = -self.irecon * self._log_nb(
|
|
231
|
+
irecon_loss = -self.irecon * self._log_nb(x, pred_xl, disp).sum(-1).mean() # Use raw counts
|
|
222
232
|
else:
|
|
223
233
|
irecon_loss = torch.zeros(1).to(self.device)
|
|
224
234
|
else:
|
|
225
|
-
|
|
226
|
-
|
|
235
|
+
# MSE mode can use either log or raw, using log for consistency with original
|
|
236
|
+
recon_loss = F.mse_loss(states_log, pred_x, reduction="none").sum(-1).mean()
|
|
237
|
+
irecon_loss = F.mse_loss(states_log, pred_xl, reduction="none").sum(-1).mean()
|
|
227
238
|
|
|
228
239
|
qz_div = torch.zeros(1).to(self.device)
|
|
229
240
|
|
|
@@ -284,10 +295,11 @@ class iaodeModel(nn.Module, scviMixin, dipMixin, betatcMixin, infoMixin):
|
|
|
284
295
|
le_ode = self.nn.latent_encoder(q_z_ode)
|
|
285
296
|
return (self.vae_reg * le + self.ode_reg * le_ode).cpu().numpy()
|
|
286
297
|
else:
|
|
298
|
+
# For inference, states is log-transformed, pass as both args
|
|
287
299
|
if self.loss_mode == "zinb":
|
|
288
|
-
q_z, q_m, q_s, pred_x, dropout_logits, le, pred_xl, dropout_logitsl = self.nn(states)
|
|
300
|
+
q_z, q_m, q_s, x, pred_x, dropout_logits, le, pred_xl, dropout_logitsl = self.nn(states, states)
|
|
289
301
|
else:
|
|
290
|
-
q_z, q_m, q_s, pred_x, le, pred_xl = self.nn(states)
|
|
302
|
+
q_z, q_m, q_s, x, pred_x, le, pred_xl = self.nn(states, states)
|
|
291
303
|
return le.cpu().numpy()
|
|
292
304
|
|
|
293
305
|
@torch.no_grad()
|
|
@@ -343,12 +355,30 @@ class iaodeModel(nn.Module, scviMixin, dipMixin, betatcMixin, infoMixin):
|
|
|
343
355
|
|
|
344
356
|
return sparse_trans
|
|
345
357
|
|
|
346
|
-
def update(self,
|
|
347
|
-
"""Perform one gradient update step
|
|
348
|
-
|
|
349
|
-
|
|
358
|
+
def update(self, states_log, states_raw=None):
|
|
359
|
+
"""Perform one gradient update step
|
|
360
|
+
|
|
361
|
+
Parameters
|
|
362
|
+
----------
|
|
363
|
+
states_log : torch.Tensor
|
|
364
|
+
Log-transformed data for encoder stability
|
|
365
|
+
states_raw : torch.Tensor, optional
|
|
366
|
+
Raw count data for NB/ZINB loss calculation.
|
|
367
|
+
If None, uses states_log (for backward compatibility with MSE mode)
|
|
368
|
+
"""
|
|
369
|
+
if not isinstance(states_log, torch.Tensor):
|
|
370
|
+
states_log = torch.tensor(states_log, dtype=torch.float).to(self.device)
|
|
350
371
|
else:
|
|
351
|
-
|
|
372
|
+
states_log = states_log.to(self.device)
|
|
373
|
+
|
|
374
|
+
# For backward compatibility, use log data if raw not provided
|
|
375
|
+
if states_raw is None:
|
|
376
|
+
states_raw = states_log
|
|
377
|
+
else:
|
|
378
|
+
if not isinstance(states_raw, torch.Tensor):
|
|
379
|
+
states_raw = torch.tensor(states_raw, dtype=torch.float).to(self.device)
|
|
380
|
+
else:
|
|
381
|
+
states_raw = states_raw.to(self.device)
|
|
352
382
|
|
|
353
383
|
# Forward pass
|
|
354
384
|
if self.use_ode:
|
|
@@ -361,7 +391,7 @@ class iaodeModel(nn.Module, scviMixin, dipMixin, betatcMixin, infoMixin):
|
|
|
361
391
|
q_z_ode,
|
|
362
392
|
pred_x_ode, dropout_logits_ode,
|
|
363
393
|
pred_xl_ode, dropout_logitsl_ode,
|
|
364
|
-
) = self.nn(
|
|
394
|
+
) = self.nn(states_log, states_raw) # Pass both log and raw
|
|
365
395
|
|
|
366
396
|
qz_div = F.mse_loss(q_z, q_z_ode, reduction="none").sum(-1).mean()
|
|
367
397
|
|
|
@@ -389,7 +419,7 @@ class iaodeModel(nn.Module, scviMixin, dipMixin, betatcMixin, infoMixin):
|
|
|
389
419
|
q_z_ode,
|
|
390
420
|
pred_x_ode,
|
|
391
421
|
pred_xl_ode,
|
|
392
|
-
) = self.nn(
|
|
422
|
+
) = self.nn(states_log, states_raw) # Pass both log and raw
|
|
393
423
|
|
|
394
424
|
qz_div = F.mse_loss(q_z, q_z_ode, reduction="none").sum(-1).mean()
|
|
395
425
|
|
|
@@ -409,43 +439,45 @@ class iaodeModel(nn.Module, scviMixin, dipMixin, betatcMixin, infoMixin):
|
|
|
409
439
|
else:
|
|
410
440
|
irecon_loss = torch.zeros(1).to(self.device)
|
|
411
441
|
else:
|
|
412
|
-
|
|
413
|
-
recon_loss
|
|
414
|
-
|
|
415
|
-
irecon_loss
|
|
442
|
+
# MSE mode - use log-transformed for consistency
|
|
443
|
+
recon_loss = F.mse_loss(states_log, pred_x, reduction="none").sum(-1).mean()
|
|
444
|
+
recon_loss += F.mse_loss(states_log, pred_x_ode, reduction="none").sum(-1).mean()
|
|
445
|
+
irecon_loss = F.mse_loss(states_log, pred_xl, reduction="none").sum(-1).mean()
|
|
446
|
+
irecon_loss += F.mse_loss(states_log, pred_xl_ode, reduction="none").sum(-1).mean()
|
|
416
447
|
|
|
417
448
|
else:
|
|
418
449
|
if self.loss_mode == "zinb":
|
|
419
|
-
q_z, q_m, q_s, pred_x, dropout_logits, le, pred_xl, dropout_logitsl = self.nn(
|
|
450
|
+
q_z, q_m, q_s, x, pred_x, dropout_logits, le, pred_xl, dropout_logitsl = self.nn(states_log, states_raw)
|
|
420
451
|
|
|
421
|
-
L =
|
|
452
|
+
L = x.sum(-1).view(-1, 1) # Use raw counts for library size
|
|
422
453
|
pred_x = pred_x * L
|
|
423
454
|
disp = torch.exp(self.nn.decoder.disp)
|
|
424
|
-
recon_loss = -self._log_zinb(
|
|
455
|
+
recon_loss = -self._log_zinb(x, pred_x, disp, dropout_logits).sum(-1).mean() # Use raw counts
|
|
425
456
|
|
|
426
457
|
if self.irecon:
|
|
427
458
|
pred_xl = pred_xl * L
|
|
428
|
-
irecon_loss = -self.irecon * self._log_zinb(
|
|
459
|
+
irecon_loss = -self.irecon * self._log_zinb(x, pred_xl, disp, dropout_logitsl).sum(-1).mean() # Use raw counts
|
|
429
460
|
else:
|
|
430
461
|
irecon_loss = torch.zeros(1).to(self.device)
|
|
431
462
|
|
|
432
463
|
else:
|
|
433
|
-
q_z, q_m, q_s, pred_x, le, pred_xl = self.nn(
|
|
464
|
+
q_z, q_m, q_s, x, pred_x, le, pred_xl = self.nn(states_log, states_raw)
|
|
434
465
|
|
|
435
466
|
if self.loss_mode == "nb":
|
|
436
|
-
L =
|
|
467
|
+
L = x.sum(-1).view(-1, 1) # Use raw counts for library size
|
|
437
468
|
pred_x = pred_x * L
|
|
438
469
|
disp = torch.exp(self.nn.decoder.disp)
|
|
439
|
-
recon_loss = -self._log_nb(
|
|
470
|
+
recon_loss = -self._log_nb(x, pred_x, disp).sum(-1).mean() # Use raw counts
|
|
440
471
|
|
|
441
472
|
if self.irecon:
|
|
442
473
|
pred_xl = pred_xl * L
|
|
443
|
-
irecon_loss = -self.irecon * self._log_nb(
|
|
474
|
+
irecon_loss = -self.irecon * self._log_nb(x, pred_xl, disp).sum(-1).mean() # Use raw counts
|
|
444
475
|
else:
|
|
445
476
|
irecon_loss = torch.zeros(1).to(self.device)
|
|
446
477
|
else:
|
|
447
|
-
|
|
448
|
-
|
|
478
|
+
# MSE mode - use log-transformed for consistency
|
|
479
|
+
recon_loss = F.mse_loss(states_log, pred_x, reduction="none").sum(-1).mean()
|
|
480
|
+
irecon_loss = F.mse_loss(states_log, pred_xl, reduction="none").sum(-1).mean()
|
|
449
481
|
|
|
450
482
|
qz_div = torch.zeros(1).to(self.device)
|
|
451
483
|
|
|
@@ -457,14 +457,17 @@ class iVAE(nn.Module, NODEMixin):
|
|
|
457
457
|
self.latent_encoder = nn.Linear(action_dim, i_dim).to(device)
|
|
458
458
|
self.latent_decoder = nn.Linear(i_dim, action_dim).to(device)
|
|
459
459
|
|
|
460
|
-
def forward(self,
|
|
460
|
+
def forward(self, x_log: torch.Tensor, x_raw: torch.Tensor = None) -> Tuple[torch.Tensor, ...]:
|
|
461
461
|
"""
|
|
462
462
|
Forward pass through VAE.
|
|
463
463
|
|
|
464
464
|
Parameters
|
|
465
465
|
----------
|
|
466
|
-
|
|
467
|
-
|
|
466
|
+
x_log : torch.Tensor
|
|
467
|
+
Log-transformed input tensor (batch_size, state_dim) for encoder stability
|
|
468
|
+
x_raw : torch.Tensor, optional
|
|
469
|
+
Raw count tensor (batch_size, state_dim) for NB/ZINB loss calculation.
|
|
470
|
+
If None, uses x_log (for MSE mode or backward compatibility)
|
|
468
471
|
|
|
469
472
|
Returns
|
|
470
473
|
-------
|
|
@@ -475,6 +478,8 @@ class iVAE(nn.Module, NODEMixin):
|
|
|
475
478
|
Latent mean
|
|
476
479
|
q_s : torch.Tensor
|
|
477
480
|
Latent log-variance
|
|
481
|
+
x_raw : torch.Tensor
|
|
482
|
+
Raw counts for loss calculation
|
|
478
483
|
pred_x : torch.Tensor
|
|
479
484
|
Reconstructed input (direct path)
|
|
480
485
|
le : torch.Tensor
|
|
@@ -492,9 +497,13 @@ class iVAE(nn.Module, NODEMixin):
|
|
|
492
497
|
dropout_logits : torch.Tensor
|
|
493
498
|
Zero-inflation parameters
|
|
494
499
|
"""
|
|
495
|
-
#
|
|
500
|
+
# Use x_log for backward compatibility if x_raw not provided
|
|
501
|
+
if x_raw is None:
|
|
502
|
+
x_raw = x_log
|
|
503
|
+
|
|
504
|
+
# Encode using log-transformed data for stability
|
|
496
505
|
if self.encoder.use_ode:
|
|
497
|
-
q_z, q_m, q_s, t = self.encoder(
|
|
506
|
+
q_z, q_m, q_s, t = self.encoder(x_log)
|
|
498
507
|
|
|
499
508
|
# Sort by pseudotime
|
|
500
509
|
idxs = torch.argsort(t)
|
|
@@ -502,7 +511,7 @@ class iVAE(nn.Module, NODEMixin):
|
|
|
502
511
|
q_z = q_z[idxs]
|
|
503
512
|
q_m = q_m[idxs]
|
|
504
513
|
q_s = q_s[idxs]
|
|
505
|
-
|
|
514
|
+
x_raw = x_raw[idxs] # Sort raw counts to match
|
|
506
515
|
|
|
507
516
|
# Remove duplicate time points
|
|
508
517
|
unique_mask = torch.ones_like(t, dtype=torch.bool)
|
|
@@ -512,7 +521,7 @@ class iVAE(nn.Module, NODEMixin):
|
|
|
512
521
|
q_z = q_z[unique_mask]
|
|
513
522
|
q_m = q_m[unique_mask]
|
|
514
523
|
q_s = q_s[unique_mask]
|
|
515
|
-
|
|
524
|
+
x_raw = x_raw[unique_mask] # Apply mask to raw counts
|
|
516
525
|
|
|
517
526
|
# Solve ODE from initial state
|
|
518
527
|
z0 = q_z[0]
|
|
@@ -531,7 +540,7 @@ class iVAE(nn.Module, NODEMixin):
|
|
|
531
540
|
pred_x_ode, dropout_logits_ode = self.decoder(q_z_ode)
|
|
532
541
|
pred_xl_ode, dropout_logitsl_ode = self.decoder(ld_ode)
|
|
533
542
|
return (
|
|
534
|
-
q_z, q_m, q_s,
|
|
543
|
+
q_z, q_m, q_s, x_raw, # Return raw counts for loss
|
|
535
544
|
pred_x, dropout_logits,
|
|
536
545
|
le, le_ode,
|
|
537
546
|
pred_xl, dropout_logitsl,
|
|
@@ -545,7 +554,7 @@ class iVAE(nn.Module, NODEMixin):
|
|
|
545
554
|
pred_x_ode = self.decoder(q_z_ode)
|
|
546
555
|
pred_xl_ode = self.decoder(ld_ode)
|
|
547
556
|
return (
|
|
548
|
-
q_z, q_m, q_s,
|
|
557
|
+
q_z, q_m, q_s, x_raw, # Return raw counts for loss
|
|
549
558
|
pred_x,
|
|
550
559
|
le, le_ode,
|
|
551
560
|
pred_xl,
|
|
@@ -555,7 +564,7 @@ class iVAE(nn.Module, NODEMixin):
|
|
|
555
564
|
)
|
|
556
565
|
|
|
557
566
|
else:
|
|
558
|
-
q_z, q_m, q_s = self.encoder(
|
|
567
|
+
q_z, q_m, q_s = self.encoder(x_log) # Encode log-transformed
|
|
559
568
|
|
|
560
569
|
# Information bottleneck
|
|
561
570
|
le = self.latent_encoder(q_z)
|
|
@@ -566,7 +575,7 @@ class iVAE(nn.Module, NODEMixin):
|
|
|
566
575
|
pred_x, dropout_logits = self.decoder(q_z)
|
|
567
576
|
pred_xl, dropout_logitsl = self.decoder(ld)
|
|
568
577
|
return (
|
|
569
|
-
q_z, q_m, q_s,
|
|
578
|
+
q_z, q_m, q_s, x_raw, # Return raw counts for loss
|
|
570
579
|
pred_x, dropout_logits,
|
|
571
580
|
le,
|
|
572
581
|
pred_xl, dropout_logitsl,
|
|
@@ -574,4 +583,4 @@ class iVAE(nn.Module, NODEMixin):
|
|
|
574
583
|
else:
|
|
575
584
|
pred_x = self.decoder(q_z)
|
|
576
585
|
pred_xl = self.decoder(ld)
|
|
577
|
-
return (q_z, q_m, q_s, pred_x, le, pred_xl)
|
|
586
|
+
return (q_z, q_m, q_s, x_raw, pred_x, le, pred_xl) # Return raw counts for loss
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|