0

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?

upe
  • 1,862
  • 1
  • 19
  • 33
Tobias Hagge
  • 231
  • 1
  • 8

1 Answers1

1

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

Tiago Peixoto
  • 5,149
  • 2
  • 28
  • 28
  • Thanks! For what it's worth, in my application, I'm passed gv1 and a helper function for "creating" new neighbor nodes, and I have to be agnostic about whether the helper function is creating the nodes or just exposing them via a GraphView. It appears that maintaining this agnosticism is possible but requires a bit of care. – Tobias Hagge Oct 01 '17 at 01:17