cellflow.model.CellFlow.prepare_data

CellFlow.prepare_data(sample_rep, control_key, perturbation_covariates, perturbation_covariate_reps=None, sample_covariates=None, sample_covariate_reps=None, split_covariates=None, max_combination_length=None, null_value=0.0)[source]

Prepare the dataloader for training from adata.

Parameters:
  • sample_rep (str) – Key in obsm of cellflow.model.CellFlow.adata where the sample representation is stored or 'X' to use X.

  • control_key (str) – Key of a boolean column in obs of cellflow.model.CellFlow.adata that defines the control samples.

  • perturbation_covariates (dict[str, Sequence[str]]) –

    A dictionary where the keys indicate the name of the covariate group and the values are keys in obs of cellflow.model.CellFlow.adata. The corresponding columns can be of the following types:

    • categorial: The column contains categories whose representation is stored in uns, see 'perturbation_covariate_reps'.

    • boolean: The perturbation is present or absent.

    • numeric: The perturbation is given as a numeric value, possibly linked to a categorical perturbation, e.g. dosages for a drug.

    If multiple groups are provided, the first is interpreted as the primary perturbation and the others as covariates corresponding to these perturbations.

  • perturbation_covariate_reps (dict[str, str] | None) – A dict where the keys indicate the name of the covariate group and the values are keys in uns storing a dictionary with the representation of the covariates.

  • sample_covariates (Sequence[str] | None) – Keys in obs indicating sample covariates. Sample covariates are defined such that each cell has only one value for each sample covariate (in constrast to 'perturbation_covariates' which can have multiple values for each cell). If None, no sample

  • sample_covariate_reps (dict[str, str] | None) – A dictionary where the keys indicate the name of the covariate group and the values are keys in uns storing a dictionary with the representation of the covariates.

  • split_covariates (Sequence[str] | None) – Covariates in obs to split all control cells into different control populations. The perturbed cells are also split according to these columns, but if any of the 'split_covariates' has a representation which should be incorporated by the model, the corresponding column should also be used in 'perturbation_covariates'.

  • max_combination_length (int | None) – Maximum number of combinations of primary 'perturbation_covariates'. If None, the value is inferred from the provided 'perturbation_covariates' as the maximal number of perturbations a cell has been treated with.

  • null_value (float) – Value to use for padding to 'max_combination_length'.

Return type:

None

Returns:

: Updates the following fields:

Example

Consider the case where we have combinations of drugs along with dosages, saved in obs as columns drug_1 and drug_2 with three different drugs DrugA, DrugB, and DrugC, and dose_1 and dose_2 for their dosages, respectively. We store the embeddings of the drugs in uns under the key drug_embeddings, while the dosage columns are numeric. Moreover, we have a covariate cell_type with values cell_typeA and cell_typeB, with embeddings stored in uns under the key cell_type_embeddings. Note that we then also have to set 'split_covariates' as we assume we have an unperturbed population for each cell type.

perturbation_covariates = {{"drug": ("drug_1", "drug_2"), "dose": ("dose_1", "dose_2")}}
perturbation_covariate_reps = {"drug": "drug_embeddings"}
adata.uns["drug_embeddings"] = {
    "drugA": np.array([0.1, 0.2, 0.3]),
    "drugB": np.array([0.4, 0.5, 0.6]),
    "drugC": np.array([-0.2, 0.3, 0.0]),
}

sample_covariates = {"cell_type": "cell_type_embeddings"}
adata.uns["cell_type_embeddings"] = {
    "cell_typeA": np.array([0.0, 1.0]),
    "cell_typeB": np.array([0.0, 2.0]),
}

split_covariates = ["cell_type"]

cf = CellFlow(adata)
cf = cf.prepare_data(
    sample_rep="X",
    control_key="control",
    perturbation_covariates=perturbation_covariates,
    perturbation_covariate_reps=perturbation_covariate_reps,
    sample_covariates=sample_covariates,
    sample_covariate_reps=sample_covariate_reps,
    split_covariates=split_covariates,
)