liana.ms.nmf#
- liana.ms.nmf(adata=None, df=None, n_components=None, k_range=range(1, 11), use_raw=False, layer=None, inplace=True, verbose=False, **kwargs)#
Fits NMF to an AnnData object.
- Parameters:
- adata
AnnData|None(default:None) Annotated data object.
- df
DataFrame|None(default:None) Alternative input for data as a
DataFrame, only used ifadatais None.- n_components
int|None(default:None) Number of components to use. If None, the number of components is estimated using the elbow method.
- k_range
range(default:range(1, 11)) Range of components to test. Default: range(1, 10).
- use_raw
bool(default:False) Whether to use the
.rawattribute of adata. Defaults to False (uses.X).- layer
str|None(default:None) Layer in anndata.AnnData.layers to use. If None, use anndata.AnnData.X.
- inplace
bool(default:True) Whether to store results in place, or else to return them.
- **kwargs
object Keyword arguments to pass to
sklearn.decomposition.NMF.
- adata
- Return type:
tuple[ndarray,ndarray,DataFrame|None,int|None] |None- Returns:
If inplace is True, it will add
NMF_WandNMF_Hto theadata.obsmandadata.varm. If n_components is None, it will also addnfm_errorsandnfm_ranktoadata.uns.If inplace is False, it will return
WandH, and if n_components is None, it will also returnerrorsandn_components. If n_components is None and inplace,errorsandn_componentswill be assigned toadata.uns. Ifdfis provided, inplace is always False.- Raises:
ValueError – If
adatais provided but it’s not a valid instance of anAnnDataobject or neither anAnnDataorDataFrameintance is provided as input
Examples
nmfexpects a non-negative matrix – typically the local ligand-receptor scores fromliana.mt.bivariate:>>> import liana as li >>> adata = li.ds.generate_toy_spatial() >>> lrdata = li.mt.bivariate(adata, resource_name="consensus", local_name="cosine", global_name=None, n_perms=None) >>> li.ms.nmf(lrdata, n_components=3, random_state=0)
Leaving
n_componentsasNoneinstead estimates the rank withliana.ms.estimate_elbow()and draws the elbow plot.Read the factors out with
liana.ms.get_factor_scores()andliana.ms.get_variable_loadings().