alphapepttools.pp.scale_and_center

alphapepttools.pp.scale_and_center#

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

Scale and center features

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

  • scaler (Literal['standard', 'robust'] (default: 'standard')) –

    Sklearn scaler to use. Available scalers are
    • standard: Mean centering and scaling by standard deviation

    • robust: Median centering and scaling by interquartile range.

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

  • center (bool (default: True)) –

    Whether to center the feature distribution at zero. If True:

    • standard: Mean-centering of the feature distribution.

    • robust: Median-centering of the feature distribution.

    Not applied if set to False.

  • scale (bool (default: True)) –

    Whether to scale the feature distribution. If True:

    • standard: Divides the feature distribution by its standard deviation to unit variance.

    • robust: Divides the feature distribution by its interquartile range, i.e. range between quantile (0.25, 0.75).

    Not applied if set to False.

  • 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 | None

Returns:

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
import alphapepttools as apt

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)
apt.pp.scale_and_center(adata, scaler="standard")

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

You can selectively center or scale the layer:

# Only apply median centering
apt.pp.scale_and_center(adata, scaler="robust", center=True, scale=False)

# Only scale, do not center
apt.pp.scale_and_center(adata, scaler="standard", center=False, scale=True)