scverse / scverse/scanpy

Dendrogram error - symmetry

Open
#2,357 0 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
2.6k
Forks
779
Avg merge
1d 4h
Merged PRs (30d)
27

Description

  • I have checked that this issue has not already been reported.
  • I have confirmed this bug exists on the latest version of scanpy.
  • (optional) I have confirmed this bug exists on the master branch of scanpy.

Hi,

I've tried to run a matrix plot with a dendrogram and I get this error regarding the distance matrix. I'm not sure whether the problem is my data or Scanpy itself.

Thanks,

Will

Minimal code sample (that we can copy&paste without having any data)
# sc.pl.rank_genes_groups_heatmap(ad, n_genes=1, key="wilcoxon", groupby="leiden", show_gene_labels=True, figsize = [10,10], dendrogram = True )
[WARNING: dendrogram data not found (using key=dendrogram_leiden). Running `sc.tl.dendrogram` with default parameters. For fine tuning it is recommended to run `sc.tl.dendrogram` independently.
    using data matrix X directly
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-71-f7d35408db0b> in <module>
----> 1 sc.pl.rank_genes_groups_heatmap(ad, n_genes=1, key="wilcoxon", groupby="leiden", show_gene_labels=True, figsize = [10,10], dendrogram = True )

~/.local/lib/python3.8/site-packages/scanpy/plotting/_tools/__init__.py in rank_genes_groups_heatmap(adata, groups, n_genes, groupby, gene_symbols, var_names, min_logfoldchange, key, show, save, **kwds)
    671     tl.dendrogram
    672     """
--> 673     return _rank_genes_groups_plot(
    674         adata,
    675         plot_type='heatmap',

~/.local/lib/python3.8/site-packages/scanpy/plotting/_tools/__init__.py in _rank_genes_groups_plot(adata, plot_type, groups, n_genes, groupby, values_to_plot, var_names, min_logfoldchange, key, show, save, return_fig, gene_symbols, **kwds)
    590         from .._anndata import heatmap
    591 
--> 592         return heatmap(
    593             adata,
    594             var_names,

~/.local/lib/python3.8/site-packages/scanpy/plotting/_anndata.py in heatmap(adata, var_names, groupby, use_raw, log, num_categories, dendrogram, gene_symbols, var_group_positions, var_group_labels, var_group_rotation, layer, standard_scale, swap_axes, show_gene_labels, show, save, figsize, vmin, vmax, vcenter, norm, **kwds)
   1085 
   1086     if dendrogram:
-> 1087         dendro_data = _reorder_categories_after_dendrogram(
   1088             adata,
   1089             groupby,

~/.local/lib/python3.8/site-packages/scanpy/plotting/_anndata.py in _reorder_categories_after_dendrogram(adata, groupby, dendrogram, var_names, var_group_labels, var_group_positions, categories)
   2132     """
   2133 
-> 2134     key = _get_dendrogram_key(adata, dendrogram, groupby)
   2135 
   2136     if isinstance(groupby, str):

~/.local/lib/python3.8/site-packages/scanpy/plotting/_anndata.py in _get_dendrogram_key(adata, dendrogram_key, groupby)
   2234             "tuning it is recommended to run `sc.tl.dendrogram` independently."
   2235         )
-> 2236         dendrogram(adata, groupby, key_added=dendrogram_key)
   2237 
   2238     if 'dendrogram_info' not in adata.uns[dendrogram_key]:

~/.local/lib/python3.8/site-packages/scanpy/tools/_dendrogram.py in dendrogram(adata, groupby, n_pcs, use_rep, var_names, use_raw, cor_method, linkage_method, optimal_ordering, key_added, inplace)
    137 
    138     corr_matrix = mean_df.T.corr(method=cor_method)
--> 139     corr_condensed = distance.squareform(1 - corr_matrix)
    140     z_var = sch.linkage(
    141         corr_condensed, method=linkage_method, optimal_ordering=optimal_ordering

~/.local/lib/python3.8/site-packages/scipy/spatial/distance.py in squareform(X, force, checks)
   2343             raise ValueError('The matrix argument must be square.')
   2344         if checks:
-> 2345             is_valid_dm(X, throw=True, name='X')
   2346 
   2347         # One-side of the dimensions is set here.

~/.local/lib/python3.8/site-packages/scipy/spatial/distance.py in is_valid_dm(D, tol, throw, name, warning)
   2418             if not (D == D.T).all():
   2419                 if name:
-> 2420                     raise ValueError(('Distance matrix \'%s\' must be '
   2421                                      'symmetric.') % name)
   2422                 else:

ValueError: Distance matrix 'X' must be symmetric.]
Versions

Scanpy 1.9.1, Pandas 1.4.1

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with sc.pl.rank_genes_groups_heatmap and follow the traceback into scanpy/plotting/_anndata.py and scanpy/tools/_dendrogram.py, especially the dendrogram call that reaches scipy.spatial.distance.squareform. Reproduce the supplied call with the reported Scanpy and Pandas versions, then verify the heatmap no longer raises the symmetry ValueError and add or update regression coverage if the repository provides a relevant test location.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
bioinformatics, data-visualization
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.