def make_graphml_tensor_graph(graphs): """ Make a tensor graph from a series (may be just 2) of graphs. @param graphs: A list of fully qualified file names for graphs. @param output: The name of the tensor graph output. """ base_graph = igraph_io.read_arbitrary(graphs[0], informat="graphml") for attr in base_graph.attributes(): del attr # get rid of attrs other than weight ecount = base_graph.ecount() # This is where edge attributes for appended graphs should start base_graph.es["weight0"] = base_graph.es["weight"] base_graph["time_idx"] = "0: %s" % os.path.splitext(os.path.basename(graphs[0]))[0] base_graph["num_edges"] = "0: %d" % base_graph.ecount() # Keep track of all the graphs in a del base_graph.es["weight"] # get rid of old attr for time_slot, graph_fn in enumerate(graphs[1:]): # Note start index g = igraph_io.read_arbitrary(graph_fn, informat="graphml") base_graph += g.get_edgelist() # Add new edges to current graph at eid old_ecount:new_ecount new_edges = [None]*ecount # Mask out all old edges ecount = base_graph.ecount() # Update edge count new_edges.extend(g.es["weight"]) # Assemble new edge weights for eids base.old_ecount:base.new_ecount base_graph.es["weight%d" % (time_slot+1)] = new_edges base_graph["time_idx"] += ", %d: %s" % \ (time_slot+1, os.path.splitext(os.path.basename(graphs[time_slot+1]))[0]) base_graph["num_edges"] += ", %d: %d" % (time_slot+1, g.ecount()) return base_graph
def main(): parser = argparse.ArgumentParser( description="Verify that given a big graph I can\ obtain the equivalent small graph") parser.add_argument("biggfn", action="store", help="The big graph filename") parser.add_argument("--b", "--bigformat", default="graphml", help="The format of\ the big graph") parser.add_argument("smallgfn", action="store", help="The small graph filename") parser.add_argument("--s", "--smallformat", default="graphml", help="The format\ of the small graph") parser.add_argument("-a", "--atlasfn", action="store", default=os.path.join(os.path.dirname(__file__), "desikan_atlas.nii"), help="The desikan atlas filename") result = parser.parse_args() gsmall = igraph_io.read_arbitrary(result.smallgfn, result.smallformat) gbig = igraph_io.read_arbitrary(result.biggfn, result.bigformat) atlas = Atlas(result.atlasfn) verify(gsmall, gbig, atlas)
def make_graphml_tensor_graph(graphs): """ Make a tensor graph from a series (may be just 2) of graphs. @param graphs: A list of fully qualified file names for graphs. @param output: The name of the tensor graph output. """ base_graph = igraph_io.read_arbitrary(graphs[0], informat="graphml") for attr in base_graph.attributes(): del attr # get rid of attrs other than weight ecount = base_graph.ecount( ) # This is where edge attributes for appended graphs should start base_graph.es["weight0"] = base_graph.es["weight"] base_graph["time_idx"] = "0: %s" % os.path.splitext( os.path.basename(graphs[0]))[0] base_graph["num_edges"] = "0: %d" % base_graph.ecount( ) # Keep track of all the graphs in a del base_graph.es["weight"] # get rid of old attr for time_slot, graph_fn in enumerate(graphs[1:]): # Note start index g = igraph_io.read_arbitrary(graph_fn, informat="graphml") base_graph += g.get_edgelist( ) # Add new edges to current graph at eid old_ecount:new_ecount new_edges = [None] * ecount # Mask out all old edges ecount = base_graph.ecount() # Update edge count new_edges.extend( g.es["weight"] ) # Assemble new edge weights for eids base.old_ecount:base.new_ecount base_graph.es["weight%d" % (time_slot + 1)] = new_edges base_graph["time_idx"] += ", %d: %s" % \ (time_slot+1, os.path.splitext(os.path.basename(graphs[time_slot+1]))[0]) base_graph["num_edges"] += ", %d: %d" % (time_slot + 1, g.ecount()) return base_graph
def main(): parser = argparse.ArgumentParser( description="Verify that given a big graph I can\ obtain the equivalent small graph" ) parser.add_argument("biggfn", action="store", help="The big graph filename") parser.add_argument( "--b", "--bigformat", default="graphml", help="The format of\ the big graph", ) parser.add_argument("smallgfn", action="store", help="The small graph filename") parser.add_argument( "--s", "--smallformat", default="graphml", help="The format\ of the small graph", ) parser.add_argument( "-a", "--atlasfn", action="store", default=os.path.join(os.path.dirname(__file__), "desikan_atlas.nii"), help="The desikan atlas filename", ) result = parser.parse_args() gsmall = igraph_io.read_arbitrary(result.smallgfn, result.smallformat) gbig = igraph_io.read_arbitrary(result.biggfn, result.bigformat) atlas = Atlas(result.atlasfn) verify(gsmall, gbig, atlas)
def make_mm_tensor_graphs(graphs, output): """ Serial writer for Weighted market martrix tensors to a single file. Format: Note that '# ' - is a comment Example: # 0:file_name0 # 1:file_name1 : : # N:file_nameN source,target,graph#,weight First there is a mapping from graph# 's (described below) to graph file names. @param graphs: A list of fully qualified file names for graphs. @param output: The name of the tensor graph output. """ f = open(output, "wb") for idx, g_fn in enumerate(graphs): f.write("# %d:%s\n" % (idx, os.path.basename(g_fn))) f.write("\n") for idx, g_fn in enumerate(graphs): # Note start index g = igraph_io.read_arbitrary(g_fn, informat="graphml") for edge in g.es: f.write("%d,%d,%d,%d\n" % (edge.source, edge.target, idx, edge["weight"])) # Consider skipping a line here f.close() print "Mission complete"