Hi,  

> How do you append to a graph from a file?
>  
> Here is a scenario of what I am trying to do I have two different csv files:
1. How about concatenating the two files first and then loading the combined 
file?  

2. If you don’t want to concatenate the physical files, you can still 
concatenate the CSV reader objects that you use to read them:

from itertools import chain

reader1 = csv.DictReader(open(“file1.csv”))
reader2 = csv.DictReader(open(“file2.csv”))

g = Graph.DictList(vertices=None, edges=chain(reader1, reader2))

3. If you already have a graph and you want to add edges from another file to 
it, you will need a lookup table that 1) maps vertex names from the first graph 
to the corresponding vertex IDs, 2) is able to create new IDs for vertex names 
from the second that it hasn’t seen yet. igraph’s UniqueIdGenerator object can 
help you with that:

# Create the UniqueIdGenerator and pre-load it with the names from the first 
graph
id_gen = UniqueIdGenerator()
for name in first_graph.vs[“name”]:
    id_gen.add(name)

# Read the second graph
reader = csv.DictReader(open(“file2.csv”))
new_edges = []
for row in reader:
    new_edges.append(id_gen[row[“source”]], id_gen[row[“target”]])

# Add the new vertices and edges to the graph
n = g.vcount()
if n < len(id_gen):
    g.add_vertices(len(id_gen) - n)
    g.vs[“name”] = id_gen.values()
g.add_edges(new_edges)

I know that it’s a bit cumbersome; the Python interface desperately needs a 
Graph.union_by_name() function but I haven’t had time to add it yet.

—  
T.

_______________________________________________
igraph-help mailing list
[email protected]
https://lists.nongnu.org/mailman/listinfo/igraph-help

Reply via email to