mofaflex.terms.MofaFlex#

class mofaflex.terms.MofaFlex(n_factors, factor_prior='Normal', weight_prior='Normal', nonnegative_factors=False, nonnegative_weights=False, guiding_vars_obs_keys=None, guiding_vars_likelihoods='Normal', guiding_vars_scales=1.0, init_factors='random', init_scale=0.1)#

A MOFA-like term representing the product of two low-rank matrices.

The factor matrix has dimensions n_samples x n_total_factors, the weight matrix has dimensions n_total_factors x n_features. See the in-depth model description for details.

Parameters:
  • n_factors (int) – Number of latent factors.

  • factor_prior (Union[Mapping[str | Sequence[str], Union[Literal['Constant', 'GaussianProcess', 'GSFA', 'Horseshoe', 'Normal', 'Laplace', 'SpikeSlab'], Prior]], Literal['Constant', 'GaussianProcess', 'GSFA', 'Horseshoe', 'Normal', 'Laplace', 'SpikeSlab'], Prior] (default: 'Normal')) – Factor priors for each group (if dict) or for all groups (otherwise). The dictionary keys may be either strings, representing individual groups, or tuples of strings, in which case the corresponding value will be used for all groups named in the tuple.

  • weight_prior (Union[Mapping[str | Sequence[str], Union[Literal['Constant', 'GaussianProcess', 'Horseshoe', 'InformedHorseshoe', 'Normal', 'Laplace', 'SpikeSlab'], Prior]], Literal['Constant', 'GaussianProcess', 'Horseshoe', 'InformedHorseshoe', 'Normal', 'Laplace', 'SpikeSlab'], Prior] (default: 'Normal')) – Weight priors for each group (if dict) or for all groups (otherwise). The dictionary keys may be either strings, representing individual views, or tuples of strings, in which case the corresponding value will be used for all views named in the tuple.

  • nonnegative_factors (Mapping[str, bool] | bool (default: False)) – Non-negativity constraints for factors for each group (if dict) or for all groups (if bool).

  • nonnegative_weights (Mapping[str, bool] | bool (default: False)) – Non-negativity constraints for weights for each view (if dict) or for all views (if bool).

  • guiding_vars_obs_keys (str | Sequence[str] | Mapping[str, Mapping[str, str]] | None (default: None)) – Keys of .obs attribute of each AnnData object that contains guiding variable values.

  • guiding_vars_likelihoods (Union[Mapping[str, str], Literal['Normal', 'Categorical', 'Bernoulli'], None] (default: 'Normal')) – Likelihood for each guiding variable (if dict) or for all guiding variables (if str).

  • guiding_vars_scales (Mapping[str, float] | float (default: 1.0)) – Scale for the likelihood of each guiding variable, to put more or less emphasis on them during training.

  • init_factors (Union[float, Literal['random', 'orthogonal', 'pca']] (default: 'random')) – Initialization method for factors.

  • init_scale (float (default: 0.1)) – Initialization scale of Normal distribution for factors.

Attributes table#

factor_covariates

Covariates for each group.

factor_covariates_names

Covariate names for each group where they could be inferred from the input.

factor_gp_group_correlation

Between-group correlation for each factor.

factor_gp_lengthscale

Inferred lengthscales for each factor.

factor_gp_scale

Inferred variance scales (smoothness) for each factor.

factor_names

Factor names.

factor_order

Ordering of factors by explained variance (highest to lowest).

n_factors

Number of unguided factors.

n_guided_factors

Number of guided factors.

n_informed_factors

Number of informed factors.

n_total_factors

Total number of factors.

warped_factor_covariates

Time-warped covariates for each group, if dynamic time warping was enabled.

warped_weight_covariates

Time-warped covariates for each group, if dynamic time warping was enabled.

weight_covariates

Covariates for each group.

weight_covariates_names

Covariate names for each group where they could be inferred from the input.

weight_gp_group_correlation

Between-group correlation for each factor.

weight_gp_lengthscale

Inferred lengthscales for each factor.

weight_gp_scale

Inferred variance scales (smoothness) for each factor.

Methods table#

get_annotations([ordered])

Annotation matrices for each view.

get_factor_gps([moment, x, batch_size, ordered])

Get all latent functions.

get_factors([moment, ordered, sparse_type])

Get the factor matrices Z for each group.

get_perturbation_effects([ordered])

The perturbation effect matrix \(\mat\beta\).

get_posterior_inclusion_probabilities([ordered])

The posterior inclusion probabilities \(p\).

get_significant_annotations()

Get the results of significance testing of annotations against factors.

get_sparse_factor_probabilities([ordered])

The posterior probabilities \(\theta\) that the value is sampled from the foreground distribution.

get_sparse_weight_probabilities([ordered])

The posterior probabilities \(\theta\) that the value is sampled from the foreground distribution.

get_weight_gps([moment, x, batch_size, ordered])

Get all latent functions.

get_weights([moment, ordered, sparse_type])

Get the weight matrices W for each view.

Attributes#

MofaFlex.factor_covariates#

Covariates for each group.

Important

This property is only available when using the GaussianProcess prior.

MofaFlex.factor_covariates_names#

Covariate names for each group where they could be inferred from the input.

Important

This property is only available when using the GaussianProcess prior.

MofaFlex.factor_gp_group_correlation#

Between-group correlation for each factor.

Important

This property is only available when using the GaussianProcess prior.

MofaFlex.factor_gp_lengthscale#

Inferred lengthscales for each factor.

Important

This property is only available when using the GaussianProcess prior.

MofaFlex.factor_gp_scale#

Inferred variance scales (smoothness) for each factor.

Important

This property is only available when using the GaussianProcess prior.

MofaFlex.factor_names#

Factor names.

MofaFlex.factor_order#

Ordering of factors by explained variance (highest to lowest).

MofaFlex.n_factors#

Number of unguided factors.

MofaFlex.n_guided_factors#

Number of guided factors.

MofaFlex.n_informed_factors#

Number of informed factors.

Important

This property is only available when using the InformedHorseshoe prior.

MofaFlex.n_total_factors#

Total number of factors.

MofaFlex.warped_factor_covariates#

Time-warped covariates for each group, if dynamic time warping was enabled.

Important

This property is only available when using the GaussianProcess prior.

MofaFlex.warped_weight_covariates#

Time-warped covariates for each group, if dynamic time warping was enabled.

Important

This property is only available when using the GaussianProcess prior.

MofaFlex.weight_covariates#

Covariates for each group.

Important

This property is only available when using the GaussianProcess prior.

MofaFlex.weight_covariates_names#

Covariate names for each group where they could be inferred from the input.

Important

This property is only available when using the GaussianProcess prior.

MofaFlex.weight_gp_group_correlation#

Between-group correlation for each factor.

Important

This property is only available when using the GaussianProcess prior.

MofaFlex.weight_gp_lengthscale#

Inferred lengthscales for each factor.

Important

This property is only available when using the GaussianProcess prior.

MofaFlex.weight_gp_scale#

Inferred variance scales (smoothness) for each factor.

Important

This property is only available when using the GaussianProcess prior.

Methods#

MofaFlex.get_annotations(ordered=False)#

Annotation matrices for each view.

Parameters:

ordered (bool (default: False)) – Whether to return the factors ordered by explained variance (highest to lowest).

Return type:

Mapping[str, DataFrame]

Important

This method is only available when using the InformedHorseshoe prior.

MofaFlex.get_factor_gps(moment='mean', x=None, batch_size=None, ordered=False)#

Get all latent functions.

Parameters:
  • moment (Literal['mean', 'std'] (default: 'mean')) – Which moment of the posterior distribution to return.

  • x (Mapping[str, ndarray | Tensor] | None (default: None)) – Covariate values for each group. If None, will return latent function values at covariate coordinates used for training.

  • batch_size (int | None (default: None)) – Minibatch size. Only has an effect if x is not None. Defaults to the minibatch size used for training.

  • ordered (bool (default: False)) – Whether to return the factors ordered by explained variance (highest to lowest).

Return type:

Mapping[str, DataFrame]

Important

This method is only available when using the GaussianProcess prior.

MofaFlex.get_factors(moment='mean', ordered=False, sparse_type='mix')#

Get the factor matrices Z for each group.

Parameters:
  • moment (Literal['mean', 'std'] (default: 'mean')) – Which moment of the posterior distribution to return.

  • ordered (bool (default: False)) – Whether to return the factors ordered by explained variance (highest to lowest).

  • sparse_type (Literal['raw', 'mix', 'thresh'] (default: 'mix')) –

    How to handle sparsity when using the spike and slab prior.

    • raw: Do nothing, return inferred values for all entries.

    • mix: Return the corresponding moment of a mixture distribution of two Normal distributions: One centered at 0 and the other centered at the inferred non-sparse value. The mixture is weighted by the inferred sparsity probability.

    • thresh: Set all values with a sparsity probablity > 0.5 to 0.

    Important

    This argument is only available when using the GSFA prior.

Return type:

dict[str, DataFrame | AnnData]

MofaFlex.get_perturbation_effects(ordered=False)#

The perturbation effect matrix \(\mat\beta\).

Parameters:

ordered (bool (default: False)) – Whether to return the factors ordered by explained variance (highest to lowest).

Return type:

Mapping[str, DataFrame]

Important

This method is only available when using the GSFA prior.

MofaFlex.get_posterior_inclusion_probabilities(ordered=False)#

The posterior inclusion probabilities \(p\).

Parameters:

ordered (bool (default: False)) – Whether to return the factors ordered by explained variance (highest to lowest).

Return type:

Mapping[str, DataFrame]

Important

This method is only available when using the GSFA prior.

MofaFlex.get_significant_annotations() Mapping[str, DataFrame]#

Get the results of significance testing of annotations against factors.

The significance testing is an implementation of PCGSE [FLM15]. While originally intended to assign annotations to uninformed factors, here it is used as a diagnostic plot to find factors that are mismatched to their annotations.

Returns:

PCGSE results for each view or None if the model does not have prior annotations.

Important

This method is only available when using the InformedHorseshoe prior.

MofaFlex.get_sparse_factor_probabilities(ordered=False)#

The posterior probabilities \(\theta\) that the value is sampled from the foreground distribution.

Parameters:

ordered (bool (default: False)) – Whether to return the factors ordered by explained variance (highest to lowest).

Return type:

Mapping[str, DataFrame]

Important

This method is only available when using the SpikeSlab prior.

MofaFlex.get_sparse_weight_probabilities(ordered=False)#

The posterior probabilities \(\theta\) that the value is sampled from the foreground distribution.

Parameters:

ordered (bool (default: False)) – Whether to return the factors ordered by explained variance (highest to lowest).

Return type:

Mapping[str, DataFrame]

Important

This method is only available when using the SpikeSlab prior.

MofaFlex.get_weight_gps(moment='mean', x=None, batch_size=None, ordered=False)#

Get all latent functions.

Parameters:
  • moment (Literal['mean', 'std'] (default: 'mean')) – Which moment of the posterior distribution to return.

  • x (Mapping[str, ndarray | Tensor] | None (default: None)) – Covariate values for each group. If None, will return latent function values at covariate coordinates used for training.

  • batch_size (int | None (default: None)) – Minibatch size. Only has an effect if x is not None. Defaults to the minibatch size used for training.

  • ordered (bool (default: False)) – Whether to return the factors ordered by explained variance (highest to lowest).

Return type:

Mapping[str, DataFrame]

Important

This method is only available when using the GaussianProcess prior.

MofaFlex.get_weights(moment='mean', ordered=False, sparse_type='mix')#

Get the weight matrices W for each view.

Parameters:
  • moment (Literal['mean', 'std'] (default: 'mean')) – Which moment of the posterior distribution to return.

  • ordered (bool (default: False)) – Whether to return the factors ordered by explained variance (highest to lowest).

  • sparse_type (Literal['raw', 'mix', 'thresh'] (default: 'mix')) –

    How to handle sparsity when using the spike and slab prior.

    • raw: Do nothing, return inferred values for all entries.

    • mix: Return the corresponding moment of a mixture distribution of two Normal distributions: One centered at 0 and the other centered at the inferred non-sparse value. The mixture is weighted by the inferred sparsity probability. This is what MOFA does.

    • thresh: Set all values with a sparsity probablity > 0.5 to 0.

    Important

    This argument is only available when using the SpikeSlab prior.

Return type:

dict[str, DataFrame]