vtraag / leidenalg

Implementation of the Leiden algorithm for various quality functions to be used with igraph in Python.
GNU General Public License v3.0
575 stars 77 forks source link

error with "pip install leiden" #114

Closed anndan7 closed 1 year ago

anndan7 commented 1 year ago

Hi, I am trying to use the leiden alg (algorithm=4) with FindClusters in Seurat in Rstudio. When I try to run this, it gives the error: "Cannot find Leiden algorithm, please install through pip (e.g. pip install leidenalg)", even though I've done library(leiden). When I go to my terminal to try pip install leidenalg, I get the following process and I am not sure how to fix, thanks!

ANACON~1\envs\r-reticulate\Lib\site-packages>python -m pip install leiden
Collecting leiden
Using cached leiden-1.0.2.tar.gz (12 kB)
Preparing metadata (setup.py) ... done
Collecting nose
Using cached nose-1.3.7-py3-none-any.whl (154 kB)
Collecting hgvs
Using cached hgvs-1.5.2-py2.py3-none-any.whl (99 kB)
Collecting pygr
Using cached pygr-0.8.2.tar.gz (1.6 MB)
Preparing metadata (setup.py) ... error
error: subprocess-exited-with-error

× python setup.py egg_info did not run successfully.
│ exit code: 1
╰─> [7 lines of output]
Traceback (most recent call last):
File "", line 2, in
File "", line 34, in
File "C:\Users\annda\AppData\Local\Temp\pip-install-4k34nfc4\pygr_a4d9c1e205a448a5be0f47150fe1116e\setup.py", line 19
print 'Setuptools not imported, falling back to distutils'
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
SyntaxError: Missing parentheses in call to 'print'. Did you mean print(...)?
[end of output]

note: This error originates from a subprocess, and is likely not a problem with pip.
error: metadata-generation-failed

× Encountered error while generating package metadata.
╰─> See above for output.

note: This is an issue with the package mentioned above, not pip.
hint: See above for details.
vtraag commented 1 year ago

Please note that the package is called leidenalg, not leiden. You should hence use pip install leidenalg. Hopefully that solves your problem?

anndan7 commented 1 year ago

Please note that the package is called leidenalg, not leiden. You should hence use pip install leidenalg. Hopefully that solves your problem?

This doesn't solve it either sadly. It says install successfully but when I try to use Leidenalg in R, it says "Cannot find Leiden algorithm, please install through pip (e.g. pip install leidenalg)"

vtraag commented 1 year ago

OK, let's see what goes wrong where then. What packages have you installed, in which language (R, Python) and in what environments (base environments, virtual environments, Anaconda environments) and in what OS?

anndan7 commented 1 year ago

OK, let's see what goes wrong where then. What packages have you installed, in which language (R, Python) and in what environments (base environments, virtual environments, Anaconda environments) and in what OS?

Ok! I'm using RStudio for this, are you asking what all packages I have installed or what packages I'm trying to use in conjunction with leiden? I am using my base environment in RStudio, OS is Windows 11.

vtraag commented 1 year ago

OK, it seems you are using Anaconda to run Python, is that correct? If so, leidenalg is probably correctly installed in the Anaconda environment, but that environment is probably not active when you launch R. As a result, the R leiden package is unable to find the Python leidenalg package.

You could try to install the package from within R using this:

library("reticulate")
py_install("leidenalg")

That way, reticulate hopefully installs the R package in an appropriate environment.

anndan7 commented 1 year ago

OK, it seems you are using Anaconda to run Python, is that correct? If so, leidenalg is probably correctly installed in the Anaconda environment, but that environment is probably not active when you launch R. As a result, the R leiden package is unable to find the Python leidenalg package.

You could try to install the package from within R using this:

library("reticulate")
py_install("leidenalg")

That way, reticulate hopefully installs the R package in an appropriate environment.

I did the py_install but I am still getting the same error message

vtraag commented 1 year ago

OK, sorry to hear. Perhaps you should check with the people from Seurat? They should probably have some idea as to how best solve this.

anndan7 commented 1 year ago

OK, sorry to hear. Perhaps you should check with the people from Seurat? They should probably have some idea as to how best solve this.

I checked with them originally and they told me to ask here because they were not sure.

vtraag commented 1 year ago

I'm not entirely sure about your setup. When I try to install a new conda environment that contains both the leiden R package and leidenalg, it all seems to work correctly.

That is, I create a new environment called r-leiden using conda create -n r-leiden -c conda-forge r-leiden leidenalg numpy. After activating the environment using conda activater-leidenI can runR(which should now run the R installed fromconda-forge). When importingleideninRusinglibrary(leiden)` you are asked to create a conda environment, which you should not, in this case, since this is already a conda environment. I then get the following output.

> library(leiden)
create conda environment (yes/no)?no
no
using environment: NA
Unable to set up conda environment r-reticulate
run in terminal:
conda init
conda create -n r-reticulate
conda environment r-reticulate installed
python modules igraph and leidenalg installed
> library(igraph)
> G <- igraph::graph.famous('Zachary')
>  part <- leiden::leiden(G)
> part
 [1] 2 2 2 2 4 4 4 2 1 1 4 2 2 2 1 1 4 2 1 2 1 2 1 3 3 3 1 3 3 1 1 3 1 1

Hopefully this setup works for you!

anndan7 commented 1 year ago

I'm not entirely sure about your setup. When I try to install a new conda environment that contains both the leiden R package and leidenalg, it all seems to work correctly.

That is, I create a new environment called r-leiden using conda create -n r-leiden -c conda-forge r-leiden leidenalg numpy. After activating the environment using conda activater-leidenI can runR(which should now run the R installed fromconda-forge). When importingleideninRusinglibrary(leiden)` you are asked to create a conda environment, which you should not, in this case, since this is already a conda environment. I then get the following output.

> library(leiden)
create conda environment (yes/no)?no
no
using environment: NA
Unable to set up conda environment r-reticulate
run in terminal:
conda init
conda create -n r-reticulate
conda environment r-reticulate installed
python modules igraph and leidenalg installed
> library(igraph)
> G <- igraph::graph.famous('Zachary')
>  part <- leiden::leiden(G)
> part
 [1] 2 2 2 2 4 4 4 2 1 1 4 2 2 2 1 1 4 2 1 2 1 2 1 3 3 3 1 3 3 1 1 3 1 1

Hopefully this setup works for you!

This helps! I did the first part in the terminal and that worked, I am just confused on how to open RStudio from there.

vtraag commented 1 year ago

I'm not sure if RStudio works nicely from an Anaconda environment: https://community.rstudio.com/t/using-rstudio-within-a-conda-environment/128780.

However, if you just use your system R (i.e. not installed via conda), I'm also able to get it working. When I install leiden using install.packages('leiden') and then do library(leiden) I then select "yes" to create a separate conda environment, giving the following result on my computer:

Log ``` create conda environment (yes/no)?yes yes sh: 1: /home/vtraag/.local/share/r-miniconda/bin/conda: not found + '/home/vtraag/mambaforge/bin/conda' 'create' '--yes' '--name' 'r-reticulate' 'python=3.8' '--quiet' '-c' 'conda-forge' Collecting package metadata (current_repodata.json): ...working... done Solving environment: ...working... done ## Package Plan ## environment location: /home/vtraag/mambaforge/envs/r-reticulate added / updated specs: - python=3.8 The following packages will be downloaded: package | build ---------------------------|----------------- python-3.8.15 |h4a9ceb5_0_cpython 19.9 MB conda-forge ------------------------------------------------------------ Total: 19.9 MB The following NEW packages will be INSTALLED: _libgcc_mutex conda-forge/linux-64::_libgcc_mutex-0.1-conda_forge _openmp_mutex conda-forge/linux-64::_openmp_mutex-4.5-2_gnu bzip2 conda-forge/linux-64::bzip2-1.0.8-h7f98852_4 ca-certificates conda-forge/linux-64::ca-certificates-2022.12.7-ha878542_0 ld_impl_linux-64 conda-forge/linux-64::ld_impl_linux-64-2.39-hcc3a1bd_1 libffi conda-forge/linux-64::libffi-3.4.2-h7f98852_5 libgcc-ng conda-forge/linux-64::libgcc-ng-12.2.0-h65d4601_19 libgomp conda-forge/linux-64::libgomp-12.2.0-h65d4601_19 libnsl conda-forge/linux-64::libnsl-2.0.0-h7f98852_0 libsqlite conda-forge/linux-64::libsqlite-3.40.0-h753d276_0 libuuid conda-forge/linux-64::libuuid-2.32.1-h7f98852_1000 libzlib conda-forge/linux-64::libzlib-1.2.13-h166bdaf_4 ncurses conda-forge/linux-64::ncurses-6.3-h27087fc_1 openssl conda-forge/linux-64::openssl-3.0.7-h0b41bf4_1 pip conda-forge/noarch::pip-22.3.1-pyhd8ed1ab_0 python conda-forge/linux-64::python-3.8.15-h4a9ceb5_0_cpython readline conda-forge/linux-64::readline-8.1.2-h0f457ee_0 setuptools conda-forge/noarch::setuptools-65.5.1-pyhd8ed1ab_0 tk conda-forge/linux-64::tk-8.6.12-h27826a3_0 wheel conda-forge/noarch::wheel-0.38.4-pyhd8ed1ab_0 xz conda-forge/linux-64::xz-5.2.6-h166bdaf_0 Preparing transaction: ...working... done Verifying transaction: ...working... done Executing transaction: ...working... done Retrieving notices: ...working... done + '/home/vtraag/mambaforge/bin/conda' 'install' '--yes' '--name' 'r-reticulate' '-c' 'conda-forge' 'conda' Collecting package metadata (current_repodata.json): done Solving environment: done ==> WARNING: A newer version of conda exists. <== current version: 4.14.0 latest version: 22.11.1 Please update conda by running $ conda update -n base -c conda-forge conda ## Package Plan ## environment location: /home/vtraag/mambaforge/envs/r-reticulate added / updated specs: - conda The following packages will be downloaded: package | build ---------------------------|----------------- brotlipy-0.7.0 |py38h0a891b7_1005 342 KB conda-forge cffi-1.15.1 | py38h4a40e3a_3 230 KB conda-forge conda-22.11.1 | py38h578d9bd_1 905 KB conda-forge conda-package-handling-2.0.2| pyh38be061_0 247 KB conda-forge conda-package-streaming-0.7.0| pyhd8ed1ab_1 17 KB conda-forge cryptography-38.0.4 | py38h80a4ca7_0 1.3 MB conda-forge pycosat-0.6.4 | py38h0a891b7_1 108 KB conda-forge python_abi-3.8 | 3_cp38 6 KB conda-forge ruamel.yaml-0.17.21 | py38h0a891b7_2 172 KB conda-forge ruamel.yaml.clib-0.2.7 | py38h1de0b5d_1 143 KB conda-forge zstandard-0.19.0 | py38h5945529_1 374 KB conda-forge ------------------------------------------------------------ Total: 3.8 MB The following NEW packages will be INSTALLED: brotlipy conda-forge/linux-64::brotlipy-0.7.0-py38h0a891b7_1005 certifi conda-forge/noarch::certifi-2022.12.7-pyhd8ed1ab_0 cffi conda-forge/linux-64::cffi-1.15.1-py38h4a40e3a_3 charset-normalizer conda-forge/noarch::charset-normalizer-2.1.1-pyhd8ed1ab_0 colorama conda-forge/noarch::colorama-0.4.6-pyhd8ed1ab_0 conda conda-forge/linux-64::conda-22.11.1-py38h578d9bd_1 conda-package-han~ conda-forge/noarch::conda-package-handling-2.0.2-pyh38be061_0 conda-package-str~ conda-forge/noarch::conda-package-streaming-0.7.0-pyhd8ed1ab_1 cryptography conda-forge/linux-64::cryptography-38.0.4-py38h80a4ca7_0 idna conda-forge/noarch::idna-3.4-pyhd8ed1ab_0 libstdcxx-ng conda-forge/linux-64::libstdcxx-ng-12.2.0-h46fd767_19 pluggy conda-forge/noarch::pluggy-1.0.0-pyhd8ed1ab_5 pycosat conda-forge/linux-64::pycosat-0.6.4-py38h0a891b7_1 pycparser conda-forge/noarch::pycparser-2.21-pyhd8ed1ab_0 pyopenssl conda-forge/noarch::pyopenssl-22.1.0-pyhd8ed1ab_0 pysocks conda-forge/noarch::pysocks-1.7.1-pyha2e5f31_6 python_abi conda-forge/linux-64::python_abi-3.8-3_cp38 requests conda-forge/noarch::requests-2.28.1-pyhd8ed1ab_1 ruamel.yaml conda-forge/linux-64::ruamel.yaml-0.17.21-py38h0a891b7_2 ruamel.yaml.clib conda-forge/linux-64::ruamel.yaml.clib-0.2.7-py38h1de0b5d_1 toolz conda-forge/noarch::toolz-0.12.0-pyhd8ed1ab_0 tqdm conda-forge/noarch::tqdm-4.64.1-pyhd8ed1ab_0 urllib3 conda-forge/noarch::urllib3-1.26.13-pyhd8ed1ab_0 zstandard conda-forge/linux-64::zstandard-0.19.0-py38h5945529_1 zstd conda-forge/linux-64::zstd-1.5.2-h6239696_4 Downloading and Extracting Packages python_abi-3.8 | 6 KB | ########################################################## | 100% conda-package-stream | 17 KB | ########################################################## | 100% conda-package-handli | 247 KB | ########################################################## | 100% brotlipy-0.7.0 | 342 KB | ########################################################## | 100% conda-22.11.1 | 905 KB | ########################################################## | 100% pycosat-0.6.4 | 108 KB | ########################################################## | 100% cffi-1.15.1 | 230 KB | ########################################################## | 100% ruamel.yaml.clib-0.2 | 143 KB | ########################################################## | 100% zstandard-0.19.0 | 374 KB | ########################################################## | 100% cryptography-38.0.4 | 1.3 MB | ########################################################## | 100% ruamel.yaml-0.17.21 | 172 KB | ########################################################## | 100% Preparing transaction: done Verifying transaction: done Executing transaction: done Retrieving notices: ...working... done using environment: r-reticulate conda environment r-reticulate installed install dependencies (yes/no)? Unable to install python modules igraph and leidenalg run in terminal: conda install -n r-reticulate -c conda-forge vtraag python-igraph pandas umap learn python modules igraph and leidenalg installed Warning message: In system2(conda, c("update", "--yes", "--name", "base", "conda")) : error in running command ```

This has now setup a new conda environment r-reticulate. The only thing then left to do is install leidenalg (and numpy) using

conda install -n r-reticulate -c conda-forge leidenalg numpy

after which I can again run

> G <- igraph::graph.famous('Zachary')
> leiden(G)
 [1] 2 2 2 2 4 4 4 2 1 1 4 2 2 2 1 1 4 2 1 2 1 2 1 3 3 3 1 3 3 1 1 3 1 1

but now from my system R, and hence this should also work from RStudio.