def task2_1(): g = Graph(directed=False) g = g.Load('./edges.txt', format='edgelist', directed=False) print("num. edges:", len(g.es())) print("num. vertices: ", len(g.vs())) print("diameter: ", g.diameter()) print("transitivity :", g.transitivity_undirected()) print("degree distr. :", g.degree_distribution()) print("degree : ", g.degree()) plot(g, layout = g.layout_kamada_kawai()) prs = g.pagerank(); prs = [prs[i]*500 for i in range(0,len(prs))] plot(g, vertex_size = prs)
def task2_2(): g = Graph(directed=False) g = g.Load('edges.txt', format='edgelist', directed=False) com = g.community_edge_betweenness() clust = com.as_clustering() print 'largest community size:', clust.giant().vcount() com_sizes = map(lambda s: s.vcount(), clust.subgraphs()) plt.hist(com_sizes, bins=max(com_sizes)) plt.title('community size distribution') plt.show(block=False) plot(clust, layout=g.layout_kamada_kawai())
def task2_2(): g = Graph(directed=False) g = g.Load('./edges.txt', format='edgelist', directed=False) com = g.community_edge_betweenness() comC = com.as_clustering() comSizes = comC.sizes() print("Size of largest community:", max(comSizes)) plt.hist(comSizes, rwidth = 0.5) plt.ylabel('Number of communities') plt.xlabel('Community size') plt.show() plot(comC, layout = g.layout_kamada_kawai(), orientation='bottom-top') print ('Clusters:', com.optimal_count)
def task2_1(): g = Graph(directed=False) g = g.Load('edges.txt', format='edgelist', directed=False) print '{} {}'.format('#edges:', g.ecount()) print '{} {}'.format('#nodes:', g.vcount()) print '{} {}'.format('diameter:', g.diameter()) print '{} {:.3f}'.format('transitivity', g.transitivity_undirected()) degrees = g.degree(g.vs) plt.hist(degrees, bins=g.maxdegree()) plt.title('degree distribution') plt.show(block=False) weigths = g.pagerank() g.vs['size'] = map(lambda x: x * 1500, weigths) plot(g, layout=g.layout_lgl())
def main(argv): if len(argv) == 0: sys.exit(0) try: opts, args = getopt.getopt(argv, "hi:o:", []) except getopt.GetoptError: print(USAGE) sys.exit(2) for opt, arg in opts: if opt in ('-h', '--help'): print(USAGE) sys.exit() elif opt in ('-i'): input_graph_path = arg elif opt in ('-o'): output_path = arg g = Graph.Load(input_graph_path) json_g = convert_to_JSON(g) with open(output_path, 'w') as f: json.dump(json_g, f)
def show_graph(input_graph_path): """ Shows a graph using the PyCairo library. Some default viewing options are currently applied TODO: Support the configuration of viewing options :param input_graph_path: The path to the graph to show """ g = Graph.Load(input_graph_path) node_colors = {"COMMIT": "black", "COMMENT": "pink", "ISSUE": "orange", "USERLIKE": "red", "PULL": "blue", "REPO": "pink"} commit_nodes = [node.index for node in g.vs if node['type'] == 'COMMIT'] g.delete_vertices(commit_nodes) graph_style = {"vertex_size": 20, "vertex_color": [node_colors[node_type] for node_type in g.vs["type"]], "vertex_label": g.vs["label"], "edge_width": [1 * edge_flow for edge_flow in g.es['forwardFlow']], "layout": g.layout("fr"), "bbox": (1000, 1000), "margin": 100} plot(g, **graph_style)
import os import igraph from igraph import Graph import MySQLdb dbh = MySQLdb.connect(host='xor', db='php_info', user='******') INFILE = "output/small_jacc_70.net" OUTFILE_SVG = "out_jax_smaller_100_large.svg" OUTFILE_PNG = "large_fully_similar.png" #g = Graph.Load("output/SimpleModuleGraphALL.net") g = Graph.Load(INFILE) #g = igraph.load("output/SimpleModuleGraph.net") #print g.es #print g.es["weight"] #print g.cliques() print len(g.vs) # delete edges that have low weight to_del = [] for e in g.es: # print e if (e['weight'] < 0.98): to_del.append(e.index) g.delete_edges(to_del) g.simplify() sub_g = g.subgraph(g.vs.select(_degree_gt=0)).decompose(minelements=20)
nx.write_edgelist(G, f, comments="Node->Node edges", data=False) f.close() print("Total time is %.5f sec" % (T.time() - start)) # # read saved graph # print("Reading graph...") # start = T.time() # Gnew = nx.read_edgelist(saved_edges,nodetype=int) # print("Total time is %.5f sec" %(T.time()-start)) # Using igraph if graph_driver == 'igraph': print("\nStarting igraph preprocessing...") start = T.time() from igraph import Graph G0 = Graph.Load(edgelist, format="edgelist") C = subg = Graph.components(G0, mode="weak") G = C.subgraph(np.argmax(C.sizes())) G = G.vs.graph G = Graph.as_undirected(G) print(G.vcount(), G.ecount()) # save graph G.write_graphmlz(datadir + "igraph_graph") # G.write_edgelist("igraph_edgelist") # # load graph # G = Graph.Load("igraph_graph",format="graphmlz") # # G = Graph.Load("igraph_edgelist",format="edgelist") # nodes = G.vs.indices # print(np.amax(nodes) + 1 == G.vcount())
def load(g_path, f_path, meta): wg = WikiGraph() wg._page_finder = pickle_load(f_path) graph_format = meta["graph_format"] wg.g = Graph.Load(g_path.open("rb"), format=graph_format) return wg
# use h5py to save and retrieve data f = h5py.File("D.hdf5", "w", libver='latest') D = f.create_dataset("D", (nland, nnodes), data=D_np, dtype=datatype, compression="gzip") print("Total time to fill in D is %.5f sec" % (T.time() - start)) # close f.close() if graph_driver == 'igraph': # load from igraph G2 = Graph.Load(datadir + "igraph_graph", format="graphmlz") # G2 = Graph.Load("igraph_edgelist",format="edgelist") G2 = Graph.as_undirected(G2) # avoid inf when calculating distances nodes = G2.vs.indices nnodes = len(nodes) print("Nodes ids are in order = ", np.amax(nodes) + 1 == G2.vcount()) # get node degrees for each index degrees = G2.vs.degree() node_degree_map = dict(zip(nodes, degrees)) ndm_sorted_by_deg = { k: v for k, v in sorted( node_degree_map.items(), key=lambda x: x[1], reverse=True) }
import os import igraph from igraph import Graph import MySQLdb import sys dbh = MySQLdb.connect(host='localhost', db='php_info', user='******') INFILE = "06attr" OUTFILE_SVG = "attr_graph_06.svg" OUTFILE_PNG = "attr_graph_06.png" g = Graph.Load("06attr.net") #g = Graph.Read_Ncol(INFILE) #print g #print g.is_directed() #g.to_undirected(False) #g = g.to_undirected() #g = Graph.Load("output/SimpleModuleGraphALL.net") #g = igraph.load("output/SimpleModuleGraph.net") #print g.es #print g.es["weight"] #print g.cliques() print g.is_directed() print len(g.vs) # delete edges that have low weight