topomics.models.BaseTopicModel#
- class topomics.models.BaseTopicModel(mdata, modalities=None)#
Base class for all models in the topomics package.
This class provides a common interface and shared functionality for all models. It can be extended by specific model implementations.
Attributes table#
Methods table#
|
Validate and process the input data. |
Standardize and validate modality keys in data_dict. |
|
Clear the cached metrics. |
|
|
Compute SHARE-Topic–style cross-modal interaction matrix P_{a,b} |
|
Fit the model to the provided data. |
Get the cell-topic matrix Θ (C × K). |
|
|
Compute mean entropy of cell-topic distributions. |
|
Get the feature-topic matrix Φ (K × G). |
|
Compute log-likelihood for each modality separately. |
|
Get normalized mixing weights showing how much each modality contributes to topic assignments. |
|
Compute perplexity (reconstruction quality). |
|
Compute perplexity for each modality separately. |
|
Get top N features for each topic in a specific modality. |
|
Compute topic diversity as average pairwise cosine distance. |
|
Predict using the fitted model on the provided data. |
Attributes#
Methods#
- BaseTopicModel.check_input(mdata, modalities)#
Validate and process the input data.
Checks that data are adata or mudata objects, and that the modalities are correctly specified.
- BaseTopicModel.check_modalities_names()#
Standardize and validate modality keys in data_dict.
Maps various synonyms to ‘rna’, ‘protein’, or ‘chromatin’, and rebuilds data_dict with standardized keys.
- BaseTopicModel.clear_metric_cache()#
Clear the cached metrics.
Call this method after retraining the model to ensure metrics are recomputed with the updated parameters.
- BaseTopicModel.cross_modality_score(mod_a, mod_b, *, normalise=True, return_df=True)#
Compute SHARE-Topic–style cross-modal interaction matrix P_{a,b}
- Parameters:
- Return type:
- Returns:
P : shape (n_feat_a, n_feat_b) – interaction score between every feature of
mod_aand every feature ofmod_b
- BaseTopicModel.fit(data)#
Fit the model to the provided data.
- Parameters:
data (The input data to fit the model.)
- BaseTopicModel.get_cell_topic_dist()#
Get the cell-topic matrix Θ (C × K).
- Return type:
- Returns:
-Θ (
ndarray) Cell-topic matrix, where C is the number of cells and K is the number of topics.
- BaseTopicModel.get_entropy(normalised=True)#
Compute mean entropy of cell-topic distributions.
Higher entropy means topics are more evenly distributed across cells. This measures the uncertainty in topic assignments per cell.
- BaseTopicModel.get_feature_topic_dist(modality)#
Get the feature-topic matrix Φ (K × G).
- Parameters:
modality (
str) – The name of the modality for which to retrieve the feature-topic matrix.- Return type:
- Returns:
Φ : np.ndarray or pd.DataFrame Feature-topic matrix, where K is the number of topics and G is the number of features. If the modality has feature names, returns a DataFrame with those names.
- BaseTopicModel.get_likelihood_per_modality(**kwargs)#
Compute log-likelihood for each modality separately.
Higher is better.
- BaseTopicModel.get_modality_weights(**kwargs)#
Get normalized mixing weights showing how much each modality contributes to topic assignments.
Only applicable for multimodal models with mixture-of-experts or similar architectures. Returns weights in range [0, 1] that sum to 1 per cell. Higher weight = model relies more on that modality for inferring topics.
- BaseTopicModel.get_perplexity(**kwargs)#
Compute perplexity (reconstruction quality).
Lower is better. Perplexity = exp(-log_likelihood / N_tokens)
- Return type:
- Returns:
float Perplexity score
- BaseTopicModel.get_perplexity_per_modality(**kwargs)#
Compute perplexity for each modality separately.
Lower is better. Perplexity = exp(-log_likelihood / N_tokens)
- BaseTopicModel.get_top_features_per_topic(modality, n_features=10, return_scores=False)#
Get top N features for each topic in a specific modality.
- Parameters:
- Return type:
- Returns:
dict[str, list[str]] or dict[str, list[tuple[str, float]]] Dictionary mapping topic names (e.g., ‘topic_0’) to lists of top feature names or (feature_name, score) tuples.
- BaseTopicModel.get_topic_diversity(modality=None)#
Compute topic diversity as average pairwise cosine distance.
Higher values indicate more distinct topics. This metric measures how different the topic-feature distributions are from each other.
- Parameters:
modality (
str|None(default:None)) – If provided, compute diversity for this specific modality’s feature-topic distribution. If None, compute diversity averaged across all modalities (default: None).- Return type:
- Returns:
float Average pairwise cosine distance between topic distributions (0-1). Higher = more diverse/distinct topics.
- BaseTopicModel.predict(data)#
Predict using the fitted model on the provided data.
- Parameters:
data (The input data for prediction.)