alphapepttools.pp.scale_and_center

alphapepttools.pp.scale_and_center#

alphapepttools.pp.scale_and_center(adata, scaler='standard', layer=None, *, copy=False)#

Scale and center data.

Either use standard or robust scaling. ‘robust’ scaling relies on interquartile range and is more resistant to outliers. Scaling operates on columns only for now.

Parameters:
  • adata (AnnData) – AnnData object with data to scale.

  • scaler (str (default: 'standard')) – Sklearn scaler to use. Available scalers are ‘standard’ and ‘robust’.

  • layer (str | None (default: None)) – Name of the layer to scale. 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:

None | AnnData

Returns:

None | anndata.AnnData If copy=False modifies the anndata object at layer inplace and returns None. If copy=True, returns a modified copy.

Examples

Apply standard scaling to data:

import anndata as ad
import pandas as pd
import numpy as np
from alphapepttools.pp.data import scale_and_center

adata = ad.AnnData(
    X=np.array([[1, 10], [2, 20], [3, 30], [4, 40]]),
    obs=pd.DataFrame({"sample": ["S1", "S2", "S3", "S4"]}),
    var=pd.DataFrame(index=["protein1", "protein2"]),
)

# Standard scaling (in-place)
scale_and_center(adata, scaler="standard")

# Robust scaling on a specific layer
adata.layers["raw"] = adata.X.copy()
scale_and_center(adata, scaler="robust", layer="raw")