|
From: | Ahmed Abdeen Hamed |
Subject: | Re: [igraph] appending and coloring nodes |
Date: | Sat, 16 Nov 2013 19:28:22 -0500 |
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 chainreader1 = 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 graphid_gen = UniqueIdGenerator()for name in first_graph.vs[“name”]:id_gen.add(name)# Read the second graphreader = 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 graphn = 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
address@hidden
https://lists.nongnu.org/mailman/listinfo/igraph-help
[Prev in Thread] | Current Thread | [Next in Thread] |