[BGL] python bindings build graph from edge list
Hi, I was trying to build a graph from an edge list and ran into some problems. The following code throws an exception on write_graphviz(). I think it is because there's no property map called node_id in the graph. import boost.graph as bgl edges = [ ( 'a', 'b' ), ( 'b', 'c' ), ( 'c', 'a' ) ] g = bgl.Graph( edges ) g.write_graphviz( 'bind-copy.dot' ) I get the following: Traceback (most recent call last): File "<stdin>", line 1, in ? KeyError: 'node_id' I couldn't manage to generate the node_id property map automatically. So after you have built the graph with the edge list constructor how do you know which vertex is which? I ended up using the following code to create the graph: def build( edges ): # Create a graph to build g = bgl.Graph() # Construct some property maps node_ids = g.vertex_properties[ 'node_id' ] = g.vertex_property_map( 'string' ) color = g.vertex_properties[ 'color' ] = g.vertex_property_map( 'string' ) centrality = g.vertex_properties[ 'centrality' ] = g.vertex_property_map( 'float' ) weight = g.edge_properties[ 'weight' ] = g.edge_property_map( 'float' ) length = g.edge_properties[ 'len' ] = g.edge_property_map( 'float' ) # create the edges and vertices name_2_vertex = { } for ( name1, name2 ) in edges: if not name_2_vertex.has_key( name1 ): v1 = name_2_vertex[ name1 ] = g.add_vertex() node_ids[ v1 ] = name1 else: v1 = name_2_vertex[ name1 ] if not name_2_vertex.has_key( name2 ): v2 = name_2_vertex[ name2 ] = g.add_vertex() node_ids[ v2 ] = name2 else: v2 = name_2_vertex[ name2 ] e = g.add_edge( v1, v2 ) weight[ e ] = 0.1 length[ e ] = 4 # Color articulation points red for v in bgl.biconnected_components( g ): color[ v ] = 'red' # Assign centrality values to property map bgl.brandes_betweenness_centrality( g, centrality ) return g This code seems to works fine. So I thought I'd post this just to see if I was missing something obvious and/or in the hope someone else might find it useful. Best, John.
Hi John, On Jun 1, 2006, at 4:02 AM, John Reid wrote:
I was trying to build a graph from an edge list and ran into some problems. The following code throws an exception on write_graphviz (). I think it is because there's no property map called node_id in the graph.
import boost.graph as bgl edges = [ ( 'a', 'b' ), ( 'b', 'c' ), ( 'c', 'a' ) ] g = bgl.Graph( edges ) g.write_graphviz( 'bind-copy.dot' )
I get the following: Traceback (most recent call last): File "<stdin>", line 1, in ? KeyError: 'node_id'
I couldn't manage to generate the node_id property map automatically. So after you have built the graph with the edge list constructor how do you know which vertex is which?
Your code above should work properly, but doesn't due to several bugs in the BGL-Python bindings. I have now fixed these bugs in Subversion. In particular: - There is now a name_map parameter for the edge list constructor, which defaults to "node_id". If name_map is not the empty string, the constructor creates a vertex property map g.vertex_properties [name_map] that gives the name of each vertex, e.g., 'a', 'b', or 'c', in your example. - If write_graphviz cannot find the node_id map, it will just use the vertex indices as identifiers in the GraphViz output file. Thanksf or reporting these problems! Doug
Douglas Gregor wrote:
Your code above should work properly, but doesn't due to several bugs in the BGL-Python bindings. I have now fixed these bugs in Subversion. In particular:
Thanks for fixing the bugs. However I can't get anonymous SVN access to work. I've never used svn before so it may just be me but the docs say I should only be challenged for authentication if necessary. Please see following: svn co https://svn.osl.iu.edu/svn/projects/viz/bgl-python Authentication realm: https://svn.osl.iu.edu:443 SourceHaven Password for 'username': Authentication realm: https://svn.osl.iu.edu:443 SourceHaven Username: svn: PROPFIND request failed on '/svn/projects/viz/bgl-python' svn: PROPFIND of '/svn/projects/viz/bgl-python': authorization failed (https://svn.osl.iu.edu) Is the server set up for anonymous access? I was following the instructions I found at: http://www.osl.iu.edu/~dgregor/bgl-python/#download Thanks, John.
On Jun 2, 2006, at 1:02 PM, John Reid wrote:
svn co https://svn.osl.iu.edu/svn/projects/viz/bgl-python Authentication realm: https://svn.osl.iu.edu:443 SourceHaven Password for 'username': Authentication realm: https://svn.osl.iu.edu:443 SourceHaven Username: svn: PROPFIND request failed on '/svn/projects/viz/bgl- python' svn: PROPFIND of '/svn/projects/viz/bgl-python': authorization failed (https://svn.osl.iu.edu)
Is the server set up for anonymous access? I was following the instructions I found at: http://www.osl.iu.edu/~dgregor/bgl-python/#download
Oops, sorry! You should now be able to access the version in Subversion with: svn co https://svn.osl.iu.edu/svn/projects_viz/bgl-python Note that "projects/viz" has changed to "projects_viz". Doug
Hi, I'm afraid now I get the following error... C:\Dev\ThirdParty\boost\bgl-python\svn>svn co https://svn.osl.iu.edu/svn/projects_viz/bgl-python svn: PROPFIND request failed on '/svn/projects_viz/!svn/bln/289' svn: PROPFIND of '/svn/projects_viz/!svn/bln/289': could not connect to server (https://svn.osl.iu.edu) Am I doing something wrong? Thanks, John. Doug Gregor wrote:
On Jun 2, 2006, at 1:02 PM, John Reid wrote:
svn co https://svn.osl.iu.edu/svn/projects/viz/bgl-python Authentication realm: https://svn.osl.iu.edu:443 SourceHaven Password for 'username': Authentication realm: https://svn.osl.iu.edu:443 SourceHaven Username: svn: PROPFIND request failed on '/svn/projects/viz/bgl-python' svn: PROPFIND of '/svn/projects/viz/bgl-python': authorization failed (https://svn.osl.iu.edu)
Is the server set up for anonymous access? I was following the instructions I found at: http://www.osl.iu.edu/~dgregor/bgl-python/#download
Oops, sorry! You should now be able to access the version in Subversion with:
svn co https://svn.osl.iu.edu/svn/projects_viz/bgl-python
Note that "projects/viz" has changed to "projects_viz".
Doug
------------------------------------------------------------------------
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
On Jul 3, 2006, at 5:30 AM, John Reid wrote:
svn: PROPFIND of '/svn/projects_viz/!svn/bln/289': could not connect to server (https://svn.osl.iu.edu)
Am I doing something wrong?
I don't think you're doing anything wrong... but Subversion is unable to connect to our server. It may have been a temporary network error, or perhaps a firewall is blocking traffic on that port. Are you using a recent (1.1 or newer) version of Subversion? Doug
It was a temporary glitch - svn works now. Thx, John. Douglas Gregor wrote:
On Jul 3, 2006, at 5:30 AM, John Reid wrote:
svn: PROPFIND of '/svn/projects_viz/!svn/bln/289': could not connect to server (https://svn.osl.iu.edu)
Am I doing something wrong?
I don't think you're doing anything wrong... but Subversion is unable to connect to our server. It may have been a temporary network error, or perhaps a firewall is blocking traffic on that port. Are you using a recent (1.1 or newer) version of Subversion?
Doug
participants (3)
-
Doug Gregor
-
Douglas Gregor
-
John Reid