scanpy
scanpy copied to clipboard
sc.pl.paga_path error in dimensions of array passed to ax.imshow
Please make sure these conditions are met
- [X] I have checked that this issue has not already been reported.
- [X] I have confirmed this bug exists on the latest version of scanpy.
- [ ] (optional) I have confirmed this bug exists on the main branch of scanpy.
What happened?
sc.pl.paga_path is supposed to output a heatmap. However, with the default parameter n_avg=1, the plotting fails. Internally, the function passes an array X, X = np.asarray(X).squeeze() to ax.imshow img = ax.imshow(X, aspect="auto", interpolation="nearest", cmap=color_map). X is one dimensional because of the squeeze() and imshow only accepts 2d arrays. It's fixed for n_avg=1 when squeeze is removed but still fails when n_avg is other numbers
Minimal code sample
import scanpy as sc
adata = sc.datasets.paul15()
sc.pp.recipe_zheng17(adata)
sc.tl.pca(adata, svd_solver="arpack")
sc.pp.neighbors(adata, n_neighbors=10, n_pcs=50)
sc.tl.leiden(adata, resolution=1.0)
sc.tl.paga(adata, groups="leiden")
sc.pl.paga(adata, color=["leiden", "Hba-a2", "Elane", "Irf8"])
adata.uns['iroot']=0
sc.tl.dpt(adata)
import matplotlib.pyplot as plt
fig,ax=plt.subplots(1,1,figsize=(7,1))
path_data = sc.pl.paga_path(
adata,
[4, 5],
["Elane"],
ax=ax,
show_node_names=False,
ytick_fontsize=12,
return_data=True,
#n_avg=1,
color_map="Greys",
groups_key="leiden",
color_maps_annotations={"dpt_pseudotime": "viridis"}
)
Error output
TypeError Traceback (most recent call last)
Cell In[1], line 15
13 import matplotlib.pyplot as plt
14 fig,ax=plt.subplots(1,1,figsize=(7,1))
---> 15 path_data = sc.pl.paga_path(
16 adata,
17 [4, 5],
18 ["Elane"],
19 ax=ax,
20 show_node_names=False,
21 ytick_fontsize=12,
22 return_data=True,
23 #n_avg=1,
24 color_map="Greys",
25 groups_key="leiden",
26 color_maps_annotations={"dpt_pseudotime": "viridis"}
27 )
File ~\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\legacy_api_wrap\__init__.py:80, in legacy_api.<locals>.wrapper.<locals>.fn_compatible(*args_all, **kw)
77 @wraps(fn)
78 def fn_compatible(*args_all: P.args, **kw: P.kwargs) -> R:
79 if len(args_all) <= n_positional:
---> 80 return fn(*args_all, **kw)
82 args_pos: P.args
83 args_pos, args_rest = args_all[:n_positional], args_all[n_positional:]
File ~\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\scanpy\plotting\_tools\paga.py:1255, in paga_path(adata, nodes, keys, use_raw, annotations, color_map, color_maps_annotations, palette_groups, n_avg, groups_key, xlim, title, left_margin, ytick_fontsize, title_fontsize, show_node_names, show_yticks, show_colorbar, legend_fontsize, legend_fontweight, normalize_to_zero_one, as_heatmap, return_data, show, save, ax)
1253 print(X.shape)
1254 if as_heatmap:
-> 1255 img = ax.imshow(X, aspect="auto", interpolation="nearest", cmap=color_map)
1256 if show_yticks:
1257 ax.set_yticks(range(len(X)))
File ~\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\matplotlib\__init__.py:1459, in _preprocess_data.<locals>.inner(ax, data, *args, **kwargs)
1456 @functools.wraps(func)
1457 def inner(ax, *args, data=None, **kwargs):
1458 if data is None:
-> 1459 return func(ax, *map(sanitize_sequence, args), **kwargs)
1461 bound = new_sig.bind(ax, *args, **kwargs)
1462 auto_label = (bound.arguments.get(label_namer)
1463 or bound.kwargs.get(label_namer))
File ~\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\matplotlib\axes\_axes.py:5665, in Axes.imshow(self, X, cmap, norm, aspect, interpolation, alpha, vmin, vmax, origin, extent, interpolation_stage, filternorm, filterrad, resample, url, **kwargs)
5657 self.set_aspect(aspect)
5658 im = mimage.AxesImage(self, cmap=cmap, norm=norm,
5659 interpolation=interpolation, origin=origin,
5660 extent=extent, filternorm=filternorm,
5661 filterrad=filterrad, resample=resample,
5662 interpolation_stage=interpolation_stage,
5663 **kwargs)
-> 5665 im.set_data(X)
5666 im.set_alpha(alpha)
5667 if im.get_clip_path() is None:
5668 # image does not already have clipping set, clip to axes patch
File ~\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\matplotlib\image.py:710, in _ImageBase.set_data(self, A)
706 self._A = self._A[:, :, 0]
708 if not (self._A.ndim == 2
709 or self._A.ndim == 3 and self._A.shape[-1] in [3, 4]):
--> 710 raise TypeError("Invalid shape {} for image data"
711 .format(self._A.shape))
713 if self._A.ndim == 3:
714 # If the input data has values outside the valid range (after
715 # normalisation), we issue a warning and then clip X to the bounds
716 # - otherwise casting wraps extreme values, hiding outliers and
717 # making reliable interpretation impossible.
718 high = 255 if np.issubdtype(self._A.dtype, np.integer) else 1
TypeError: Invalid shape (633,) for image data
Versions
-----
anndata 0.9.2
scanpy 1.10.1
-----
PIL 9.5.0
anyio NA
arrow 1.3.0
asttokens NA
astunparse 1.6.3
attr 23.1.0
attrs 23.1.0
babel 2.13.0
backcall 0.2.0
certifi 2023.07.22
cffi 1.16.0
charset_normalizer 3.3.0
cloudpickle 2.2.1
colorama 0.4.6
comm 0.1.4
cycler 0.10.0
cython_runtime NA
dask 2023.10.0
dateutil 2.8.2
debugpy 1.8.0
decorator 5.1.1
defusedxml 0.7.1
executing 2.0.0
fastjsonschema NA
fqdn NA
h5py 3.9.0
idna 3.4
igraph 0.10.8
ipykernel 6.25.2
ipywidgets 8.1.1
isoduration NA
jedi 0.19.1
jinja2 3.1.2
joblib 1.3.2
json5 NA
jsonpointer 2.4
jsonschema 4.19.1
jsonschema_specifications NA
jupyter_events 0.7.0
jupyter_server 2.7.3
jupyterlab_server 2.25.0
kiwisolver 1.4.4
legacy_api_wrap NA
leidenalg 0.10.1
llvmlite 0.41.0
markupsafe 2.1.3
matplotlib 3.7.1
matplotlib_inline 0.1.6
mpl_toolkits NA
mpmath 1.3.0
natsort 8.4.0
nbformat 5.9.2
numba 0.58.0
numpy 1.24.3
opt_einsum v3.3.0
overrides NA
packaging 23.1
pandas 2.0.3
parso 0.8.3
patsy 0.5.3
pickleshare 0.7.5
platformdirs 3.11.0
prometheus_client NA
prompt_toolkit 3.0.39
psutil 5.9.5
pure_eval 0.2.2
pyarrow 15.0.2
pydev_ipython NA
pydevconsole NA
pydevd 2.9.5
pydevd_file_utils NA
pydevd_plugins NA
pydevd_tracing NA
pydot 2.0.0
pygments 2.16.1
pyparsing 3.0.9
pythoncom NA
pythonjsonlogger NA
pytz 2023.3
pywin32_bootstrap NA
pywin32_system32 NA
pywintypes NA
referencing NA
requests 2.31.0
rfc3339_validator 0.1.4
rfc3986_validator 0.1.1
rpds NA
scipy 1.10.1
seaborn 0.13.0
send2trash NA
session_info 1.0.0
simplejson 3.19.2
six 1.16.0
sklearn 1.3.1
sniffio 1.3.0
sparse 0.14.0
stack_data 0.6.3
statsmodels 0.14.0
sympy 1.12
texttable 1.7.0
threadpoolctl 3.2.0
tlz 0.12.0
toolz 0.12.0
torch 2.0.1+cpu
tornado 6.3.3
tqdm 4.66.1
traitlets 5.10.1
typing_extensions NA
uri_template NA
urllib3 2.0.6
vscode NA
wcwidth 0.2.8
webcolors 1.13
websocket 1.6.3
win32api NA
win32com NA
win32con NA
win32trace NA
winerror NA
yaml 6.0.1
zipp NA
zmq 25.1.1
-----
IPython 8.16.1
jupyter_client 8.3.1
jupyter_core 5.3.2
jupyterlab 4.0.7
notebook 7.0.5
-----
Python 3.11.9 (tags/v3.11.9:de54cf5, Apr 2 2024, 10:12:12) [MSC v.1938 64 bit (AMD64)]
Windows-10-10.0.22621-SP0
-----
Session information updated at 2024-04-23 21:26