def import_graph(self, graph_loc): extension = graph_loc.split('.')[-1] if extension == 'edgelist': return Graph.Read_Edgelist(graph_loc) elif extension == 'gml': return Graph.Read_GML(graph_loc) elif extension == 'graphml': return Graph.Read(graph_loc) elif extension == 'dl': with open(graph_loc, 'r') as to_read: data_reached = False edge_list = [] for line in to_read: if data_reached: edge = line.split(' ')[0:2] if edge in edge_list or [edge[1], edge[0] ] in edge_list: continue edge_list.append(edge) elif line == 'data:\n': data_reached = True return Graph.TupleList(edge_list, directed=False)
# network file (node 0) base_node = 0 # TODO: Support overriding of settings from caller. Disabled for now. # Overwrite parameter values, when specified in the query # directed_values = ['directed', 'dir', 'd', 'true', 'yes', 'y'] # undirected_values = ['false', 'no', 'n', 'undirected', 'un'] # if request.query.metrics: metrics = request.query.metrics # if request.query.directed.lower() in directed_values: directed = True # if request.query.b_directed.lower() in undirected_values: betweenness_directed = False # if request.query.c_mode: closeness_mode = request.query.c_mode.upper() # if request.query.k_mode: coreness_mode = request.query.k_mode.upper() # if request.query.base_node: base_node = request.query.base_node # Read and parse the network g = G.Read(sys.stdin, 'ncol', directed = directed) if verbose: print 'read network. %d nodes and %d edges' %(g.vcount(), g.ecount()) # Calculate metrics, and generate output header output = 'node' if metrics.find('m') >= 0: if directed: #create an undirected copy of the graph for computing thr Louvain method g_und = g.copy() g_und.to_undirected(mode="collapse") else: g_und = g clustering = g_und.community_multilevel() node_clusters = {}
def read_graph(path_name, format=None): """ Read a graph from a file. It can read some file formats, such as GraphML or CSV. XML-like files such as GraphML have to be written in a standard format. See example below for GraphML files. If format is None, it will be detected automatically. It might cause errors. It is preferred that the format has been set. The function creates a Graph with the igraph library. :param path_name: path of the file :type path_name: str :param format: format of the file :type format: str :return: the graph :rtype: igraph.Graph Here is an example of how the GraphML file has to be written. This GraphML file uses the standards of the igraph.graph.write_graphml() method. :Example: <?xml version="1.0" encoding="UTF-8"?> <graphml xmlns="http://graphml.graphdrawing.org/xmlns" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd"> <key id="v_id" for="node" attr.name="id" attr.type="string"/> <key id="e_weight" for="edge" attr.name="weight" attr.type="double"/> <key id="e_id" for="edge" attr.name="id" attr.type="string"/> <graph id="network_a" edgedefault="undirected"> <node id="n0"> <data key="v_id">1</data> </node> <node id="n1"> <data key="v_id">2</data> </node> <node id="n2"> <data key="v_id">3</data> </node> <edge source="n0" target="n1"> <data key="e_weight">1</data> <data key="e_id">e0</data> </edge> <edge source="n1" target="n2"> <data key="e_weight">1</data> <data key="e_id">e1</data> </edge> <edge source="n2" target="n3"> <data key="e_weight">1</data> <data key="e_id">e2</data> </edge> </graph> </graphml> .. warning: In the GraphML file, the attribute which defines the weights of the edges has to be given the name "weight" with the attribute "attr.name". """ graph = None if format is not None and format.lower() == consts.FILE_FORMAT_CSV: # The separator in CSV files is the comma. graph = Graph.Read_Adjacency(path_name, sep=",", comment_char="#", attribute=consts.EDGE_WEIGHT_ATTR) else: graph = Graph.Read(path_name, format) return graph
''' # graph_driver = 'networkx' graph_driver = 'igraph' print("Using graph driver: ", graph_driver) HOME = os.getenv("HOME") datadir = HOME + "/Research/data/" network = str(sys.argv[1]) edgelist = datadir + "com-" + network + ".ungraph.remapped.txt" # if graph_driver == 'igraph': print("\nStarting igraph preprocessing...") start = T.time() print("Loading %s ..." %edgelist) G = Graph.Read(edgelist,format="edgelist", directed=False) # print("Getting largest connected component...") # C = Graph.components(G,mode="weak") # del G # G = C.subgraph(np.argmax(C.sizes())) # del C # G = G.vs.graph # G = Graph.as_undirected(G) print("Graph properties: ", G.vcount(),G.ecount()) # save graph print("writing graph to file...") G.write_graphmlz(datadir + "igraph_graph_" + network,compresslevel=3) # G.write_edgelist("igraph_edgelist") # # load graph
#full = Graph.Full(100) #full.es["weight"] = rand(full.ecount()) #g = preserve_strength_weights(full, steps_to_recalculate=1) #scatter(sort(full.strength(weights='weight')), sort(g.strength(weights='weight'))) #xlabel('Original') #ylabel('Randomized') #gca().set_aspect('equal') #figure() #scatter(sort(full.es["weight"]), sort(g.es['weight'])) #gca().set_aspect('equal') # <codecell> filename = "Wodehouse.txt" print filename g = Graph.Read(filename, format='pajek') f, weight_controls, topology_controls = rich_club_analysis(g, n_samples=50) savefig(filename[:-4] + '.pdf') nets = { 'empirical': g, 'weight_controls': weight_controls, 'topology_controls': topology_controls } pickle.dump(nets, open(filename[:-4] + ".p", "wb")) # <codecell> filename = "Wodehouse_reweighted.txt" print filename g = Graph.Read(filename, format='pajek') f, weight_controls, topology_controls = rich_club_analysis(g, n_samples=50)
from igraph import Graph, plot, Layout, RainbowPalette from math import sin, cos import collections filename = "higgs-social_network.edgelist" # Read graph graph = Graph.Read(filename, "edgelist") coreness = graph.coreness() vertexes = {} for i in range(0, len(coreness)): vertexes[i] = coreness[i] # sort vertexes = sorted(vertexes, key=vertexes.get, reverse=False) # Set vertexes color pal = RainbowPalette(n=max(coreness)+1) # n=max(coreness)) for v in graph.vs: v["color"] = pal.get(coreness[v.index]) if graph.degree(v.index) <= 19: v["size"] = 32 elif graph.degree(v.index) <= 76: v["size"] = 24 elif graph.degree(v.index) <= 304: v["size"] = 20 elif graph.degree(v.index) <= 1216: v["size"] = 15 elif graph.degree(v.index) <= 4864: