pygraphviz icon indicating copy to clipboard operation
pygraphviz copied to clipboard

Multi-edge graph does not work (single edge returned)

Open pawelmarkowski opened this issue 6 years ago • 19 comments

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')]

pawelmarkowski avatar Jul 31 '18 15:07 pawelmarkowski

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

pawelmarkowski avatar Jul 31 '18 15:07 pawelmarkowski

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).

dschult avatar Jul 31 '18 17:07 dschult

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?

pawelmarkowski avatar Jul 31 '18 19:07 pawelmarkowski

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 avatar Jul 31 '18 20:07 dschult

@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

pawelmarkowski avatar Jul 31 '18 21:07 pawelmarkowski

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.

dschult avatar Jul 31 '18 22:07 dschult

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')]

thekswenson avatar Dec 07 '18 18:12 thekswenson

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).

bogdanvuk avatar Jan 17 '19 22:01 bogdanvuk

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.

dschult avatar Jan 18 '19 02:01 dschult

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.

thekswenson avatar Feb 01 '19 14:02 thekswenson

@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 avatar Feb 01 '19 14:02 thekswenson

@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 avatar Feb 05 '19 12:02 bogdanvuk

@bogdanvuk : YES. Thanks very much for the help!

thekswenson avatar Feb 13 '19 15:02 thekswenson

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).

bnlawrence avatar May 07 '19 06:05 bnlawrence

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

corentinbettiol avatar Oct 23 '23 16:10 corentinbettiol

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.

dschult avatar Oct 23 '23 17:10 dschult

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 avatar Oct 24 '23 07:10 corentinbettiol

@corentinbettiol you can try using graphviz with conda-forge too?

MridulS avatar Oct 24 '23 08:10 MridulS

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')]

dschult avatar Oct 26 '23 02:10 dschult