On 26.09.2016 11:06, Patrick Totzke wrote:
Hi again,
I'm using boolean PropertyMaps to represent subsets of graph vertices in combination with `GraphView(g, vfilt=Mask).vertices()` for iteration. I often need the complement of such sets and would like to complement a mask using `numpy.invert`, for efficiency:
""" Mask = graph.new_vertex_property('bool')
# this fails import numpy notm = numpy.invert(M.ma) NotMask = graph.new_vertex_property('bool', vals=notm)
# this works, but is O(|V|) it = imap(lambda x:not Mask[x], graph.vertices()) NotMask = graph.new_vertex_property("bool", vals=it) """
The issue seems to be that in boolean PropertyMap internally uses a PropertyArray of dtype `uint8`, and not `bool`: In the example above (my graph has 6 nodes), Mask.ma is
PropertyArray([0, 0, 0, 0, 0, 0], dtype=uint8)
So of course, numpy.invert(M.ma) yields
PropertyArray([255, 255, 255, 255, 255, 255], dtype=uint8)
and not, as expected, the array with only 1's.
You should use numpy.logical_not() instead.
Is there a reason for using uint8 instead of booleans?
Yes, efficiency. Vector of bools (i.e. vector<bool>) tends to be considerably slower.
And is there a O(1) operation to invert masks?
Of course not, that would be magic. Both logical_lot() and invert() are O(N). They are faster than imap because the loops are performed in C, not python. Best, Tiago -- Tiago de Paula Peixoto <tiago@skewed.de>