Ohm Ohm - 7 months ago 41
Python Question

Unite several Graph objects of igraph-python including attributes

I have several subgraphs that I would like to unite back into one graph, currently I save them in a dictionary like:

In [364]: result
Out[364]:
{0: <igraph.Graph at 0x7f5b0f684de0>,
1: <igraph.Graph at 0x7f5b0f684af8>,
2: <igraph.Graph at 0x7f5b0f517050>,
3: <igraph.Graph at 0x7f5b0f517148>,
4: <igraph.Graph at 0x7f5b0f517240>}


Each one of these subgraphs have a the attribute
ind
:

In [367]: result[1].vs['name']
Out[367]: ['633', '634', '971']

In [368]: result[2].vs['name']
Out[368]: ['637']


But when I try to unite them into one igraph.Graph object it seems that they loose the
ind
attribute:

G = igraph.Graph()
G+=result[0]
G+=result[1]
G+=result[2]
G.vs["name"]
Traceback (most recent call last):

File "<ipython-input-370-72297b64297b>", line 1, in <module>
G.vs["name"]

KeyError: 'Attribute does not exist'


What am I doing wrong here?

Here is a sketch of what I am trying to do:

import igraph
sub1 = igraph.Graph.Full(3)
sub1.vs["name"] = ["1", "2", "3"]
sub2 = igraph.Graph.Full(2)
sub2.vs["name"] = ["4", "5"]
result = [sub1,sub2]
G = igraph.Graph()
G += result[0]
G += result[1]
G.vs["name"]
Traceback (most recent call last):

File "<ipython-input-15-bc406e721319>", line 1, in <module>
G.vs["name"]

KeyError: 'Attribute does not exist'

Answer

You aren't doing anything wrong here; unfortunately the Python interface does not support preserving the vertex attributes when a graph is added to another one. You have to add the attributes separately:

G = Graph()
G += result[0]
G += result[1]
G += result[2]
G.vs["ind"] = sum((subgraph.vs["ind"] for subgraph in result), [])