def read(dir, keys=[]): assert os.path.exists(dir) if len(keys)==0: keys = inspect(dir) datadict = {} for key in keys: fname = os.path.join(dir,key+'.npy') if os.path.exists(fname): datadict[key] = np.load(fname) fname = os.path.join(dir,'ma_segment_graph.pickle') if os.path.exists(fname): datadict['ma_segment_graph'] = igraph.read(fname) fname = os.path.join(dir,'ma_segment_lidx.pickle') if os.path.exists(fname): datadict['ma_segment_lidx'] = pickle.load(open(fname, 'rb')) fname = os.path.join(dir, 'ma_clusters') if os.path.exists(fname): datadict['ma_clusters'] = [] for f in glob(os.path.join(fname, '*.pickle')): datadict['ma_clusters'].append(igraph.read(f)) return datadict
def main(): g1 = igraph.read("/Users/irene/Desktop/pfe/arguments_politique start.gml", format= "gml") g2 = igraph.read("/Users/irene/Desktop/pfe/arguments_politiques_complets.gml", format = "gml") g3 = igraph.read("/Users/irene/Desktop/pfe/arguments_politique1.gml", format = "gml") gcomp1 = get_giant_component(g1) gcomp2 = igraph.Graph.induced_subgraph(g2,gcomp1.vs["name"]) print graph_summary(g1) print graph_summary(g2)
def construir_red_autocorrelacion(self, conjuntos, nombre, contar_coinc, etiquetas_nodos): '''Construye la red de autocorrelación de un conjunto de conjuntos Parámetros Conjuntos es una lista de listas''' print(len(conjuntos)) cantidad_nodos = len(conjuntos) red = open(nombre + ".net", 'w') red.write("*Vertices " + str(cantidad_nodos) + "\n") if etiquetas_nodos != None: for i, etiqueta in enumerate(etiquetas_nodos): red.write(str(i + 1) + ' "' + etiqueta + '"\n') red.write("*Edges\n") for i in range(cantidad_nodos - 1): for j in range(i + 1, cantidad_nodos): #print(i,j) coinc = contar_coinc(conjuntos[i], conjuntos[j]) if (coinc > 0): print(i, j, " ", coinc) red.write( str(i + 1) + " " + str(j + 1) + " " + str(coinc) + "\n") red.close() return igraph.read(nombre + ".net", format="pajek"), red.name
def main(): parser = argparse.ArgumentParser(description="""Run CONGO from the command line. Mostly meant as a demo -- only prints one cover.""") parser.add_argument('-d', '--demo', action='store_true', help="""Run a demo with the famous Zachary's Karate Club data set. Overrides all other options.""") parser.add_argument('-l', '--label', default='CONGA_index', nargs='?', const='label', help="""Choose which attribute of the graph to print. When this option is present with no parameters, defaults to 'label'. When the option is not present, defaults to the index.""") parser.add_argument('-n', '--num_clusters', type=int, help="""Specify the number of clusters to use.""") parser.add_argument('-w', '--height', default=2, type=int, help="""The lengh of the longest shortest paths that CONGO considers.""") parser.add_argument('file', nargs='?', help="""The path to the file in igraph readable format.""") args = parser.parse_args() if args.demo: run_demo() return if not args.file: print("CONGO.py: error: no file specified.\n") print(parser.parse_args('-h'.split())) return # only works for undirected G = ig.read(args.file).as_undirected() result = congo(G, args.height) if args.num_clusters: result.pretty_print_cover(args.num_clusters, label=args.label) else: result.pretty_print_cover(result.optimal_count, label=args.label)
def main(): with rasengan.tictoc("Loading Graph"): graph = igraph.read(args.graph_fn) graph.to_undirected(mode="collapse", combine_edges=dict(weights="first")) with rasengan.tictoc("Creating Adjacent Node List"): adjacent_edge_list = graph.get_inclist() adjacent_node_list = graph.get_adjlist() # 1234 is just a random number. assert (adjacent_node_list[1234][0] in graph.es[adjacent_edge_list[1234][0]].tuple) total_vertices = float(len(adjacent_edge_list)) with rasengan.tictoc("Creating Local Node Prob List"): edge_prob_list = [] for idx, edges in enumerate(adjacent_edge_list): if idx % 1000 == 0: print idx / total_vertices * 100 weights = np.array(graph.es[edges]["weight"]) edge_prob_list.append(weights / weights.sum()) queries = read_queries(args.query_fn) for qid, query in queries.iteritems(): for start in query: weighted_random_walk(graph, start, adjacent_node_list, edge_prob_list, path_maxlength=args.path_maxlength, n_runs=args.n_runs)
def svg_from_graph(topo_file="topology.graphml", gfx_file="graph.svg", layout="lgl", width=800, height=800, node_size=14, node_color="lightblue", edge_color="grey", label_key="id"): g = igraph.read(topo_file) # g.to_undirected() # Use this if you want the graph to be undirected # Example of alternative layout: g.layout_reingold_tilford(root=0) width = int(width) height = int(height) node_size = int(node_size) node_colors = [node_color] * len(g.vs) edge_colors = [edge_color] * len(g.es) node_labels = [v[label_key][:] for v in g.vs] g.write_svg(gfx_file, layout=layout, width=width, height=height, vertex_size=node_size, font_size=node_size, labels=node_labels, colors=node_colors, edge_colors=edge_colors)
def igraph_draw_traj(filname,pold,polar=True,layout=None): import igraph as ig g = ig.read(filname,format="graphml") pols=[] for i in g.vs: pols.append(pold[i['id']]) # print pols if polar: rgbs = [(1-(i+1.)/2,(i+1.)/2,0) for i in pols] else: rgbs = [(1-i,i,0) for i in pols] # print filname GGG=nx.read_graphml(filname) g.vs["label"] = GGG.nodes() visual_style = {} visual_style["vertex_size"] = 15 visual_style['vertex_color']=rgbs#'pink' visual_style['vertex_label_size']='10' visual_style["vertex_label"] = g.vs["label"] if layout==None: layout=g.layout("kk") # else: visual_style["layout"] = layout visual_style["bbox"] = (700, 700) visual_style["margin"] = 100 return g,visual_style,layout
def compute_wl_embeddings_discrete(data_directory, h): graph_filenames = retrieve_graph_filenames(data_directory) graphs = [ig.read(filename) for filename in graph_filenames] wl = WeisfeilerLehman() label_dicts = wl.fit_transform(graphs, h) # Each entry in the list represents the label sequence of a single # graph. The label sequence contains the vertices in its rows, and # the individual iterations in its columns. # # Hence, (i, j) will contain the label of vertex i at iteration j. label_sequences = [ np.full((len(graph.vs), h + 1), np.nan) for graph in graphs ] for iteration in sorted(label_dicts.keys()): for graph_index, graph in enumerate(graphs): labels_raw, labels_compressed = label_dicts[iteration][graph_index] # Store label sequence of the current iteration, i.e. *all* # of the compressed labels. label_sequences[graph_index][:, iteration] = labels_compressed return label_sequences
def construir_red_autocorrelacion(self, conjuntos, nombre, contar_coinc, etiquetas_nodos): '''Construye la red de autocorrelación de un conjunto de conjuntos en formato pajek y la escribe en un archivo nombre.net Parámetros conjuntos : Lista de listas de identificadores de papers nombre : Nombre del archivo .net contar_coinc: Función para contar los elementos de la intersección entre dos conjuntos cualesquiera etiquetas_nodos : etiqueta de cada nodo de la red que corresponde a cada lista de conjuntos. Retorna Tupla con un objeto igraph que sirve para calcular medidas de la red y el nombre de la red''' print(len(conjuntos)) cantidad_nodos = len(conjuntos) red = open(nombre+".net", 'w') red.write("*Vertices "+str(cantidad_nodos)+"\n") if etiquetas_nodos!=None: for i,etiqueta in enumerate(etiquetas_nodos): red.write(str(i+1)+' "'+etiqueta.encode('utf-8', "ignore")+'"\n') red.write("*Edges\n") for i in range(cantidad_nodos-1): for j in range(i+1, cantidad_nodos): #print(i,j) coinc = contar_coinc(conjuntos[i],conjuntos[j]) if(coinc > 0): print(i,j," ",coinc) red.write(str(i+1)+" "+str(j+1)+" "+str(coinc)+"\n") red.close() return igraph.read(nombre+".net",format="pajek"), red.name
def clustering(filename, counter): G = igraph.read(filename,directed=False,format='ncol') D = G.community_multilevel() MIN_CLUSTER_SIZE = 1 style = {} style['vertex_size'] = 10 kept_clusters = 0 clusters = [] for i in D.subgraphs(): if len(i.vs) > MIN_CLUSTER_SIZE: kept_clusters += 1 clusters.append(i) style["layout"] = i.layout("fr", maxiter=250) # "fr" or "lgl" work nicely igraph.plot(i, 'cluster_visualization/cluster_'+str(counter)+'_'+str(kept_clusters)+'.pdf', **style) print('total clusters -->',len(D.subgraphs())) print('kept clusters -->',kept_clusters) return clusters
def main(gpath): G = ig.read(gpath) components = G.components() nxG = nx.read_gpickle(gpath.replace('.gml', '')) nodes = sorted(nxG.nodes()) node_ix = dict(zip(nodes, range(len(nodes)))) node_id_to_comp = np.zeros((G.vcount(), 2), dtype=int) Ps = [] for i, component in enumerate( sorted(components, key=lambda c: len(c), reverse=True)): sG = G.subgraph(component) if sG.vcount() > 1: print("Component %d" % sG.vcount()) P = sG.shortest_paths_dijkstra() for n in range(sG.vcount()): label = sG.vs[n]['label'] node_n = node_ix[label] assert np.sum(node_id_to_comp[node_n, :]) == 0 node_id_to_comp[node_n, :] = (i, n) Ps.append(P) output_path = "../generated-data/pairwise_features/%s_shortest_path_len_sparse" % ( os.path.basename(gpath).replace('.gml', '')) np.savez(output_path, Ps=Ps, node_id_to_comp=node_id_to_comp)
def main(args): parser = argparse.ArgumentParser() parser.add_argument('input', metavar='graph.pickle', help='Input graph file') parser.add_argument( '--connection', default='postgresql://*****:*****@localhost/osrm', help='Postgres connection string. Default %(default)s' ) parser.add_argument('--verbose', action='store_true', help='Increase logging level') args = parser.parse_args() logging.basicConfig( level=logging.INFO if args.verbose else logging.WARNING) log.info("Creating DB engine") engine = create_engine(args.connection, echo=False) log.info("Creating DB session") Session = sessionmaker(bind=engine) session = Session() log.info("Creating OSRM tables") models.Base.metadata.create_all(engine) session.query(models.OSRMRouteNode).delete() g = igraph.read(args.input) export_nodes(g, session)
def construir_red_autocorrelacion(self, conjuntos, nombre, contar_coinc, etiquetas_nodos): '''Construye la red de autocorrelación de un conjunto de conjuntos Parámetros Conjuntos es una lista de listas''' print(len(conjuntos)) cantidad_nodos = len(conjuntos) red = open(nombre+".net", 'w') red.write("*Vertices "+str(cantidad_nodos)+"\n") if etiquetas_nodos!=None: for i,etiqueta in enumerate(etiquetas_nodos): red.write(str(i+1)+' "'+str(etiqueta)+'"\n') red.write("*Edges\n") for i in range(cantidad_nodos-1): #print i for j in range(i+1, cantidad_nodos): #print(i,j) coinc = contar_coinc(conjuntos[i],conjuntos[j]) if(coinc > 0): #print(i,j," ",coinc) red.write(str(i+1)+" "+str(j+1)+" "+str(coinc)+"\n") red.close() print 'acabó la red' return igraph.read(nombre+".net",format="pajek"), red.name
def main(): parser = argparse.ArgumentParser(description="""Run CONGA from the command line. Mostly meant as a demo -- only prints one cover.""") parser.add_argument('-m', '--modularity_measure', choices=['lazar'], help="""Calculate the modularities using the specified modularity measure. Currently only supports lazar.""") parser.add_argument('-n', '--num_clusters', type=int, help="""Specify the number of clusters to use.""") parser.add_argument('-d', '--demo', action='store_true', help="""Run a demo with the famous Zachary's Karate Club data set. Overrides all other options.""") parser.add_argument('-l', '--label', default='CONGA_index', nargs='?', const='label', help="""Choose which attribute of the graph to print. When this option is present with no parameters, defaults to 'label'. When the option is not present, defaults to the index.""") parser.add_argument('file', nargs='?', help="""The path to the file in igraph readable format.""") args = parser.parse_args() if args.demo: run_demo() return if not args.file: print("CONGA.py: error: no file specified.\n") print(parser.parse_args('-h'.split())) return # only works for undirected G = ig.read(args.file).as_undirected() result = CONGA(G, calculate_modularities=args.modularity_measure, optimal_count=args.num_clusters) result.pretty_print_cover(result.optimal_count, label=args.label)
def convert_graph(gfn, informat, save_dir, *outformats): """ Convert between igraph supported formats. No conversion to MAT or NPY available. Positional arguments: ==================== gfn - the graph file name informat - the input format of the graph save_dir - the directory where we save result to outformat - a list of output formats """ try: if informat in [ "graphml", "ncol", "edgelist", "lgl", "pajek", "graphdb" ]: g = igraph.read(gfn, None) elif informat == "mat": g = csc_to_igraph(loadAnyMat(gfn)) elif informat == "npy": g = csc_to_igraph(np.load(gfn).item()) elif informat == "attredge": g = attredge_to_igraph(gfn) else: err_msg = "[ERROR]: Unknown format '%s'. Please check format and retry!" % informat print err_msg return (None, err_msg) except Exception, err_msg: print err_msg return (None, "[ERROR]: " + str(err_msg))
def igraph_draw_traj(filname, pold, polar=True, layout=None): import igraph as ig g = ig.read(filname, format="graphml") pols = [] for i in g.vs: pols.append(pold[i['id']]) # print pols if polar: rgbs = [(1 - (i + 1.) / 2, (i + 1.) / 2, 0) for i in pols] else: rgbs = [(1 - i, i, 0) for i in pols] # print filname GGG = nx.read_graphml(filname) g.vs["label"] = GGG.nodes() visual_style = {} visual_style["vertex_size"] = 15 visual_style['vertex_color'] = rgbs #'pink' visual_style['vertex_label_size'] = '10' visual_style["vertex_label"] = g.vs["label"] if layout == None: layout = g.layout("kk") # else: visual_style["layout"] = layout visual_style["bbox"] = (700, 700) visual_style["margin"] = 100 return g, visual_style, layout
def main(gpath, steps): G = ig.read(gpath) output = G.community_walktrap(steps=steps) if hasattr(output, 'as_clustering'): output = output.as_clustering() comms = { G.vs['label'][i]: "comm %d" % output.membership[i] for i in range(G.vcount()) } print("Num communities: %d" % (len(np.unique(list(comms.values()))))) comms_to_nodes = defaultdict(set) for g, c in comms.items(): comms_to_nodes[c].add(g) comm_sizes = np.array([len(comms_to_nodes[c]) for c in comms_to_nodes]) print("Median communitiy size: %0.2f" % np.median(comm_sizes)) print("# communities with 1 node: %d" % np.sum(comm_sizes == 1)) output_path = '../generated-data/communities/%s_%dsteps.json' % ( os.path.basename(gpath).replace('.gml', ''), steps) with open(output_path, 'w') as f: json.dump(comms, f)
def svg_from_graph(topo_file="topology.graphml", gfx_file="graph.svg", layout="lgl", width=800, height=800, node_size=14, node_color="lightblue", edge_color="grey", label_key="id"): g = igraph.read(topo_file) # g.to_undirected() # Use this if you want the graph to be undirected # Example of alternative layout: g.layout_reingold_tilford(root=0) width = int(width) height = int (height) node_size = int(node_size) node_colors = [node_color]*len(g.vs) edge_colors = [edge_color]*len(g.es) node_labels = [v[label_key][:] for v in g.vs] g.write_svg(gfx_file, layout=layout, width=width, height=height, vertex_size=node_size, font_size=node_size, labels=node_labels, colors=node_colors, edge_colors=edge_colors)
def write_degree_sequences(gml_dir, deg_dir): gml_df = buildGMLcatalog(gml_dir) fpV = gml_df['fp_gml'] analysis_df = pd.DataFrame(columns=['num_edges', 'Weighted', 'Directed', 'Bipartite', 'Multigraph', 'Multiplex', 'fp_gml', 'n', 'alpha', 'xmin','ntail', 'Lpl', 'ppl', 'dexp', 'dln', 'dstrexp', 'dplwc', 'meandeg']) for fp in fpV: g = igraph.read(fp) #### find what kind of graph this is (follow hierarchical ordering of types) # check first for multiplex row = gml_df[gml_df.fp_gml==fp] if row['Multiplex'].item() == 1: processmultiplex(g,fp, deg_dir, analysis_df) elif row['Bipartite'].item() == 1: processbipartite(g, fp, deg_dir,analysis_df) elif row['Multigraph'].item() == 1: processmultigraph(g, fp, deg_dir,analysis_df) elif row['Weighted'].item() == 1: processweighted(g, fp, deg_dir,analysis_df) elif row['Directed'].item() == 1: processdirected(g, fp, deg_dir,analysis_df) else: readdeg(g, fp,deg_dir,analysis_df) return analysis_df
def CompareWithKSplit(): refGraph = igraph.read(os.path.join(sourcesDir,sourceFunctionName)) refGraph['name'] = sourceFunctionName if (os.name != "nt"): #p = multiprocessing.Pool(initializer=workerInit,initargs=[len(refGraph.vs)]) p = multiprocessing.Pool() mapper = p.imap_unordered #mapper = itertools.imap else: mapper = itertools.imap #workerInit(len(refGraph.vs)) reportFile = CounterXlsReport(csvName + "-K=" + str(myK) + "-" + sourceFunctionName) print "Prepping db - (func files) - target=" +os.path.join(sourcesDir,sourceFunctionName) sourcesList3k = list(mapper(identity,split2k.ksplitGraphObject(myK,refGraph,True))) print "For k=" + str(myK) + " we have - " + str(len(sourcesList3k)) print "end db prep" from simple_db import DBSimpleClient db = DBSimpleClient() params = [doOneFunctionFileRW,itertools.izip(db.get_all(),itertools.cycle([sourcesList3k]))] #if (mapper != itertools.imap): # params.append(50) for allFields in mapper(*params): for fields in allFields: reportFile.writeLine(fields)
def start_create_gml(workfolder): workfolder_gml = gt.createFolder(workfolder + "GML\\") workfolder_att = gt.createFolder(workfolder + "ATT\\") i = 0 for fp in os.listdir(workfolder): fname = os.path.splitext(fp) i += 1 if fname[-1] == '.repost': # or fname[-1]=='.comment': wbfilep = workfolder + fp print i, wbfilep gmlfp = workfolder_gml + fname[0] + '.gml' g = ig.Graph() if not os.path.exists(gmlfp): g = weibo2g.start(wbfilep, gmlfp, addcommentlist=True) #ig.read(gmlfp)# else: g = ig.read(gmlfp) "add time slice function" for percent in percentlist: lengthNow = int(round(len(timelist) * percent)) lengthNow = lengthNow if lengthNow > 1 else 1 timelistPercentNow = timelist[:lengthNow] timelistPeriodNow = selecTime(timelistPercentNow, periodcnt) for timep in timelistPeriodNow: g = g.subgraph_edges(es.select(createdtimetos_le=timep)) #grt.analyzeNetNodes(g,workfolder_att,str(fname[0])) grt.analyzeNetNodes_New(g, workfolder_att, str(fname[0])) print grt.analysisNet(g)
async def query(request): req_file = request.files.get("file") file_name = req_file.name body = req_file.body.decode("unicode_escape") with open("files/" + file_name, "w+") as file: file.write(body) file_path = "files/" + file_name graph = ig.read(file_path, format="graphml") colors = { "#3217A7":10008, "#F0F007": 205005, "White":163001, "#9725A4":135007, "#000000": 164010, "#156911":189010, '#A71717':157000 } for pos, x in enumerate(graph.vs['color']): graph.vs[pos]['color'] = colors[x] for pos, x in enumerate(graph.es['color']): graph.es[pos]['color'] = colors[x] return redirect( graphistry.bind( source='src', destination='dst', point_color='color', edge_color='color' ).plot(graph) )
def main(): graph_file_name = f'{DATA_DIR}/graph_{sys.argv[1]}.gml' if not os.path.isfile(graph_file_name): print('First argument must be name of existing file with model') return start_date = datetime.datetime.now() print('Reading graph...') g = igraph.read(graph_file_name) alg = sys.argv[2] print('Looking for community') if alg == 'lp': lp = find_communities_label_propagation(g) save(g, lp) elif alg == 'ml': ml = find_communities_multilevel(g) save(g, ml) elif alg == 'im': im = find_communities_infomap(g) save(g, im) else: print('Incorrect second argument. Must be one of [lp, ml, im]') return end_date = datetime.datetime.now() print(f'{sys.argv[1]},{alg},{(end_date - start_date).total_seconds()}')
def superFunctionsGenerator(): for exeName in filter(lambda x: inWhiteList(x, 'exe'), os.listdir(targetsDir)): print "loading - " + exeName + " ... " currentExeDir = os.path.join( targetsDir, os.path.join(exeName, functionsGraphsDirectoryName)) for funcFileName in filter( lambda x: inWhiteList( os.path.splitext(x)[0], 'functionName'), filter(lambda x: x.endswith(myExt), os.listdir(currentExeDir))): print "FUNC begin - " + funcFileName + " ... " tarGraph = igraph.read( os.path.join(currentExeDir, funcFileName)) tarGraph['name'] = funcFileName #funcFileName and exe are only for the timeout print yield { 'tarGraph': tarGraph, 'refGraph': refGraph, 'sourcesList3k': sourcesList3k, 'funcFileName': funcFileName, 'exeName': exeName } print "FUNC finished " + funcFileName print "finished loading " + exeName
def convert_graph(gfn, informat, save_dir, *outformats): """ Convert between igraph supported formats. No conversion to MAT or NPY available. Positional arguments: ==================== gfn - the graph file name informat - the input format of the graph save_dir - the directory where we save result to outformat - a list of output formats """ try: if informat in ["graphml", "ncol", "edgelist", "lgl", "pajek", "graphdb"]: g = igraph.read(gfn, None) elif informat == "mat": g = csc_to_igraph(loadAnyMat(gfn)) elif informat == "npy": g = csc_to_igraph(np.load(gfn).item()) else: err_msg = "[ERROR]: Unknown format '%s'. Please check format and re-try!" % informat print err_msg return err_msg except Exception, err_msg: print err_msg return "[ERROR]: "+str(err_msg)
def main(): parser = argparse.ArgumentParser(description="Convert an igraph to a csc object") parser.add_argument("graph_fn", action="store", help="The name of the igraph to read from disk. *Must be gml format!") parser.add_argument("-g", "--gen_graph", action="store_true", help="Generate a new ER graph") parser.add_argument("-n", "--num_nodes", action="store", type=int, help="The number of nodes in the ER graph") parser.add_argument("-p", "--probability", action="store", type=float, help="The probability of connectivity of each node to another in the graph") parser.add_argument("-s", "--save", action="store_true", help="Save conversion to disk") parser.add_argument("-f", "--save_fn", action="store", default="csc_matlab", help="Save file name") parser.add_argument("-t", "--test", action="store_true", help="Run test only!") result = parser.parse_args() if result.test: test() exit(1) if os.path.exists(result.graph_fn): g = igraph.read(result.graph_fn, format="gml") elif result.gen_graph or result.num_nodes or result.probability: assert (result.gen_graph and result.num_nodes and result.probability), "You must set all ER parameters i.e. n, p" g = igraph.Graph.Erdos_Renyi(n=result.num_nodes, p=result.probability) igraph.write(g, result.save_fn+".gml", format="gml") else: sys.stderr.writelines("Invalid path %s ... and all (i.e. n, p, g) ER parameters not set so no action taken. \n EXITING NOW! \n") exit(-1) igraph_to_csc(g, result.save, result.save_fn)
def main(timestamp): nng = ig.read(f'/tmp/{timestamp}_leiden_graph.gml', format='graphml') partition = la.find_partition(nng, la.ModularityVertexPartition) clusters = partition.membership clusters = np.array(clusters).astype(str) np.savetxt(f'/tmp/{timestamp}_leiden_clusters.csv', clusters, delimiter=',', newline='\n', fmt='%s')
def nx_to_igraph(G): temp_dir = tempfile.mkdtemp() tmpfile = os.path.join(temp_dir, 'graph.graphml') nx.write_graphml(G, tmpfile) ig = igraph.read(tmpfile, format="graphml") os.remove(tmpfile) os.rmdir(temp_dir) return (ig)
def main(): G = ig.read("enron.gml") test_degree_centrality_hypothesis(G) test_closeness_centrality_hypothesis(G) test_betweenness_centrality_hypothesis(G) test_eigenvector_centrality_hypothesis(G) test_pagerank_hypothesis(G) test_hits_authority_hypothesis(G)
def max_bcentrality_igraph(filename): G = ig.read(filename) bcs = ig.betweenness(G) index, value = max(enumerate(bcs), key=operator.itemgetter(1)) vs = ig.VertexSeq(G) print "node with max betweenness centrality =", vs[index] print "indegree of this node =", vs[index].indegree() print "outdegree of this node =", vs[index].outdegree()
def main(graph_path, selected_features=None): G = nx.read_gpickle(graph_path) nodes = list(sorted(G.nodes())) nodes_to_features = collections.defaultdict(dict) extract_features_networkx(G, nx_features, nodes_to_features, selected_features) iG = ig.read(graph_path + '.gml') extract_features_ig(iG, ig_features, nodes_to_features, selected_features) # features are ordered according to nodes F = [] feature_labels = [] selected_features = nx_features + ig_features if selected_features is None else selected_features for feature in selected_features: Ff = [] for n in nodes: v = nodes_to_features[n] Ff.append(v[feature]) Ff = np.array(Ff) if len(Ff.shape) == 1: Ff = Ff[:, None] if Ff.shape[1] == 1: feature_labels.append(feature) else: for i in range(Ff.shape[1]): feature_labels.append("%s_%d" % (feature, i)) F.append(Ff) F = np.hstack(F) mu = np.mean(F, axis=0) std = np.std(F, axis=0) print(mu) print(std) # normalize F = stats.zscore(F, axis=0) print(F.shape) print(np.min(F, axis=0)) print(np.max(F, axis=0)) print(np.mean(F, axis=0)) print(np.std(F, axis=0)) print(feature_labels) output_path = '../generated-data/features/%s_topology' % ( os.path.basename(graph_path)) np.savez(output_path, F=F, feature_labels=feature_labels, mu=mu, std=std)
def __init__(self, network=None, time_serie=None, output='network.NET'): self.time_serie = time_serie if network is None: self.network = None self.N = len(self.time_serie) else: self.network = igraph.read(network, format="pajek") self.N = self.network.vcount() self.output = output
def load(filename): print("Loading "+filename) g = ig.read( filename, format="ncol", directed=True, names=True ) g.delete_vertices([0,1]) print("Done") return(g)
def main(): parser = argparse.ArgumentParser( description= """Run CONGA from the command line. Mostly meant as a demo -- only prints one cover.""" ) parser.add_argument( "-m", "--modularity_measure", choices=["lazar"], help="""Calculate the modularities using the specified modularity measure. Currently only supports lazar.""", ) parser.add_argument( "-n", "--num_clusters", type=int, help="""Specify the number of clusters to use.""", ) parser.add_argument( "-d", "--demo", action="store_true", help= """Run a demo with the famous Zachary's Karate Club data set. Overrides all other options.""", ) parser.add_argument( "-l", "--label", default="CONGA_index", nargs="?", const="label", help="""Choose which attribute of the graph to print. When this option is present with no parameters, defaults to 'label'. When the option is not present, defaults to the index.""", ) parser.add_argument( "file", nargs="?", help="""The path to the file in igraph readable format.""") args = parser.parse_args() if args.demo: run_demo() return if not args.file: print("conga.py: error: no file specified.\n") print(parser.parse_args("-h".split())) return # only works for undirected G = ig.read(args.file).as_undirected() result = conga( G, calculate_modularities=args.modularity_measure, optimal_count=args.num_clusters, ) result.pretty_print_cover(result.optimal_count, label=args.label)
def main(): g = igraph.read('tag_wordnet.lgl', 'lgl') # draw graph for v in g.vs: v['label'] = v['name'] p = igraph.drawing.Plot('tag_wordnet.pdf', igraph.drawing.BoundingBox(6000, 6000)) p.add(g) p.save()
def q7(): G = read_gdf("../../data/network_analysis/MiddleEastern.gdf") nx.write_gml(G, "/tmp/MiddleEastern.gml") G = ig.read("/tmp/MiddleEastern.gml") bcs = G.betweenness() labels = [v["label"] for v in G.vs()] label_bcs = sorted(zip(labels, bcs), key=operator.itemgetter(1), reverse=True) ingredient_set = set(["coriander", "garlic", "walnut", "dill"]) print [x for x in label_bcs if x[0] in ingredient_set]
def runSimulation(self, plot=True): full = self.time if plot == True: self.movie = gm.GraphMovie() # attrs = {'fillcolor':fields ,'width':0.5,'height':0.5,'fontsize':0.0,'nodedegreesize':False, 'plotlabel':'agent0_'+str(full-ttime) } # A = nx.to_agraph(net); # 1 A.layout(prog = 'twopi'); while full > 0: if plot == True and self.time == full: self.net = igraph.read(self.string + ".gml") for i, v in enumerate(self.net.vs): v['label'] = str(i) v['color'] = self.fields[0, i] self.movie.addGraph(self.net) elif plot == True and self.time != full: self.net = igraph.read(self.string + ".gml") for i, v in enumerate(self.net.vs): v['label'] = str(i) if i == self.edges[sampvar[0]][0] or i == self.edges[ sampvar[0]][1]: v['color'] = 0.0 else: v['color'] = self.fields[0, i] # e = self.net.es[int(sampvar[0])]; # print e; # print "edges" # print sampvar[0] # print self.edges[sampvar[0]][0]; print self.edges[sampvar[0]][1]; # e['color'] = 0.0; # set edge color self.movie.addGraph(self.net) sampvar = np.random.randint(0, len(self.edges), (1)) # the edge(interaction) sampling process interact = float(self.fields[0, self.edges[sampvar[0]][0]] + self.fields[0, self.edges[sampvar[0]][1]]) / 2 self.fields[0, self.edges[sampvar[0]][0]] = float( self.fields[0, self.edges[sampvar[0]][0]] + interact) / 2 self.fields[0, self.edges[sampvar[0]][1]] = float( self.fields[0, self.edges[sampvar[0]][1]] + interact) / 2 # attrs['fillcolor'] = fields; full = full - 1 # attrs['plotlabel'] = 'agent0_'+str(full-ttime); self.movie.doMovieLayout() self.movie.renderMovie(name=self.string)
def setGraph(self, g: Union[str, Graph]): if isinstance(g, str): g = igraph.read(g) self.g = wrapGraph(g) for mode in self.modes: if mode.onSetGraph(): break self.resetViewRect() self.update()
def simple_read_net(): '''读取pajek格式文件''' g = ig.read("testdata/GR3_60.NET", format="pajek") # 设置边和顶点颜色 g.vs["color"] = "#3d679d" g.es["color"] = "red" graphStyle = {"vertex_size": 12, 'margin': 6} graphStyle["layout"] = g.layout("fr") #设置布局 g.write_svg('GR3_60_graph.svg', width=600, height=600, **graphStyle)
def main(): g = igraph.read('tag_wordnet.lgl', 'lgl') for v in g.vs: v['label'] = v['name'] d = g.community_fastgreedy() p = igraph.drawing.Plot('graphtree.pdf', igraph.drawing.BoundingBox(5000, 13000)) # p.add(g) p.add(d) p.save()
def igraph_from_tuples(v_names): tmp = tempfile.mktemp(suffix='.ncol', prefix='graph_', dir='tmp/') fp = open(tmp, "w") for (name_a, name_b) in v_names: fp.write("{} {}\n".format(name_a, name_b)) fp.close() # will store node names under the 'name' vertex attribute. g = igraph.read(tmp, format='ncol', directed=False, names=True) os.remove(tmp) return g
def to_igraph(graph): # Initialise the optimiser, using default settings import networkx as nx import igraph as ig g = ig.Graph() if anonymize: nx.write_graphml(anonymize(graph), "/tmp/test_graph.graphml") else: nx.write_graphml(graph, "/tmp/test_graph.graphml") return ig.read("/tmp/test_graph.graphml", format='graphml')
def nx_to_igraph(network): try: import igraph except ImportError: raise ImportError('requires igraph ') file_descriptor, file_path = tempfile.mkstemp(suffix='.gml') nx.write_gml(network, file_path) igraph_network = igraph.read(file_path) return igraph_network
def nx_to_igraph(network): try: import igraph except ImportError: raise ImportError('requires igraph ', ) file_descriptor, file_path = tempfile.mkstemp(suffix='.gml') nx.write_gml(network, file_path) igraph_network = igraph.read(file_path) return igraph_network
def CompareWithKSplit(): refGraph = igraph.read(os.path.join(sourcesDir,sourceFunctionName)) refGraph['name'] = sourceFunctionName if (os.name != "nt"): #p = multiprocessing.Pool(initializer=workerInit,initargs=[len(refGraph.vs)]) p = multiprocessing.Pool() mapper = p.imap_unordered #mapper = itertools.imap else: mapper = itertools.imap #workerInit(len(refGraph.vs)) reportFile = CounterXlsReport(csvName + "-K=" + str(myK) + "-" + sourceFunctionName) print "Prepping db - (func files) - target=" +os.path.join(sourcesDir,sourceFunctionName) sourcesList3k = list(mapper(identity,split2k.ksplitGraphObject(myK,refGraph,True))) print "For k=" + str(myK) + " we have - " + str(len(sourcesList3k)) print "end db prep" def inWhiteList(value,atrbName): for whiteListEntry in filter(lambda x: x['source']==sourceName,whiteList): #print "CHECK " + atrbName if whiteListEntry[atrbName] == value: return True return False def superFunctionsGenerator(): for exeName in filter(lambda x:inWhiteList(x,'exe'),os.listdir(targetsDir)): print "loading - " + exeName + " ... " currentExeDir = os.path.join(targetsDir,os.path.join(exeName,functionsGraphsDirectoryName)) for funcFileName in filter(lambda x:inWhiteList(os.path.splitext(x)[0],'functionName'),filter(lambda x:x.endswith(myExt),os.listdir(currentExeDir))): print "FUNC begin - " + funcFileName + " ... " tarGraph = igraph.read(os.path.join(currentExeDir,funcFileName)) tarGraph['name'] = funcFileName #funcFileName and exe are only for the timeout print yield {'tarGraph':tarGraph,'refGraph':refGraph,'sourcesList3k':sourcesList3k,'funcFileName':funcFileName,'exeName':exeName} print "FUNC finished " + funcFileName print "finished loading " + exeName params = [doOneFunctionFileRW,superFunctionsGenerator()] #if (mapper != itertools.imap): # params.append(50) for allFields in mapper(*params): for fields in allFields: reportFile.writeLine(fields)
def make_projection(graph, atts): """ makes bipartite projections, returns seller projection""" # PREPARE EDGE ATTRIBUTES graph.es['val'] = list(atts['vals']) graph.es['hs'] = list(atts['hs']) graph.es['dest'] = list(atts['dest']) graph.es['hss'] = list(atts['hss']) graph.es['dest_source'] = list(atts['dest_source']) graph.es['imp_name'] = list(atts['imp_name']) # PREPARE VERTEX ATTRIBUTES # The strength member function sums all of the edge values graph.vs['val'] = graph.strength(graph.vs, weights='val') # Get list of exporters who sell to the US us_list = what_sellers(graph.es, 'USA') graph.vs['US'] = 0 graph.vs[us_list]['US'] = 1 # Get list of exporters who sell to a seleted foreign coutnry us_list = what_sellers(graph.es, 'VEN') graph.vs['VEN'] = 0 graph.vs[us_list]['VEN'] = 1 # Get most frequent hs by exporter hs_tup = source_hs(graph.es,'hss') graph.vs['hs_source'] = 0 graph.vs[hs_tup[0]]['hs_source'] = hs_tup[1] # Get most frequent destimation dest_tup = source_hs(graph.es,'dest_source') graph.vs['dest_source'] = 0 graph.vs[dest_tup[0]]['dest_source'] = dest_tup[1] # SIZES FROM graph.csv size = 10046 edge_size = 58031 big_size = 40789 sub = size # MAKE THE TWO TYPES (SELLER AND BUYER) graph.vs['type'] = [1] * big_size graph.vs[sub:]['type'] = [0] * (big_size - sub) # PROEJECT AND ADD ATTRIBUTES proj2, proj1 = graph.bipartite_projection() proj1.vs['val'] = graph.vs[0:sub]['val'] proj1.vs['val'] = graph.vs[0:sub]['val'] # Get most valuable importer max_imp = pd.read_pickle('max_imp.pickle') proj1.vs['imp_name'] = max_imp # WRITE AND READ proj1.write_pickle('proj1.pickle') proj1 = ig.read('proj1.pickle') print(ig.summary(proj1)) return proj1, proj2
def load_dat(): graph = ig.read('igraph_small.csv',format='edge') vals = pd.read_csv('vals_small.csv')['val'] hs = pd.read_csv('vals_small.csv')['hs10'] hss = pd.read_csv('vals_small.csv')['hs_source'] dest = pd.read_csv('vals_small.csv')['dest_alf'] dest_source = pd.read_csv('vals_small.csv')['dest'] imp_name = pd.read_csv('vals_small.csv')['imp_name'] atts = {'vals': vals,'hs': hs,'dest': dest, 'hss': hss,'dest_source': dest_source, 'imp_name': imp_name} return graph, atts
def main(filepath,feature): pwd = os.getcwd() # This should be your home directory (e.g. /Users/blahblah). filename = os.path.split(filepath)[1] f = open(pwd + "/%s/"%feature + filename + ".%s.txt"%feature,"w") writer = csv.writer(f) G = igraph.read(filepath) G.simplify() G.to_undirected() largest_c = G.clusters(mode="STRONG").giant() exec("f_value = %s(largest_c)"%feature) writer.writerow((filepath,f_value)) f.close()
def _read_celltype_graph(self, connmatrix_file, format='gml', cellcount_file=None): celltype_graph = None cellcount_dict = {} if cellcount_file is not None: with open(cellcount_file, 'r') as popfile: reader = csv.reader(popfile) for line in reader: if len(line) > 0: cellcount_dict[line[0]] = int(line[1]) if format == 'csv': celltype_graph = igraph.Graph(len(cellcount_dict), directed=True) index = 0 for celltype, cellcount in cellcount_dict.items(): celltype_graph.vs[index]['label'] = celltype celltype_graph.vs[index]['count'] = cellcount index += 1 reader = csv.reader(file(connmatrix_file)) header = reader.next() row = 0 for line in reader: if len(line) <= 0: continue pre = header[row] source = celltype_graph.vs.select(label_eq=pre) if len(source) > 0: source = source[0] row += 1 col = 0 for entry in line: post = header[col] target = celltype_graph.vs.select(label_eq=post) if len(target) > 0: target = target[0] value = int(entry) celltype_graph.add_edges((source.index, target.index)) eid = celltype_graph.get_eid(source.index, target.index) celltype_graph.es[eid]['weight'] = value col += 1 elif format == 'gml': celltype_graph = igraph.read(f=connmatrix_file, format=format) celltype_graph['doc'] = 'Celltype-based connectivity data. \ count of node *n* is the number of cells of type *n* \ that are present in the model. weight of edge (a, b) \ is the number of cells of type *a* that connect to \ each cell of type *b*.' return celltype_graph
def HACK_convert_nx_igraph(nx_graph): """ There exist no current method to convert a nx graph to an igraph. So this is a hack which does sp. Args: nx_graph: input nx_graph to be converted to igraph Returns: ig_graph: converted igraph """ nx.write_pajek(nx_graph, "/tmp/rohan.net") ig_graph = igraph.Graph() ig_graph = igraph.read("/tmp/rohan.net", format="pajek") return ig_graph
def superFunctionsGenerator(): for exeName in os.listdir(targetsDir): print "loading - " + exeName + " ... " , currentExeDir = os.path.join(targetsDir,os.path.join(exeName,functionsGraphsDirectoryName)) for funcFileName in filter(lambda x:x.endswith(myExt),os.listdir(currentExeDir)): tarGraph = igraph.read(os.path.join(currentExeDir,funcFileName)) tarGraph['name'] = funcFileName #funcFileName and exe are only for the timeout print yield {'tarGraph':tarGraph,'refGraph':refGraph,'sourcesList3k':sourcesList3k,'funcFileName':funcFileName,'exeName':exeName} print "finished loading " + exeName
def main(infile): g = igraph.read(infile, 'lgl') d = g.community_fastgreedy() mem = d.cut(CUT) categories = [[] for i in xrange(CUT)] r = re.compile(r'(?P<name>.+)\((?P<count>\d+)\)') for i, cat_no in enumerate(mem): m = r.match(g.vs[i]['name']) categories[cat_no].append((int(m.group('count')), m.group('name'))) for i, cat in enumerate(categories): print '** category %d **' % i cat.sort(lambda x, y: -cmp(x, y)) for c in cat: print "%s : %d" % (c[1], c[0])
def load_gml(path, attr_name="value"): g = igraph.read(path) elist = g.get_edgelist() min_id = min(chain.from_iterable(elist)) if min_id == 1: offset = True else: offset = False if offset: elist = [(i-1, j-1) for i, j in elist] try: labels = [int(v) for v in g.vs.get_attribute_values(attr_name)] except: labels = [v for v in g.vs.get_attribute_values(attr_name)] return elist, labels
def plotDegDistr(gml_file): g = read(gml_file) print g.summary() xs, ys = zip(*[(left, count) for left, _, count in g.degree_distribution().bins()]) _sum = sum(ys) _novy = [float(ys[i]) / _sum for i in range(0, len(ys))] plt.stem(xs, _novy) plt.yscale("log") plt.xscale("log") plt.ylabel('p(k)') plt.xlabel('k') plt.show()
def main(infile, outfile): g = igraph.read(infile, 'lgl') # write XGMML root = etree.XML('<?xml version="1.0" encoding="UTF-8" standalone="yes"?><graph label="Network" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:cy="http://www.cytoscape.org" xmlns="http://www.cs.rpi.edu/XGMML" directed="0" />') for v in g.vs: etree.SubElement(root, 'node', label = v['name'].decode('utf-8'), id = str(v.index)) for e in g.es: etree.SubElement(root, 'edge', source = str(e.source), target = str(e.target), label = '') file = open(outfile, 'w') file.write(etree.tostring(root, encoding = 'utf-8', pretty_print = True)) file.close()
def read_graph_paper(self, graph_str): try: graph_file = open(self.path+graph_str+'.txt') except IOError: print 'The file '+self.path+graph_str+'.txt'+' does not exist!' return None g = ig.read(self.path+graph_str+'.net') for r in graph_file: lines = r.split('\t') id = int(lines[0]) cluster = str(lines[1]) label = str(lines[2]).rstrip('\n') g.vs[id]['class'] = cluster g.vs[id]['label'] = label graph_file.close() return g