| from collections.abc import Callable |
| from dataclasses import dataclass |
| from typing import Any |
|
|
| import jax |
| import jax.numpy as jnp |
|
|
| from src.utils._types import ArrayLike |
|
|
| __all__ = [ |
| "BaseDataMixin", |
| "ConditionData", |
| "PredictionData", |
| "TrainingData", |
| "ValidationData", |
| ] |
|
|
|
|
| @dataclass |
| class ReturnData: |
| split_covariates_mask: jnp.ndarray | None |
| split_idx_to_covariates: dict[int, tuple[Any, ...]] |
| perturbation_covariates_mask: jnp.ndarray | None |
| perturbation_idx_to_covariates: dict[int, tuple[Any, ...]] |
| perturbation_idx_to_id: dict[int, Any] |
| condition_data: dict[str, ArrayLike] |
| control_to_perturbation: dict[int, ArrayLike] |
| max_combination_length: int |
| condition_data_id: jnp.ndarray |
|
|
|
|
| class BaseDataMixin: |
| """Base class for data containers.""" |
|
|
| @property |
| def n_controls(self) -> int: |
| """Returns the number of control covariate values.""" |
| return len(self.split_idx_to_covariates) |
|
|
| @property |
| def n_perturbations(self) -> int: |
| """Returns the number of perturbation covariate combinations.""" |
| return len(self.perturbation_idx_to_covariates) |
|
|
| @property |
| def n_perturbation_covariates(self) -> int: |
| """Returns the number of perturbation covariates.""" |
| return len(self.condition_data) |
|
|
| def _format_params(self, fmt: Callable[[Any], str]) -> str: |
| params = { |
| "n_controls": self.n_controls, |
| "n_perturbations": self.n_perturbations, |
| } |
| return ", ".join(f"{name}={fmt(val)}" for name, val in params.items()) |
|
|
| def __repr__(self) -> str: |
| return f"{self.__class__.__name__}[{self._format_params(repr)}]" |
|
|
|
|
| @dataclass |
| class ConditionData(BaseDataMixin): |
| """Data container containing condition embeddings. |
| |
| Parameters |
| ---------- |
| condition_data |
| Dictionary with embeddings for conditions. |
| max_combination_length |
| Maximum number of covariates in a combination. |
| null_value |
| Token to use for masking `null_value`. |
| data_manager |
| Data manager used to generate the data. |
| condition_data_id |
| Mapping from perturbation index to condition ID. |
| """ |
|
|
| condition_data: dict[str, ArrayLike] |
| max_combination_length: int |
| perturbation_idx_to_covariates: dict[int, tuple[str, ...]] |
| perturbation_idx_to_id: dict[int, Any] |
| null_value: Any |
| data_manager: Any |
| condition_data_id: jnp.ndarray |
|
|
| @dataclass |
| class TrainingData(BaseDataMixin): |
| """Training data. |
| |
| Parameters |
| ---------- |
| cell_data |
| The representation of cell data, e.g. PCA of gene expression data. |
| split_covariates_mask |
| Mask of the split covariates. |
| split_idx_to_covariates |
| Dictionary explaining values in ``split_covariates_mask``. |
| perturbation_covariates_mask |
| Mask of the perturbation covariates. |
| perturbation_idx_to_covariates |
| Dictionary explaining values in ``perturbation_covariates_mask``. |
| condition_data |
| Dictionary with embeddings for conditions. |
| control_to_perturbation |
| Mapping from control index to target distribution indices. |
| max_combination_length |
| Maximum number of covariates in a combination. |
| data_manager |
| The data manager |
| condition_data_id |
| Mapping from perturbation index to condition ID. |
| """ |
|
|
| cell_data: jax.Array |
| split_covariates_mask: jax.Array |
| split_idx_to_covariates: dict[int, tuple[Any, ...]] |
| perturbation_covariates_mask: jax.Array |
| perturbation_idx_to_covariates: dict[ |
| int, tuple[str, ...] |
| ] |
| perturbation_idx_to_id: dict[int, Any] |
| condition_data: dict[str, ArrayLike] |
| control_to_perturbation: dict[int, ArrayLike] |
| max_combination_length: int |
| null_value: Any |
| data_manager: Any |
| condition_data_id: jnp.ndarray |
| cell_data_id: Any |
|
|
|
|
| @dataclass |
| class ValidationData(BaseDataMixin): |
| """Data container for the validation data. |
| |
| Parameters |
| ---------- |
| cell_data |
| The representation of cell data, e.g. PCA of gene expression data. |
| split_covariates_mask |
| Mask of the split covariates. |
| split_idx_to_covariates |
| Dictionary explaining values in ``split_covariates_mask``. |
| perturbation_covariates_mask |
| Mask of the perturbation covariates. |
| perturbation_idx_to_covariates |
| Dictionary explaining values in ``perturbation_covariates_mask``. |
| condition_data |
| Dictionary with embeddings for conditions. |
| control_to_perturbation |
| Mapping from control index to target distribution indices. |
| max_combination_length |
| Maximum number of covariates in a combination. |
| data_manager |
| The data manager |
| condition_data_id |
| Mapping from perturbation index to condition ID. |
| n_conditions_on_log_iteration |
| Number of conditions to use for computation callbacks at each logged iteration. |
| If :obj:`None`, use all conditions. |
| n_conditions_on_train_end |
| Number of conditions to use for computation callbacks at the end of training. |
| If :obj:`None`, use all conditions. |
| """ |
|
|
| cell_data: jax.Array |
| split_covariates_mask: jax.Array |
| split_idx_to_covariates: dict[int, tuple[Any, ...]] |
| perturbation_covariates_mask: jax.Array |
| perturbation_idx_to_covariates: dict[ |
| int, tuple[str, ...] |
| ] |
| perturbation_idx_to_id: dict[int, Any] |
| condition_data: dict[str, ArrayLike] |
| control_to_perturbation: dict[int, jax.Array] |
| max_combination_length: int |
| null_value: Any |
| data_manager: Any |
| condition_data_id: jnp.ndarray |
| n_conditions_on_log_iteration: int | None = None |
| n_conditions_on_train_end: int | None = None |
|
|
|
|
| @dataclass |
| class PredictionData(BaseDataMixin): |
| """Data container to perform prediction. |
| |
| Parameters |
| ---------- |
| src_data |
| Dictionary with data for source cells. |
| condition_data |
| Dictionary with embeddings for conditions. |
| control_to_perturbation |
| Mapping from control index to target distribution indices. |
| covariate_encoder |
| Encoder for the primary covariate. |
| max_combination_length |
| Maximum number of covariates in a combination. |
| null_value |
| Token to use for masking ``null_value``. |
| condition_data_id |
| Mapping from condition name to condition ID embedding. |
| """ |
|
|
| cell_data: jax.Array |
| split_covariates_mask: jax.Array |
| split_idx_to_covariates: dict[int, tuple[Any, ...]] |
| perturbation_idx_to_covariates: dict[ |
| int, tuple[str, ...] |
| ] |
| perturbation_idx_to_id: dict[int, Any] |
| condition_data: dict[str, ArrayLike] |
| control_to_perturbation: dict[int, ArrayLike] |
| max_combination_length: int |
| null_value: Any |
| data_manager: Any |
| condition_data_id: jnp.ndarray |
|
|