gnodevae 0.0.3__py3-none-any.whl

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.
@@ -0,0 +1,385 @@
1
+ from .GODEVAE_env import GNODEVAE_Env_r, GNODEVAE_Env_Subgraph
2
+ from anndata import AnnData
3
+ import numpy as np
4
+ import pandas as pd
5
+ import torch
6
+ import tqdm
7
+ from typing import Optional
8
+ import time
9
+ import psutil
10
+ from torch_geometric.data import Data
11
+
12
+
13
+ class GNODEVAE_agent_base:
14
+ """Base class for GNODEVAE agents, providing common fitting and utility methods."""
15
+
16
+ def __init__(self, *, scale1: float = 0.5, scale2: float = 0.5,
17
+ w_recon: float = 1.0,
18
+ w_kl: float = 1.0,
19
+ w_adj: float = 1.0,
20
+ w_recon_ode: float = 1.0,
21
+ w_z_div: float = 1.0,
22
+ latent_type: str = 'q_m',
23
+ **kwargs):
24
+ super().__init__(
25
+ w_recon=w_recon,
26
+ w_kl=w_kl,
27
+ w_adj=w_adj,
28
+ w_recon_ode=w_recon_ode,
29
+ w_z_div=w_z_div,
30
+ latent_type=latent_type,
31
+ **kwargs
32
+ )
33
+ self.scale1 = scale1
34
+ self.scale2 = scale2
35
+
36
+ def fit(self, epochs: int = 300, update_steps: int = 10, silent: bool = False):
37
+ """
38
+ Fits the model for a specified number of epochs.
39
+ """
40
+ self.resource = []
41
+ start_time = time.time()
42
+
43
+ try:
44
+ with tqdm.tqdm(total=epochs, desc='Fitting', ncols=200, disable=silent, miniters=update_steps) as pbar:
45
+ for i in range(epochs):
46
+ step_start_time = time.time()
47
+ self.step_ode()
48
+ step_end_time = time.time()
49
+
50
+ # Resource tracking
51
+ process = psutil.Process()
52
+ cpu_mem = process.memory_info().rss / (1024 ** 2)
53
+ gpu_mem = torch.cuda.memory_allocated(self.device) / (1024 ** 2) if torch.cuda.is_available() else 0.0
54
+ self.resource.append((step_end_time - step_start_time, cpu_mem, gpu_mem))
55
+
56
+ if (i + 1) % update_steps == 0:
57
+ recent_losses = self.loss[-update_steps:]
58
+ recent_scores = self.mix_score[-update_steps:]
59
+ recent_resources = self.resource[-update_steps:]
60
+
61
+ loss = np.mean([sum(loss_step) for loss_step in recent_losses])
62
+ ari, nmi, asw, ch, db, pc = np.mean(recent_scores, axis=0)
63
+ st, cm, gm = np.mean(recent_resources, axis=0)
64
+
65
+ pbar.set_postfix({
66
+ 'Loss': f'{loss:.2f}', 'ARI': f'{ari:.2f}', 'NMI': f'{nmi:.2f}',
67
+ 'ASW': f'{asw:.2f}', 'C_H': f'{ch:.2f}', 'D_B': f'{db:.2f}',
68
+ 'P_C': f'{pc:.2f}', 'Step Time': f'{st:.2f}s',
69
+ 'CPU Mem': f'{cm:.0f}MB', 'GPU Mem': f'{gm:.0f}MB'
70
+ }, refresh=False)
71
+
72
+ pbar.update(1)
73
+
74
+ except Exception as e:
75
+ print(f"An error occurred during fitting: {e}")
76
+ raise e
77
+
78
+ end_time = time.time()
79
+ self.time_all = end_time - start_time
80
+ return self
81
+
82
+ def partition_time(self) -> pd.DataFrame:
83
+ """
84
+ Calculates the pseudo-time for each cell based on the encoder's output.
85
+ """
86
+ idx_ls = []
87
+ t_ls = []
88
+ c_ls = []
89
+ self.godevae.eval()
90
+ with torch.no_grad():
91
+ for i, cd in enumerate(self.cdata):
92
+ states = cd.x
93
+ edge_index = cd.edge_index
94
+ edge_weight = cd.edge_attr
95
+ _, _, _, t = self.godevae.encoder(states, edge_index, edge_weight)
96
+
97
+ indices = cd.y.cpu().numpy()
98
+ times = t.cpu().numpy()
99
+
100
+ idx_ls.append(indices)
101
+ t_ls.append(times)
102
+ c_ls.append(np.full_like(indices, str(i)))
103
+
104
+ all_indices = np.concatenate(idx_ls)
105
+ all_times = np.concatenate(t_ls)
106
+ all_partitions = np.concatenate(c_ls)
107
+
108
+ df = pd.DataFrame({'c': all_partitions, 't': all_times.flatten()}, index=all_indices)
109
+ df = df.sort_index()
110
+ return df
111
+
112
+
113
+ class GNODEVAE_agent_subgraph(GNODEVAE_agent_base, GNODEVAE_Env_Subgraph):
114
+ """
115
+ GNODEVAE agent that uses subgraph sampling.
116
+ """
117
+ def __init__(
118
+ self,
119
+ adata: AnnData,
120
+ layer: str = 'counts',
121
+ n_var: Optional[int] = None,
122
+ tech: str = 'PCA',
123
+ n_neighbors: int = 15,
124
+ batch_tech: Optional[str] = None,
125
+ all_feat: bool = False,
126
+ hidden_dim: int = 128,
127
+ latent_dim: int = 10,
128
+ ode_hidden_dim: int = 25,
129
+ encoder_type: str = 'graph',
130
+ graph_type: str = 'GAT',
131
+ structure_decoder_type: str = 'mlp',
132
+ feature_decoder_type: str = 'linear',
133
+ hidden_layers: int = 2,
134
+ decoder_hidden_dim: int = 128,
135
+ dropout: float = 0.05,
136
+ use_residual: bool = True,
137
+ Cheb_k: int = 1,
138
+ alpha: float = 0.5,
139
+ threshold: float = 0,
140
+ sparse_threshold: Optional[int] = None,
141
+ lr: float = 1e-4,
142
+ beta: float = 1.0,
143
+ graph: float = 1.0,
144
+ w_recon: float = 1.0,
145
+ w_kl: float = 1.0,
146
+ w_adj: float = 1.0,
147
+ w_recon_ode: float = 1.0,
148
+ w_z_div: float = 1.0,
149
+ device: torch.device = torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu'),
150
+ scale1: float = .5,
151
+ scale2: float = .5,
152
+ latent_type: str = 'q_m',
153
+ # Subgraph-specific parameters
154
+ subgraph_size: int = 512,
155
+ num_subgraphs_per_epoch: int = 10,
156
+ sampling_method: str = 'random',
157
+ **kwargs,
158
+ ):
159
+ super().__init__(
160
+ adata=adata,
161
+ layer=layer,
162
+ n_var=n_var,
163
+ tech=tech,
164
+ n_neighbors=n_neighbors,
165
+ batch_tech=batch_tech,
166
+ all_feat=all_feat,
167
+ hidden_dim=hidden_dim,
168
+ latent_dim=latent_dim,
169
+ ode_hidden_dim=ode_hidden_dim,
170
+ encoder_type=encoder_type,
171
+ graph_type=graph_type,
172
+ structure_decoder_type=structure_decoder_type,
173
+ feature_decoder_type=feature_decoder_type,
174
+ hidden_layers=hidden_layers,
175
+ decoder_hidden_dim=decoder_hidden_dim,
176
+ dropout=dropout,
177
+ use_residual=use_residual,
178
+ Cheb_k=Cheb_k,
179
+ alpha=alpha,
180
+ threshold=threshold,
181
+ sparse_threshold=sparse_threshold,
182
+ lr=lr,
183
+ beta=beta,
184
+ graph=graph,
185
+ w_recon=w_recon,
186
+ w_kl=w_kl,
187
+ w_adj=w_adj,
188
+ w_recon_ode=w_recon_ode,
189
+ w_z_div=w_z_div,
190
+ device=device,
191
+ scale1=scale1,
192
+ scale2=scale2,
193
+ latent_type=latent_type,
194
+ subgraph_size=subgraph_size,
195
+ num_subgraphs_per_epoch=num_subgraphs_per_epoch,
196
+ sampling_method=sampling_method,
197
+ **kwargs
198
+ )
199
+
200
+ def _get_latent_representation(self, latent_name: str) -> np.ndarray:
201
+ """Helper to get latent representations for the full graph."""
202
+ self.godevae.eval()
203
+ with torch.no_grad():
204
+ # Create a Data object for the full graph
205
+ full_graph_data = Data(
206
+ x=torch.tensor(self.X, dtype=torch.float, device=self.device),
207
+ edge_index=torch.tensor(self.edge_index, dtype=torch.long, device=self.device),
208
+ edge_attr=torch.tensor(self.edge_weight, dtype=torch.float, device=self.device),
209
+ y=torch.tensor(self.y, dtype=torch.long, device=self.device)
210
+ )
211
+
212
+ # Get the appropriate latent representation method
213
+ take_latent_method = getattr(self, f"take_{latent_name}")
214
+
215
+ # Perform a forward pass on the full graph
216
+ latent = take_latent_method(full_graph_data)
217
+
218
+ return latent
219
+
220
+ def get_latent(self) -> np.ndarray:
221
+ """Gets the base latent representation for all cells."""
222
+ return self._get_latent_representation("latent")
223
+
224
+ def get_odelatent(self) -> np.ndarray:
225
+ """Gets the ODE-evolved latent representation for all cells."""
226
+ return self._get_latent_representation("odelatent")
227
+
228
+ def get_mix_latent(self) -> np.ndarray:
229
+ """Gets the mixed latent representation for all cells."""
230
+ latent = self.get_latent()
231
+ odelatent = self.get_odelatent()
232
+ mix_latent = self.scale1 * latent + self.scale2 * odelatent
233
+ return mix_latent
234
+
235
+ def score_final(self) -> None:
236
+ """Calculates and stores the final score on the base latent space."""
237
+ latent = self.get_latent()
238
+ self.final_score = self._calc_score(latent)
239
+
240
+ def score_odefinal(self) -> None:
241
+ """Calculates and stores the final score on the ODE-evolved latent space."""
242
+ latent = self.get_odelatent()
243
+ self.ode_final_score = self._calc_score(latent)
244
+
245
+ def score_mixfinal(self) -> None:
246
+ """Calculates and stores the final score on the mixed latent space."""
247
+ latent = self.get_mix_latent()
248
+ self.mix_final_score = self._calc_score(latent)
249
+
250
+
251
+ class GNODEVAE_agent_r(GNODEVAE_agent_base, GNODEVAE_Env_r):
252
+ """
253
+ GNODEVAE agent that uses graph partitioning.
254
+ """
255
+ def __init__(
256
+ self,
257
+ adata: AnnData,
258
+ layer: str = 'counts',
259
+ n_var: Optional[int] = None,
260
+ tech: str = 'PCA',
261
+ n_neighbors: int = 15,
262
+ batch_tech: Optional[str] = None,
263
+ all_feat: bool = False,
264
+ hidden_dim: int = 128,
265
+ latent_dim: int = 10,
266
+ ode_hidden_dim: int = 25,
267
+ encoder_type: str = 'graph',
268
+ graph_type: str = 'GAT',
269
+ structure_decoder_type: str = 'mlp',
270
+ feature_decoder_type: str = 'linear',
271
+ hidden_layers: int = 2,
272
+ decoder_hidden_dim: int = 128,
273
+ dropout: float = 0.05,
274
+ use_residual: bool = True,
275
+ Cheb_k: int = 1,
276
+ alpha: float = 0.5,
277
+ threshold: float = 0,
278
+ sparse_threshold: Optional[int] = None,
279
+ lr: float = 1e-4,
280
+ beta: float = 1.0,
281
+ graph: float = 1.0,
282
+ w_recon: float = 1.0,
283
+ w_kl: float = 1.0,
284
+ w_adj: float = 1.0,
285
+ w_recon_ode: float = 1.0,
286
+ w_z_div: float = 1.0,
287
+ scale1: float = .5,
288
+ scale2: float = .5,
289
+ latent_type: str = 'q_m',
290
+ device: torch.device = torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu'),
291
+ # Partition-specific parameters
292
+ num_parts: int = 10,
293
+ **kwargs,
294
+ ):
295
+ super().__init__(
296
+ adata=adata,
297
+ layer=layer,
298
+ n_var=n_var,
299
+ tech=tech,
300
+ n_neighbors=n_neighbors,
301
+ batch_tech=batch_tech,
302
+ all_feat=all_feat,
303
+ hidden_dim=hidden_dim,
304
+ latent_dim=latent_dim,
305
+ ode_hidden_dim=ode_hidden_dim,
306
+ encoder_type=encoder_type,
307
+ graph_type=graph_type,
308
+ structure_decoder_type=structure_decoder_type,
309
+ feature_decoder_type=feature_decoder_type,
310
+ hidden_layers=hidden_layers,
311
+ decoder_hidden_dim=decoder_hidden_dim,
312
+ dropout=dropout,
313
+ use_residual=use_residual,
314
+ Cheb_k=Cheb_k,
315
+ alpha=alpha,
316
+ threshold=threshold,
317
+ sparse_threshold=sparse_threshold,
318
+ lr=lr,
319
+ beta=beta,
320
+ graph=graph,
321
+ w_recon=w_recon,
322
+ w_kl=w_kl,
323
+ w_adj=w_adj,
324
+ w_recon_ode=w_recon_ode,
325
+ w_z_div=w_z_div,
326
+ device=device,
327
+ scale1=scale1,
328
+ scale2=scale2,
329
+ latent_type=latent_type,
330
+ num_parts=num_parts,
331
+ **kwargs
332
+ )
333
+
334
+ def _get_latent_representation(self, latent_name: str) -> np.ndarray:
335
+ """Helper to get latent representations, ensuring correct ordering."""
336
+ ls_l = []
337
+ self.godevae.eval()
338
+ with torch.no_grad():
339
+ take_latent_method = getattr(self, f"take_{latent_name}")
340
+ for cd in self.cdata:
341
+ latent = take_latent_method(cd)
342
+ ls_l.append(latent)
343
+ latent = np.vstack(ls_l)
344
+ return latent
345
+
346
+ def get_latent(self) -> np.ndarray:
347
+ """Gets the base latent representation for all cells."""
348
+ latent = self._get_latent_representation("latent")
349
+ # Reorder according to original cell indices
350
+ lut = dict(zip(self.idx, latent))
351
+ latent_ordered = np.vstack([lut[i] for i in range(self.n_obs)])
352
+ return latent_ordered
353
+
354
+ def get_odelatent(self) -> np.ndarray:
355
+ """Gets the ODE-evolved latent representation for all cells."""
356
+ latent = self._get_latent_representation("odelatent")
357
+ # Reorder according to original cell indices
358
+ lut = dict(zip(self.idx, latent))
359
+ latent_ordered = np.vstack([lut[i] for i in range(self.n_obs)])
360
+ return latent_ordered
361
+
362
+ def get_mix_latent(self) -> np.ndarray:
363
+ """Gets the mixed latent representation for all cells."""
364
+ latent = self.get_latent()
365
+ odelatent = self.get_odelatent()
366
+ mix_latent = self.scale1 * latent + self.scale2 * odelatent
367
+ return mix_latent
368
+
369
+ def score_final(self) -> None:
370
+ """Calculates and stores the final score on the base latent space."""
371
+ latent = self._get_latent_representation("latent")
372
+ self.final_score = self._calc_score(latent)
373
+
374
+ def score_odefinal(self) -> None:
375
+ """Calculates and stores the final score on the ODE-evolved latent space."""
376
+ latent = self._get_latent_representation("odelatent")
377
+ self.ode_final_score = self._calc_score(latent)
378
+
379
+ def score_mixfinal(self) -> None:
380
+ """Calculates and stores the final score on the mixed latent space."""
381
+ latent = self._get_latent_representation("latent")
382
+ odelatent = self._get_latent_representation("odelatent")
383
+ self.mix_final_score = self._calc_score(latent)
384
+
385
+