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 inobsmofcellflow.model.CellFlow.adatawhere the sample representation is stored or'X'to useX.control_key (
str) – Key of a boolean column inobsofcellflow.model.CellFlow.adatathat 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
obsofcellflow.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) – Adictwhere the keys indicate the name of the covariate group and the values are keys inunsstoring a dictionary with the representation of the covariates.sample_covariates (
Sequence[str] |None) – Keys inobsindicating 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). IfNone, no samplesample_covariate_reps (
dict[str,str] |None) – A dictionary where the keys indicate the name of the covariate group and the values are keys inunsstoring a dictionary with the representation of the covariates.split_covariates (
Sequence[str] |None) – Covariates inobsto 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'. IfNone, 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:
- Returns:
: Updates the following fields:
cellflow.model.CellFlow.data_manager- thecellflow.data.DataManagerobject.cellflow.model.CellFlow.train_data- the training data.
Example
Consider the case where we have combinations of drugs along with dosages, saved in
obsas columnsdrug_1anddrug_2with three different drugsDrugA,DrugB, andDrugC, anddose_1anddose_2for their dosages, respectively. We store the embeddings of the drugs inunsunder the keydrug_embeddings, while the dosage columns are numeric. Moreover, we have a covariatecell_typewith valuescell_typeAandcell_typeB, with embeddings stored inunsunder the keycell_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, )