File size: 1,119 Bytes
0161e74 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | import anndata as ad
import pandas as pd
import numpy as np
def build_generated_anndata(
embeddings: np.ndarray, # shape: (N, D)
condition_id: np.ndarray, # shape: (N, 2)
pert_col: str = "perturbation", # first perturbation column name
pert_col2: str = "perturbation_2", # second perturbation (if combo)
combo_col: str = "pert_combo", # combined label
) -> ad.AnnData:
"""
Construct AnnData from model-generated embeddings and condition IDs.
"""
N = embeddings.shape[0]
if condition_id.shape[0] != N:
raise ValueError("condition_id and embeddings must have same first dimension.")
# obs: per-cell annotation
obs = pd.DataFrame({
pert_col: condition_id[:, 0],
pert_col2: condition_id[:, 1],
combo_col: [f"{a}_{b}" for a, b in condition_id],
}, index=[f"cell_{i}" for i in range(N)])
# construct AnnData
adata = ad.AnnData(
X=embeddings, # could also store in obsm
obs=obs
)
# also store embedding explicitly for plotting
adata.obsm["embedding"] = embeddings
return adata |