autoencodix 0.2.0__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.
Potentially problematic release.
This version of autoencodix might be problematic. Click here for more details.
- autoencodix/__init__.py +27 -0
- autoencodix/base/.ipynb_checkpoints/_base_pipeline-checkpoint.py +1274 -0
- autoencodix/base/.ipynb_checkpoints/_base_trainer-checkpoint.py +247 -0
- autoencodix/base/__init__.py +21 -0
- autoencodix/base/_base_autoencoder.py +138 -0
- autoencodix/base/_base_dataset.py +103 -0
- autoencodix/base/_base_evaluator.py +34 -0
- autoencodix/base/_base_loss.py +311 -0
- autoencodix/base/_base_pipeline.py +1355 -0
- autoencodix/base/_base_predictor.py +6 -0
- autoencodix/base/_base_preprocessor.py +1033 -0
- autoencodix/base/_base_trainer.py +259 -0
- autoencodix/base/_base_visualizer.py +578 -0
- autoencodix/base/sampleconfig.yaml +132 -0
- autoencodix/configs/__init__.py +21 -0
- autoencodix/configs/default_config.py +652 -0
- autoencodix/configs/disentanglix_config.py +15 -0
- autoencodix/configs/maskix_config.py +39 -0
- autoencodix/configs/ontix_config.py +35 -0
- autoencodix/configs/stackix_config.py +33 -0
- autoencodix/configs/vanillix_config.py +18 -0
- autoencodix/configs/varix_config.py +15 -0
- autoencodix/configs/xmodalix_config.py +43 -0
- autoencodix/disentanglix.py +72 -0
- autoencodix/evaluate/.ipynb_checkpoints/evaluate-checkpoint.py +532 -0
- autoencodix/evaluate/__init__.py +4 -0
- autoencodix/evaluate/_general_evaluator.py +730 -0
- autoencodix/evaluate/_xmodalix_evaluator.py +376 -0
- autoencodix/imagix.py +76 -0
- autoencodix/losses/disentanglix_loss.py +215 -0
- autoencodix/losses/maskix_loss.py +94 -0
- autoencodix/losses/vanillix_loss.py +28 -0
- autoencodix/losses/varix_loss.py +102 -0
- autoencodix/losses/xmodal_loss.py +304 -0
- autoencodix/maskix.py +108 -0
- autoencodix/modeling/.ipynb_checkpoints/__init__-checkpoint.py +16 -0
- autoencodix/modeling/.ipynb_checkpoints/_captum_forward-checkpoint.py +16 -0
- autoencodix/modeling/__init__.py +20 -0
- autoencodix/modeling/_captum_forward.py +20 -0
- autoencodix/modeling/_classifier.py +45 -0
- autoencodix/modeling/_imagevae_architecture.py +296 -0
- autoencodix/modeling/_imgfast_architecture.py +288 -0
- autoencodix/modeling/_layer_factory.py +71 -0
- autoencodix/modeling/_maskix_architecture.py +190 -0
- autoencodix/modeling/_ontix_architecture.py +305 -0
- autoencodix/modeling/_vanillix_architecture.py +136 -0
- autoencodix/modeling/_varix_architecture.py +192 -0
- autoencodix/notebooks/__init__.py +0 -0
- autoencodix/ontix.py +136 -0
- autoencodix/py.typed +0 -0
- autoencodix/stackix.py +122 -0
- autoencodix/trainers/__init__.py +6 -0
- autoencodix/trainers/_general_trainer.py +525 -0
- autoencodix/trainers/_maskix_trainer.py +116 -0
- autoencodix/trainers/_ontix_trainer.py +77 -0
- autoencodix/trainers/_stackix_orchestrator.py +522 -0
- autoencodix/trainers/_stackix_trainer.py +209 -0
- autoencodix/trainers/_xmodal_trainer.py +1006 -0
- autoencodix/utils/.ipynb_checkpoints/_explainer-checkpoint.py +118 -0
- autoencodix/utils/.ipynb_checkpoints/_model_output-checkpoint.py +30 -0
- autoencodix/utils/.ipynb_checkpoints/_utils-checkpoint.py +626 -0
- autoencodix/utils/.ipynb_checkpoints/adata_converter-checkpoint.py +99 -0
- autoencodix/utils/.ipynb_checkpoints/feature_importance-checkpoint.py +823 -0
- autoencodix/utils/__init__.py +22 -0
- autoencodix/utils/_annealer.py +193 -0
- autoencodix/utils/_bulkreader.py +232 -0
- autoencodix/utils/_explainer.py +246 -0
- autoencodix/utils/_imgreader.py +404 -0
- autoencodix/utils/_internals.py +23 -0
- autoencodix/utils/_llm_explainer.py +318 -0
- autoencodix/utils/_losses.py +20 -0
- autoencodix/utils/_model_output.py +30 -0
- autoencodix/utils/_result.py +393 -0
- autoencodix/utils/_screader.py +76 -0
- autoencodix/utils/_traindynamics.py +187 -0
- autoencodix/utils/_utils.py +690 -0
- autoencodix/utils/adata_converter.py +111 -0
- autoencodix/utils/example_data.py +398 -0
- autoencodix/utils/feature_importance.py +829 -0
- autoencodix/utils/prompts.py +21 -0
- autoencodix/vanillix.py +141 -0
- autoencodix/varix.py +80 -0
- autoencodix/visualize/__init__.py +5 -0
- autoencodix/visualize/_general_visualizer.py +913 -0
- autoencodix/visualize/_imagix_visualizer.py +111 -0
- autoencodix/visualize/_xmodal_visualizer.py +841 -0
- autoencodix/visualize/visualize.py +1214 -0
- autoencodix/xmodalix.py +318 -0
- autoencodix-0.2.0.dist-info/METADATA +143 -0
- autoencodix-0.2.0.dist-info/RECORD +91 -0
- autoencodix-0.2.0.dist-info/WHEEL +4 -0
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
from typing import Optional, Union, Tuple, Dict
|
|
2
|
+
|
|
3
|
+
import torch
|
|
4
|
+
import torch.nn as nn
|
|
5
|
+
|
|
6
|
+
from autoencodix.base._base_autoencoder import BaseAutoencoder
|
|
7
|
+
from autoencodix.utils._model_output import ModelOutput
|
|
8
|
+
from autoencodix.configs.default_config import DefaultConfig
|
|
9
|
+
|
|
10
|
+
from ._layer_factory import LayerFactory
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class VarixArchitecture(BaseAutoencoder):
|
|
14
|
+
"""Variational Autoencoder implementation with separate encoder and decoder construction.
|
|
15
|
+
|
|
16
|
+
Attributes:
|
|
17
|
+
input_dim: number of input features
|
|
18
|
+
config: Configuration object containing model architecture parameters
|
|
19
|
+
encoder: Encoder network of the autoencoder
|
|
20
|
+
decoder: Decoder network of the autoencoder
|
|
21
|
+
mu: Linear layer to compute the mean of the latent distribution
|
|
22
|
+
logvar: Linear layer to compute the log-variance of the latent distribution
|
|
23
|
+
|
|
24
|
+
"""
|
|
25
|
+
|
|
26
|
+
def __init__(
|
|
27
|
+
self,
|
|
28
|
+
config: Optional[Union[None, DefaultConfig]],
|
|
29
|
+
input_dim: int,
|
|
30
|
+
ontologies: Optional[Union[Tuple, Dict]] = None,
|
|
31
|
+
feature_order: Optional[Union[Tuple, Dict]] = None,
|
|
32
|
+
) -> None:
|
|
33
|
+
"""Initialize the Vanilla Autoencoder with the given configuration.
|
|
34
|
+
|
|
35
|
+
Args:
|
|
36
|
+
config: Configuration object containing model parameters.
|
|
37
|
+
input_dim: Number of input features.
|
|
38
|
+
"""
|
|
39
|
+
|
|
40
|
+
if config is None:
|
|
41
|
+
config = DefaultConfig()
|
|
42
|
+
self._config: DefaultConfig = config
|
|
43
|
+
super().__init__(config=config, input_dim=input_dim)
|
|
44
|
+
self.input_dim: int = input_dim
|
|
45
|
+
self._mu: nn.Module
|
|
46
|
+
self._logvar: nn.Module
|
|
47
|
+
self._encoder: nn.Module
|
|
48
|
+
self._decoder: nn.Module
|
|
49
|
+
|
|
50
|
+
# populate self.encoder and self.decoder
|
|
51
|
+
self._build_network()
|
|
52
|
+
self.apply(self._init_weights)
|
|
53
|
+
|
|
54
|
+
def _build_network(self) -> None:
|
|
55
|
+
"""Construct the encoder and decoder networks.
|
|
56
|
+
|
|
57
|
+
Handles cases where `n_layers=0` by skipping the encoder and using only mu/logvar.
|
|
58
|
+
"""
|
|
59
|
+
enc_dim = LayerFactory.get_layer_dimensions(
|
|
60
|
+
feature_dim=self.input_dim,
|
|
61
|
+
latent_dim=self._config.latent_dim,
|
|
62
|
+
n_layers=self._config.n_layers,
|
|
63
|
+
enc_factor=self._config.enc_factor,
|
|
64
|
+
)
|
|
65
|
+
#
|
|
66
|
+
|
|
67
|
+
# Case 1: No Hidden Layers (Direct Mapping)
|
|
68
|
+
self._encoder = nn.Sequential()
|
|
69
|
+
self._mu = nn.Linear(self.input_dim, self._config.latent_dim)
|
|
70
|
+
self._logvar = nn.Linear(self.input_dim, self._config.latent_dim)
|
|
71
|
+
|
|
72
|
+
# Case 2: At Least One Hidden Layer
|
|
73
|
+
if self._config.n_layers > 0:
|
|
74
|
+
encoder_layers = []
|
|
75
|
+
# print(enc_dim)
|
|
76
|
+
for i, (in_features, out_features) in enumerate(
|
|
77
|
+
zip(enc_dim[:-1], enc_dim[1:])
|
|
78
|
+
):
|
|
79
|
+
# since we add mu and logvar, we will remove the last layer
|
|
80
|
+
if i == len(enc_dim) - 2:
|
|
81
|
+
break
|
|
82
|
+
encoder_layers.extend(
|
|
83
|
+
LayerFactory.create_layer(
|
|
84
|
+
in_features=in_features,
|
|
85
|
+
out_features=out_features,
|
|
86
|
+
dropout_p=self._config.drop_p,
|
|
87
|
+
last_layer=False, # only for decoder relevant
|
|
88
|
+
)
|
|
89
|
+
)
|
|
90
|
+
|
|
91
|
+
self._encoder = nn.Sequential(*encoder_layers)
|
|
92
|
+
self._mu = nn.Linear(enc_dim[-2], self._config.latent_dim)
|
|
93
|
+
self._logvar = nn.Linear(enc_dim[-2], self._config.latent_dim)
|
|
94
|
+
|
|
95
|
+
# Construct Decoder (Same for Both Cases)
|
|
96
|
+
dec_dim = enc_dim[::-1] # Reverse the dimensions and copy
|
|
97
|
+
decoder_layers = []
|
|
98
|
+
for i, (in_features, out_features) in enumerate(zip(dec_dim[:-1], dec_dim[1:])):
|
|
99
|
+
last_layer = i == len(dec_dim) - 2
|
|
100
|
+
decoder_layers.extend(
|
|
101
|
+
LayerFactory.create_layer(
|
|
102
|
+
in_features=in_features,
|
|
103
|
+
out_features=out_features,
|
|
104
|
+
dropout_p=self._config.drop_p,
|
|
105
|
+
last_layer=last_layer,
|
|
106
|
+
)
|
|
107
|
+
)
|
|
108
|
+
|
|
109
|
+
self._decoder = nn.Sequential(*decoder_layers)
|
|
110
|
+
|
|
111
|
+
def encode(self, x: torch.Tensor) -> Tuple[torch.Tensor, torch.Tensor]:
|
|
112
|
+
"""Encode the input tensor x.
|
|
113
|
+
|
|
114
|
+
Args:
|
|
115
|
+
x: Input tensor
|
|
116
|
+
|
|
117
|
+
Returns:
|
|
118
|
+
Encoded tensor
|
|
119
|
+
|
|
120
|
+
"""
|
|
121
|
+
|
|
122
|
+
latent = x # for case where n_layers=0
|
|
123
|
+
if self._config.n_layers > 0:
|
|
124
|
+
latent = self._encoder(x)
|
|
125
|
+
mu = self._mu(latent)
|
|
126
|
+
logvar = self._logvar(latent)
|
|
127
|
+
# numeric stability
|
|
128
|
+
logvar = torch.clamp(logvar, 0.01, 20)
|
|
129
|
+
mu = torch.where(mu < 0.0000001, torch.zeros_like(mu), mu)
|
|
130
|
+
return mu, logvar
|
|
131
|
+
|
|
132
|
+
def reparameterize(self, mu: torch.Tensor, logvar: torch.Tensor) -> torch.Tensor:
|
|
133
|
+
"""Reparameterization trick for VAE
|
|
134
|
+
|
|
135
|
+
Args:
|
|
136
|
+
mu: torch.Tensor
|
|
137
|
+
logvar: torch.Tensor
|
|
138
|
+
|
|
139
|
+
Returns:
|
|
140
|
+
torch.Tensor
|
|
141
|
+
|
|
142
|
+
"""
|
|
143
|
+
std = torch.exp(0.5 * logvar)
|
|
144
|
+
eps = torch.randn_like(std)
|
|
145
|
+
return mu + eps * std
|
|
146
|
+
|
|
147
|
+
def get_latent_space(self, x: torch.Tensor) -> torch.Tensor:
|
|
148
|
+
"""Returns the latent space representation of the input.
|
|
149
|
+
|
|
150
|
+
Args:
|
|
151
|
+
x: Input tensor
|
|
152
|
+
|
|
153
|
+
Returns:
|
|
154
|
+
Latent space representation
|
|
155
|
+
|
|
156
|
+
"""
|
|
157
|
+
mu, logvar = self.encode(x)
|
|
158
|
+
z = self.reparameterize(mu, logvar)
|
|
159
|
+
return z
|
|
160
|
+
|
|
161
|
+
def decode(self, x: torch.Tensor) -> torch.Tensor:
|
|
162
|
+
"""Decode the latent tensor x
|
|
163
|
+
|
|
164
|
+
Args:
|
|
165
|
+
x: Latent tensor
|
|
166
|
+
|
|
167
|
+
Returns:
|
|
168
|
+
Decoded tensor
|
|
169
|
+
"""
|
|
170
|
+
|
|
171
|
+
return self._decoder(x)
|
|
172
|
+
|
|
173
|
+
def forward(self, x: torch.Tensor) -> ModelOutput:
|
|
174
|
+
"""Forward pass of the model, fill
|
|
175
|
+
|
|
176
|
+
Args:
|
|
177
|
+
x: Input tensor
|
|
178
|
+
|
|
179
|
+
Returns:
|
|
180
|
+
ModelOutput object containing the reconstructed tensor and latent tensor
|
|
181
|
+
|
|
182
|
+
"""
|
|
183
|
+
mu, logvar = self.encode(x)
|
|
184
|
+
z = self.reparameterize(mu, logvar)
|
|
185
|
+
x_hat = self.decode(z)
|
|
186
|
+
return ModelOutput(
|
|
187
|
+
reconstruction=x_hat,
|
|
188
|
+
latentspace=z,
|
|
189
|
+
latent_mean=mu,
|
|
190
|
+
latent_logvar=logvar,
|
|
191
|
+
additional_info=None,
|
|
192
|
+
)
|
|
File without changes
|
autoencodix/ontix.py
ADDED
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
from typing import Dict, Optional, Type, Union, Tuple, List
|
|
2
|
+
import torch
|
|
3
|
+
import numpy as np
|
|
4
|
+
|
|
5
|
+
from autoencodix.base._base_dataset import BaseDataset
|
|
6
|
+
from autoencodix.base._base_loss import BaseLoss
|
|
7
|
+
from autoencodix.base._base_pipeline import BasePipeline
|
|
8
|
+
from autoencodix.base._base_trainer import BaseTrainer
|
|
9
|
+
from autoencodix.base._base_visualizer import BaseVisualizer
|
|
10
|
+
from autoencodix.base._base_preprocessor import BasePreprocessor
|
|
11
|
+
from autoencodix.base._base_autoencoder import BaseAutoencoder
|
|
12
|
+
from autoencodix.base._base_evaluator import BaseEvaluator
|
|
13
|
+
from autoencodix.data._datasetcontainer import DatasetContainer
|
|
14
|
+
from autoencodix.data._datasplitter import DataSplitter
|
|
15
|
+
from autoencodix.data.datapackage import DataPackage
|
|
16
|
+
from autoencodix.data._numeric_dataset import NumericDataset
|
|
17
|
+
from autoencodix.data.general_preprocessor import GeneralPreprocessor
|
|
18
|
+
from autoencodix.evaluate._general_evaluator import GeneralEvaluator
|
|
19
|
+
|
|
20
|
+
# from autoencodix.modeling._varix_architecture import VarixArchitecture
|
|
21
|
+
from autoencodix.modeling._ontix_architecture import OntixArchitecture
|
|
22
|
+
from autoencodix.trainers._ontix_trainer import OntixTrainer
|
|
23
|
+
from autoencodix.utils._result import Result
|
|
24
|
+
from autoencodix.configs.default_config import DefaultConfig
|
|
25
|
+
|
|
26
|
+
from autoencodix.configs.ontix_config import OntixConfig
|
|
27
|
+
from autoencodix.utils._losses import VarixLoss
|
|
28
|
+
from autoencodix.visualize._general_visualizer import GeneralVisualizer
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
## Copy from Varix with ontology addition
|
|
32
|
+
class Ontix(BasePipeline):
|
|
33
|
+
"""Ontix specific version of the BasePipeline class.
|
|
34
|
+
|
|
35
|
+
Inherits preprocess, fit, predict, evaluate, and visualize methods from BasePipeline.
|
|
36
|
+
|
|
37
|
+
This class extends BasePipeline. See the parent class for a full list
|
|
38
|
+
of attributes and methods.
|
|
39
|
+
|
|
40
|
+
Additional Attributes:
|
|
41
|
+
_default_config: Is set to OntixConfig here.
|
|
42
|
+
|
|
43
|
+
"""
|
|
44
|
+
|
|
45
|
+
def __init__(
|
|
46
|
+
self,
|
|
47
|
+
ontologies: Union[Tuple, List], # Addition to Varix, mandotory for Ontix
|
|
48
|
+
sep: Optional[str] = "\t", # Addition to Varix, optional to read in ontologies
|
|
49
|
+
data: Optional[Union[DataPackage, DatasetContainer]] = None,
|
|
50
|
+
trainer_type: Type[BaseTrainer] = OntixTrainer,
|
|
51
|
+
dataset_type: Type[BaseDataset] = NumericDataset,
|
|
52
|
+
model_type: Type[BaseAutoencoder] = OntixArchitecture,
|
|
53
|
+
loss_type: Type[BaseLoss] = VarixLoss,
|
|
54
|
+
preprocessor_type: Type[BasePreprocessor] = GeneralPreprocessor,
|
|
55
|
+
visualizer: Type[BaseVisualizer] = GeneralVisualizer,
|
|
56
|
+
evaluator: Optional[Type[BaseEvaluator]] = GeneralEvaluator,
|
|
57
|
+
result: Optional[Result] = None,
|
|
58
|
+
datasplitter_type: Type[DataSplitter] = DataSplitter,
|
|
59
|
+
custom_splits: Optional[Dict[str, np.ndarray]] = None,
|
|
60
|
+
config: Optional[DefaultConfig] = None,
|
|
61
|
+
) -> None:
|
|
62
|
+
"""Initialize Ontix pipeline with customizable components.
|
|
63
|
+
|
|
64
|
+
Some components are passed as types rather than instances because they require
|
|
65
|
+
data that is only available after preprocessing.
|
|
66
|
+
|
|
67
|
+
See parent class for full list of Arguments.
|
|
68
|
+
|
|
69
|
+
Raises:
|
|
70
|
+
TypeError: if ontologies are not a Tuple or List.
|
|
71
|
+
|
|
72
|
+
"""
|
|
73
|
+
self._default_config = OntixConfig()
|
|
74
|
+
if isinstance(ontologies, tuple):
|
|
75
|
+
self.ontologies = ontologies
|
|
76
|
+
elif isinstance(ontologies, list):
|
|
77
|
+
if sep is None:
|
|
78
|
+
raise ValueError(
|
|
79
|
+
"If ontologies are provided as a list, the seperator 'sep' cannot be None. "
|
|
80
|
+
)
|
|
81
|
+
ontologies_dict_list = [
|
|
82
|
+
self._read_ont_file(ont_file, sep=sep) for ont_file in ontologies
|
|
83
|
+
]
|
|
84
|
+
self.ontologies = tuple(ontologies_dict_list)
|
|
85
|
+
else:
|
|
86
|
+
raise TypeError(
|
|
87
|
+
f"Expected ontologies to be of type tuple or list, got {type(ontologies)}."
|
|
88
|
+
)
|
|
89
|
+
|
|
90
|
+
config.latent_dim = len(
|
|
91
|
+
self.ontologies[0]
|
|
92
|
+
) # Set latent dim to number of nodes in the ontology
|
|
93
|
+
|
|
94
|
+
super().__init__(
|
|
95
|
+
data=data,
|
|
96
|
+
dataset_type=dataset_type,
|
|
97
|
+
trainer_type=trainer_type,
|
|
98
|
+
model_type=model_type,
|
|
99
|
+
loss_type=loss_type,
|
|
100
|
+
preprocessor_type=preprocessor_type,
|
|
101
|
+
visualizer=visualizer,
|
|
102
|
+
evaluator=evaluator,
|
|
103
|
+
result=result,
|
|
104
|
+
datasplitter_type=datasplitter_type,
|
|
105
|
+
config=config,
|
|
106
|
+
custom_split=custom_splits,
|
|
107
|
+
ontologies=self.ontologies,
|
|
108
|
+
)
|
|
109
|
+
if not isinstance(self.config, OntixConfig):
|
|
110
|
+
raise TypeError(
|
|
111
|
+
f"For Ontix Pipeline, we only allow OntixConfig as type for config, got {type(self.config)}"
|
|
112
|
+
)
|
|
113
|
+
|
|
114
|
+
def _read_ont_file(self, file_path: str, sep: str = "\t") -> dict:
|
|
115
|
+
"""Function to read-in text files of ontologies with format child - separator - parent into an dictionary.
|
|
116
|
+
|
|
117
|
+
Args:
|
|
118
|
+
file_path: Path to file with ontology
|
|
119
|
+
sep: Separator used in file
|
|
120
|
+
Returns:
|
|
121
|
+
ont_dic: Dictionary containing the ontology as described in the text file.
|
|
122
|
+
|
|
123
|
+
"""
|
|
124
|
+
ont_dic = dict()
|
|
125
|
+
with open(file_path, "r") as ont_file:
|
|
126
|
+
for line in ont_file:
|
|
127
|
+
id_parent = line.strip().split(sep)[1]
|
|
128
|
+
id_child = line.split(sep)[0]
|
|
129
|
+
|
|
130
|
+
if id_parent in ont_dic:
|
|
131
|
+
ont_dic[id_parent].append(id_child)
|
|
132
|
+
else:
|
|
133
|
+
ont_dic[id_parent] = list()
|
|
134
|
+
ont_dic[id_parent].append(id_child)
|
|
135
|
+
|
|
136
|
+
return ont_dic
|
autoencodix/py.typed
ADDED
|
File without changes
|
autoencodix/stackix.py
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
from typing import Dict, Optional, Type, Union, List
|
|
2
|
+
import torch
|
|
3
|
+
import numpy as np
|
|
4
|
+
|
|
5
|
+
import anndata as ad # type: ignore
|
|
6
|
+
from autoencodix.base._base_dataset import BaseDataset
|
|
7
|
+
from autoencodix.utils._utils import config_method
|
|
8
|
+
from autoencodix.base._base_loss import BaseLoss
|
|
9
|
+
from autoencodix.base._base_pipeline import BasePipeline
|
|
10
|
+
from autoencodix.base._base_trainer import BaseTrainer
|
|
11
|
+
from autoencodix.base._base_visualizer import BaseVisualizer
|
|
12
|
+
from autoencodix.base._base_preprocessor import BasePreprocessor
|
|
13
|
+
from autoencodix.base._base_autoencoder import BaseAutoencoder
|
|
14
|
+
from autoencodix.base._base_evaluator import BaseEvaluator
|
|
15
|
+
from autoencodix.data._datasetcontainer import DatasetContainer
|
|
16
|
+
from autoencodix.data._datasplitter import DataSplitter
|
|
17
|
+
from autoencodix.data.datapackage import DataPackage
|
|
18
|
+
from autoencodix.data._numeric_dataset import NumericDataset
|
|
19
|
+
from autoencodix.evaluate._general_evaluator import GeneralEvaluator
|
|
20
|
+
from autoencodix.modeling._varix_architecture import VarixArchitecture
|
|
21
|
+
from autoencodix.utils._result import Result
|
|
22
|
+
from autoencodix.configs.default_config import DefaultConfig
|
|
23
|
+
|
|
24
|
+
from autoencodix.configs.stackix_config import StackixConfig
|
|
25
|
+
from autoencodix.utils._losses import VarixLoss
|
|
26
|
+
from autoencodix.data._stackix_preprocessor import StackixPreprocessor
|
|
27
|
+
from autoencodix.data._stackix_dataset import StackixDataset
|
|
28
|
+
from autoencodix.trainers._stackix_trainer import StackixTrainer
|
|
29
|
+
from autoencodix.visualize._general_visualizer import GeneralVisualizer
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
class Stackix(BasePipeline):
|
|
33
|
+
"""Stackix pipeline for training multiple VAEs on different modalities and stacking their latent spaces.
|
|
34
|
+
|
|
35
|
+
This pipeline uses:
|
|
36
|
+
1. StackixPreprocessor to prepare data for multi-modality training
|
|
37
|
+
2. StackixTrainer to train individual VAEs, extract latent spaces, and train the final stacked model
|
|
38
|
+
|
|
39
|
+
Like other pipelines, it follows the standard BasePipeline interface and workflow.
|
|
40
|
+
|
|
41
|
+
Additional Attributes:
|
|
42
|
+
_default_config: Is set to StackixConfig here.
|
|
43
|
+
|
|
44
|
+
"""
|
|
45
|
+
|
|
46
|
+
def __init__(
|
|
47
|
+
self,
|
|
48
|
+
data: Optional[Union[DataPackage, DatasetContainer]] = None,
|
|
49
|
+
trainer_type: Type[BaseTrainer] = StackixTrainer,
|
|
50
|
+
dataset_type: Type[BaseDataset] = StackixDataset,
|
|
51
|
+
model_type: Type[BaseAutoencoder] = VarixArchitecture,
|
|
52
|
+
loss_type: Type[BaseLoss] = VarixLoss,
|
|
53
|
+
preprocessor_type: Type[BasePreprocessor] = StackixPreprocessor,
|
|
54
|
+
visualizer: Type[BaseVisualizer] = GeneralVisualizer,
|
|
55
|
+
evaluator: Optional[Type[BaseEvaluator]] = GeneralEvaluator,
|
|
56
|
+
result: Optional[Result] = None,
|
|
57
|
+
datasplitter_type: Type[DataSplitter] = DataSplitter,
|
|
58
|
+
custom_splits: Optional[Dict[str, np.ndarray]] = None,
|
|
59
|
+
config: Optional[DefaultConfig] = None,
|
|
60
|
+
ontologies: Optional[Union[List, Dict]] = None,
|
|
61
|
+
) -> None:
|
|
62
|
+
"""Initialize the Stackix pipeline.
|
|
63
|
+
|
|
64
|
+
See parent class for full list of Args.
|
|
65
|
+
"""
|
|
66
|
+
self._default_config = StackixConfig()
|
|
67
|
+
super().__init__(
|
|
68
|
+
data=data,
|
|
69
|
+
dataset_type=dataset_type
|
|
70
|
+
or NumericDataset, # Fallback, but not directly used
|
|
71
|
+
trainer_type=trainer_type,
|
|
72
|
+
model_type=model_type,
|
|
73
|
+
loss_type=loss_type,
|
|
74
|
+
preprocessor_type=preprocessor_type,
|
|
75
|
+
visualizer=visualizer,
|
|
76
|
+
evaluator=evaluator,
|
|
77
|
+
result=result,
|
|
78
|
+
datasplitter_type=datasplitter_type,
|
|
79
|
+
config=config,
|
|
80
|
+
custom_split=custom_splits,
|
|
81
|
+
ontologies=ontologies,
|
|
82
|
+
)
|
|
83
|
+
if not isinstance(self.config, StackixConfig):
|
|
84
|
+
raise TypeError(
|
|
85
|
+
f"For Stackix Pipeline, we only allow StackixConfig as type for config, got {type(self.config)}"
|
|
86
|
+
)
|
|
87
|
+
|
|
88
|
+
def _process_latent_results(
|
|
89
|
+
self, predictor_results: Result, predict_data: DatasetContainer
|
|
90
|
+
):
|
|
91
|
+
"""Processes the latent spaces from the StackixTrainer prediction results.
|
|
92
|
+
|
|
93
|
+
Creates a correctly annotated AnnData object.
|
|
94
|
+
This method overrides the BasePipeline implementation to specifically handle
|
|
95
|
+
the aligned latent space from the unpaired/stacked workflow.
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
Args:
|
|
99
|
+
predictor_results: Result object after predict step
|
|
100
|
+
predict_data: not used here, only to keep interface structure
|
|
101
|
+
|
|
102
|
+
"""
|
|
103
|
+
latent = predictor_results.latentspaces.get(epoch=-1, split="test")
|
|
104
|
+
sample_ids = predictor_results.sample_ids.get(epoch=-1, split="test")
|
|
105
|
+
if latent is None:
|
|
106
|
+
import warnings
|
|
107
|
+
|
|
108
|
+
warnings.warn(
|
|
109
|
+
"No latent space found in predictor results. Cannot create AnnData object."
|
|
110
|
+
)
|
|
111
|
+
return
|
|
112
|
+
|
|
113
|
+
self.result.adata_latent = ad.AnnData(X=latent)
|
|
114
|
+
self.result.adata_latent.obs_names = sample_ids
|
|
115
|
+
self.result.adata_latent.var_names = [
|
|
116
|
+
f"Latent_{i}" for i in range(latent.shape[1]) # ty: ignore
|
|
117
|
+
]
|
|
118
|
+
|
|
119
|
+
# 4. Update the main result object with the rest of the prediction results.
|
|
120
|
+
self.result.update(predictor_results)
|
|
121
|
+
|
|
122
|
+
print("Successfully created annotated latent space object (adata_latent).")
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
from ._general_trainer import GeneralTrainer
|
|
2
|
+
from ._stackix_orchestrator import StackixOrchestrator
|
|
3
|
+
from ._stackix_trainer import StackixTrainer
|
|
4
|
+
from ._xmodal_trainer import XModalTrainer
|
|
5
|
+
|
|
6
|
+
__all__ = ["GeneralTrainer", "StackixTrainer", "StackixOrchestrator", "XModalTrainer"]
|