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.
GNODEVAE/env.py ADDED
@@ -0,0 +1,474 @@
1
+ from .trainer import Trainer, Trainer_r
2
+ from .mixin import envMixin, scMixin
3
+ import numpy as np
4
+ import torch
5
+ from torch_geometric.loader import ClusterData
6
+ from torch_geometric.data import Data
7
+ from sklearn.cluster import KMeans
8
+ from typing import Optional, List, Tuple
9
+ from anndata import AnnData
10
+ import scanpy as sc
11
+
12
+ class BaseEnv(envMixin, scMixin):
13
+ def __init__(
14
+ self,
15
+ adata: AnnData,
16
+ layer: str,
17
+ n_var: int,
18
+ tech: str,
19
+ n_neighbors: int,
20
+ latent_dim: int,
21
+ batch_tech: Optional[str],
22
+ all_feat: bool,
23
+ device: torch.device,
24
+ num_parts: int,
25
+ *args,
26
+ **kwargs,
27
+ ):
28
+ # Initialize mixins
29
+ envMixin.__init__(self)
30
+ scMixin.__init__(self)
31
+
32
+ self.device = device
33
+ self.score: List[Tuple[float, float, float, float, float, float]] = []
34
+
35
+ self._register_adata(adata, layer, n_var, tech, n_neighbors, latent_dim, batch_tech, all_feat)
36
+ self._register_cdata(num_parts)
37
+
38
+ def step(self) -> None:
39
+ """
40
+ Perform a single training step.
41
+
42
+ This method iterates over the clustered data, updates the model parameters,
43
+ computes the latent representation, and calculates clustering and correlation scores.
44
+
45
+ Returns
46
+ -------
47
+ None
48
+ """
49
+ ls_l = []
50
+ for cd in self.cdata:
51
+ self.update(cd)
52
+ latent = self.take_latent(cd)
53
+ ls_l.append(latent)
54
+ latent = np.vstack(ls_l)
55
+ score = self._calc_score(latent)
56
+ self.score.append(score)
57
+
58
+ def _register_adata(
59
+ self,
60
+ adata: AnnData,
61
+ layer: str,
62
+ n_var: int,
63
+ tech: str,
64
+ n_neighbors: int,
65
+ latent_dim: int,
66
+ batch_tech: Optional[str],
67
+ all_feat: bool
68
+ ) -> None:
69
+ """
70
+ Register AnnData object and preprocess the data.
71
+
72
+ This method extracts the input features, graph structure, and cluster labels
73
+ from the AnnData object.
74
+
75
+ Parameters
76
+ ----------
77
+ adata : AnnData
78
+ Annotated data matrix (e.g., single-cell data).
79
+ layer : str
80
+ Layer of the AnnData object to store original features.
81
+ n_var : int
82
+ Number of highly variable genes to select.
83
+ tech : str
84
+ Decomposition method to use (PCA, NMF, FastICA, TruncatedSVD, FactorAnalysis, LatentDirichletAllocation).
85
+ n_neighbors : int
86
+ Number of neighbors for graph construction.
87
+ latent_dim : int
88
+ Latent space dimension for clustering.
89
+ batch_tech : Optional[str]
90
+ Method to correct batch effects ('harmony' or 'scvi').
91
+ all_feat : bool
92
+ Whether to use all features or only highly variable ones.
93
+
94
+ Returns
95
+ -------
96
+ None
97
+ """
98
+ self._preprocess(adata, layer, n_var)
99
+ self._decomposition(adata, tech, latent_dim)
100
+
101
+ if batch_tech:
102
+ self._batchcorrect(adata, batch_tech, tech, layer)
103
+
104
+ # Determine the representation to use for neighborhood graph
105
+ if batch_tech == 'harmony':
106
+ use_rep = f'X_harmony_{tech}'
107
+ elif batch_tech == 'scvi':
108
+ use_rep = 'X_scvi'
109
+ else:
110
+ use_rep = f'X_{tech}'
111
+
112
+ # Construct the neighborhood graph
113
+ sc.pp.neighbors(adata, n_neighbors=n_neighbors, use_rep=use_rep)
114
+
115
+ # Select features
116
+ if all_feat:
117
+ self.X = np.log1p(adata.layers[layer].toarray())
118
+ else:
119
+ self.X = adata[:, adata.var['highly_variable']].X.toarray()
120
+
121
+ self.n_obs, self.n_var = self.X.shape
122
+
123
+ # Perform clustering
124
+ self.labels = KMeans(n_clusters=latent_dim).fit_predict(self.X)
125
+
126
+ # Extract graph information
127
+ coo = adata.obsp['connectivities'].tocoo()
128
+ self.edge_index = np.array([coo.row, coo.col])
129
+ self.edge_weight = coo.data
130
+
131
+ # Assign indices
132
+ self.y = np.arange(adata.shape[0])
133
+
134
+ def _register_cdata(
135
+ self,
136
+ num_parts: int,
137
+ ) -> None:
138
+ """
139
+ Register clustered graph data for batched training.
140
+
141
+ This method partitions the graph data into clusters for efficient training.
142
+
143
+ Parameters
144
+ ----------
145
+ num_parts : int
146
+ Number of partitions for clustering the graph data.
147
+
148
+ Returns
149
+ -------
150
+ None
151
+ """
152
+ data = Data(
153
+ x=torch.tensor(self.X, dtype=torch.float, device=self.device),
154
+ edge_index=torch.tensor(self.edge_index, dtype=torch.long, device=self.device),
155
+ edge_attr=torch.tensor(self.edge_weight, dtype=torch.float, device=self.device),
156
+ y=torch.tensor(self.y, dtype=torch.long, device=self.device),
157
+ )
158
+ self.cdata = ClusterData(data, num_parts=num_parts)
159
+ ls_y = []
160
+ for cd in self.cdata:
161
+ ls_y.append(cd.y.cpu().numpy())
162
+ self.idx = np.hstack(ls_y)
163
+
164
+
165
+ class Env_r(BaseEnv, Trainer_r):
166
+ """
167
+ Environment class for training and evaluating the GraphVAE model.
168
+
169
+ This class extends the `Trainer` and `envMixin` classes to provide functionality
170
+ for handling AnnData objects, clustering, and training the model in a batched manner
171
+ using `ClusterData`.
172
+
173
+ Parameters
174
+ ----------
175
+ adata : AnnData
176
+ Annotated data matrix (e.g., single-cell data).
177
+ layer : str
178
+ Layer of the AnnData object to use for input features.
179
+ n_var : int
180
+ Number of highly variable genes to select.
181
+ tech : str
182
+ Decomposition method to use (PCA, NMF, FastICA, TruncatedSVD, FactorAnalysis, LatentDirichletAllocation).
183
+ n_neighbors : int
184
+ Number of neighbors for graph construction.
185
+ latent_dim : int
186
+ Latent space dimension for clustering.
187
+ batch_tech : Optional[str]
188
+ Method to correct batch effects ('harmony' or 'scvi').
189
+ all_feat : bool
190
+ Whether to use all features or only highly variable ones
191
+ hidden_dim : int
192
+ Hidden layer dimension for the encoder.
193
+ latent_dim : int
194
+ Latent space dimension.
195
+ encoder_type : str
196
+ Type of graph convolutional layer ('GCN', 'Cheb', 'SAGE', 'Graph', 'TAG', 'ARMA', 'GAT', 'Transformer', 'SG', 'SSG').
197
+ encoder_hidden_layers : int
198
+ Number of hidden layers in the graph encoder.
199
+ decoder_type : str
200
+ Type of graph decoder ('Bilinear', 'InnerProduct', 'MLP').
201
+ decoder_hidden_dim : int
202
+ Hidden dimension for the MLPDecoder (if used).
203
+ feature_decoder_hidden_layers : int
204
+ Number of hidden layers in the feature decoder.
205
+ dropout : float
206
+ Dropout rate.
207
+ use_residual : bool, optional
208
+ Whether to use residual connections, by default True.
209
+ Cheb_k : int, optional
210
+ The order of Chebyshev polynomials for ChebConv, by default None.
211
+ alpha : float, optional
212
+ Teleport probability, by default 0.5.
213
+ lr : float
214
+ Learning rate for the optimizer.
215
+ beta : float
216
+ Weight for the KL divergence term in the loss function.
217
+ graph : float
218
+ Weight for the graph reconstruction loss.
219
+ device : torch.device
220
+ Device to run the model on (e.g., 'cpu' or 'cuda').
221
+ num_parts : int
222
+ Number of partitions for clustering the graph data.
223
+ *args
224
+ Additional positional arguments.
225
+ **kwargs
226
+ Additional keyword arguments.
227
+
228
+ Attributes
229
+ ----------
230
+ X : np.ndarray
231
+ Input feature matrix (log-transformed).
232
+ n_obs : int
233
+ Number of observations (nodes).
234
+ n_var : int
235
+ Number of variables (features).
236
+ labels : np.ndarray
237
+ Cluster labels for the input data.
238
+ edge_index : np.ndarray
239
+ Edge indices of the graph.
240
+ edge_weight : np.ndarray
241
+ Edge weights of the graph.
242
+ y : np.ndarray
243
+ Node indices.
244
+ cdata : ClusterData
245
+ Clustered graph data for batched training.
246
+ idx : np.ndarray
247
+ Node indices for all clusters.
248
+ score : List[Tuple[float, float, float, float, float, float]]
249
+ List of clustering and correlation scores for each training step.
250
+ """
251
+
252
+ def __init__(
253
+ self,
254
+ adata: AnnData,
255
+ layer: str,
256
+ n_var: int,
257
+ tech: str,
258
+ n_neighbors: int,
259
+ batch_tech: Optional[str],
260
+ all_feat: bool,
261
+ hidden_dim: int,
262
+ latent_dim: int,
263
+ encoder_type: str,
264
+ graph_type: str,
265
+ structure_decoder_type: str,
266
+ feature_decoder_type: str,
267
+ hidden_layers: int,
268
+ decoder_hidden_dim: int,
269
+ dropout: float,
270
+ use_residual: bool,
271
+ Cheb_k: int,
272
+ alpha: float,
273
+ threshold: float,
274
+ sparse_threshold: Optional[int],
275
+ lr: float,
276
+ beta: float,
277
+ graph: float,
278
+ device: torch.device,
279
+ num_parts: int,
280
+ *args,
281
+ **kwargs,
282
+ ):
283
+ BaseEnv.__init__(
284
+ self,
285
+ adata,
286
+ layer,
287
+ n_var,
288
+ tech,
289
+ n_neighbors,
290
+ latent_dim,
291
+ batch_tech,
292
+ all_feat,
293
+ device,
294
+ num_parts,
295
+ *args,
296
+ **kwargs,
297
+ )
298
+ Trainer_r.__init__(
299
+ self,
300
+ self.n_var,
301
+ hidden_dim,
302
+ latent_dim,
303
+ encoder_type,
304
+ graph_type,
305
+ structure_decoder_type,
306
+ feature_decoder_type,
307
+ hidden_layers,
308
+ decoder_hidden_dim,
309
+ dropout,
310
+ use_residual,
311
+ Cheb_k,
312
+ alpha,
313
+ threshold,
314
+ sparse_threshold,
315
+ lr,
316
+ beta,
317
+ graph,
318
+ device,
319
+ )
320
+
321
+
322
+ class Env(BaseEnv, Trainer):
323
+ """
324
+ Environment class for training and evaluating the GraphVAE model.
325
+
326
+ This class extends the `Trainer` and `envMixin` classes to provide functionality
327
+ for handling AnnData objects, clustering, and training the model in a batched manner
328
+ using `ClusterData`.
329
+
330
+ Parameters
331
+ ----------
332
+ adata : AnnData
333
+ Annotated data matrix (e.g., single-cell data).
334
+ layer : str
335
+ Layer of the AnnData object to use for input features.
336
+ n_var : int
337
+ Number of highly variable genes to select.
338
+ tech : str
339
+ Decomposition method to use (PCA, NMF, FastICA, TruncatedSVD, FactorAnalysis, LatentDirichletAllocation).
340
+ n_neighbors : int
341
+ Number of neighbors for graph construction.
342
+ latent_dim : int
343
+ Latent space dimension for clustering.
344
+ batch_tech : Optional[str]
345
+ Method to correct batch effects ('harmony' or 'scvi').
346
+ all_feat : bool
347
+ Whether to use all features or only highly variable ones
348
+ hidden_dim : int
349
+ Hidden layer dimension for the encoder.
350
+ latent_dim : int
351
+ Latent space dimension.
352
+ encoder_type : str
353
+ Type of graph convolutional layer ('GCN', 'Cheb', 'SAGE', 'Graph', 'TAG', 'ARMA', 'GAT', 'Transformer', 'SG', 'SSG').
354
+ encoder_hidden_layers : int
355
+ Number of hidden layers in the graph encoder.
356
+ decoder_type : str
357
+ Type of graph decoder ('Bilinear', 'InnerProduct', 'MLP').
358
+ decoder_hidden_dim : int
359
+ Hidden dimension for the MLPDecoder (if used).
360
+ feature_decoder_hidden_layers : int
361
+ Number of hidden layers in the feature decoder.
362
+ dropout : float
363
+ Dropout rate.
364
+ use_residual : bool, optional
365
+ Whether to use residual connections, by default True.
366
+ Cheb_k : int, optional
367
+ The order of Chebyshev polynomials for ChebConv, by default None.
368
+ alpha : float, optional
369
+ Teleport probability, by default 0.5.
370
+ lr : float
371
+ Learning rate for the optimizer.
372
+ beta : float
373
+ Weight for the KL divergence term in the loss function.
374
+ graph : float
375
+ Weight for the graph reconstruction loss.
376
+ device : torch.device
377
+ Device to run the model on (e.g., 'cpu' or 'cuda').
378
+ num_parts : int
379
+ Number of partitions for clustering the graph data.
380
+ *args
381
+ Additional positional arguments.
382
+ **kwargs
383
+ Additional keyword arguments.
384
+
385
+ Attributes
386
+ ----------
387
+ X : np.ndarray
388
+ Input feature matrix (log-transformed).
389
+ n_obs : int
390
+ Number of observations (nodes).
391
+ n_var : int
392
+ Number of variables (features).
393
+ labels : np.ndarray
394
+ Cluster labels for the input data.
395
+ edge_index : np.ndarray
396
+ Edge indices of the graph.
397
+ edge_weight : np.ndarray
398
+ Edge weights of the graph.
399
+ y : np.ndarray
400
+ Node indices.
401
+ cdata : ClusterData
402
+ Clustered graph data for batched training.
403
+ idx : np.ndarray
404
+ Node indices for all clusters.
405
+ score : List[Tuple[float, float, float, float, float, float]]
406
+ List of clustering and correlation scores for each training step.
407
+ """
408
+
409
+ def __init__(
410
+ self,
411
+ adata: AnnData,
412
+ layer: str,
413
+ n_var: int,
414
+ tech: str,
415
+ n_neighbors: int,
416
+ batch_tech: Optional[str],
417
+ all_feat: bool,
418
+ interpretable: bool,
419
+ hidden_dim: int,
420
+ latent_dim: int,
421
+ idim: Optional[int],
422
+ encoder_type: str,
423
+ encoder_hidden_layers: int,
424
+ decoder_type: str,
425
+ decoder_hidden_dim: int,
426
+ feature_decoder_hidden_layers: int,
427
+ dropout: float,
428
+ use_residual: bool,
429
+ Cheb_k: Optional[int],
430
+ alpha: Optional[float],
431
+ lr: float,
432
+ beta: float,
433
+ graph: float,
434
+ device: torch.device,
435
+ num_parts: int,
436
+ *args,
437
+ **kwargs,
438
+ ):
439
+ BaseEnv.__init__(
440
+ self,
441
+ adata,
442
+ layer,
443
+ n_var,
444
+ tech,
445
+ n_neighbors,
446
+ latent_dim,
447
+ batch_tech,
448
+ all_feat,
449
+ device,
450
+ num_parts,
451
+ *args,
452
+ **kwargs,
453
+ )
454
+ Trainer.__init__(
455
+ self,
456
+ interpretable,
457
+ self.n_var,
458
+ hidden_dim,
459
+ latent_dim,
460
+ idim,
461
+ encoder_type,
462
+ encoder_hidden_layers,
463
+ decoder_type,
464
+ decoder_hidden_dim,
465
+ feature_decoder_hidden_layers,
466
+ dropout,
467
+ use_residual,
468
+ Cheb_k,
469
+ alpha,
470
+ lr,
471
+ beta,
472
+ graph,
473
+ device,
474
+ )