This article summarizes how to install the network analysis library graph-tool. This library is similar to networkx and is a library that implements functions for manipulating graph data and calculating network features from python.
While networkx is implemented in pure python, graph-tool is implemented in C ++ using OpenMP, so the calculation is very fast, which is an advantage over networkx.
On the other hand, it cannot be installed by simply pip install
like the pure python library, and it depends on many libraries including new C ++ specifications and GUI libraries, so it is very difficult to build manually. difficult.
As of 2020.10.1, the latest version 2.35 can be installed with a package management tool, which makes the installation much easier. Here is a summary of the steps to install on macOS.
I usually use pyenv and pipenv for python version control and package management, so I'll show you how to use them as much as possible.
The installation method is summarized on this page. https://git.skewed.de/count0/graph-tool/-/wikis/installation-instructions
Roughly speaking, there are the following methods.
--How to use docker --How to use anaconda --How to install on your system with homebrew --Manual build
After trying various things, each has advantages and disadvantages, but the method using ** conda seems to be the easiest and most practical **. (In the first place, I personally don't like conda and manage the virtual environment with pipenv, but it was still easy to use conda)
Here, the installation method is described about how to use docker and how to use conda.
--Advantage: The easiest to use --Disadvantage: It's hard to put in additional packages
First, download the image. In addition to graph-tool, numpy and jupyter are already installed in this docker image.
$ docker pull tiagopeixoto/graph-tool
To start it, execute the following command.
$ docker run -p 8888:8888 -p 6006:6006 -it -u user -w /home/user tiagopeixoto/graph-tool bash
$ jupyter notebook --ip 0.0.0.0
Now you can use jupyter by accessing http: // localhost: 8888 browser.
When you're done, press Ctrl-C to quit jupyter and quit the shell.
Since the container itself remains even after exiting, you can restart the container with docker start <container>
, and more
$ docker exec -it -u user <container> jupyter notebook --ip 0.0.0.0
You can restart jupyter in the same way with.
In actual operation, it is often easier to mount the directory at hand as follows.
$ docker run -p 8888:8888 -p 6006:6006 -it -u user -w /home/user -v $(pwd):/home/user tiagopeixoto/graph-tool bash
This method is good until you first start using it, but it gets harder if you try to customize it a bit.
I tried to add an additional package with pip
, but I couldn't use the pip
command in this image. I tried to install jupyter's nb extensions, but I didn't know how to install it.
--Advantage: Easy to introduce other packages with conda --Disadvantage: Cannot be unified when using other tools such as pipenv
Here, install anaconda using pyenv.
$ pyenv install anaconda3-2020.02
$ pyenv local anaconda3-2020.02 #Use the anaconda you just installed below
Then install the graph-tool (and other) packages with the conda
command.
$ conda create --name gt -c conda-forge graph-tool # `gt`Build a virtual environment named conda-from forge graph-install tool
$ conda install -n gt -c conda-forge ipython jupyter #Do this when installing additional packages
As a caveat here, be sure to add -c conda-forge
to specify that you want to install the package from conda-forge.
Also, it is good to specify -c conda-forge
when installing additional packages as well as graph-tool. It seems that the default Anaconda official package and graph-tool often conflict.
The usual way to use the built virtual environment gt
is to conda activate
, but to use the conda activate
command, edit ~ / .zshrc
. There is a need.
I don't want to change the existing environment so much, so I decided to use pyenv
to switch the environment as follows.
When you execute pyenv versions
, you can see that the virtual environment created earlier is created with the name ʻanaconda3-2020.02 / envs / gt. Therefore, switch this environment with a command such as
pyenv local`.
$ pyenv local anaconda3-2020.02/envs/gt
As a test, let's run the following test code.
hello.py
import graph_tool.all as gt
g = gt.Graph()
v1 = g.add_vertex()
v2 = g.add_vertex()
e = g.add_edge(v1, v2)
gt.graph_draw(g, vertex_text=g.vertex_index, output="two-nodes.pdf")
$ python hello.py
Success if the following image file is created with the name "two-nodes.pdf" after execution.
Recommended Posts