cell2net.preprocessing.binarize#

cell2net.preprocessing.binarize(data, atac_mod='atac', layer=None)#

Binarize the data matrix in an AnnData or MuData object

This function converts non-zero values in the specified data matrix (either X or a specified layer) to 1, effectively binarizing the data. It supports both dense and sparse matrix formats.

Parameters:
  • data (AnnData | MuData) – The input data object containing the matrix to be binarized. If a MuData object is provided, the atac_mod parameter specifies which modality to use.

  • atac_mod (str (default: 'atac')) – The modality to use when data is a MuData object. Defaults to “atac”.

  • layer (str | None (default: None)) – The specific layer of the AnnData object to binarize. If None, adata.X is binarized. Defaults to None.

Return type:

None

Returns:

The input object is modified in place, with the specified matrix binarized.

Raises:

TypeError – If the input data is not an AnnData or MuData object, or if the specified atac_mod is not found in the MuData object.

Notes

  • For sparse matrices, this function modifies the .data attribute directly to ensure efficient processing without densifying the matrix.

  • For dense matrices, non-zero values are updated directly in place.

Examples

>>> from anndata import AnnData
>>> import numpy as np
>>> from scipy.sparse import csr_matrix
>>> import cell2net as cn
>>> # Example with a dense matrix
>>> X = np.array([[0, 2, 0], [3, 0, 1]])
>>> adata = AnnData(X)
>>> cn.pp.binarize(adata)
>>> print(adata.X)
>>> # Example with a sparse matrix
>>> X_sparse = csr_matrix([[0, 2, 0], [3, 0, 1]])
>>> adata = AnnData(X_sparse)
>>> binarize(adata)
>>> print(adata.X.toarray())
>>> # Example with a specified layer
>>> adata.layers["counts"] = X
>>> binarize(adata, layer="counts")
>>> print(adata.layers["counts"])