pydot / pydot-ng

Python interface to Graphviz's Dot language compatible with Python 2 nad Python 3
MIT License
22 stars 10 forks source link

[98] possible bug in graph_from_adjacency_matrix #35

Open prmtl opened 9 years ago

prmtl commented 9 years ago

Reported by giampisa...@gmail.com, 2015-01-26T13:22:22Z

Hi in the function graph_from_adjacency_matrix, in order to define edges, a string is concatenated with an integer, which generates an error:

  Edge( node_prefix + node_orig, 
        node_prefix + node_dest) )

(node_prefix is a string, node_orig and node_dest are integers).

This is how you can reproduce the problem:

  1. define an adjacency matrix:
import numpy as np
am = np.array([[0, 1, 0], [1, 0, 0], [0, 1, 1]])
  1. run the function:
import pydot
bdot = pydot.graph_from_adjacency_matrix(am, directed=True)

The expected output is to obtain a bdot object, instead you get:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/dist-packages/pydot.py", line 302, in graph_from_adjacency_matrix
    Edge( node_prefix + node_orig, 
TypeError: coercing to Unicode: need string or buffer, int found

Similarly, if you run

bdot = pydot.graph_from_adjacency_matrix(np.asarray(a), node_prefix='', directed=True)

to avoid eventual problems with unicode, you get:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/dist-packages/pydot.py", line 302, in graph_from_adjacency_matrix
    Edge( node_prefix + node_orig, 
TypeError: cannot concatenate 'str' and 'int' objects

I am using Python 2.7.6, pydot version 1.0.28, on Ubuntu 14.04, 64 bits. From: https://code.google.com/p/pydot/issues/detail?id=98