BayraktarLab / cell2location

Comprehensive mapping of tissue cell architecture via integrated single cell and spatial transcriptomics (cell2location model)
https://cell2location.readthedocs.io/en/latest/
Apache License 2.0
321 stars 58 forks source link

ValueError: The condensed distance matrix must contain only finite values. #306

Open hkeremcil opened 1 year ago

hkeremcil commented 1 year ago

I am trying to run the code below:

from cell2location.utils import select_slide
slide = select_slide(adata_vis, 'unopened_count_visium')

from cell2location import run_colocation
res_dict, slide = run_colocation(
    slide,
    model_name='CoLocatedGroupsSklearnNMF',
    train_args={
      'n_fact': np.arange(21, 22), # IMPORTANT: use a wider range of the number of factors (5-30)
      'sample_name_col': 'sample', # columns in adata_vis.obs that identifies sample
      'n_restarts': 3 # number of training restarts
    },
    # the hyperparameters of NMF can be also adjusted:
    model_kwargs={'alpha': 0.01, 'init': 'random', "nmf_kwd_args": {"tol": 0.000001}},
    export_args={'path': f'{run_name}/unopened-CoLocatedComb/'}
)

And I get this error:

### Analysis name: CoLocatedGroupsSklearnNMF_21combinations_123locations_47factors
/usr/local/lib/python3.10/dist-packages/numpy/lib/function_base.py:2853: RuntimeWarning: invalid value encountered in divide
  c /= stddev[:, None]
/usr/local/lib/python3.10/dist-packages/numpy/lib/function_base.py:2854: RuntimeWarning: invalid value encountered in divide
  c /= stddev[None, :]
/usr/local/lib/python3.10/dist-packages/numpy/lib/function_base.py:2853: RuntimeWarning: invalid value encountered in divide
  c /= stddev[:, None]
/usr/local/lib/python3.10/dist-packages/numpy/lib/function_base.py:2854: RuntimeWarning: invalid value encountered in divide
  c /= stddev[None, :]
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
[<ipython-input-73-092b94ab1f9b>](https://localhost:8080/#) in <cell line: 5>()
      3 
      4 from cell2location import run_colocation
----> 5 res_dict, slide = run_colocation(
      6     slide,
      7     model_name='CoLocatedGroupsSklearnNMF',

4 frames
[/usr/local/lib/python3.10/dist-packages/scipy/cluster/hierarchy.py](https://localhost:8080/#) in linkage(y, method, metric, optimal_ordering)
   1063 
   1064     if not np.all(np.isfinite(y)):
-> 1065         raise ValueError("The condensed distance matrix must contain only "
   1066                          "finite values.")
   1067 

ValueError: The condensed distance matrix must contain only finite values.

However, it works perfectly fine when I do this by choosing any of the other slides or without choosing slides. This slide is a part of adata_vis; therefore, I cannot understand why I do not encounter this error when running that code directly with adata_vis without choosing a slide. I checked that slide using this:

are_all_finite = np.all(np.isfinite(slide.X.data))

And all values seem to be finite. I find this issue very strange. I am currently stuck with this, and I highly appreciate any help.

Many thanks in advance!

vitkl commented 1 year ago

This means that NMF output several factors with identical loadings for all locations/cell types, so plotting of those values using hierarchical clustering to order dimensions failed. Check if the options of the function allow you to switch cluster rows/cols off. Otherwise find factor number under which this step doesn’t fail.

hkeremcil commented 1 year ago

Thank you very much for your response. I could not find such an option in the parameters, and I don't think editing the source code to change the function would be a good idea, at least for me.

Regarding the factor number, I tried everything between 1 to 40. It immediately gives an error for every factor number. I started to have this issue with several other slides as well when I used the pipeline on more data. Could there be anything else to try? Or did I misunderstand what you suggested?

watermelontreesjs commented 1 year ago

I also meet the same problem, do you solve it?

watermelontreesjs commented 1 year ago

@vitkl , I tried factor number between 5 to 30, but all the factor meet the same error, do you have any advise to solve this problem?