liana.utils.get_variable_loadings#
- liana.utils.get_variable_loadings(adata=None, varm_key=None, view_sep=None, variable_sep=None, pair_sep=None, var_names=None, pair_names=None, drop_columns=True, loadings=None)#
Extract variable loadings from an AnnData object.
- Parameters:
adata (
AnnData|MuData(default:None)) – Annotated data object.varm_key (
str(default:None)) – Key to use when extracting variable loadings frommdata.varm. Ignored whenloadingsis provided.view_sep (
str|None(default:None)) – Separator to use when splitting view:variable names into view and variablevariable_sep (
str|None(default:None)) – Separator to use when splitting variable names intovar_namespair_sep (
str|None(default:None)) – Separator to use when splitting view names intopair_namesvar_names (
list(default:None)) – Variable names given to the splitted variable (‘ligand_complex’ and ‘receptor_complex’ by default)pair_names (
list(default:None)) – Variable names given to the splitted pair (‘source’ and ‘target’ by default)drop_columns (
bool(default:True)) – If True, drop theview:variablecolumnloadings (
DataFrame|dict|None(default:None)) – Pre-extracted loadings to use instead of reading fromadata.varm. Either a features-by-factorsDataFrame, or a dict of per-view features-by-factors DataFrames (e.g. the output of a MOFA-Flex model’sget_weights()), which is concatenated feature-wise. When provided,adataandvarm_keyare ignored and the existing factor column names are preserved.
- Return type:
DataFrame- Returns:
Returns a pandas DataFrame with the variable loadings for the specified index.
- Raises:
ValueError – If
varm_keynot found in.varm(whenloadingsis not provided)
Examples
varm_keypoints at the feature by factor matrix written by a factorization model – hereliana.multi.nmf()on the local scores ofliana.method.bivariate, whosevar_namesare'ligand^receptor':>>> import liana as li >>> adata = li.testing.generate_toy_spatial() >>> lrdata = li.mt.bivariate(adata, resource_name='consensus', ... local_name='cosine', global_name=None, ... n_perms=None) >>> li.multi.nmf(lrdata, n_components=3, random_state=0) >>> loadings = li.ut.get_variable_loadings(lrdata, varm_key='NMF_H', ... variable_sep='^')
The separators split those composite names back into their parts, and the rows are ordered by the absolute loading on the first factor:
>>> loadings.head(3).round(3) ligand_complex receptor_complex Factor1 Factor2 Factor3 6 HLA-DPB1 CD4 2.518 0.0 0.114 4 HLA-DQB1 CD4 2.498 0.0 0.034 0 HLA-DRA CD4 2.486 0.0 0.160
Views built by
liana.multi.lrs_to_views()name their variables'source&target:ligand^receptor', whichview_sep=':',variable_sep='^'andpair_sep='&'split in the same way.