ocelli.tl.modality_weights#

ocelli.tl.modality_weights(adata: AnnData, modalities=None, out: str = 'weights', n_pairs: int = 1000, n_jobs: int = -1, random_state=None, verbose: bool = False, copy: bool = False)#

Compute multimodal weights for each cell

This function calculates cell-specific weights for each modality based on the distances between cells in the corresponding modality spaces. The weights are normalized to emphasize the contribution of each modality for each cell.

Note

It is necessary to run ocelli.pp.neighbors before using this function, as the computed nearest neighbors are required for estimating multimodal weights.

Parameters:
  • adata (anndata.AnnData) – The annotated data matrix.

  • modalities (list or None) – List of keys in adata.obsm storing modalities. If None, the list is taken from adata.uns[‘modalities’]. (default: None)

  • out (str) – Key in adata.obsm where the computed multimodal weights are saved. (default: ‘weights’)

  • n_pairs (int) – Number of cell pairs used to estimate empirical cumulative distribution functions (ECDFs) of distances. (default: 1000)

  • n_jobs (int) – Number of parallel jobs to use. If -1, all available CPUs are used. (default: -1)

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

  • verbose (bool) – Whether to print progress notifications. (default: False)

  • copy (bool) – Whether to return a copy of adata. If False, updates are made in-place. (default: False)

Returns:

  • If copy=False: Updates adata with the following field:
    • adata.obsm[out]: DataFrame containing cell-specific weights for each modality.

  • If copy=True: Returns a modified copy of adata with the weights.

Return type:

anndata.AnnData or None

Example:
import ocelli as oci
from anndata import AnnData
import numpy as np
import pandas as pd

# Example data
adata = AnnData(X=np.random.rand(100, 50))
adata.obsm['modality1'] = np.random.rand(100, 10)
adata.obsm['modality2'] = np.random.rand(100, 15)
adata.uns['modalities'] = ['modality1', 'modality2']

# Compute nearest neighbors
oci.pp.neighbors(adata, x=['modality1', 'modality2'], n_neighbors=20)

# Compute multimodal weights
oci.tl.modality_weights(adata, n_pairs=500, n_jobs=4, verbose=True)