mtopic.tl.export_params

Contents

mtopic.tl.export_params#

mtopic.tl.export_params(model, mdata, prefix=None, filter_topics=True, filter_threshold=0.01, normalize=True)#

Export topic-cell and feature-topic distributions to a MuData object and filter insignificant topics.

This function exports topic-cell distributions (gamma) and feature-topic distributions (lambda) from the given model into the obsm and varm attributes of a MuData object. Users can apply optional filtering to remove topics with probabilities below a specified threshold, which helps focus on meaningful patterns.

Parameters:
  • model (mtopic.tl.MTM or mtopic.tl.sMTM) – An instance of mtopic.tl.MTM or mtopic.tl.sMTM containing the parameters (gamma and lambda_) to be exported.

  • mdata (muon.MuData) – A MuData object to which the parameters will be exported.

  • prefix (str, optional) – A string prefix for keys under which the parameters will be stored in MuData. If None, no prefix is added. Default is None.

  • filter_topics (bool, optional) – Whether to filter topics based on their maximum probability across cells. If True, only topics with a maximum probability above filter_threshold are retained. Default is True.

  • filter_threshold (float, optional) – The threshold for filtering topics when filter_topics is True. Topics with a maximum probability below this threshold are removed. Default is 0.01.

  • normalize (bool, optional) – Whether to normalize the topic-cell distributions (gamma). If True, normalizes rows of gamma so that each row sums to 1. Default is True.

Returns:

None

Updates:
  • mdata.obsm[‘{prefix}_topics’]: A DataFrame containing topic-cell distributions for each cell/spot, optionally filtered.

  • mdata[modality].varm[‘{prefix}_signatures’]: A DataFrame containing feature-topic distributions for each modality, filtered to include only selected topics.

Example:
import mtopic

# Load MuData object
mdata = mtopic.read.h5mu("path/to/file.h5mu")

# Initialize MTM model
model = mtopic.tl.MTM(mdata, n_topics=20)

# Fit the model using Variational Inference
model.VI(n_iter=20)

# Export model parameters to MuData object
mtopic.tl.export_params(model, mdata)

# Access exported parameters
print(mdata.obsm['topics'])  # Topic-cell distributions
print(mdata['rna'].varm['signatures'])  # Feature-topic distributions for 'rna' modality
Notes:
  • Gamma (Topic-cell distributions): Stored in obsm as {prefix}_topics. Each row corresponds to a sample, and each column represents a topic.

  • Lambda (Feature-topic distributions): Stored in varm for each modality as {prefix}_signatures. Each column corresponds to a topic, and each row represents a feature.

  • If prefix is None, parameters are stored with keys ‘topics’ and ‘signatures’.