scverse / muon

muon is a multimodal omics Python framework
https://muon.scverse.org/
BSD 3-Clause "New" or "Revised" License
218 stars 31 forks source link

MOFA on multiple groups with single view #113

Open martinrohbeck opened 1 year ago

martinrohbeck commented 1 year ago

Hi there,

I am trying to run MOFA on a MuData object with a single view and multiple groups, i.e. all feature dimensions are shared across the groups. However, muon is throwing some error complaining about dimensions. ValueError: Value passed for key 'LFs' is of incorrect shape. Values of varm must match dimensions (1,) of parent. Value had shape (2000, 15) while it should have had (1000,).

To reproduce the error, run:

import muon as mu
import numpy as np
from mudata import MuData, AnnData

n1, n2, d = 100, 500, 1000

ad1 = AnnData(np.random.normal(size=(n1, d)))
ad2 = AnnData(np.random.normal(size=(n2, d)))

ad1.obs_names = [f"dat1-cell{i+1}" for i in range(n1)]
ad2.obs_names = [f"dat2-cell{i+1}" for i in range(n2)]
ad1.obs["group_id"] = "group_dat1"
ad2.obs["group_id"] = "group_dat2"

mdata = MuData({"dat1": ad1, "dat2": ad2}, axis=1)

mu.tl.mofa(mdata, groups_label="group_id", use_obs="union", n_factors=15, convergence_mode="fast")

I never used Muon/MuData before, so my mdata object might already be incorrectly constructed. Sorry, if this is the case. However, assuming the mdata object is as expected, the error might begin here: https://github.com/scverse/muon/blob/31a45d76f7a1d97eee34866e525788f960e7d504/muon/_core/tools.py#L178 where the value of M is set to 2, although it should likely be 1 - representing a single modality?

If you agree that this is indeed a bug, I'm happy to dig into it and open a PR.

I installed the latest version of muon from master, including muon 0.1.5 mudata 0.2.3 python 3.11.4

gtca commented 1 year ago

Hey @martinrohbeck,

The explanation is rather that the axis interface of MuData objects is newer than most muon interfaces, and the latter still need to be upgraded to handle non-standard axes. For multi-group MOFA (this issue) this is going to be rather straightforward but in general custom axes are undefined behaviour for muon so far.