Hello!
Unfortunately the Python interface is currently lacking such a
function, but you can work around this if the graphs involved have the
same number of vertices. Basically, what you need to do is to use the
permute_vertices() method on one of your graphs and permute its
vertices such that the IDs will be in the same order as in the
original graph, then use union(). If the ID sets of the two graphs are
different (i.e. only partially overlapping), then it is a bit more
complicated because first you need to extend both graphs with isolated
vertices corresponding to the "missing" IDs so they have the same ID
set, and then call permute_vertices() on one of them, and then finally
call union().
Attached is a Python script containing a union_by_attr() function that
does what you probably need. It is incomplete because it does not copy
any other attributes from the original graphs to the union (i.e.
attributes are lost because union() does not support attribute
unification yet). Copying the vertex attributes should be
straightforward, though.
T.
On Thu, Oct 6, 2016 at 9:32 AM, Kuu <[email protected]> wrote:
> Hello!
>
> I have two graphs and each of them have vertices with the "id" attribute.
>
> I want to merge both of them by this attribute but I cannot find a method
> for doing so (I'm on Python btw). I saw union and compose methods, but they
> seem to join vertices by index, I'm not sure about this as it is not
> explained in the docs, but my tests seem to point that.
>
> So my questions: how do compose/union methods work? Is there a way of doing
> the merge by id/name attribute instead of the vertex index?
>
> Thank you
>
> Regards,
> Javier
>
>
> _______________________________________________
> igraph-help mailing list
> [email protected]
> https://lists.nongnu.org/mailman/listinfo/igraph-help
>
#!/usr/bin/env python
from igraph import Graph
def unify_vertex_ids(g1, g2, attr):
"""Given two graphs g1 and g2, extends them with isolated vertices and
permutes the vertices of g2 to ensure that both graphs have the same
vertex ID set in the same order, assuming that the vertex attribute
named ``attr`` stores the vertex IDs.
"""
ids1, ids2 = g1.vs[attr], g2.vs[attr]
assert len(set(ids1)) == len(ids1), "Vertex IDs in g1 must be unique"
assert len(set(ids2)) == len(ids2), "Vertex IDs in g2 must be unique"
ids_missing_from_g1 = sorted(set(ids2) - set(ids1))
ids_missing_from_g2 = sorted(set(ids1) - set(ids2))
if ids_missing_from_g1:
g1 = g1.copy()
n1 = g1.vcount()
g1.add_vertices(len(ids_missing_from_g1))
g1.vs[n1:][attr] = ids_missing_from_g1
if ids_missing_from_g2:
g2 = g2.copy()
n2 = g2.vcount()
g2.add_vertices(len(ids_missing_from_g2))
g2.vs[n2:][attr] = ids_missing_from_g2
reverse_index = dict((v, k) for k, v in enumerate(g1.vs[attr]))
permutation_vector = [reverse_index[id] for id in g2.vs[attr]]
g2 = g2.permute_vertices(permutation_vector)
return g1, g2
def union_by_attr(g1, g2, attr):
"""Takes the union of two graphs g1 and g2 based on the given vertex
attribute.
"""
g1, g2 = unify_vertex_ids(g1, g2, attr)
result = g1.union(g2)
result.vs[attr] = g1.vs[attr]
return result
g1 = Graph.GRG(100, 0.2)
g1.vs["id"] = [i * 3 for i in xrange(100)]
g2 = Graph.GRG(100, 0.2)
g2.vs["id"] = [i * 5 for i in xrange(100)]
merged_graph = union_by_attr(g1, g2, "id")
print(merged_graph.vs["id"])_______________________________________________
igraph-help mailing list
[email protected]
https://lists.nongnu.org/mailman/listinfo/igraph-help