bioFAM / mofapy2

Multi-omics factor analysis v2
https://biofam.github.io/MOFA2/
GNU Lesser General Public License v3.0
36 stars 28 forks source link

`AttributeError: Module 'scipy' has no attribute 'shape'` #22

Closed mdmanurung closed 1 month ago

mdmanurung commented 9 months ago

Dear authors,

I encountered this error when running mofapy2 (v 0.7.0) via muon.

It reached this point:

- Automatic Relevance Determination prior on the factors: True
- Automatic Relevance Determination prior on the weights: True
- Spike-and-slab prior on the factors: False
- Spike-and-slab prior on the weights: True
Likelihoods:
- View 0 (ADT): gaussian
- View 1 (RNA): gaussian

GPU mode is activated

But then the error kicks in.

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
File /exports/archive/hg-funcgenom-research/mdmanurung/conda/envs/totalvi/lib/python3.9/site-packages/scipy/__init__.py:137, in __getattr__(name)
    136 try:
--> 137     return globals()[name]
    138 except KeyError:

KeyError: 'shape'

During handling of the above exception, another exception occurred:

AttributeError                            Traceback (most recent call last)
Cell In[84], line 1
----> 1 mu.tl.mofa(mdata, gpu_mode=True, use_var="highly_variable")

File /exports/archive/hg-funcgenom-research/mdmanurung/conda/envs/totalvi/lib/python3.9/site-packages/muon/_core/tools.py:586, in mofa(data, groups_label, use_raw, use_layer, use_var, use_obs, likelihoods, n_factors, scale_views, scale_groups, center_groups, ard_weights, ard_factors, spikeslab_weights, spikeslab_factors, n_iterations, convergence_mode, use_float32, gpu_mode, gpu_device, svi_mode, svi_batch_size, svi_learning_rate, svi_forgetting_rate, svi_start_stochastic, smooth_covariate, smooth_warping, smooth_kwargs, save_parameters, save_data, save_metadata, seed, outfile, expectations, save_interrupted, verbose, quiet, copy)
    570     ent.set_smooth_options(
    571         scale_cov=smooth_kwargs["scale_cov"],
    572         start_opt=smooth_kwargs["start_opt"],
   (...)
    582         frac_inducing=smooth_kwargs["frac_inducing"],
    583     )
    585 logging.info(f"[{datetime.now().strftime('%Y-%m-%d %H:%M:%S')}] Building the model...")
--> 586 ent.build()
    587 logging.info(f"[{datetime.now().strftime('%Y-%m-%d %H:%M:%S')}] Running the model...")
    588 ent.run()

File /exports/archive/hg-funcgenom-research/mdmanurung/conda/envs/totalvi/lib/python3.9/site-packages/mofapy2/run/entry_point.py:1405, in entry_point.build(self)
   1397 else:
   1398     tmp = buildBiofam(
   1399         self.data,
   1400         self.dimensionalities,
   (...)
   1403         self.train_opts,
   1404     )
-> 1405 tmp.main()
   1407 # Create BayesNet class
   1408 if self.train_opts["stochastic"]:

File /exports/archive/hg-funcgenom-research/mdmanurung/conda/envs/totalvi/lib/python3.9/site-packages/mofapy2/build_model/build_model.py:56, in buildBiofam.main(self)
     47 self.init_model = initModel(
     48     dim=self.dim,
     49     data=self.data,
   (...)
     52     seed=self.train_opts["seed"],
     53 )
     55 # Build all nodes
---> 56 self.build_nodes()
     58 # Define markov blankets
     59 self.createMarkovBlankets()

File /exports/archive/hg-funcgenom-research/mdmanurung/conda/envs/totalvi/lib/python3.9/site-packages/mofapy2/build_model/build_model.py:65, in buildBiofam.build_nodes(self)
     62 """Method to build all nodes"""
     64 # Build general nodes
---> 65 self.build_Z()
     66 self.build_W()
     67 self.build_Tau()

File /exports/archive/hg-funcgenom-research/mdmanurung/conda/envs/totalvi/lib/python3.9/site-packages/mofapy2/build_model/build_model.py:104, in buildBiofam.build_Z(self)
     95     self.init_model.initSZ(
     96         qmean_T1="pca",
     97         Y=self.data,
     98         impute=True,
     99         weight_views=self.train_opts["weight_views"],
    100     )
    101 else:
    102     # self.init_model.initZ(qmean=0)
    103     # self.init_model.initZ(qmean="random")
--> 104     self.init_model.initZ(
    105         qmean="pca",
    106         Y=self.data,
    107         impute=True,
    108         weight_views=self.train_opts["weight_views"],
    109     )

File /exports/archive/hg-funcgenom-research/mdmanurung/conda/envs/totalvi/lib/python3.9/site-packages/mofapy2/build_model/init_model.py:145, in initModel.initZ(self, pmean, pvar, qmean, qvar, qE, qE2, Y, impute, weight_views)
    142         exit()
    144 # Initialise the node
--> 145 self.nodes["Z"] = Z_Node(
    146     dim=(self.N, self.K),
    147     pmean=pmean,
    148     pvar=pvar,
    149     qmean=qmean,
    150     qvar=qvar,
    151     qE=qE,
    152     qE2=qE2,
    153     weight_views=weight_views,
    154 )

File /exports/archive/hg-funcgenom-research/mdmanurung/conda/envs/totalvi/lib/python3.9/site-packages/mofapy2/core/nodes/Z_nodes.py:20, in Z_Node.__init__(self, dim, pmean, pvar, qmean, qvar, qE, qE2, weight_views)
     17 def __init__(
     18     self, dim, pmean, pvar, qmean, qvar, qE=None, qE2=None, weight_views=False
     19 ):
---> 20     super().__init__(
     21         dim=dim, pmean=pmean, pvar=pvar, qmean=qmean, qvar=qvar, qE=qE, qE2=qE2
     22     )
     24     self.mini_batch = None
     25     self.factors_axis = 1

File /exports/archive/hg-funcgenom-research/mdmanurung/conda/envs/totalvi/lib/python3.9/site-packages/mofapy2/core/nodes/variational_nodes.py:148, in UnivariateGaussian_Unobserved_Variational_Node.__init__(self, dim, pmean, pvar, qmean, qvar, qE, qE2)
    146 Unobserved_Variational_Node.__init__(self, dim)
    147 # Initialise the P and Q distributions
--> 148 self.P = UnivariateGaussian(dim=dim, mean=pmean, var=pvar)
    149 self.Q = UnivariateGaussian(dim=dim, mean=qmean, var=qvar, E=qE, E2=qE2)

File /exports/archive/hg-funcgenom-research/mdmanurung/conda/envs/totalvi/lib/python3.9/site-packages/mofapy2/core/distributions/univariate_gaussian.py:46, in UnivariateGaussian.__init__(self, dim, mean, var, E, E2)
     43     self.to_float32()
     45 # Check that dimensionalities match
---> 46 self.CheckDimensionalities()

File /exports/archive/hg-funcgenom-research/mdmanurung/conda/envs/totalvi/lib/python3.9/site-packages/mofapy2/core/distributions/basic_distributions.py:68, in Distribution.CheckDimensionalities(self)
     66 """General method to do a sanity check on the dimensionalities"""
     67 # p_dim = set(map(s.shape, self.params.values()))
---> 68 e_dim = set(map(s.shape, self.expectations.values()))
     69 # assert len(p_dim) == 1, "Parameters have different dimensionalities"
     70 assert len(e_dim) == 1, "Expectations have different dimensionalities"

File /exports/archive/hg-funcgenom-research/mdmanurung/conda/envs/totalvi/lib/python3.9/site-packages/scipy/__init__.py:139, in __getattr__(name)
    137     return globals()[name]
    138 except KeyError:
--> 139     raise AttributeError(
    140         f"Module 'scipy' has no attribute '{name}'"
    141     )

Do you know how to solve this? My scipy version is 1.12.0. Thanks in advance.

Regards, Mikhael

cheny36 commented 9 months ago

I had the same error, changing s.shape to np.shape in the mofapy2/core/distributions/basic_distributions.py file and s.outer to np.outer in the mofapy2/core/BayesNet.py files fixed the error for me.

thjimmylee commented 8 months ago

Thanks @cheny36 it worked for me too!

cyianor commented 7 months ago

This is due to a deprecation in SciPy 1.12.0 where they decided not to re-export NumPy functions anymore.

See the release notes for SciPy 1.13.0. In particular:

Functions from NumPy's main namespace which were exposed in SciPy's main namespace, such as numpy.histogram exposed by scipy.histogram, have been removed from SciPy's main namespace. Please use the functions directly from numpy. This was originally performed for SciPy 1.12.0 however was missed from the release notes so is included here for completeness.

YiweiNiu commented 7 months ago

Hi, I had a similar issue which may be related to this. The version of mofapy2 is 0.7.1

######################################
## Training the model with seed 2023 ##
######################################

ELBO before training:
Z=-261260.27  W=-2048499.87  Tau=-1131537.48  Y=-24105205301.50  AlphaZ=-3679.08  AlphaW=-1839.54  ThetaW=0.00  
Total: -24108652117.74

- ELBO decomposition:  Z=-104160.44  W=-1594968.94  Tau=-2030458.39  Y=518485963.26  AlphaZ=-4168.85  AlphaW=-2110.58  ThetaW=0.00  
- Time spent in ELBO computation: 0.3%
Traceback (most recent call last):
  File "/work/home/software/anaconda3/envs/scanpy/lib/python3.10/site-packages/scipy/__init__.py", line 137, in __getattr__
    return globals()[name]
KeyError: 'asarray'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/work/home/project/multiome/mofa_multiome/multiome_40samp/mofa_test.py", line 116, in <module>
    mdata = run_mofa(mdata=mdata, outfile=f"{outdir}/data/mofa_test_model.hdf5")
  File "/work/home/project/multiome/mofa_multiome/multiome_40samp/mofa_test.py", line 62, in run_mofa
    mu.tl.mofa(mdata, groups_label=groups_label, n_factors=n_factors, seed=2023,
  File "/work/home/software/anaconda3/envs/scanpy/lib/python3.10/site-packages/muon/_core/tools.py", line 586, in mofa
    ent.run()
  File "/work/home/software/anaconda3/envs/scanpy/lib/python3.10/site-packages/mofapy2/run/entry_point.py", line 26, in saver
    func(self, *args, **kwargs)
  File "/work/home/software/anaconda3/envs/scanpy/lib/python3.10/site-packages/mofapy2/run/entry_point.py", line 1400, in run
    train_model(self.model)
  File "/work/home/software/anaconda3/envs/scanpy/lib/python3.10/site-packages/mofapy2/build_model/train_model.py", line 26, in train_model
    model.iterate()
  File "/work/home/software/anaconda3/envs/scanpy/lib/python3.10/site-packages/mofapy2/core/BayesNet.py", line 368, in iterate
    self.print_verbose_message(i)
  File "/work/home/software/anaconda3/envs/scanpy/lib/python3.10/site-packages/mofapy2/core/BayesNet.py", line 404, in print_verbose_message
    r2 = s.asarray(self.calculate_variance_explained(total=True)).mean(axis=0)
  File "/work/home/software/anaconda3/envs/scanpy/lib/python3.10/site-packages/scipy/__init__.py", line 139, in __getattr__
    raise AttributeError(
AttributeError: Module 'scipy' has no attribute 'asarray'
YiweiNiu commented 7 months ago

Hi, changing scipy to numpy solved the issue.

r2 = np.asarray(self.calculate_variance_explained(total=True)).mean(axis=0)
cyianor commented 7 months ago

Pull request #27 would fix this, but we need one of the package developers to look at it. @gtca maybe?

ahmed344 commented 6 months ago

Me too I had similar problems. Downgrading to scipy 1.11.4 solved it for me. pip install scipy==1.11.4

mbensouda commented 6 months ago

Hi same here, I have scipy 1.13.0 and got the error: "Module 'scipy' has no attribute 'mod'" Downgrading also to scipy 1.11.4 solved the previous bug.

cyianor commented 1 month ago

@gtca this should also be fixed now by PR #27 and should be closed