Can't plot pomegranate graph (pygraphviz not found)

对着背影说爱祢 提交于 2021-02-19 04:33:06

问题


I can't understand what is going on but I no longer seem to be able to plot a pomegranate graph from inside PyCharm. I'm using conda as package manager and have gone though the usual:

conda install graphviz
conda install python-graphviz

but every time I call model.plot() from inside PyCharm I get

Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "/Applications/PyCharm.app/Contents/helpers/pydev/_pydev_bundle/pydev_umd.py", line 198, in runfile
    pydev_imports.execfile(filename, global_vars, local_vars)  # execute the script
  File "/Applications/PyCharm.app/Contents/helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "/Users/.../xai/import.py", line 36, in <module>
    model.plot()
  File "pomegranate/BayesianNetwork.pyx", line 281, in pomegranate.BayesianNetwork.BayesianNetwork.plot
ValueError: must have pygraphviz installed for visualization

I have obviously already tried installing pygraphviz but it seems to make no difference


回答1:


I've just faced the same problem. I solved it installing the graphviz development package. In openSUSE repository, it is called graphviz-devel.

Its description says:

The graphviz-devel package contains all that's necessary for developing programs that use the graphviz libraries including man3 pages.




回答2:


Faced a similar problem and while the solution from @Baumann did not work for me, installing matplotlib fixed my problem (python 3.6 under Win10 and WSL).

pip install matplotlib

Background for suggesting this: the pomegranate code in BayesianNetwork.pyx catches multiple imports exceptions with the same try statement (see code below available also on github, latest commit f116357 and in my case even though i had installed pygraphviz not having matplotlib installed was resulting in the frustrating exception being raised.

line 40 onwards:

try:
    import tempfile
    import pygraphviz
    import matplotlib.pyplot as plt
    import matplotlib.image
except ImportError:
    pygraphviz = None

then line 222 onwards:

if pygraphviz is not None:
    G = pygraphviz.AGraph(directed=True)

    for state in self.states:
        G.add_node(state.name, color='red')

    for parent, child in self.edges:
        G.add_edge(parent.name, child.name)

    if filename is None:
        with tempfile.NamedTemporaryFile() as tf:
            G.draw(tf.name, format='png', prog='dot')
            img = matplotlib.image.imread(tf.name)
            plt.imshow(img)
            plt.axis('off')
    else:
        G.draw(filename, format='pdf', prog='dot')

else:
    raise ValueError("must have pygraphviz installed for visualization")


来源:https://stackoverflow.com/questions/53630549/cant-plot-pomegranate-graph-pygraphviz-not-found

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!