Search code examples
pythongraph-tool

Do graph_tool property maps work in GraphViews?


The GraphView object in the python graph_tool package does not seem to work as I expect when the base graph is itself a GraphView object. Here is some code that shows the issue:

from graph_tool import *
import numpy as np

g = Graph()
p1 = g.new_vertex_property("bool")
gv1 = GraphView(g, p1)
p2 = gv1.new_vertex_property("bool")
gv2 = GraphView(gv1, p2)

print gv1.num_vertices()

This prints 0 as expected; we haven't added any vertices.

v = g.add_vertex()
p1[v] = True
print gv1.num_vertices()

This prints 1 as expected. Changing the property map updates the view.

for w in gv1.vertices():
    p2[w] = True
print gv2.num_vertices()

This prints 0, which I did not expect. Changing the property map doesn't seem to update the view.

p2[v] = True
print gv2.num_vertices()

Using the vertex objects from g instead of gv1 doesn't seem to help; 0 is again printed.

gv2 = GraphView(gv1, p2)
print gv2.num_vertices()

This prints 1, suggesting that the problem is not with the property map but, somehow, the view.

What am I missing?


Solution

  • When GraphView objets are composed, i.e. generated from another GraphView object, the underlying property maps need to be combined, using numpy.logical_and(), which means it needs to be copied. So, in your example, the internal property map used by gv2 will be a copy of p2 combined with p1, so that if you modify either p1 or p2, it will no longer affect gv2, after it's created.

    If you want to alter the map used by gv2, you can obtain it with gv2.get_edge_filter().