scverse / scanpy

Single-cell analysis in Python. Scales to >1M cells.
https://scanpy.readthedocs.io
BSD 3-Clause "New" or "Revised" License
1.82k stars 587 forks source link

scanpy/api/pl.py "cannot import name stacked_violin" from 'scanpy.plotting._anndata' #1397

Open aravind1338 opened 3 years ago

aravind1338 commented 3 years ago

Minimal code sample (that we can copy&paste without having any data)

import scanpy.api as sc
  File "./scanpy_normalization.py", line 4, in <module>
    import scanpy.api as sc
  File "/usr/local/lib/python3.8/site-packages/scanpy/api/__init__.py", line 27, in <module>
    from . import pl
  File "/usr/local/lib/python3.8/site-packages/scanpy/api/pl.py", line 1, in <module>
    from ..plotting._anndata import scatter, violin, ranking, clustermap, stacked_violin, heatmap, dotplot, matrixplot, tracksplot
ImportError: cannot import name 'stacked_violin' from 'scanpy.plotting._anndata' (/usr/local/lib/python3.8/site-packages/scanpy/plotting/_anndata.py)

This is with the latest version of scanpy. I looked at the code and scanpy/apt/pl.py still has from ..plotting._anndata import scatter, violin, ranking, clustermap, stacked_violin, heatmap, dotplot, matrixplot, tracksplot, even as the plotting library has been refactored and the dotplot, matrixplot and stacked_violin are now in separate files.

I tested this a few days ago and it was working fine then, the update to anndata probably happened in the last couple of days

ivirshup commented 3 years ago

Thanks for the bug report! I think you should use:

import scanpy as sc

instead of

import scanpy.api as sc

Is there any reason you're using scanpy.api?

@flying-sheep @falexwolf, was there a reason we didn't just make those statements equivalent?

aravind1338 commented 3 years ago

I'm actually testing and tweaking someone else's code that was written a while ago. I assume they used import scanpy.api as sc because it was appropriate then. I personally resolved my issue by downgrading versions, I just wanted to bring this up!

WeimiaoWu commented 3 years ago

I'm actually testing and tweaking someone else's code that was written a while ago. I assume they used import scanpy.api as sc because it was appropriate then. I personally resolved my issue by downgrading versions, I just wanted to bring this up!

I encountered the same issue. Which version are you using to fix this?

WeimiaoWu commented 3 years ago

I'm actually testing and tweaking someone else's code that was written a while ago. I assume they used import scanpy.api as sc because it was appropriate then. I personally resolved my issue by downgrading versions, I just wanted to bring this up!

I encountered the same issue. Which version are you using to fix this?

nm, downgrading to 1.5.1 fixed my problem. Thanks!

detrout commented 3 years ago

Hi I had this problem as well with 1.6.0 it was triggered by scanpy's test code.

scanpy.api (unittest.loader._FailedTest) ... ERROR

======================================================================
ERROR: scanpy.api (unittest.loader._FailedTest)
----------------------------------------------------------------------
ImportError: Failed to import test module: scanpy.api
Traceback (most recent call last):
  File "/usr/lib/python3.9/unittest/loader.py", line 470, in _find_test_path
    package = self._get_module_from_name(name)
  File "/usr/lib/python3.9/unittest/loader.py", line 377, in _get_module_from_name
    __import__(name)
  File "/<<PKGBUILDDIR>>/.pybuild/cpython3_3.9_scanpy/build/scanpy/api/__init__.py", line 27, in <module>
    from . import pl
  File "/<<PKGBUILDDIR>>/.pybuild/cpython3_3.9_scanpy/build/scanpy/api/pl.py", line 1, in <module>
    from ..plotting._anndata import scatter, violin, ranking, clustermap, stacked_violin, heatmap, dotplot, matrixplot, tracksplot
ImportError: cannot import name 'stacked_violin' from 'scanpy.plotting._anndata' (/<<PKGBUILDDIR>>/.pybuild/cpython3_3.9_scanpy/build/scanpy/plotting/_anndata.py)

----------------------------------------------------------------------
Ran 1 test in 0.000s

I ended up with this patch to get the tests to run successfully.

--- a/scanpy/api/pl.py
+++ b/scanpy/api/pl.py
@@ -1,4 +1,7 @@
-from ..plotting._anndata import scatter, violin, ranking, clustermap, stacked_violin, heatmap, dotplot, matrixplot, tracksplot
+from ..plotting._anndata import scatter, violin, ranking, clustermap, heatmap, tracksplot
+from ..plotting._stacked_violin import stacked_violin
+from ..plotting._dotplot import dotplot
+from ..plotting._matrixplot import matrixplot

 from ..plotting._preprocessing import filter_genes_dispersion, highly_variable_genes