alphapepttools.pp.normalize

Contents

alphapepttools.pp.normalize#

alphapepttools.pp.normalize(adata, layer=None, strategy='total_mean', key_added=None, *, copy=False)#

Normalize measured counts per sample

Parameters:
  • adata (AnnData) – Count data

  • layer (Optional[str] (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

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

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

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

Example

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

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

normalize(adata)
adata.X
> np.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
# Unchanged
> array([
    [0.8, 1. ],
    [2. , 0. ],
    [0. , 2. ]
])

# Normalized
adata.layers["normalized"]
> np.array([
[0.85925926, 1.07407407],
[1.93333333, 0.        ],
[0.        , 1.93333333]]
)

Or we return a copy of the object

new_adata = normalize(adata, copy=True)