alphapepttools.pp.nanlog

Contents

alphapepttools.pp.nanlog#

alphapepttools.pp.nanlog(adata, base=2, verbosity=1, layer=None, *, copy=False)#

Logarithmize a data matrix.

Apply arbitrary base logarithm transformation to AnnData.X, replacing invalid values with np.nan. Similar to the underlying numpy log functions, invalid values are replaced with np.nan, but a more detailed summary of which values were replaced is provided.

Current invalid values include:
  • NaN values

  • Zero values

  • Negative values

  • Positive infinity

  • Negative infinity

Parameters:
  • adata (AnnData) – Input data; negatives and/or zeros are converted to np.nan

  • base (int (default: 2)) – Base of the logarithm. Defaults to 2 (log2).

  • verbosity (int (default: 1)) – If 1, log warnings for invalid values found in the data.

  • layer (str | None (default: None)) – Name of the layer to transform. If None (default), the data matrix X is used.

  • copy (bool (default: False)) – Whether to return a modified copy (True) of the anndata object. If False (default) modifies the object inplace

Return type:

AnnData

Returns:

Log-transformed data with invalid values replaced by np.nan. If copy=False modifies the anndata object at layer inplace and returns None. If copy=True, returns a modified copy.

Examples

Apply log transformation with different bases:

import anndata as ad
import numpy as np
import pandas as pd
from alphapepttools.pp.transform import nanlog

# Create sample data with some invalid values
adata = ad.AnnData(
    X=np.array([[100, 0, 10], [50, -5, 20], [200, 100, 30]]),
    obs=pd.DataFrame({"sample": ["S1", "S2", "S3"]}),
    var=pd.DataFrame(index=["protein1", "protein2", "protein3"]),
)

# Apply log2 transformation (default)
nanlog(adata)
# Zero and negative values are replaced with NaN

# Apply log10 transformation on a layer
adata.layers["raw"] = adata.X.copy()
nanlog(adata, base=10, layer="raw")

# Return a copy instead of modifying inplace
adata_log = nanlog(adata, base=2, copy=True)