pygraphviz
pygraphviz copied to clipboard
Multi-edge graph does not work (single edge returned)
Example code (tested on fedora and ubuntu) pygraphviz installed via pip pygraphviz==1.3
` G=pgv.AGraph(strict=False)
G.add_edge('a','b','first')
G.add_edge('a','b','second')
print(sorted(G.edges(keys=True))) ` returns [('a', 'b', 'first')]
Graphviz Architecture: amd64 Multi-Arch: foreign Version: 2.38.0-12ubuntu2.1 Depends: libc6 (>= 2.15), libcdt5, libcgraph6, libexpat1 (>= 2.0.1), libgd3 (>= 2.1.0~alpha~), libgvc6, libgvpr2, libx11-6, libxaw7, libxmu6, libxt
I have verified that this works on other systems -- indeed it is verbatim in the doctest for AGraph.add_edge
. So it has been tested regularly. This is a very strange result.
Your versions of GraphViz and libcgraph, etc look fine. Not sure what else to say -- maybe check that you are doing what you think you are doing (i.e. that strict
worked, etc).
Thanks for the answer ... I do not know what I can say too ... I checked it into two independent machines (private laptop and server) .... Two equal results with missing edge :( On the laptop I use python 3.6, maybe that is the problem?
I was able to reconstruct your output! (using GraphViz 2.40.1 and python 2.7 instead of GraphViz 2.38.0 with python3.6)
Python 3.6 should not be a problem. Python 3.7 runs into a StopIteration error, but otherwise the versions that you can get pygraphviz to install seem to work fine (including python 2.7).
The GraphViz version is likely what you need to watch for. I had GraphViz 2.40.1 instead of 2.38.0. Your version info suggests you have GraphViz 2.38.0 but is it possible you have multiple versions and pygraphviz is getting the libcgraph from a different version of GraphViz? I'm not sure how to check that.
@dschult you are right! On laptop with Fedora I have: graphviz.x86_64 2.40.1-22.fc28 @updates graphviz-devel.x86_64 2.40.1-22.fc28 @updates graphviz-devil.x86_64 2.40.1-22.fc28 @updates graphviz-gd.x86_64 2.40.1-22.fc28 @updates
Should I downgrade graphviz?
Server has keras docker which is based on Ubuntu. I will send my modified Dockerfile on the morning
Yes -- there's a bug that is fixed in 2.42 whenever that gets released. We'll shift to that when its ready. But meanwhile only 2.38 works.
Could you please describe the actions I need to do to downgrade? I've downgraded my graphviz package but still encounter this problem.
dpkg: warning: downgrading graphviz from 2.40.1-5 to 2.38.0-12ubuntu2.1
In [1]: from pygraphviz import *
In [3]: G=AGraph(strict=False)
In [4]: G.add_edge('a','b','first')
In [5]: G.add_edge('a','b','second')
In [6]: sorted(G.edges(keys=True)) Out[6]: [(u'a', u'b', u'first')]
I made it work by downgrading to 2.38. I've built graphviz from source I downloaded from http://www.linuxfromscratch.org/blfs/view/7.9/general/graphviz.html.
When installing pygraphviz using pip, I needed to link against the libraries built in this process, and not the ones provided by my distribution (Ubuntu 18.04).
Should be fixed when version 2.42 comes out. But that doesn't seem to be a quick process. Thanks for confirming that switching to v2.38 works.
Thanks for the hint @bogdanvuk. Could yo please give me more detailed instructions on how to "link against the libraries built in the process"?
I did "make install" on the graphviz files and then "pip install pygraphviz"... I'm not sure how to "link against the libraries built" while using pip.
@dschult : I just put this into ipython3 (python version 3.6.7)
In [1]: from pygraphviz import *
In [2]: G=AGraph(strict=False)
In [3]: G.add_edge('a','b','first')
In [4]: G.add_edge('a','b','second')
In [5]: sorted(G.edges(keys=True))
Out[5]: [('a', 'b', 'first')]
As you can see, it does not seem to be working with python 3.6.7 either. I'm really hard-up for a solution here, as my program is constantly crashing due to this bug.
@thekswenson: The procedure installs graphviz in ~/graphviz-2.38.0
folder. Before using pygraphviz, you will need to set paths as described at the bottom of the procedure. Alternatively, you might also install these libraries system wide, but I haven't tried that.
sudo pip3 uninstall pygraphviz
sudo apt purge libgraphviz-dev
cd ~
wget http://pkgs.fedoraproject.org/repo/pkgs/graphviz/graphviz-2.38.0.tar.gz/5b6a829b2ac94efcd5fa3c223ed6d3ae/graphviz-2.38.0.tar.gz
tar xzf graphviz-2.38.0.tar.gz
cd graphviz-2.38.0
./configure --prefix ~/graphviz-2.38.0
make
make install
export CPATH=~/graphviz-2.38.0/include:$CPATH
export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:~/graphviz-2.38.0/lib/pkgconfig
sudo -E pip3 install --no-cache-dir pygraphviz
Before using pygraphviz, export the following:
export PATH=~/graphviz-2.38.0/bin:$PATH
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:~/graphviz-2.38.0/lib
Hope it helps!
@bogdanvuk : YES. Thanks very much for the help!
I'm on MacOS using an anaconda version of python (and pygraphviz) ... and have this problem. Folks might be interested to know that this bug only manifests if you actually use the key to discriminate between the duplicate edges. If you don't need to refer to it, and don't use the key, the bug doesn't manifest (at least for me).
I have the problem on a clean venv using the latest version of pygraphviz
& graphviz 2.43.0
as well.
edit: okay, maybe not the same error:
Here's what I have:
(.venv) me@computer> cat a.py
import pygraphviz
graph = pygraphviz.AGraph(directed=True)
graph.add_edge("A", "B")
graph.add_edge("A", "B", "edge1")
graph.add_edge("A", "B", "edge2")
(.venv) me@computer> python3 a.py
Traceback (most recent call last):
File "/home/me/a-test-with-pygraphviz/.venv/lib/python3.11/site-packages/pygraphviz/agraph.py", line 509, in add_edge
eh = gv.agedge(self.handle, uh, vh, key, _Action.create)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/me/a-test-with-pygraphviz/.venv/lib/python3.11/site-packages/pygraphviz/graphviz.py", line 118, in agedge
return _graphviz.agedge(g, t, h, name, createflag)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
KeyError: 'agedge: no key'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/me/a-test-with-pygraphviz/a.py", line 4, in <module>
graph.add_edge("A", "B", "dezefref")
File "/home/me/a-test-with-pygraphviz/.venv/lib/python3.11/site-packages/pygraphviz/agraph.py", line 512, in add_edge
eh = gv.agedge(self.handle, uh, vh, key, _Action.find)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/me/a-test-with-pygraphviz/.venv/lib/python3.11/site-packages/pygraphviz/graphviz.py", line 118, in agedge
return _graphviz.agedge(g, t, h, name, createflag)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
KeyError: 'agedge: no key'
(.venv) me@computer> dot -V
dot - graphviz version 2.43.0 (0)
(.venv) me@computer> python3 -m pip freeze | grep pygraphviz
pygraphviz==1.11
Is there a reason you are using Graphviz 2.43.0? The current version is 9.0.0.
I don't know that it makes any difference, but it seems like an obvious place to start.
Is there a reason you are using Graphviz 2.43.0?
Yeah, I'm on ubuntu 20.04 LTS, still on Expanded Security Maintenance (ESM).
The version of graphviz available on Focal Fossa apt repo is still on 2.43.2.
I may need to build a newer version of graphviz myself since it seems that there's no APT archive for an up-to-date graphviz version for older (but still maintained) ubuntu releases.
@corentinbettiol you can try using graphviz with conda-forge too?
Ahhh... whoops... I got distracted by the version number and lost track of the issue itself. My apologies.
To allow multiedges in pygraphviz the AGraph must have its strict
parameter set to False
.
import pygraphviz
A = pygraphviz.AGraph(directed=True, strict=False)
A.add_edge(1, 2, "a")
A.add_edge(1, 2, "b")
A.edges(keys=True) # -> [('1', '2', 'a'), ('1', '2', 'b')]