alphapepttools.pp.normalize

Contents

alphapepttools.pp.normalize#

alphapepttools.pp.normalize(adata, layer=None, strategy='total_mean', group_column=None, key_added=None, *, inplace=True)#

Normalize measured counts per sample

Parameters:
  • adata (AnnData) – Count data

  • layer (str | None (default: None)) – Layer that will be normalized. If None uses anndata.AnnData.X

  • strategy (Literal['total_mean', 'total_median'] (default: 'total_mean')) –

    Normalization strategy

    • total_mean The intensity of each feature is adjusted by a normalizing factor so that the

    total sample intensity is equal to the mean of the total sample intensities across all samples - total_median The intensity of each feature is adjusted by a normalizing factor so that the total sample intensity is equal to the median of the total sample intensities across all samples

  • group_column (str | None (default: None)) – Column name in adata.obs defining groups for group-wise normalization. If None (default), computes statistics across all samples. If specified, computes statistics separately for each group. This is useful when working with data from different batches with different intensity distributions.

  • key_added (str | None (default: None)) – If not None, adds normalization factors to column in adata.obs

  • inplace (bool (default: True)) – If True (default), modifies adata inplace and returns None. If False, returns a modified copy of adata.

  • copy –

    Deprecated since version 0.5.0: Use inplace instead. copy will be removed in version 0.6.0.

Return type:

AnnData | None

Returns:

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

Examples

Create an AnnData object with intensity data:

adata = ad.AnnData(X=np.array([[0.8, 1.0], [2.0, 0.0], [0.0, 2.0]]))
adata.X
> array([[0.8, 1.0],
        [2.0, 0.0],
        [0.0, 2.0]])

The anndata object gets normalized in place. Per default, the .X attribute will be modified:

normalize(adata)
adata.X
> array([[0.85925926, 1.07407407],
        [1.93333333, 0.        ],
        [0.        , 1.93333333]])

Alternatively, we can normalize a different layer:

adata.layers["normalized"] = adata.X.copy()
normalize(adata, strategy="total_mean", layer="normalized")
adata.X
> array([[0.8, 1.0],
        [2.0, 0.0],
        [0.0, 2.0]])
adata.layers["normalized"]
> array([[0.85925926, 1.07407407],
        [1.93333333, 0.        ],
        [0.        , 1.93333333]])

Or, with inplace=False, we leave adata unchanged and return a normalized object:

new_adata = normalize(adata, inplace=False)