On 11/30/2010 06:00 PM, Derek Ditch wrote:
I read in another thread about how someone wanted to do reverse lookup
in property maps. My project is similar, and I'm looking to use the
vertex/edge filters for achieving this. I don't quite understand
exactly the best way to use them though.
Let's say I have a vertex property called 'color'. I have red, green,
and blue vertices. If I want to mask the graph so as to only consider
colors = g.vertex_properties['color'].get_array()
filter = numpy.ndarray( colors.size, dtype=bool )
filter = [ x == "red" for x in colors ]
g.set_vertex_filter( filter )
You have to pass a PropertyMap instance to g.set_vertex_filter(), not an
ndarray. And you can't get an ndarray for a property map which has a
non-scalar type (such as string). Therefore the correct way would be
something like this:
colors = g.vertex_properties['color']
filter = g.new_vertex_property("bool")
filter.a = [color[v] == "red" for v in g.vertices()]
Is this correct and the most efficient way to do this?
working on a very large dataset with comparisons that aren't as
It all depends, really. If you intend to change the filter several
times, this is probably the best method. If you want to just do it once,
and do lots of computations afterwards, you are probably better off
removing the filtered vertices, and working with a non-filtered graph.
And just for clarification, does this mask out edges
attached to green
and blue vertices so that algorithms won't even see that they exist?
For instance, pagerank won't consider these edges when performing the
random walk. Also, let's say that I apply an edge filter instead, the
resulting graph will still contain all the vertices, even if they're
not connected to anything.
Well, the code above masks outs _vertices_ not edges, except of course
those which are incident on masked vertices. But otherwise your
interpretation is correct.
Tiago de Paula Peixoto <tiago(a)skewed.de>