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,118 @@
|
|
|
1
|
+
import torch
|
|
2
|
+
import numpy as np
|
|
3
|
+
import scipy
|
|
4
|
+
from autoencodix.base._base_autoencoder import BaseAutoencoder
|
|
5
|
+
from ._model_output import ModelOutput
|
|
6
|
+
import torch.nn as nn
|
|
7
|
+
import pandas as pd
|
|
8
|
+
from autoencodix.configs.default_config import DefaultConfig
|
|
9
|
+
from autoencodix.modeling._captum_forward import CaptumForward
|
|
10
|
+
from captum.attr import (
|
|
11
|
+
LRP,
|
|
12
|
+
DeepLiftShap,
|
|
13
|
+
GradientShap,
|
|
14
|
+
IntegratedGradients,
|
|
15
|
+
Lime,
|
|
16
|
+
LimeBase,
|
|
17
|
+
)
|
|
18
|
+
|
|
19
|
+
class FeatureImportanceExplainer:
|
|
20
|
+
"""
|
|
21
|
+
More complete version with:
|
|
22
|
+
- method selection (DeepLiftShap, IG, etc.)
|
|
23
|
+
- baseline construction (mean / random / grouped)
|
|
24
|
+
- subset sampling
|
|
25
|
+
- reproducible randomness
|
|
26
|
+
"""
|
|
27
|
+
|
|
28
|
+
def __init__(
|
|
29
|
+
self,
|
|
30
|
+
adata_ACX,
|
|
31
|
+
model,
|
|
32
|
+
method: str = "DeepLiftShap",
|
|
33
|
+
n_subset: int = 100,
|
|
34
|
+
seed_int: int = 12,
|
|
35
|
+
baseline_type: str = "mean", # ["mean", "random"]
|
|
36
|
+
baseline_group: str = "all", # "all" or obs_col category
|
|
37
|
+
obs_col: str = None, # column in .obs for grouping
|
|
38
|
+
):
|
|
39
|
+
super(FeatureImportanceExplainer, self).__init__()
|
|
40
|
+
self.adata_ACX = adata_ACX
|
|
41
|
+
self.model = model
|
|
42
|
+
self.latent_dim = model.config.latent_dim
|
|
43
|
+
|
|
44
|
+
self.method = method
|
|
45
|
+
if self.method not in {"DeepLiftShap", "IntegratedGradients"}:
|
|
46
|
+
raise ValueError(
|
|
47
|
+
f"Invalid method {method}."
|
|
48
|
+
)
|
|
49
|
+
self.n_subset = n_subset
|
|
50
|
+
self.seed_int = seed_int
|
|
51
|
+
self.baseline_type = baseline_type
|
|
52
|
+
self.baseline_group = baseline_group
|
|
53
|
+
self.obs_col = obs_col
|
|
54
|
+
|
|
55
|
+
torch.manual_seed(seed_int)
|
|
56
|
+
np.random.seed(seed_int)
|
|
57
|
+
|
|
58
|
+
def explain(self):
|
|
59
|
+
adata_ACX = self.adata_ACX
|
|
60
|
+
gene_names = adata_ACX.var_names
|
|
61
|
+
inputs, baselines = return_inputs_baseline(adata_ACX,self.baseline_group,self.baseline_type)
|
|
62
|
+
indices_keep = np.random.choice(
|
|
63
|
+
inputs.shape[0], size=self.n_subset, replace=False
|
|
64
|
+
)
|
|
65
|
+
#for latent_dim in range(self.latent_dim):
|
|
66
|
+
all_attr = []
|
|
67
|
+
for latent_dim in range(self.latent_dim):
|
|
68
|
+
cp_forward_dim = CaptumForward(model=self.model,dim=latent_dim )
|
|
69
|
+
if self.method == "DeepLiftShap":
|
|
70
|
+
cp_explainer = DeepLiftShap(cp_forward_dim)
|
|
71
|
+
if self.method == "IntegratedGradients":
|
|
72
|
+
cp_explainer = IntegratedGradients(cp_forward_dim)
|
|
73
|
+
attributions, convergence = cp_explainer.attribute(
|
|
74
|
+
inputs=inputs[indices_keep].float(),
|
|
75
|
+
baselines=baselines[indices_keep].float(),
|
|
76
|
+
return_convergence_delta=True
|
|
77
|
+
)
|
|
78
|
+
avg_abs_attributions = attributions.abs().mean(dim=0)
|
|
79
|
+
all_attr.append(avg_abs_attributions.detach().cpu())
|
|
80
|
+
attr_matrix = torch.stack(all_attr).T.numpy()
|
|
81
|
+
df_attributions = pd.DataFrame(
|
|
82
|
+
attr_matrix,
|
|
83
|
+
index=list(gene_names),
|
|
84
|
+
columns=[f"latent_dimension_{i}" for i in range(self.latent_dim)],
|
|
85
|
+
)
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
return df_attributions
|
|
89
|
+
|
|
90
|
+
def return_inputs_baseline(input_adata,baseline_group,baseline_type):
|
|
91
|
+
inputs = torch.tensor(
|
|
92
|
+
input_adata.X.toarray()
|
|
93
|
+
if scipy.sparse.issparse(input_adata.X)
|
|
94
|
+
else input_adata.X
|
|
95
|
+
)
|
|
96
|
+
if baseline_group == "all":
|
|
97
|
+
if baseline_type == "mean":
|
|
98
|
+
baseline_mean = inputs.mean(axis=0) # gene_means
|
|
99
|
+
baselines = torch.tensor(np.tile(baseline_mean, (inputs.shape[0], 1)))
|
|
100
|
+
if baseline_type == "random_sample":
|
|
101
|
+
baseline_random = inputs[torch.randint(0, inputs.size(0), (1,)).item()]
|
|
102
|
+
baselines = torch.tensor(np.tile(baseline_random, (inputs.shape[0], 1)))
|
|
103
|
+
else:
|
|
104
|
+
input_adata_filtered = input_adata[input_adata.obs[obs_col] == baseline_group]
|
|
105
|
+
inputs_filtered = torch.tensor(
|
|
106
|
+
input_adata_filtered.X.toarray()
|
|
107
|
+
if scipy.sparse.issparse(input_adata_filtered.X)
|
|
108
|
+
else input_adata_filtered.X
|
|
109
|
+
)
|
|
110
|
+
if baseline_type == "mean":
|
|
111
|
+
baseline_mean = inputs_filtered.mean(axis=0) # gene_means
|
|
112
|
+
baselines = torch.tensor(np.tile(baseline_mean, (inputs.shape[0], 1)))
|
|
113
|
+
if baseline_type == "random_sample":
|
|
114
|
+
baseline_random = inputs_filtered[
|
|
115
|
+
torch.randint(0, inputs_filtered.size(0), (1,)).item()
|
|
116
|
+
]
|
|
117
|
+
baselines = torch.tensor(np.tile(baseline_random, (inputs.shape[0], 1)))
|
|
118
|
+
return inputs, baselines
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
from typing import Optional
|
|
2
|
+
from dataclasses import dataclass
|
|
3
|
+
import torch
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
# internal check done
|
|
7
|
+
# write tests: done
|
|
8
|
+
@dataclass
|
|
9
|
+
class ModelOutput:
|
|
10
|
+
"""A structured output dataclass for autoencoder models.
|
|
11
|
+
|
|
12
|
+
This class is used to encapsulate the outputs of autoencoder models in a
|
|
13
|
+
consistent format, allowing for flexibility in the type of outputs returned
|
|
14
|
+
by different architectures.
|
|
15
|
+
|
|
16
|
+
Attributes:
|
|
17
|
+
reconstruction: The reconstructed input data.
|
|
18
|
+
latent_mean: The mean of the latent space distribution, applicable for models like VAEs.
|
|
19
|
+
latent_logvar: The log variance of the latent space distribution, applicable for models like VAEs.
|
|
20
|
+
additional_info: A dictionary to store any additional information or intermediate outputs.
|
|
21
|
+
"""
|
|
22
|
+
|
|
23
|
+
reconstruction: torch.Tensor
|
|
24
|
+
latentspace: torch.Tensor
|
|
25
|
+
latent_mean: Optional[torch.Tensor] = None
|
|
26
|
+
latent_logvar: Optional[torch.Tensor] = None
|
|
27
|
+
additional_info: Optional[dict] = None
|
|
28
|
+
|
|
29
|
+
def __iter__(self):
|
|
30
|
+
yield self
|