cellina.make_counterfactual_adata

cellina.make_counterfactual_adata#

cellina.make_counterfactual_adata(adata, indices_basal, indices_counterfactual, spatial_column, precomputed=True, n_neighbours=50, random_state=0, connectivity_key='spatial_connectivities', cf_conn_key='spatial_connectivities_cf', cf_obsm_key='spatial_x_cf')#

Create a counterfactual AnnData keeping everything from the original except .obsm[spatial_column], which is replaced with counterfactual spatial neighbourhood features.

Parameters:
  • adata – Original AnnData.

  • indices_basal – Indices of basal/control cells to keep in .X and obs.

  • indices_counterfactual – Indices of cells to use as the counterfactual neighbourhood.

  • spatial_column – Key in .obsm where the spatial features are stored / written.

  • precomputed (bool (default: True)) – If True (default), sample rows from existing .obsm[spatial_column] of counterfactual cells (fast, uses precomputed features). If False, rebuild spatial features from scratch: all edges incident on seed nodes are removed from the original connectivity graph and replaced with bidirectional edges connecting each seed to nodes sampled from indices_counterfactual.

  • n_neighbours (int (default: 50)) – Number of neighbours to sample per basal cell from indices_counterfactual without replacement. Defaults to 50. When precomputed=False: sample exactly n_neighbours edges per seed.

  • random_state (int (default: 0)) – Seed for reproducibility.

  • connectivity_key (str (default: 'spatial_connectivities')) – Key in adata.obsp for the original spatial connectivity matrix. Only used when precomputed=False.

  • cf_conn_key (str (default: 'spatial_connectivities_cf')) – Key written to adata.obsp for the counterfactual connectivity matrix. Only used when precomputed=False.

  • cf_obsm_key (str (default: 'spatial_x_cf')) – Key written to adata.obsm for the counterfactual spatial features. Written in both precomputed=True and precomputed=False.

Returns:

: -adata_cf (AnnData)

Copy of original AnnData (basal cells only) with updated .obsm[spatial_column].