def correlation_betweenness_degree_on_ErdosNetwork(): G = nx.read_pajek("dataset/Erdos971.net") isolated_nodes = nx.isolates(G) G.remove_nodes_from(isolated_nodes) print nx.info(G) ND, ND_lambda = ECT.get_number_of_driver_nodes(G) print "ND = ", ND print "ND lambda:", ND_lambda ND, driverNodes = ECT.get_driver_nodes(G) print "ND =", ND degrees = [] betweenness = [] tot_degree = nx.degree_centrality(G) tot_betweenness = nx.betweenness_centrality(G,weight=None) for node in driverNodes: degrees.append(tot_degree[node]) betweenness.append(tot_betweenness[node]) with open("results/driver_degree_Erdos.txt", "w") as f: for x in degrees: print >> f, x with open("results/driver_betweenness_Erdos.txt", "w") as f: for x in betweenness: print >> f, x with open("results/tot_degree_Erdos.txt", "w") as f: for key, value in tot_degree.iteritems(): print >> f, value with open("results/tot_betweenness_Erdos.txt", "w") as f: for key, value in tot_betweenness.iteritems(): print >> f, value
def main(): # Load Zachary data, randomly delete nodes, and report zachary=nx.Graph(nx.read_pajek("karate.net")) # Do not want graph in default MultiGraph format zachary.name="Original Zachary Data" print(nx.info(zachary)) zachary_subset=rand_delete(zachary, 15) # Remove half of the structure zachary_subset.name="Randomly Deleted Zachary Data" print(nx.info(zachary_subset)) # Create model, and simulate zachary_model=gmm.gmm(zachary_subset,R=karate_rule,T=node_ceiling_34) gmm.algorithms.simulate(zachary_model,4,poisson=False,new_name="Simulation from sample") # Use tau=4 because data is so small (it's fun!) # Report and visualize print(nx.info(zachary_model.get_base())) fig=plt.figure(figsize=(30,10)) fig.add_subplot(131) nx.draw_spring(zachary,with_labels=False,node_size=45,iterations=5000) plt.text(0.01,-0.1,"Original Karate Club",color="darkblue",size=20) fig.add_subplot(132) nx.draw_spring(zachary_subset,with_labels=False,node_size=45,iterations=5000) plt.text(0.01,-0.1,"Random sample of Karate Club",color="darkblue",size=20) fig.add_subplot(133) nx.draw_spring(zachary_model.get_base(),with_labels=False,node_size=45,iterations=5000) plt.text(0.01,-0.1,"Simulation from random sample",color="darkblue",size=20) plt.savefig("zachary_simulation.png")
def main(): g=net.Graph() #snowball_sampling(g,'navalny') #print 'done' print 'loading' g=net.read_pajek('lj_trim_graph.net') print len(g) #g=trim_degrees(g) #net.write_pajek(g,'lj_trim_graph.net') #print len(g) print 'done' #find the celebrities print 'calculating degrees' degrees=net.degree(g) s_degrees=sorted_map(degrees) res_degrees=s_degrees[0:9] print res_degrees print 'done' #find the gossipmongers print 'calculating closeness' closeness=net.closeness_centrality(g) s_closeness=sorted_map(closeness) res_closeness=s_closeness[0:9] print res_closeness print 'done' #find bottlenecks print 'calculating betweenness' betweenness=net.betweenness_centrality(g) s_betweenness=sorted_map(betweenness) res_betweenness=s_betweenness[0:9] print res_betweenness print 'done'
def read_any(fname): """Attempt to read file in any format. - First, file name can be specified as a schema prefix, as in 'gml:FILE_NAME'. - Then, it can be specified via filename extension. - Then, it can be specified via filename extension after .bz2 or .gz. Note: We do not decompress it, but rely on networkx to do decompression. - Then, look at the first 512 bytes to see if the file is either in pajek or gml format. - If read is unsuccessful, raise UnknownTypeError. If read is successful, return a networkx graph object. A list of all readers in networkx: import networkx [ x for x in dir(networkx) if x.startswith('read_') ] """#%' \n'.join(x for x in dir(networkx) if x.startswith('read_')) # Try a schema prefix based match: m = re.match(r'^(\w+?):(.*)$', fname) if m: schema, fname = m.group(1), m.group(2) # Look for networkx.read_schema. If it exists, use that to # read and return. reader = _get_reader(schema) if reader is not None: return reader(fname) # Try known file suffix based reading: base, ext = os.path.splitext(fname) #if ext == '.gml': # return networkx.read_gml(fname) # look for any reader in networkx with this extension. reader = _get_reader(ext[1:]) if reader is not None: return reader(fname) # If file is compressed, look at true extension, see if it has a # reader. if ext in ('.gz', '.bz2'): newext = os.path.splitext(base) reader = _get_reader(newext[1:]) if reader is not None: return reader(fname) # Look inside for the schema: data = open(fname).read(512) if _test_pajek(data): return networkx.read_pajek(fname) if _test_gml(data): return networkx.read_gml(fname) if _test_edgelist(data): return networkx.read_edgelist(fname, data=[('weight', float)]) if _test_gexf(data): return networkx.read_gexf(fname) if _test_graphml(data): return networkx.read_graphml(fname) # So it is probably an edgelist. # Raise exception raise UnknownTypeError("Can't open %s"%fname)
def main(): G = nx.read_pajek('karate.paj') A = bfs(G, '1') print(A.edges) nx.draw_spring(A, with_labels=True) plt.savefig("grafo_bfs.png")
def load_network(self, pajek_filepath, show=False): graph = nx.read_pajek(pajek_filepath) if show: nx.draw_circular(graph) plt.show() return graph
def read_pajek(fname, constructor=networkx.DiGraph): g = networkx.read_pajek(fname) print g.__class__ # Test for multi-plicitiy for node in g.nodes_iter(): neighbors = g.neighbors(node) assert len(neighbors) == len(set(neighbors)), "Not a simple graph..." return constructor(g)
def test_read_pajek(self): G = nx.parse_pajek(self.data) Gin = nx.read_pajek(self.fname) assert sorted(G.nodes()) == sorted(Gin.nodes()) assert_edges_equal(G.edges(), Gin.edges()) assert self.G.graph == Gin.graph for n in G: assert G.nodes[n] == Gin.nodes[n]
def exectueCliqueAlgorithm(directory): os.chdir(directory) for file in glob.glob("*.net"): file_name = file.split(".")[0] + ".clu" multigraph = nx.read_pajek(file) modelGraph = nx.Graph(multigraph) file_name = file.split(".")[0] + ".clu" file_directory = os.path.join("../../results-kclique/", file_name) f = open(file_directory, "w+") communities = community.k_clique_communities(modelGraph, 3) lines = [None] * (len(modelGraph) + 1) group_number = 1 for group in communities: group_as_string = str(group_number) for node_value in group: id = modelGraph.nodes.get(node_value)['id'] index = int(id) lines[index] = group_as_string group_number += 1 lines[0] = "*Vertices " + str(len(modelGraph)) for index, x in enumerate(lines): if x is None: lines[index] = str(group_number) group_number += 1 f.writelines('\n'.join(lines)) f.write("\n") f.close() os.chdir("../../radatools/Communities_Tools/") st = os.stat('./Compare_Partitions.exe') os.chmod("./Compare_Partitions.exe", st.st_mode | stat.S_IEXEC) if ("model" in directory) and ("rb125" in file_name): index = 1 while (index <= 3): os.system("./Compare_Partitions.exe ../../results-kclique/" + file_name + " ../" + directory + "rb125-" + str(index) + ".clu" + " ../../results-kclique/" + file_name + "-" + str(index) + ".exit " + " V") index += 1 else: os.system("./Compare_Partitions.exe ../../results-kclique/" + file_name + " ../" + directory + file_name + " ../../results-kclique/" + file_name + ".exit " + " V") st = os.stat('./Modularity_Calculation.exe') os.chmod("./Modularity_Calculation.exe", st.st_mode | stat.S_IEXEC) os.system("./Modularity_Calculation.exe ../" + directory + file + " ../../results-kclique/" + file_name + " 0 0 UN TC 2 >> " + " ../../results-kclique/" + file_name + ".modularity") os.chdir("../" + directory) os.chdir("../../source")
def test_write_pajek(self): import io G = nx.parse_pajek(self.data) fh = io.BytesIO() nx.write_pajek(G, fh) fh.seek(0) H = nx.read_pajek(fh) assert_nodes_equal(list(G), list(H)) assert_edges_equal(list(G.edges()), list(H.edges()))
def read_graph(G, gfile): print "Reading in artist graph..." try: G = nx.read_pajek(gfile) print "Read successfully!" print "The artist graph currently contains " + str(len(G)) + " artists." print "The artist graph currently contains " + str(nx.number_strongly_connected_components(G)) + " strongly connected components." except IOError: print "Could not find artistGraph"
def _read_pajek(*args, **kwargs): """Read Pajek file and make sure that we get an nx.Graph or nx.DiGraph""" G = nx.read_pajek(*args, **kwargs) edges = G.edges() if len(set(edges)) < len(edges): # multiple edges log.warning("Network contains multiple edges. These will be ignored.") if G.is_directed(): return nx.DiGraph(G) else: return nx.Graph(G)
def _read_pajek(*args, **kwargs): """Read Pajek file and make sure that we get an nx.Graph or nx.DiGraph""" G = nx.read_pajek(*args, **kwargs) edges = G.edges() if len(set(edges)) < len(edges): # multiple edges log.warning("Network contains multiple edges. These will be ignored.") if G.is_directed(): return nx.DiGraph(G) else: return nx.Graph(G)
def test_author_interaction(): clean_data = './test/integration_test/data/clean_data.json' graph_nodes = './test/integration_test/data/graph_nodes.csv' graph_edges = './test/integration_test/data/graph_edges.csv' pajek_file = './.tmp/integration_test/lib/analysis/author/graph/generate/author_graph.net' req_output1 = './test/integration_test/data/req_data/test_generate1.net' req_output2 = './test/integration_test/data/req_data/test_generate2.net' author_interaction(clean_data, graph_nodes, graph_edges, pajek_file, ignore_lat=True) output_graph = nx.read_pajek(pajek_file) req_grpah = nx.read_pajek(req_output1) assert nx.is_isomorphic(output_graph, req_grpah) author_interaction(clean_data, graph_nodes, graph_edges, pajek_file, ignore_lat=False) output_graph = nx.read_pajek(pajek_file) req_grpah = nx.read_pajek(req_output2) assert nx.is_isomorphic(output_graph, req_grpah)
def __init__(self, readName, dumpName, removeList=None, remainList=None, subset=None): self.dumpName = dumpName self.subset = subset self.removeList = removeList self.remainList = remainList self.G = nx.read_pajek(readName)
def get_graph(graph_file, map_file, trim=False): """ graph_file --> is the file to convert to a networkx graph trim --> either takes an integer or 'False'. If integer, returned graph will call return graph with nodes having degree greater than integer """ the_graph = net.DiGraph(net.read_pajek(graph_file)) id_map = make_id_map(map_file) net.relabel_nodes(the_graph, id_map, copy=False) if trim: the_graph = trim_degrees(the_graph, degree=trim) return the_graph
def reading_networks(self, net_path): folder = "networks/"; G = nx.read_pajek(folder+net_path) G_is_directed = nx.is_directed(G) if G_is_directed: G = nx.DiGraph(G) else: G = nx.Graph(G) return G
def main(): # Unos pocetnog grada unosGrada = input("Unesite pocetni grad") # Niz u kojeg spremamo gradove niz = [] # Citanje u pajek pbliku G = nx.read_pajek('airports-split 1.net') G1 = nx.DiGraph(G) # Citanje iz filea kako bi dobili sve vrhove niz = citanjeIzFilea() bellmanFord(G1, unosGrada, niz)
def maxIncidencija(): G = nx.read_pajek("euler.net") G1 = nx.Graph(G) max = 0 for el in G1.nodes: if max < G1.degree(el): max = G1.degree(el) print(max) for el in G1.nodes: if (G1.degree(el) == max): print("Cvor s max brojem incidentnih vrhova: {0}".format(el))
def read_file_net(g,path=None): #read .net file. Ues the function of nx.read_pajek() g.clear() try: mg=nx.read_pajek(path,encoding='ascii') #g=g.to_undirected() g=nx.Graph(mg)#Tanslate mg(MultiGraph) to g(Graph) except: print "readFileTxt error" return g
def main(): #print 'main running!' #g=nx.read_adjlist("te.adj",nodetype=int) #ad=list() #mi=list() #su=list() ##print sel3(g,3,ad,mi,su) g=nx.Graph() g=nx.read_pajek("a.net") sh(g) nx.clustering(g)
def __init__(self, graph=None, root='A1-networks', dir = 'toy', file='circle9.net'): """ If a graph in networkx format is taken initialize the object with it, if not, reads the graph from the file 'file' into directory 'root/dir/file' :param graph: Graph in a networkx format :param root: Root directory where graphs dirs are located :param dir: Graph directory within Root where network files are located :param file: Name of the network file to open """ if graph is None: self.graph = nx.read_pajek(os.path.join(root, dir, file)) else: self.graph = graph
def test_unicode(self): import io G = nx.Graph() name1 = chr(2344) + chr(123) + chr(6543) name2 = chr(5543) + chr(1543) + chr(324) G.add_edge(name1, 'Radiohead', foo=name2) fh = io.BytesIO() nx.write_pajek(G, fh) fh.seek(0) H = nx.read_pajek(fh) assert_nodes_equal(list(G), list(H)) assert_edges_equal(list(G.edges()), list(H.edges())) assert G.graph == H.graph
def __init__(self, graph=None, root='networks', dir='toy', file=''): """ If a graph in NetworkX format is provided, initialize the Analyzer object with it, else, read the graph from the Pajek format file 'file' from the directory 'root/dir/'. :param root: String. Root directory where graphs directories are located. :param dir: String. Graph directory within Root where network files are located. :param file: String. Name of the network file to open. """ if graph is None: self.graph = nx.read_pajek(os.path.join(root, dir, file)) else: self.graph = graph
def graph_degree_analysis(self, network_path, network_name, network_ext): network_full_path = network_path + network_name + network_ext G = nx.read_pajek(network_full_path) G_is_directed = nx.is_directed(G) if G_is_directed: G = nx.DiGraph(G) else: G = nx.Graph(G) G_nodes = G.nodes() G_num_nodes = len(G_nodes) network_degrees = sorted(nx.degree(G).values()) "Sorted degrees in a graph" k_min_degree = network_degrees[0] "Minimum degree of the graph" k_max_degree = network_degrees[-1] "Maximum degree of the graph" nBins = 15 degrees_log = np.linspace(np.log(k_min_degree), np.log(k_max_degree + 1), nBins) # degree distribution degree_freqs = nx.degree_histogram(G) degrees_ = np.exp(degrees_log) degrees_[0] = network_degrees[0] pdf_ = np.zeros(nBins) for i in xrange(len(degrees_)): for j in xrange(len(degree_freqs)): if j >= degrees_[i] and j < degrees_[i + 1]: pdf_[i] += degree_freqs[j] degrees_pdf = np.asarray(pdf_) / float(G_num_nodes) degrees_ccdf = [sum(degree_freqs[int(np.ceil(k)):]) for k in degrees_] "a)pdf-plotting" self.network_plotting(network_name, degrees_log, degrees_pdf, degrees_, "") "b)ccdf-plotting" self.network_plotting(network_name, degrees_log, degrees_ccdf, degrees_, "Cumulative ")
def pipeline(path): """ Run the whole pipline. """ graph = nx.read_pajek(path) # trimmed in and outdegree indegree, outdegree = prune_zero_outdegree(graph) # clustering coefficient, diameter, expected shortest path lengths clustering, diameter, small_world = get_metrics(graph) print(clustering, diameter, small_world) # values for PairsP 0.119 7 4.004 # plot the indegree and outdegree histograms plot(indegree, outdegree)
def test_save_net_nx_graph(self): graph = nx.DiGraph() graph.add_nodes_from([1, 2, 3]) graph.add_edge(1, 3) saver.save_net_nx_graph(graph, self.current_directory, 'test_save_net_nx') assert os.path.exists(self.current_directory + '/test_save_net_nx.net') assert os.path.isfile(self.current_directory + '/test_save_net_nx.net') g = nx.read_pajek(self.current_directory + '/test_save_net_nx.net') assert nx.is_isomorphic(graph, g) os.remove(self.current_directory + '/test_save_net_nx.net')
def main(): # Greedy best first search d = DefaultDict(list) d2 = DefaultDict(list) d3 = DefaultDict(list) # Unos pocetnog grada unosGrada = input("Unesite pocetni grad") unosKrajnjegGrada = input("Unesite krajni grad") # Niz u kojeg spremamo gradove niz = [] nzi2 = [] # Citanje u pajek pbliku G = nx.read_pajek('airports-astar.net') G1 = nx.Graph(G) # G2 = nx.DiGraph(G) # A* algoritam zadatak 1 print(" \n A* algoritam ------------------------------") Astar = aStarAlgorithm(citanjeKordinata("airports-astar.net"), unosGrada, unosKrajnjegGrada) putPreden, udaljenostPredena, vrijemeOdradivanjaAlgoritma = Astar.a_star() print( "Put preden je: {0}, udaljenost predena je: {1}, vrijeme odradivanja algoritma je: {2}" .format(putPreden, udaljenostPredena, vrijemeOdradivanjaAlgoritma)) print(" \n -----------------------------------------") # BFS ALGORITAM print("Best first search algoritam ---------------------") # Citanje iz filea niz, niz2 = citanjeIzFilea(d, d2, d3) # print(niz) #print(udaljenostDoCilja(d3["LHR"][0], d3["BER"][0])) print("\n") listaPredenihGradova = greedyBFS(d, d2, d3, unosGrada, unosKrajnjegGrada, niz, niz2) suma = ispisKonacnogPuta(listaPredenihGradova, d2) print("\n Suma udaljenosti puta je:{0}".format(suma)) # Zadatak 2 put, distanca, vrijemeAlgoritma = Astar.a_star() print(put) print(distanca) # Graf graficki prikazan """ generate_pajek(G)
def generate_demo_network_raga_recognition(network_file, community_file, output_network, colors = cons_net.colors, colorify = True, mydatabase = ''): """ This function generates a network used as a demo for demonstrating relations between phrases. The configuration used to generate this network should ideally be the one that is used for the raga recognition task reported in the paper. """ #loading the network full_net = nx.read_pajek(network_file) #loading community data comm_data = json.load(open(community_file,'r')) #loading all the phrase data comm_char.fetch_phrase_attributes(comm_data, database = mydatabase, user= '******') #getting all the communities from which we dont want any node in the graph in the demo #obtaining gamaka communities gamaka_comms = comm_char.find_gamaka_communities(comm_data)[0] #obtaining communities with only phrases from one mbid one_mbid_comms = comm_char.get_comm_1MBID(comm_data) #collect phrases which should be removed from the graph phrases = [] for c in gamaka_comms: for n in comm_data[c]: phrases.append(int(n['nId'])) for c in one_mbid_comms: for n in comm_data[c]: phrases.append(int(n['nId'])) print len(phrases) #removing the unwanted phrases full_net = raga_recog.remove_nodes_graph(full_net, phrases) # colorify the nodes according to raga labels if colorify: cmd1 = "select raagaId from file where id = (select file_id from pattern where id =%d)" con = psy.connect(database='ICASSP2016_10RAGA_2S', user='******') cur = con.cursor() for n in full_net.nodes(): cur.execute(cmd1%(int(n))) ragaId = cur.fetchone()[0] full_net.node[n]['color'] = ragaId #saving the network nx.write_gexf(full_net, output_network)
def init_network(self, name, filename, data): G = nx.read_pajek("../data_sources/{}".format(filename)) G1 = nx.DiGraph(G) G2 = deepcopy(G1) for node in G2.nodes(data=True): x = node[1]['x'] y = node[1]['y'] z = float() pos = (x,y,z) del_keys = [k for k in node[1].keys()] for key in del_keys: del node[1][key] node[1]['pos'] = pos node[1]['node_name'] = node[0] node[1]['layer'] = name if G2.out_degree(node[0]) == 0: node[1]['type'] = "TARGET" elif G2.in_degree(node[0]) == 0: node[1]['type'] = "NEGOTIATED" else: node[1]['type'] = "INTERMEDIATE" node[1]['val'] = self.data[node[0]] if node[1]['val'] is None: node[1]['data_status'] = 0.0 else: node[1]['data_status'] = 1.0 node[1]['time'] = 0 node[1]['val_ts'] = {node[1]['time']:node[1]['val']} node[1]['data_status_ts'] = {node[1]['time']:node[1]['data_status']} node[1]['intermediate_vals'] = [] G3 = nx.convert_node_labels_to_integers(G2) for e in G3.edges(data=False): G3.edges[e]['layer'] = name G3.edges[e]['type'] = 'INTERNAL' G3.edges[e]['time'] = 0 G4 = nx.MultiDiGraph(G3) return G4
def main(): G1 = nx.read_pajek('karate.paj') G2 = nx.read_pajek('dolphins.paj') bfs_tree1 = bfs(G1.copy(), '1') bfs_tree2 = bfs(G2.copy(), '1') print('numero de vertices de G1: {:d}\n' 'numero de arestas de G1: {:d}\n'.format(G1.number_of_nodes(), G1.number_of_edges())) print('numero de vertices da G2: {:d}\n' 'numero de arestas da G2: {:d}\n'.format(G2.number_of_nodes(), G2.number_of_edges())) print('numero de vertices da BFS Karate: {:d}\n' 'numero de arestas da BFS Karate: {:d}\n'.format(bfs_tree1.number_of_nodes(), bfs_tree1.number_of_edges())) print('numero de vertices da BFS Dolphins: {:d}\n' 'numero de arestas da BFS Dolphins: {:d}\n'.format(bfs_tree2.number_of_nodes(), bfs_tree2.number_of_edges())) draw(bfs_tree1) draw(bfs_tree2)
def splitMachines( path, K): net = [[] for i in range(K)] machines = [] G = nx.read_pajek(path) for node in G.nodes_iter(): i = random.randint(0,K-1) net[i].append(node) for b in range(K): machines.append(G.subgraph(net[b])) return machines
def exportD3(readName, dumpName): #G = nx.path_graph(4) #G = nx.barbell_graph(6,3) G = nx.read_pajek(readName) for n in G: print 'node', n G.node[n]['name'] = G.node[n]['label'] G.node[n]['size'] = G.node[n]['color'] d = json_graph.node_link_data(G) json.dump(d, open(dumpName, 'w')) print('wrote node-link json data to {}'.format(dumpName))
def operator_extract_communities(self, method, year, discharge_type, type_community_detection): folder_path_in = self.get_operator_folder_path( SharedCareAreaScript.TYPE_NETWORK) filename_in = f'{method}_{year}_{discharge_type}' filepath_in = f'{folder_path_in}/{filename_in}' folder_path_out = self.get_operator_folder_path( SharedCareAreaScript.TYPE_COMMUNITIES) filename_out = f'{method}_{year}_{discharge_type}_{type_community_detection}' filepath_out = f'{folder_path_out}/{filename_out}' # exists = os.path.exists(f'{filepath_out}.csv') # if exists and not drop: # return G = nx.read_pajek(f'{filepath_in}.pajek') community_detection = CommunityDetection.build_community_detection( type_community_detection) df_communities = community_detection.find_communities( (G, filepath_in, filename_in)) df_communities.rename( columns={ SharedCareArea.zcta.db_field: 'NODE_ID', SharedCareArea.sca_id.db_field: 'COMMUNITY_ID' }) df_communities.to_csv(f'{filepath_out}.csv', index=False) df_communities.to_hdf(f'{filepath_out}.hdf', 'df') sca_dict = { SharedCareArea.method.name: method, SharedCareArea.year.name: year, SharedCareArea.type_discharge.name: discharge_type, SharedCareArea.type_community_detection.name: type_community_detection } dao = BaseDAO(SharedCareArea) dao.delete(**sca_dict) list_shared_care_areas = list( map(self.get_shared_care, [(sca_dict, row) for (index, row) in df_communities.iterrows()])) dao.insert(list_shared_care_areas)
def main(): """The main function""" try: file_name = sys.argv[1] except IndexError: print "Input file not given!" raise base_name = file_name.split('.')[0] net = nx.read_pajek(file_name) nx.draw_graphviz(net, with_labels=True, font_size=4, node_size=100) plt.draw() plt.savefig(base_name + '.eps')
def read_graph(_file, extension, weighted=False, directed=False): # this function is used to read graph data from file and return graph if extension == '.graphml': graph = nx.read_graphml(_file) elif extension == '.gml': graph = nx.read_gml(_file) elif extension == '.net': graph = nx.read_pajek(_file) elif extension == '.el': graph = load_el_network(_file, weighted, directed) else: graph = None return graph
def a_star(self): start = time.time() graph = nx.read_pajek("airports-astar.net") nx.set_node_attributes(graph, self.coords_dict, "coordinates") path = nx.astar_path(graph, self.start_air, self.end_air, heuristic=self.heuristic) distance = nx.astar_path_length(graph, self.start_air, self.end_air, self.heuristic) end = time.time() completionTime = end - start return path, distance, completionTime
def main(): global P G = nx.read_pajek('dolphins.paj') #print G.nodes() dfs(G, G.nodes()[25]) #print G.nodes()[25] T = nx.Graph() #insere arestas em T T.add_edges_from([(u, v) for u, v in P.items()]) #posicionamento do grafo T pos = nx.spring_layout(T) nx.draw_networkx_nodes(T, pos, node_size=700) #obter dicionário com as distâncias ud = {v: (v, data['ud']) for v, data in G.nodes(data=True)} nx.draw_networkx_labels(T, pos, labels=ud, font_size=8, label_pos=0.5) nx.draw_networkx_edges(T, pos) plt.show()
def filterAndSaveNetwork(networkFile, outputNetworkFile, DISPARITY_FILTER_SIGNIF_LEVEL): """ This function filters the network and saves it in another file for further analysis """ G = nx.read_pajek(networkFile) #converting to undirected network G = nx.Graph(G) if DISPARITY_FILTER_SIGNIF_LEVEL > 0: G = filter_graph_edges(G, DISPARITY_FILTER_SIGNIF_LEVEL) #saving weighted network/graph as a pajek file nx.write_pajek(G, outputNetworkFile)
def likes_top_50(fans_level): name = 'net' + str(int(fans_level / 1000)) + 'k' g = nx.read_pajek(name + '.net') print(nx.info(g)) in_degree = g.in_degree() sorted_in_degree = sorted(in_degree.items(), key=lambda item: item[1], reverse=True) print('\ntop 50', 'likes of', name, 'graph:\n') for i in range(200): id, likes_num = sorted_in_degree[i] cur.execute('select name, fo_num from People where id = ?', (id, )) name, fo_num = cur.fetchone() print(i + 1, name, fo_num, likes_num)
def readFromPayek(): G = nx.read_pajek("euler.net") # print(G.edges) # print(G.nodes) # G1.number_of_nodes() # G1.number_of_edges() # G.degree[1] koliko ima brida pojedini vrh # list(G.adj[1]) listat susjednih nodova # print(G1["A"]) je isto ko i G1.adj("A") G1 = nx.Graph(G) d = {key: [] for key in G1.nodes} for el in G1.nodes: for key in G1[el].keys(): d[el].append(key) pprint.pprint(d)
def main(): G = nx.read_pajek('karate.paj') #print G.nodes() P = bfs(G, G.nodes()[10]) T = nx.Graph() #insere arestas em T T.add_edges_from([(u, v) for u, v in P.items()]) #posicionamento do grafo T pos = nx.spring_layout(T) #plotar vertices de T nx.draw_networkx_nodes(T, pos, node_size=700) #obter dicionário com as distâncias dist = {v: (v, data['lambda']) for v, data in G.nodes(data=True)} #plotar arestas de T nx.draw_networkx_labels(T, pos, labels=dist) nx.draw_networkx_edges(T, pos) plt.show()
def load_and_process_graph(filename): """Load the graph, normalize edge weights, compute pagerank, and store all this back in node data.""" # Load the graph graph = nx.DiGraph(nx.read_pajek(filename)) print "Loaded a graph (%d nodes, %d edges)" % (len(graph), len(graph.edges())) # Compute the normalized edge weights for node in graph: edges = graph.edges(node, data=True) total_weight = sum([data['weight'] for (_, _, data) in edges]) for (_, _, data) in edges: data['weight'] = data['weight'] / total_weight # Get its PageRank, alpha is 1-tau where [RAB2009 says \tau=0.15] page_ranks = nx.pagerank(graph, alpha=1-TAU) for (node, page_rank) in page_ranks.items(): graph.node[node][PAGE_RANK] = page_rank return graph
def read_graph(self, subgraph_file=None): if subgraph_file is None: subraph_file = self.context_graph_file logging.info("Writing graph.") # write the graph out file_format = subgraph_file.split(".")[-1] if file_format == "graphml": return nx.read_graphml(subgraph_file) elif file_format == "gml": return nx.read_gml(subgraph_file) elif file_format == "gexf": return nx.read_gexf(subgraph_file) elif file_format == "net": return nx.read_pajek(subgraph_file) elif file_format == "yaml": return nx.read_yaml(subgraph_file) elif file_format == "gpickle": return nx.read_gpickle(subgraph_file) else: logging.warning("File format not found, returning empty graph.") return nx.MultiDiGraph()
def read_graph(filename): """Read graph from file, raise IOError if cannot do it.""" graph = None if filename.endswith('.yaml'): try: graph = nx.read_yaml(filename) except ImportError: print('E: cannot read graph from file in YAML format.') print('Please install PyYAML or other similar package ' 'to use this functionality.') raise IOError else: print('Read graph from {0} in YAML format.'.format(filename)) elif filename.endswith('.gml'): try: graph = nx.read_gml(filename) except ImportError: print('E: cannot read graph from file in GML format.') print('Please install GML or other similar package ' 'to use this functionality.') raise IOError else: print('Read graph from {0} in GML format.'.format(filename)) elif filename.endswith('.net'): graph = nx.read_pajek(filename) print('Read graph from {0} in PAJECK format.'.format(filename)) elif filename.endswith('.gexf'): graph = nx.read_gexf(filename) print('Read graph from {0} in GEXF format.'.format(filename)) elif filename.endswith('.graphml'): graph = nx.read_graphml(filename) print('Read graph from {0} in GraphML format.'.format(filename)) else: with open(filename, 'rb') as f: graph = pickle.load(f) print('Read graph from {0} in pickle format.'.format(filename)) return graph
import os import csv import helper as hp import networkx as nx # Example Networks from Literature OC = nx.read_pajek("results/networks/reference/OClinks_w.net") OC.name = "Opsahl" FR = nx.read_pajek("results/networks/reference/Freemans_EIES-3_n32.net") FR.name = "Freeman" CP = nx.read_pajek("results/networks/reference/Cross_Parker-Consulting_info.net") CP.name = "Cross_Parker" networks = [OC, FR, CP] def create_dummy_partition(network): csv_writer = csv.writer(open("data/partitions/%s_partition.csv" % network.name, "wb")) i = 0 for node in network.nodes(): i += 1 csv_writer.writerow([node, "a", i, 1]) def create_dummy_stats(network): csv_writer = csv.writer(open("results/stats/%s_lists_stats.csv" % network.name, "wb")) csv_writer.writerow(["Community name", "Based on Projects", "# lists,Total unique members on all lists"]) csv_writer.writerow(["a", "a", 10, 10])
confs = exps # Configurations for conf in confs: # Loading/Creating the network # ER if op == 0: k = conf p = k / N network = "ER_N" + str(N) + "_exp" + str(k).replace(".","_") if create: G = nx.erdos_renyi_graph(N, p, seed=None, directed=False) G = nx.Graph(G) G.remove_edges_from(G.selfloop_edges()) nx.write_pajek(G,os.getcwd()+r"\Networks_Pajek\NET_" + network + ".net") else: G = nx.read_pajek(os.getcwd()+r"\Networks_Pajek\NET_" + network + ".net") # CM else: exp = conf network = "CM_N" + str(N) + "_exp" + str(exp).replace(".","_") if create: degree_sequence = nx.utils.create_degree_sequence(N, nx.utils.powerlaw_sequence, exponent=exp) if not sum(degree_sequence) % 2 == 0: print("caution") degree_sequence[1] += 1 G = nx.configuration_model(degree_sequence, create_using=None, seed=None) G = nx.Graph(G) G.remove_edges_from(G.selfloop_edges()) nx.write_pajek(G,os.getcwd()+r"\Networks_Pajek\NET_" + network + ".net") else: G = nx.read_pajek(os.getcwd()+r"\Networks_Pajek\NET_" + network + ".net")
import os import sys import networkx as net import matplotlib.pyplot as plot print 'Reading retweets from Egypt' e=net.read_pajek("C:\Development\python\SNABook-master\chapter1\egypt_retweets.net") print 'Find connected components in network with '+str(len(e))+' nodes.' nccs=net.connected_component_subgraphs(e) print len(nccs) x=[len(c) for c in net.connected_component_subgraphs(e)] plot.hist(x) plot.savefig("connected_comps_hist.png")
def main(argv): mode=argv[1] path="net.net" G=nx.read_pajek(path) G=nx.Graph(G) #-------------1-------------------------- Nnodes = G.number_of_nodes() Nedges = G.number_of_edges() degree=list(G.degree().values()) meandeg=0 for x in degree: meandeg+=x meandeg=meandeg/float(Nnodes) if mode=="1": print "number of nodes: ", Nnodes print "number of links: ",Nedges print "average degree: ", meandeg plt.title("degree distribution") plt.hist(degree) #------------------------------------------------ #------------------2--------------------------- clustering=list(nx.clustering(G).values()) ave_cluster=nx.average_clustering(G) if mode=="2": print "average clustering: ",ave_cluster print "-------------------------" print "-----ER-Network--------------" print "average degree: ", 2*Nedges/float(Nnodes) print "-------------------------" plt.title("clustering distribution") plt.bar(np.linspace(0,18,19),clustering) cluster_degree=[] i=0 for x in degree: if x > 1: cluster_degree.append(x) else: clustering.pop(i) i+=1 if mode == "3": plt.title("degree over clustering coefficient") plt.plot(clustering,cluster_degree,'o') #-----------------4--------------------- neighbours=list(nx.k_nearest_neighbors(G).values()) if mode=="4": print("average neares neighbour degree: ",sum(neighbours)/float(Nnodes)) #------------------5--------------------------- r=nx.degree_pearson_correlation_coefficient(G) if mode=="5": print("r: ",r) #-------------------6------------------------- eig_cent=list(nx.eigenvector_centrality(G).values()) eig_top=sorted(range(len(eig_cent)), key=lambda i:eig_cent[i])[-7:] deg_top=sorted(range(len(degree)), key=lambda i:degree[i])[-7:] if mode=="6": print("degree top 7:",deg_top) print("eigenvector centrality top 7: ", eig_top) #-----------------7-------------------- pagerank_1=list(nx.pagerank(G,alpha=0.1).values()) pagerank_2=list(nx.pagerank(G,alpha=0.85).values()) pagerank_3=list(nx.pagerank(G,alpha=0.99).values()) if mode=="7": plt.plot(range(Nnodes),pagerank_1,'r') plt.plot(range(Nnodes),pagerank_2,'g') plt.plot(range(Nnodes),pagerank_3,'y') if mode=="8": Gcc=sorted(nx.connected_component_subgraphs(G), key = len, reverse=True)[0] pos=nx.spring_layout(Gcc) nx.draw_networkx_nodes(Gcc,pos,node_size=20) nx.draw_networkx_edges(Gcc,pos,alpha=0.4) plt.show()
from sc_pagerank import computePR, initializePR import sc_api_calls as scac import multiprocessing as mp from cc_mp_classes import Consumer, Task, bookTasks # A global artist graph used to iterate through the various algorithms. # Each node is artist id, with edges weighted by activity between then. artistGraph = nx.MultiDiGraph() tasks = mp.Queue() results = mp.Queue() try: print "Reading in artist graph..." artistGraph = nx.read_pajek('artistGraph.net') print "Read successfully!" print "The artist graph currently contains " + str(len(artistGraph)) + " artists." print "The artist graph currently contains " + str(nx.number_strongly_connected_components(artistGraph)) + " strongly connected components." except IOError: print "Could not find artistGraph" def my_shuffle(A): random.shuffle(A) return A rartists = my_shuffle(artistGraph.nodes()) for artist_id in rartists: # initialize the task queue
interactor_index_dict[interactor] = i i += 1 # pickle.dump(interactor_index_dict, open( r'.\Data\interactor_index_dict.p', "wb" ) ) pickle.dump(interactor_index_dict, open(os.path.expanduser("~/PycharmProjects/pluripotency/Data/interactor_name_dict.p"), "wb" ) ) f.write('*Edges\n') for interaction in interactions: f.write(str(interactor_index_dict[interaction[0]])+' '+str(interactor_index_dict[interaction[1]])+'\n') f.close() # G = nx.read_pajek(r'.\Data\Homo_sapiens_interactions.net') G = nx.read_pajek(os.path.expanduser("~/PycharmProjects/pluripotency/Data/Homo_sapiens_interactions.net")) # nx.write_edgelist(G, r'.\Data\edge_list.txt') # betweenes_centrality = nx.betweenness_centrality(G) # pickle.dump(betweenes_centrality, open(r'.\Data\betweenes_centrality_dict_2012.p', 'wb')) # degree_centrality = nx.degree_centrality(G) # pickle.dump(degree_centrality, open(r'.\Data\degree_centrality_dict_2012.p', 'wb')) # closenes_centrality = nx.closeness_centrality(G) # pickle.dump(closenes_centrality, open(r'.\Data\closenes_centrality_dict_2012.p', 'wb')) # page_rank_centrality = nx.pagerank_numpy(G) # betweenes_centrality = pickle.load(open(r'.\Data\betweenes_centrality_dict.p', 'rb')) # degree_centrality = pickle.load(open(r'.\Data\degree_centrality_dict.p','rb')) # closenes_centrality = pickle.load(open(r'.\Data\closenes_centrality_dict.p', 'rb'))
def main(): print "\n\nPrograma per l'analisi de xarxes complexes i la deteccio de comunitats." #print glob.glob('./networks/*.*') ruta = glob.glob('./networks/*.*') #troba la ruta dels arxius que indiquem print "\nXarxes disponibles a la carpeta networks:" llista = [] for i in range(len(ruta)): stemp = ruta[i].split('/') semp = stemp[2].split('.') llista.append(semp) llista.sort() #Afegit per linux, ja que no ordena la llista per defecte conta = 1 for y in range(len(llista)): if llista[y][0]!=llista[y-1][0]: print '[',conta,']',llista[y][0] print 'disponible en format: ',llista[y][1] conta+=1 #per tal de que el numero de xarxa apareixi correcte else: print ' ',llista[y][1] nb = raw_input("\nEscriu el nom de la xarxa a llegir, 'conversor' per entrar al conversor o 'sortir' per tancar el programa\n") if nb == 'sortir': sys.exit() elif nb == 'conversor': con.main() main() global nom nom = nb #per passar-ho al gephi #print ('Xarxa %s' % (nb)) print "\n" formats = list() #llista dels formats disponibles buida conex = False #inicialment no sabem si el nom es correcte pes = False # Variable per saber si la xarxa es amb pesos o sense for o in range(len(ruta)): stemp = ruta[o].split('/') semp = stemp[2].split('.') if(semp[0] == nb): #comprovo si coincideix amb el nom de l'arxiu #print "catch" #print semp[1] formats.append(semp[1]) #afegeixo format a la llista conex = True #el nom coincideix if len(formats) > 1: print "Tria en quin format vols cargar l'arxiu.\n Formats disponibles: ",formats #print formats fm = raw_input("Escriu el nom del format escollit\n") elif conex == False: # si el nom no existeix print "Nom de arxiu incorrecte" main() else: fm = formats fm = fm[0] #converteixo de list a string ###### cargar el graf depenent del format de l'arxiu ##### if fm == 'graphml': try: #print 'estic dins del graphml' arllegit = Graph.Read_GraphML("./networks/"+nb+".graphml") #llegit per igraph nllegit = nx.read_graphml("./networks/"+nb+".graphml") #llegit per networkx #ft.save_jsonfile()####### info = GraphSummary(arllegit) infor = info.__str__() if infor[7] == 'U' and infor[9] == '-': print "La xarxa carregada es indirecte i sense pesos" elif infor[7] == 'D' and infor[9] == '-': print "La xarxa carregada es directe i sense pesos" elif infor[7] == 'U' and infor[9] == 'W': print "La xarxa carregada es indirecte i amb pesos" pes = True elif infor[7] == 'D' and infor[9] == 'W': print "La xarxa carregada es directe i amb pesos" pes = True except IOError: print 'Error', arllegit else: print 'Arxiu carregat\n' menugraphml(arllegit,nllegit,pes) ####CRIDO FUNCIO DE ESCOLLIR L'ALGORISME PER GRAPHML### #num = escollir(arllegit,nllegit) #print num elif fm == 'net': try: #print 'estic dins del net' arllegitnet = Graph.Read_Pajek("./networks/"+nb+".net") #llegit per igraph nllegitnet = nx.read_pajek("./networks/"+nb+".net") #llegit per networkx info = GraphSummary(arllegitnet) infor = info.__str__() #de moment aquesta opcio de amb o sense pesos nomes esta implementada aqui if infor[7] == 'U' and infor[9] == '-': print "La xarxa carregada es indirecte i sense pesos" elif infor[7] == 'D' and infor[9] == '-': print "La xarxa carregada es directe i sense pesos" elif infor[7] == 'U' and infor[9] == 'W': print "La xarxa carregada es indirecte i amb pesos" pes = True elif infor[7] == 'D' and infor[9] == 'W': print "La xarxa carregada es directe i amb pesos" pes = True except IOError: print 'Error', arllegitnet else: print 'Arxiu carregat\n' menunet(arllegitnet,nllegitnet,pes) ####CRIDO FUNCIO DE ESCOLLIR L'ALGORISME PER NET PAJEK### #num = escollir(arllegitnet,nllegitnet) #num = netescollir(arllegitnet,nllegitnet) #print num elif fm == 'gml': try: #print 'estic dins del graphml' arllegitgml = Graph.Read_GML("./networks/"+nb+".gml") #llegit per igraph nllegitgml = nx.read_gml("./networks/"+nb+".gml") #llegit per networkx info = GraphSummary(arllegitgml) infor = info.__str__() if infor[7] == 'U' and infor[9] == '-': print "La xarxa carregada es indirecte i sense pesos" elif infor[7] == 'D' and infor[9] == '-': print "La xarxa carregada es directe i sense pesos" elif infor[7] == 'U' and infor[9] == 'W': print "La xarxa carregada es indirecte i amb pesos" pes = True elif infor[7] == 'D' and infor[9] == 'W': print "La xarxa carregada es directe i amb pesos" pes = True except IOError: print 'Error', arllegitgml else: print 'Arxiu carregat\n' menugml(arllegitgml,nllegitgml,pes) ####CRIDO FUNCIO DE ESCOLLIR L'ALGORISME PER GRAPHML### #num = escollir(arllegit,nllegit) #print num else: print "El format especificat no existeix\n" main()
def EdgeAttackErdosNetwork(): start_time = time.time() n = 429 fraction = 0.2 E = 1312 E_rm = int(0.2 * E) run_cnt = 30 #******** Run Node Attack 1 ********# tot_ND1 = [0] * (E_rm + 1) tot_T1 = [0] * (E_rm + 1) rndseed = 1 for i in range(run_cnt): G = nx.read_pajek("dataset/Erdos971_revised.net") G1 = max(nx.connected_component_subgraphs(G),key=len) print ">>>>>>>>>>>>>>> Random Attack run time count: ", i + 1, "<<<<<<<<<<<<<<<<<<" print "graph info", nx.info(G1) random.seed(rndseed) ND1, T1 = RandomEdgeAttack(G1, remove_fraction=fraction) tot_ND1 = [x + y for x, y in zip(tot_ND1, ND1)] rndseed += 1 tot_ND1 = [((x + 0.0) / run_cnt) for x in tot_ND1] tot_T1 = T1 tot_ND1 = [(x + 0.0) / (n + 0.0) for x in tot_ND1] tot_T1 = [(x + 0.0) / (E + 0.0) for x in tot_T1] with open("results2/edge_attack1_ErdosNetwork.csv", "w") as f: writer = csv.writer(f, delimiter=',') writer.writerows(zip(tot_T1, tot_ND1)) run_cnt = 1 random.seed() #******** Run Node Attack 2 ********# tot_ND1 = [0] * (E_rm + 1) tot_T1 = [0] * (E_rm + 1) for i in range(run_cnt): G = nx.read_pajek("dataset/Erdos971_revised.net") G1 = max(nx.connected_component_subgraphs(G),key=len) print ">>>>>>>>>>>>>>> Initial Degree Attack run time count: ", i + 1, "<<<<<<<<<<<<<<<<<<" print "graph info", nx.info(G1) ND1, T1 = InitialEdgeDegreeAttack(G1, remove_fraction=fraction) tot_ND1 = [x + y for x, y in zip(tot_ND1, ND1)] tot_ND1 = [((x + 0.0) / run_cnt) for x in tot_ND1] tot_T1 = T1 tot_ND1 = [(x + 0.0) / (n + 0.0) for x in tot_ND1] tot_T1 = [(x + 0.0) / (E + 0.0) for x in tot_T1] with open("results2/edge_attack2_ErdosNetwork.csv", "w") as f: writer = csv.writer(f, delimiter=',') writer.writerows(zip(tot_T1, tot_ND1)) run_cnt = 1 random.seed() #******** Run Node Attack 3 ********# tot_ND1 = [0] * (E_rm + 1) tot_T1 = [0] * (E_rm + 1) for i in range(run_cnt): G = nx.read_pajek("dataset/Erdos971_revised.net") G1 = max(nx.connected_component_subgraphs(G),key=len) print ">>>>>>>>>>>>>>> Recalculated Degree Attack run time count: ", i + 1, "<<<<<<<<<<<<<<<<<<" print "graph info", nx.info(G1) ND1, T1 = RecalculatedEdgeDegreeAttack(G1, remove_fraction=fraction) tot_ND1 = [x + y for x, y in zip(tot_ND1, ND1)] tot_ND1 = [((x + 0.0) / run_cnt) for x in tot_ND1] tot_T1 = T1 tot_ND1 = [(x + 0.0) / (n + 0.0) for x in tot_ND1] tot_T1 = [(x + 0.0) / (E + 0.0) for x in tot_T1] with open("results2/edge_attack3_ErdosNetwork.csv", "w") as f: writer = csv.writer(f, delimiter=',') writer.writerows(zip(tot_T1, tot_ND1)) run_cnt = 1 random.seed() #******** Run Node Attack 4 ********# tot_ND1 = [0] * (E_rm + 1) tot_T1 = [0] * (E_rm + 1) for i in range(run_cnt): G = nx.read_pajek("dataset/Erdos971_revised.net") G1 = max(nx.connected_component_subgraphs(G),key=len) print ">>>>>>>>>>>>>>> Initial Betweenness Attack run time count: ", i + 1, "<<<<<<<<<<<<<<<<<<" print "graph info", nx.info(G1) ND1, T1 = InitialEdgeBetweennessAttack(G1, remove_fraction=fraction) tot_ND1 = [x + y for x, y in zip(tot_ND1, ND1)] tot_ND1 = [((x + 0.0) / run_cnt) for x in tot_ND1] tot_T1 = T1 tot_ND1 = [(x + 0.0) / (n + 0.0) for x in tot_ND1] tot_T1 = [(x + 0.0) / (E + 0.0) for x in tot_T1] with open("results2/edge_attack4_ErdosNetwork.csv", "w") as f: writer = csv.writer(f, delimiter=',') writer.writerows(zip(tot_T1, tot_ND1)) run_cnt = 1 random.seed() #******** Run Node Attack 5 ********# tot_ND1 = [0] * (E_rm + 1) tot_T1 = [0] * (E_rm + 1) for i in range(run_cnt): G = nx.read_pajek("dataset/Erdos971_revised.net") G1 = max(nx.connected_component_subgraphs(G),key=len) print ">>>>>>>>>>>>>>> Recalculated Betweenness Attack run time count: ", i + 1, "<<<<<<<<<<<<<<<<<<" print "graph info", nx.info(G1) ND1, T1 = RecalculatedEdgeBetweennessAttack(G1, remove_fraction=fraction) tot_ND1 = [x + y for x, y in zip(tot_ND1, ND1)] tot_ND1 = [((x + 0.0) / run_cnt) for x in tot_ND1] tot_T1 = T1 tot_ND1 = [(x + 0.0) / (n + 0.0) for x in tot_ND1] tot_T1 = [(x + 0.0) / (E + 0.0) for x in tot_T1] with open("results2/edge_attack5_ErdosNetwork.csv", "w") as f: writer = csv.writer(f, delimiter=',') writer.writerows(zip(tot_T1, tot_ND1)) print "--- cost time %s seconds ---" %(time.time() - start_time)
def EdgeAttackUSAir(): start_time = time.time() n = 332 fraction = 0.2 E = 2126 E_rm = int(0.2 * E) run_cnt = 100 #******** Run Edge Attack 1 ********# tot_ND1 = [0] * (E_rm + 1) tot_T1 = [0] * (E_rm + 1) rndseed = 1; for i in range(run_cnt): G1 = nx.read_pajek("dataset/USAir97.net") print ">>>>>>>>>>>>>>> Random Attack run time count: ", i + 1, "<<<<<<<<<<<<<<<<<<" print "graph info", nx.info(G1) random.seed(rndseed) ND1, T1 = RandomEdgeAttack(G1, remove_fraction=fraction) tot_ND1 = [x + y for x, y in zip(tot_ND1, ND1)] rndseed += 1; tot_ND1 = [((x + 0.0) / run_cnt) for x in tot_ND1] tot_T1 = T1 tot_ND1 = [(x + 0.0) / (n + 0.0) for x in tot_ND1] tot_T1 = [(x + 0.0) / (E + 0.0) for x in tot_T1] with open("results2/edge_attack1_USAir.csv", "w") as f: writer = csv.writer(f, delimiter=',') writer.writerows(zip(tot_T1, tot_ND1)) run_cnt = 3 #******** Run Edge Attack 2 ********# tot_ND1 = [0] * (E_rm + 1) tot_T1 = [0] * (E_rm + 1) for i in range(run_cnt): G1 = nx.read_pajek("dataset/USAir97.net") print ">>>>>>>>>>>>>>> Initial Degree Attack run time count: ", i + 1, "<<<<<<<<<<<<<<<<<<" print "graph info", nx.info(G1) ND1, T1 = InitialEdgeDegreeAttack(G1, remove_fraction=fraction) tot_ND1 = [x + y for x, y in zip(tot_ND1, ND1)] tot_ND1 = [((x + 0.0) / run_cnt) for x in tot_ND1] tot_T1 = T1 tot_ND1 = [(x + 0.0) / (n + 0.0) for x in tot_ND1] tot_T1 = [(x + 0.0) / (E + 0.0) for x in tot_T1] with open("results2/edge_attack2_USAir.csv", "w") as f: writer = csv.writer(f, delimiter=',') writer.writerows(zip(tot_T1, tot_ND1)) run_cnt = 3 #******** Run Edge Attack 3 ********# tot_ND1 = [0] * (E_rm + 1) tot_T1 = [0] * (E_rm + 1) for i in range(run_cnt): G1 = nx.read_pajek("dataset/USAir97.net") print ">>>>>>>>>>>>>>> Recalculated Degree Attack run time count: ", i + 1, "<<<<<<<<<<<<<<<<<<" print "graph info", nx.info(G1) ND1, T1 = RecalculatedEdgeDegreeAttack(G1, remove_fraction=fraction) tot_ND1 = [x + y for x, y in zip(tot_ND1, ND1)] tot_ND1 = [((x + 0.0) / run_cnt) for x in tot_ND1] tot_T1 = T1 tot_ND1 = [(x + 0.0) / (n + 0.0) for x in tot_ND1] tot_T1 = [(x + 0.0) / (E + 0.0) for x in tot_T1] with open("results2/edge_attack3_USAir.csv", "w") as f: writer = csv.writer(f, delimiter=',') writer.writerows(zip(tot_T1, tot_ND1)) run_cnt = 3 #******** Run Edge Attack 4 ********# tot_ND1 = [0] * (E_rm + 1) tot_T1 = [0] * (E_rm + 1) for i in range(run_cnt): G1 = nx.read_pajek("dataset/USAir97.net") print ">>>>>>>>>>>>>>> Initial Betweenness Attack run time count: ", i + 1, "<<<<<<<<<<<<<<<<<<" print "graph info", nx.info(G1) ND1, T1 = InitialEdgeBetweennessAttack(G1, remove_fraction=fraction) tot_ND1 = [x + y for x, y in zip(tot_ND1, ND1)] tot_ND1 = [((x + 0.0) / run_cnt) for x in tot_ND1] tot_T1 = T1 tot_ND1 = [(x + 0.0) / (n + 0.0) for x in tot_ND1] tot_T1 = [(x + 0.0) / (E + 0.0) for x in tot_T1] with open("results2/edge_attack4_USAir.csv", "w") as f: writer = csv.writer(f, delimiter=',') writer.writerows(zip(tot_T1, tot_ND1)) run_cnt = 3 #******** Run Edge Attack 5 ********# tot_ND1 = [0] * (E_rm + 1) tot_T1 = [0] * (E_rm + 1) for i in range(run_cnt): G1 = nx.read_pajek("dataset/USAir97.net") print ">>>>>>>>>>>>>>> Recalculated Betweenness Attack run time count: ", i + 1, "<<<<<<<<<<<<<<<<<<" print "graph info", nx.info(G1) ND1, T1 = RecalculatedEdgeBetweennessAttack(G1, remove_fraction=fraction) tot_ND1 = [x + y for x, y in zip(tot_ND1, ND1)] tot_ND1 = [((x + 0.0) / run_cnt) for x in tot_ND1] tot_T1 = T1 tot_ND1 = [(x + 0.0) / (n + 0.0) for x in tot_ND1] tot_T1 = [(x + 0.0) / (E + 0.0) for x in tot_T1] with open("results2/edge_attack5_USAir.csv", "w") as f: writer = csv.writer(f, delimiter=',') writer.writerows(zip(tot_T1, tot_ND1)) print "--- cost time %s seconds ---" %(time.time() - start_time)
queueCalc("nx.rich_club_coefficient", "Rich Club Coefficient", argFileName, "richClubCoefficient", networkName, networkHash) queueCalc("nx.flow_hierarchy", "Flow Heirarchy", argFileName, "flowHierarchy", networkName, networkHash) # Parses a .gml or pajek-formatted network and loads as a networkx network object def loadNetwork(f, ext): if ext == "gml": try: return nx.read_gml(f) except Exception, e: print("Couldn't load " + f + " as gml.") return False elif ext == "net": try: return nx.read_pajek(f) except Exception, e: print("Couldn't load " + f + " as pajek.") return False else: # assume it's just an adjacency list try: return nx.read_adjlist(f) except Exception, e: print(e) print("Couldn't load " + f + " as adjacency list.") # This is an interruptible thread that is created by the calcOne() function # for all of the networkx computations. The purpose of this is to allow # very long calculations to be interruptible and, in the future, be parallelized. # It uses a trace that monitors each line of execution and monitors an internal `killed` # state that can be toggled to instantly kill the thread cleanly from within.
import networkx as nx import sys if __name__ == '__main__': usage = "usage: python %prog pajek_filename tagert_filename" print("# loading input...") h = nx.read_pajek(sys.argv[1]) print("Finding cliques...") cliques = list(nx.find_cliques(h)) f = open(sys.argv[2], 'w') print("Printing the cliques...") for item in cliques: if(len(item))>=4: f.writelines(', '.join(str(j) for j in item) + '\n') f.close()
import networkx as net import matplotlib.pyplot as plot g=net.read_pajek('russians.net') len(g) deg=net.degree(g) print deg['valerois'] print min(deg.values()) print max(deg.values()) ### This function returns a sorted degree list -- useful for celebrity-spotting def sorted_map(map): ms = sorted(map.iteritems(), key=lambda (k,v): (-v,k)) return ms ds=sorted_map(deg) ### Top 10 people in the list print ds[0:9] h=plot.hist(deg.values(),100) ## display a histogram of node degrees plot.loglog(h[1][1:],h[0]) ## plot the same histogram in Log-Log space plot.show() def trim_degrees(g, degree=1): g2=g.copy() d=net.degree(g2) for n in g2.nodes(): if d[n]<=degree: g2.remove_node(n) return g2
def main(): print "\n\nPrograma per l'analisi de xarxes complexes i la deteccio de comunitats." #root = Tk() #print glob.glob('./networks/*.*') ruta = glob.glob('./networks/*.*') #troba la ruta dels arxius que indiquem print "\nXarxes disponibles a la carpeta networks:" llista = [] filename = askopenfilename(initialdir=("./networks/")) #print filename ftemp = filename.split('/') #print ftemp ful = ftemp[-1] #print ful nb = ful.split('.') ###### cargar el graf depenent del format de l'arxiu ##### if nb[1] == 'graphml': try: #print 'estic dins del graphml' arllegit = Graph.Read_GraphML("./networks/"+nb[0]+".graphml") #llegit per igraph nllegit = nx.read_graphml("./networks/"+nb[0]+".graphml") #llegit per networkx #ft.save_jsonfile()####### info = GraphSummary(arllegit) infor = info.__str__() if infor[7] == 'U': print "La xarxa carregada es indirecte" elif infor[7] == 'D': print "La xarxa carregada es directe" except IOError: print 'Error', arllegit else: print 'Arxiu carregat\n' menugraphml(arllegit,nllegit) ####CRIDO FUNCIO DE ESCOLLIR L'ALGORISME PER GRAPHML### #num = escollir(arllegit,nllegit) #print num elif nb[1] == 'net': try: #print 'estic dins del net' arllegitnet = Graph.Read_Pajek("./networks/"+nb[0]+".net") #llegit per igraph nllegitnet = nx.read_pajek("./networks/"+nb[0]+".net") #llegit per networkx info = GraphSummary(arllegitnet) infor = info.__str__() if infor[7] == 'U': print "La xarxa carregada es indirecte" elif infor[7] == 'D': print "La xarxa carregada es directe" except IOError: print 'Error', arllegitnet else: print 'Arxiu carregat\n' menunet(arllegitnet,nllegitnet)