LouisFaure / scFates

a scalable python suite for tree inference and advanced pseudotime analysis from scRNAseq data.
https://scfates.readthedocs.io/
BSD 3-Clause "New" or "Revised" License
47 stars 1 forks source link

KeyError: 'explode' exception while plotting clusters #27

Closed quantumdot closed 4 months ago

quantumdot commented 4 months ago

While attempting to plot trends for found clusters:

scf.tl.cluster(adata, n_neighbors=20, n_pcs=10, metric="correlation")
for c in adata.var["clusters"].unique():
    scf.pl.trends(adata, features=adata.var_names[adata.var.clusters==c], basis="pca")

I encountered the following exception:

KeyError                                  Traceback (most recent call last)
Cell In[25], [line 2]
      [1] for c in adata.var["clusters"].unique():
----> [2]    scf.pl.trends(adata, features=adata.var_names[adata.var.clusters==c], basis="pca")

File [c:\Users\username\AppData\Local\miniconda3\envs\scFates2\lib\site-packages\scFates\plot\features.py:452], in trends(adata, features, cluster, highlight_features, n_features, root_milestone, milestones, module, branch, annot, title, feature_cmap, pseudo_cmap, plot_emb, plot_heatmap, wspace, show_segs, basis, heatmap_space, offset_names, fontsize, style, ordering, ord_thre, figsize, axemb, show, output_mean, save, return_genes, **kwargs)
    [447] axheatmap.add_patch(patch)
    [448] axheatmap.hlines(
    [449]     fitted_sorted.shape[0], 0, fitted_sorted.shape[1], color="k", clip_on=True
    [450] )
--> [452] adjust_text(
    [453]     texts,
    [454]     ax=axheatmap,
    [455]     add_objects=[patch],
    [456]     va="center",
    [457]     ha="left",
    [458]     autoalign=False,
    [459]     expand_text=(1.05, 1.2),
    [460]     lim=5000,
    [461]     only_move={"text": "y", "objects": "x"},
    [462]     precision=0.1,
    [463]     expand_points=(1.2, 1.05),
    [464] )
    [466] for i in range(len(xs)):
    [467]     xx = [
    [468]         xs[i] + 1,
    [469]         fitted_sorted.shape[1] + fitted_sorted.shape[1] * offset_names,
    [470]     ]

File [c:\Users\username\AppData\Local\miniconda3\envs\scFates2\lib\site-packages\adjustText\__init__.py:506], in adjust_text(texts, x, y, objects, avoid_self, force_text, force_static, force_pull, force_explode, expand, explode_radius, ensure_inside_axes, expand_axes, only_move, ax, min_arrow_len, time_lim, iter_lim, *args, **kwargs)
    [504] if explode_radius > 0 and np.all(np.asarray(force_explode) > 0):
    [505]     explode_x, explode_y = explode(coords, static_coords, explode_radius)
--> [506]     if "x" not in only_move["explode"]:
    [507]         explode_x = np.zeros_like(explode_x)
    [508]     if "y" not in only_move["explode"]:

KeyError: 'explode'

In order to workaround the exception, I found the following patch to adjustText/__init__.py in the adjustText package allowed me to continue analysis and at least produce plots. The idea is to ensure these four keys are present in the only_move dict which should prevent the exception, but this is probably a pretty naïve fix.

476a477,481
>     if isinstance(only_move, dict):
>         for key in ["text", "static", "explode", "pull"]:
>             if key not in only_move:
>                 only_move[key] = ''
>

Although I can now produce the plots, they seem to have an issue with the label positioning: image

Here is my conda environment:

# Name                    Version                   Build  Channel
_r-mutex                  1.0.1               anacondar_1    conda-forge
adjusttext                1.0.4                    pypi_0    pypi
anndata                   0.10.5.post1             pypi_0    pypi
array-api-compat          1.4.1                    pypi_0    pypi
asttokens                 2.0.5              pyhd3eb1b0_0
backcall                  0.2.0              pyhd3eb1b0_0
bzip2                     1.0.8                he774522_0
ca-certificates           2023.12.12           haa95532_0
cffi                      1.16.0           py39h2bbff1b_0
colorama                  0.4.6            py39haa95532_0
comm                      0.1.2            py39haa95532_0
contourpy                 1.2.0                    pypi_0    pypi
cycler                    0.12.1                   pypi_0    pypi
debugpy                   1.6.7            py39hd77b12b_0
decorator                 5.1.1              pyhd3eb1b0_0
elpigraph-python          0.3.1                    pypi_0    pypi
exceptiongroup            1.2.0            py39haa95532_0
executing                 0.8.3              pyhd3eb1b0_0
fcsparser                 0.2.8                    pypi_0    pypi
fonttools                 4.48.1                   pypi_0    pypi
get-annotations           0.1.2                    pypi_0    pypi
h5py                      3.10.0                   pypi_0    pypi
igraph                    0.11.4                   pypi_0    pypi
importlib-metadata        7.0.1            py39haa95532_0
importlib-resources       6.1.1                    pypi_0    pypi
importlib_metadata        7.0.1                hd3eb1b0_0
intel-openmp              2024.0.0         h57928b3_49841    conda-forge
ipykernel                 6.28.0           py39haa95532_0
ipython                   8.15.0           py39haa95532_0
jax                       0.4.24                   pypi_0    pypi
jaxlib                    0.4.23                   pypi_0    pypi
jaxopt                    0.8.3                    pypi_0    pypi
jedi                      0.18.1           py39haa95532_1
jinja2                    3.1.3            py39haa95532_0
joblib                    1.3.2                    pypi_0    pypi
jupyter_client            8.6.0            py39haa95532_0
jupyter_core              5.5.0            py39haa95532_0
kiwisolver                1.4.5                    pypi_0    pypi
leidenalg                 0.10.2                   pypi_0    pypi
libblas                   3.9.0              21_win64_mkl    conda-forge
libffi                    3.4.4                hd77b12b_0
liblapack                 3.9.0              21_win64_mkl    conda-forge
libsodium                 1.0.18               h62dcd97_0
libsqlite                 3.45.1               hcfcfb64_0    conda-forge
libzlib                   1.2.13               hcfcfb64_5    conda-forge
llvmlite                  0.42.0                   pypi_0    pypi
m2w64-bwidget             1.9.10                        2    conda-forge
m2w64-bzip2               1.0.6                         6    conda-forge
m2w64-expat               2.1.1                         2    conda-forge
m2w64-fftw                3.3.4                         6    conda-forge
m2w64-flac                1.3.1                         3    conda-forge
m2w64-gcc-libgfortran     5.3.0                         6    conda-forge
m2w64-gcc-libs            5.3.0                         7    conda-forge
m2w64-gcc-libs-core       5.3.0                         7    conda-forge
m2w64-gettext             0.19.7                        2    conda-forge
m2w64-gmp                 6.1.0                         2    conda-forge
m2w64-gsl                 2.1                           2    conda-forge
m2w64-libiconv            1.14                          6    conda-forge
m2w64-libjpeg-turbo       1.4.2                         3    conda-forge
m2w64-libogg              1.3.2                         3    conda-forge
m2w64-libpng              1.6.21                        2    conda-forge
m2w64-libsndfile          1.0.26                        2    conda-forge
m2w64-libtiff             4.0.6                         2    conda-forge
m2w64-libvorbis           1.3.5                         2    conda-forge
m2w64-libwinpthread-git   5.0.0.4634.697f757               2    conda-forge
m2w64-libxml2             2.9.3                         3    conda-forge
m2w64-mpfr                3.1.4                         4    conda-forge
m2w64-pcre2               10.34                         0    conda-forge
m2w64-speex               1.2rc2                        3    conda-forge
m2w64-speexdsp            1.2rc3                        3    conda-forge
m2w64-tcl                 8.6.5                         3    conda-forge
m2w64-tk                  8.6.5                         3    conda-forge
m2w64-tktable             2.10                          5    conda-forge
m2w64-wineditline         2.101                         5    conda-forge
m2w64-xz                  5.2.2                         2    conda-forge
m2w64-zlib                1.2.8                        10    conda-forge
markupsafe                2.1.3            py39h2bbff1b_0
matplotlib                3.8.3                    pypi_0    pypi
matplotlib-inline         0.1.6            py39haa95532_0
mellon                    1.4.1                    pypi_0    pypi
mkl                       2024.0.0         h66d3029_49657    conda-forge
ml-dtypes                 0.3.2                    pypi_0    pypi
msys2-conda-epoch         20160418                      1    conda-forge
natsort                   8.4.0                    pypi_0    pypi
nest-asyncio              1.5.6            py39haa95532_0
networkx                  2.8.8                    pypi_0    pypi
numba                     0.59.0                   pypi_0    pypi
numpy                     1.26.4                   pypi_0    pypi
openssl                   3.2.1                hcfcfb64_0    conda-forge
opt-einsum                3.3.0                    pypi_0    pypi
packaging                 23.1             py39haa95532_0
palantir                  1.3.2                    pypi_0    pypi
pandas                    1.5.3                    pypi_0    pypi
parso                     0.8.3              pyhd3eb1b0_0
patsy                     0.5.6                    pypi_0    pypi
pickleshare               0.7.5           pyhd3eb1b0_1003
pillow                    10.2.0                   pypi_0    pypi
pip                       23.3.1           py39haa95532_0
platformdirs              3.10.0           py39haa95532_0
plotly                    5.19.0                   pypi_0    pypi
progressbar2              4.3.2                    pypi_0    pypi
prompt-toolkit            3.0.43           py39haa95532_0
psutil                    5.9.0            py39h2bbff1b_0
pure_eval                 0.2.2              pyhd3eb1b0_0
pycparser                 2.21               pyhd3eb1b0_0
pygam                     0.9.1                    pypi_0    pypi
pygments                  2.15.1           py39haa95532_1
pynndescent               0.5.11                   pypi_0    pypi
pyparsing                 3.1.1                    pypi_0    pypi
python                    3.9.18          h4de0772_1_cpython    conda-forge
python-dateutil           2.8.2              pyhd3eb1b0_0
python-igraph             0.11.4                   pypi_0    pypi
python-utils              3.8.2                    pypi_0    pypi
python_abi                3.9                      4_cp39    conda-forge
pytz                      2023.3.post1     py39haa95532_0
pywin32                   305              py39h2bbff1b_0
pyzmq                     25.1.2           py39hd77b12b_0
r-base                    4.1.3               hdca333a_12    conda-forge
r-lattice                 0.22_5            r41h6d2157b_0    conda-forge
r-matrix                  1.6_5             r41hb9981e2_0    conda-forge
r-mgcv                    1.9_1             r41hb9981e2_0    conda-forge
r-nlme                    3.1_164           r41he816bda_0    conda-forge
rpy2                      3.5.11          py39r41hd2bd0d5_3    conda-forge
scanpy                    1.9.8                    pypi_0    pypi
scfates                   1.0.6                    pypi_0    pypi
scikit-learn              1.4.0                    pypi_0    pypi
scikit-misc               0.3.1                    pypi_0    pypi
scipy                     1.11.4                   pypi_0    pypi
seaborn                   0.13.2                   pypi_0    pypi
session-info              1.0.0                    pypi_0    pypi
setuptools                68.2.2           py39haa95532_0
shapely                   2.0.2                    pypi_0    pypi
simplegeneric             0.8.1            py39haa95532_2
simpleppt                 1.1.4                    pypi_0    pypi
six                       1.16.0             pyhd3eb1b0_1
stack_data                0.2.0              pyhd3eb1b0_0
statsmodels               0.14.1                   pypi_0    pypi
stdlib-list               0.10.0                   pypi_0    pypi
tbb                       2021.8.0             h59b6b97_0
tenacity                  8.2.3                    pypi_0    pypi
texttable                 1.7.0                    pypi_0    pypi
threadpoolctl             3.3.0                    pypi_0    pypi
tk                        8.6.13               h5226925_1    conda-forge
tornado                   6.3.3            py39h2bbff1b_0
tqdm                      4.66.2                   pypi_0    pypi
traitlets                 5.7.1            py39haa95532_0
typing_extensions         4.9.0            py39haa95532_1
tzdata                    2023d                h04d1e81_0
tzlocal                   2.1              py39haa95532_1
ucrt                      10.0.20348.0         haa95532_0
umap-learn                0.5.5                    pypi_0    pypi
vc                        14.2                 h21ff451_1
vc14_runtime              14.38.33130         h82b7239_18    conda-forge
vs2015_runtime            14.38.33130         hcb4865c_18    conda-forge
wcwidth                   0.2.5              pyhd3eb1b0_0
wheel                     0.41.2           py39haa95532_0
xz                        5.4.5                h8cc25b3_0
zeromq                    4.3.5                hd77b12b_0
zipp                      3.17.0           py39haa95532_0
LouisFaure commented 4 months ago

Can you try with a previous version of adjustText? such as 0.7.3

quantumdot commented 4 months ago

Downgraded to adjusttext==0.8, and now the exception I reported above is avoided, and the plots now look as they are supposed to. However, I now encounter a different exception:

IndexError                                Traceback (most recent call last)
Cell In[24], line 1
----> 1 g=scf.pl.trends(adata, plot_emb=False, highlight_features=[], ordering="max",
      2               fig_heigth=6, pseudo_cmap=flat_huslmap, ord_thre=.95, return_genes=True)
      3 scf.pl.matrix(adata, features=g, nbins=20,
      4               cmap="RdBu_r", annot_top=False,
      5               colorbar_title="max\nnormalized\nactivity")

File c:\Users\username\AppData\Local\miniconda3\envs\scFates\lib\site-packages\scFates\plot\features.py:452, in trends(adata, features, cluster, highlight_features, n_features, root_milestone, milestones, module, branch, annot, title, feature_cmap, pseudo_cmap, plot_emb, plot_heatmap, wspace, show_segs, basis, heatmap_space, offset_names, fontsize, style, ordering, ord_thre, figsize, axemb, show, output_mean, save, return_genes, **kwargs)
    447 axheatmap.add_patch(patch)
    448 axheatmap.hlines(
    449     fitted_sorted.shape[0], 0, fitted_sorted.shape[1], color="k", clip_on=True
    450 )
--> 452 adjust_text(
    453     texts,
    454     ax=axheatmap,
    455     add_objects=[patch],
    456     va="center",
    457     ha="left",
    458     autoalign=False,
    459     expand_text=(1.05, 1.2),
    460     lim=5000,
    461     only_move={"text": "y", "objects": "x"},
    462     precision=0.1,
    463     expand_points=(1.2, 1.05),
    464 )
    466 for i in range(len(xs)):
    467     xx = [
    468         xs[i] + 1,
    469         fitted_sorted.shape[1] + fitted_sorted.shape[1] * offset_names,
    470     ]

File c:\Users\username\AppData\Local\miniconda3\envs\scFates\lib\site-packages\adjustText\__init__.py:567, in adjust_text(texts, x, y, add_objects, ax, expand_text, expand_points, expand_objects, expand_align, autoalign, va, ha, force_text, force_points, force_objects, lim, precision, only_move, avoid_text, avoid_points, avoid_self, save_steps, save_prefix, save_format, add_step_numbers, *args, **kwargs)
    565 ax = ax or plt.gca()
    566 r = get_renderer(ax.get_figure())
--> 567 transform = texts[0].get_transform()
    568 if (x is not None) & (y is not None):
    569     for ix, tupxy in enumerate(zip(x, y)):

IndexError: list index out of range

Finally, I tried adjusttext==0.7.3 and receive no errors. It would be nice if requirements.txt was updated to pin adjusttext package version accordingly.

LouisFaure commented 4 months ago

This now fixed with scfates v1.0.7, thanks for the notice!