def hubs(network, n=20, d="directed"): """Find top n nodes with highest degree and write results in the new file. Parameters ---------- network : network edge list n : int number of wanted nodes d : directed or undirected if directed is selected than two new files will be created. One for in-degree and one for out-degree """ n = int(n) if d == "directed": g = nx.read_weighted_edgelist(network, create_using=nx.DiGraph()) if g.number_of_nodes() < n: n = int(g.number_of_nodes()) degree_list_in = [node for node in g.in_degree().iteritems()] degree_list_in.sort(key=lambda x: x[1]) degree_list_in.reverse() degree_list_out = [node for node in g.out_degree().iteritems()] degree_list_out.sort(key=lambda x: x[1]) degree_list_out.reverse() with open(network.rsplit(".", 1)[0] + "_hubs_in.txt", "w", encoding="utf-8") as write_f_in: for i, value in enumerate(degree_list_in): if i < n: write_f_in.write(str(value[0]) + "\t\tIn-degree: " + str(value[1]) + "\n") else: break with open(network.rsplit(".", 1)[0] + "_hubs_out.txt", "w", encoding="utf-8") as write_f_out: for i, value in enumerate(degree_list_out): if i < n: write_f_out.write(str(value[0]) + "\t\tOut-degree: " + str(value[1]) + "\n") else: break elif d == "undirected": g = nx.read_weighted_edgelist(network) if g.number_of_nodes() < n: n = int(g.number_of_nodes()) degree_list = [node for node in g.degree().iteritems()] degree_list.sort(key=lambda x: x[1]) degree_list.reverse() with open(network.rsplit(".", 1)[0] + "_hubs.txt", "w", encoding="utf-8") as write_f: for i, value in enumerate(degree_list): if i < n: write_f.write(str(value[0]) + "\t\tDegree: " + str(value[1]) + "\n") else: break
def jaccard(network1, network2, d="directed"): """Returns Jaccard similarity coefficient and distance of two different networks of the same sets of nodes. Parameters ---------- network1 : first network edge list network2 : second network edge list d : directed or undirected type of graph Returns ------- j : float Jaccard similarity coefficient jd : float Jaccard distance """ if d == "directed": g1 = nx.read_weighted_edgelist(network1, create_using=nx.DiGraph()) g2 = nx.read_weighted_edgelist(network2, create_using=nx.DiGraph()) elif d == "undirected": g1 = nx.read_weighted_edgelist(network1) g2 = nx.read_weighted_edgelist(network2) union = nx.compose(g1, g2) inter = nx.intersection(g1, g2) j = float(inter.number_of_edges()) / float(union.number_of_edges()) jd = 1 - j return j, jd
def weightiest_edges(network, n=20, d="directed"): """Find top n edges with highest weights and write results in the new file. Parameters ---------- network : network edge list n : int number of wanted edges d : directed or undirected type of graph """ if d == "directed": g = nx.read_weighted_edgelist(network, create_using=nx.DiGraph()) elif d == "undirected": g = nx.read_weighted_edgelist(network) if g.number_of_edges() < n: n = g.number_of_edges() weight_dict = {(u, v): i['weight'] for (u, v, i) in g.edges(data=True)} weight_list = [edge for edge in weight_dict.iteritems()] weight_list.sort(key=lambda x: x[1]) weight_list.reverse() with open(network.rsplit(".", 1)[0] + "_weightiest_edges.txt", "w", encoding="utf-8") as write_f: for i, value in enumerate(weight_list): if i < n: write_f.write(str(value[0][0]) + "\t\t: " + str(value[0][1]) + "\t\t" + str(value[1]) + "\n") else: break
def total_overlap(network1, network2, d="directed"): """Returns value of total overlap measure for two given networks of the same sets of nodes. Parameters ---------- network1 : first network edge list network2 : second network edge list d : directed or undirected type of graph Returns ------- t_overlap : float """ if d == "directed": g1 = nx.read_weighted_edgelist(network1, create_using=nx.DiGraph()) g2 = nx.read_weighted_edgelist(network2, create_using=nx.DiGraph()) elif d == "undirected": g1 = nx.read_weighted_edgelist(network1) g2 = nx.read_weighted_edgelist(network2) overlap = 0 for i in g1.edges(): if g2.has_edge(i[0], i[1]): overlap += 1 t_overlap = (float(overlap) / float(nx.compose(g1, g2).number_of_edges())) return t_overlap
def main(): parser = argparse.ArgumentParser() parser.add_argument('edgelist') parser.add_argument('outfile', nargs='?') parser.add_argument('-t', '--interconnectivity', default=0.83, type=float) parser.add_argument('-d', '--density', default=0.83, type=float) parser.add_argument('-m', '--min-edge', default=0.05, type=float) parser.add_argument('-l', '--linkage', default='average') parser.add_argument('-a', '--authorprefeat', default='generated/Author_prefeat.pickle') args = parser.parse_args() if args.outfile == None: args.outfile = args.edgelist.replace('.prob','') + '.clusters' threshold_min_weight = args.min_edge threshold_interconnectivity = args.interconnectivity threshold_density = args.density print_err("Loading graph") G_sim = nx.read_weighted_edgelist(enforce_min(skip_comments(open(args.edgelist, 'rb')), threshold_min_weight), nodetype=int, delimiter=',') print_err('Loaded (V={:}, E={:})'.format(len(G_sim), G_sim.size())) print_err("Clustering") clusters = hcluster(G_sim, threshold_interconnectivity, args.linkage) print_err("Writing clusters") G_nsim = nx.read_weighted_edgelist(skip_comments(open(args.edgelist, 'rb')), nodetype=int, delimiter=',') print_err("Loading pickled author pre-features") authors = pickle.load(open(args.authorprefeat, 'rb')) outputClusters(clusters, open(args.outfile, 'wb'), G_nsim, authors, threshold_density)
def getAllPaths(): #import matplotlib.pyplot as plt g = nx.read_weighted_edgelist("hb.txt") #print g["ASPA0085"]["HOHA0402"] fp = open("allpaths.txt", 'w') try: counter = 1 for eachPath in nx.all_shortest_paths(g, u"ASPA0085", u"GLUA0194"): if not isValidPath(eachPath): continue fp.write("path%d" % counter) for eachResidue in eachPath: fp.write('%10s' % eachResidue) fp.write('\n') counter += 1 except nx.exception.NetworkXNoPath: fp.write("No connected pathway\n") finally: fp.close()
def selectivity(network): """Calculate selectivity for each node in graph and write results in dictionary. Parameters ---------- network : edge list of network Returns ------- selectivity_dict : dict a dictionary where keys are graph nodes and values are calculated selectivity """ g = nx.read_weighted_edgelist(network) selectivity_dict = {} for node in g.nodes(): s = g.degree(node, weight="weight") k = g.degree(node, weight=None) if k > 0: selectivity = s / k selectivity_dict[node] = selectivity else: selectivity_dict[node] = 0 return selectivity_dict
def reciprocity(network): """Returns reciprocity of the given network. Parameters ---------- network : edge list of the network Returns ------- r : float network reciprocity a : float the ratio of observed to possible directed links ro : float Garlaschelli and Loffredo's definition of reciprocity """ g = nx.read_weighted_edgelist(network, create_using=nx.DiGraph()) self_loops = g.number_of_selfloops() r = sum([g.has_edge(e[1], e[0]) for e in g.edges_iter()]) / float(g.number_of_edges()) a = (g.number_of_edges() - self_loops) / (float(g.number_of_nodes()) * float((g.number_of_nodes() - 1))) ro = float((r - a)) / float((1 - a)) return r, a, ro
def edges(currentNode): # hardcoded source for graph g = nx.read_weighted_edgelist("example_4_pathfind.graph", nodetype=str,create_using=nx.DiGraph()) # output successors for node in g.successors(currentNode.value()): dlvhex.output( (node,) )
def read_test_trps_txt(path, toNX=False, skip = 0): # Accepts path to TEST_FNAME # If toNX, returns a nx.DiGraph, otherwise returns a ndarray # Can be given a number of rows to skip, ndarray case only if toNX: return nx.read_weighted_edgelist(path + TEST_FNAME, create_using=nx.DiGraph(), nodetype=int) return np.loadtxt(path + TEST_FNAME, skiprows = skip)
def loadGraphs(filenames, verb=False): """ Given a list of files, returns a dictionary of graphs Required parameters: filenames: - List of filenames for graphs Optional parameters: verb: - Toggles verbose output statements """ # Initializes empty dictionary if type(filenames) is not list: filenames = [filenames] gstruct = OrderedDict() for idx, files in enumerate(filenames): if verb: print("Loading: " + files) # Adds graphs to dictionary with key being filename fname = os.path.basename(files) try: gstruct[fname] = nx.read_weighted_edgelist(files) except: try: gstruct[fname] = nx.read_gpickle(files) except: gstruct[fname] = nx.read_graphml(files) return gstruct
def main(): parser = createParser() options = parser.parse_args() gtGraphNames = glob.glob("{0}/*.sim.cut".format(options.gtruth)) gtGraphs = { fn.split("/")[-1][:-8] : nx.read_edgelist(fn) for fn in gtGraphNames } print(gtGraphs) print(gtGraphNames) oGraphNames = [ "{0}/{1}.out.ppi".format(options.other, k) for k in gtGraphs.keys() ] oGraphs = { fn.split("/")[-1][:-8] : nx.read_weighted_edgelist(fn) for fn in oGraphNames } inputGraphNames = glob.glob("{0}/bZIP*.cut".format(options.other)) print(inputGraphNames) inputGraph = nx.read_edgelist(inputGraphNames[0]) print(oGraphNames) cutoff = 0.99 paranaGraph = graphWithCutoff(options.parana, 0.0) c = findSuggestedCutoff( paranaGraph, inputGraph, cutoff ) evaluation.printStats( filteredGraph(paranaGraph, inputGraph.nodes(), cutoff=c ), inputGraph ) print >>sys.stderr, "Parana 2.0 : {0}".format(getCurve(paranaGraph, inputGraph)) for gtName, gtGraph in gtGraphs.iteritems(): print(gtName) c = findSuggestedCutoff( paranaGraph, gtGraph, cutoff ) print("Parana cutoff = {0}".format(c)) print("==================") evaluation.printStats( filteredGraph(oGraphs[gtName], gtGraph.nodes()), gtGraph ) print >>sys.stderr, "Pinney et. al : {0}".format(getCurve(oGraphs[gtName], gtGraph)) evaluation.printStats( filteredGraph(paranaGraph, gtGraph.nodes(), cutoff=c ), gtGraph ) print >>sys.stderr, "Parana 2.0 : {0}".format(getCurve(paranaGraph, gtGraph)) print("\n") sys.exit(0)
def create_network(self, nodes, edges, nodes_max_length, edges_min_weight): # limit network size top_nodes=self.get_top_nodes(nodes,nodes_max_length) print "%d top_nodes"%len(top_nodes) top_edges=self.get_edges_containing_nodes(edges, top_nodes) print "%d top_edges"%len(top_edges) weighted_edges = self.get_weigthed_edges(top_edges, edges_min_weight) weighted_edges_str=[ str(nodes.index(w[0]))+" "+str(nodes.index(w[1]))+" "+str(w[2]) for w in weighted_edges ] # create graph object G = nx.read_weighted_edgelist(weighted_edges_str, nodetype=str, delimiter=" ",create_using=nx.DiGraph()) # dimensions N,K = G.order(), G.size() print "Nodes: ", N print "Edges: ", K return G
def total_weighted_overlap(network1, network2, d="directed"): """Returns value of total weighted overlap measure for two given networks of the same sets of nodes. Parameters ---------- network1 : first network edge list network2 : second network edge list d : directed or undirected type of graph Returns ------- t_w_overlap : float """ if d == "directed": g1 = nx.read_weighted_edgelist(network1, create_using=nx.DiGraph()) g2 = nx.read_weighted_edgelist(network2, create_using=nx.DiGraph()) elif d == "undirected": g1 = nx.read_weighted_edgelist(network1) g2 = nx.read_weighted_edgelist(network2) union = nx.compose(g1, g2) w_max_g1 = 0 w_max_g2 = 0 for (u,d,v) in g1.edges(data=True): if v['weight'] > w_max_g1: w_max_g1 = v['weight'] for (u,d,v) in g2.edges(data=True): if v['weight'] > w_max_g2: w_max_g2 = v['weight'] overall_weight = 0 for (u,d,v) in union.edges(data=True): overall_weight += v['weight'] sum_list = [min(float((g1.edge[u][d]['weight'] / w_max_g1)), float((g2.edge[u][d]['weight'] / w_max_g2))) for (u,d,v) in g1.edges(data=True) if g2.has_edge(u, d)] overlap = sum(sum_list) t_w_overlap = (float(overlap) / float(overall_weight)) return t_w_overlap
def load(self): """ Loads itself from a file. The data structure could get quite large, caching to disk is a good idea ** note ** replace with Redis in production -- Redis dependency is removed for Open Source release to decrease complexity """ l.info("<<<<<<<< Loading Word-Graph>>>>>>>") self.word_graph=net.read_weighted_edgelist("wordgraph_edgelist.txt")
def edges(fileName, currentNode): # Sources will be loaded from the file g = nx.read_weighted_edgelist(fileName.value().strip('"'), nodetype=str, create_using=nx.DiGraph()) # Output successor nodes of the current node including weight for node in g.successors(currentNode.value()): weight = g[currentNode.value()][node]["weight"] # produce one output tuple dlvhex.output((node, int(weight)))
def __init__(self): self.G = nx.read_weighted_edgelist( CliqueSpy.get_data_dir() + CliqueSpy.SINGER_GRAPH_FILE, delimiter=';', encoding='utf-8' ) self.G.remove_edges_from(self.G.selfloop_edges()) self.singer_dict = CliqueSpy.read_dict(CliqueSpy.get_data_dir() + CliqueSpy.SINGER_DICT)
def grapheme_net(syllable_network, d="directed", w="weighted"): """Creates grapheme network. The structure of grapheme network depends on a existing network of syllables. Two graphemes are linked if they co-occur as neighbours within a syllable. Parameters ---------- syllable_network : edge list of a syllable network d : directed or undirected type of graph w : weighted or unweighted if weighted is selected than the weight of the link between two graphemes will be proportional to the overall frequencies of the corresponding graphemes co-occurring within syllable from a syllable network """ if d == "directed": syllable_net = nx.read_weighted_edgelist(syllable_network, create_using=nx.DiGraph()) g = nx.DiGraph() elif d == "undirected": syllable_net = nx.read_weighted_edgelist(syllable_network) g = nx.Graph() for node in syllable_net.nodes(): graphemes = list(node) for i, gr in enumerate(graphemes): if i > 0: if w == "weighted": if g.has_edge(graphemes[i - 1], graphemes[i]): g[graphemes[i - 1]][graphemes[i]]['weight'] += 1 else: g.add_edge(graphemes[i - 1], graphemes[i], weight=1) elif w == "unweighted": g.add_edge(graphemes[i - 1], graphemes[i]) if w == "unweighted": nx.write_edgelist(g, syllable_network.rsplit(".", 1)[0] + "_grapheme.edges") elif w == "weighted": nx.write_weighted_edgelist(g, syllable_network.rsplit(".", 1)[0] + "_grapheme.edges") return g
def __init__(self, graphFilePath,randomSeed=None,weigthed=True): with open(graphFilePath) as f: if weigthed: self.G = nx.read_weighted_edgelist(f, create_using=nx.DiGraph(),nodetype=int); self.weight = 'weight'; else: self.G = nx.read_edgelist(f, create_using=nx.DiGraph(),nodetype=int); self.random = rand.Random(randomSeed); self.randomSeed = randomSeed;
def friendsOfDegree(personOfInterest): # graph of the friends will be loaded from the external file g = nx.read_weighted_edgelist("example_2_1.edgelist", nodetype=str,create_using=nx.DiGraph()) # Take successor nodes of the node of interest. friendList = g.successors(personOfInterest.value()) # Output the successor nodes for node in friendList: dlvhex.output((node,)) # outputs direct fiends
def edges(currentNode): # Sources will be loaded from the file g = nx.read_weighted_edgelist("example_4_3.edgelist", nodetype=str,create_using=nx.DiGraph()) # Take successor nodes of the current node friendList = g.successors(currentNode.value()) # Output the successors for node in friendList: dlvhex.output((node,)) #sends city and weight to the output dlvhex.output((currentNode.value(),)) #sends city and weight to the output
def main(): for _, _, file_names in os.walk(DATA_DIR): for file_name in file_names: if file_name.endswith('.gz'): print 'Reading data from file %s' % file_name path = DATA_DIR + file_name graph = nx.read_weighted_edgelist(path, create_using=nx.DiGraph()) out_name = path.replace('.gz', '.gpickle') print 'Saving data to %s' % out_name graph = directed_to_undirected_multi(graph) nx.write_gpickle(graph, out_name)
def read_train_trps_txt(path, toNX=False, skip = 0, fn = ''): # Accepts path to TRAIN_FNAME # If toNX, returns a nx.DiGraph, otherwise returns a ndarray # Can be given a number of rows to skip, ndarray case only if len(fn)>0: fname = fn else: fname = TRAIN_FNAME if toNX: return nx.read_weighted_edgelist(path + fname, create_using=nx.DiGraph(), nodetype=int) return np.loadtxt(path + fname, skiprows = skip)
def ego_word_subnet(word_network, word, radius=1, d="directed", w="weighted", neighborhood="all"): """Creates word-ego network which is a subnetwork of neighbours centered at one specified node (word) within a given radius. Parameters ---------- word_network : edge list of original network word : string subnetwork will be created of neighbours centered at specified word radius : int radius from which subnetwork will be created d : directed or undirected type of graph w : weighted or unweighted if weighted is selected than the weight of the link between two words will be proportional to the overall frequencies of the corresponding words co-occurrence within a original network neighborhood : successors, predecessors or all """ if d == "directed": word_net = nx.read_weighted_edgelist(word_network, create_using=nx.DiGraph()) if neighborhood == "successors": sg = nx.ego_graph(word_net, word, radius) elif neighborhood == "predecessors": sg = nx.ego_graph(word_net.reverse(), word, radius) elif neighborhood == "all": sg = nx.ego_graph(word_net, word, radius, undirected=True) elif d == "undirected": word_net = nx.read_weighted_edgelist(word_network) sg = nx.ego_graph(word_net, word, radius) if w == "unweighted": nx.write_edgelist(sg, word_network.rsplit(".", 1)[0] + "_ego_subnetwork.edges") elif w == "weighted": nx.write_weighted_edgelist(sg, word_network.rsplit(".", 1)[0] + "_ego_subnetwork.edges") return sg
def study(name,theta_init = None) : # graphe = nx.read_edgelist("Internet_hyperbolic_map/AS_ARK200906_topology.txt") graphe = nx.read_weighted_edgelist(name) gamma = exponent.get_exponent(graphe) # gamma = 2.1 # Verify gamma : # exponent.plot_gamma(graphe) Nobs = graphe.number_of_nodes() k_bar_obs = np.mean(graphe.degree().values()) k_max_obs = max(graphe.degree().values()) C_obs = nx.average_clustering(graphe, count_zeros=False) print Nobs, k_bar_obs, k_max_obs, C_obs alpha, kappa0, kappaC, P0, k_bar = equations.solve(gamma, k_bar_obs, k_max_obs) # Verify equations #equations.verify(alpha, kappa0, kappaC, P0, k_bar, gamma, k_bar_obs, k_max_obs) #N = Nobs / (1 - P0) #print gamma, alpha, kappa0, kappaC, P0, k_bar, N """ alpha = 0.58 kappa0 = 0.9 kappaC = 4790 P0 = 0.33 N = 35685 k_bar = 9.86 """ # Estimating beta ''' beta,path_followed = generate_networks.approximated_beta(N,k_bar,kappa0,gamma,C_obs, start=1.01, step = 0.01,nb_networks=100) print beta ''' # Verify beta: # print path_followed beta = 1.02 #beta =1.45 # Creating map: graphe = nx.convert_node_labels_to_integers(graphe) constants = kappa0,gamma,beta,Nobs,k_bar r_opt = loglikelihood.rayon(graphe,constants) if theta_init is None : theta_init = 2*math.pi*np.random.randint(0,Nobs,Nobs)/Nobs theta_opt = loglikelihood.optimisation_likelihood(graphe,constants,theta_init=theta_init.copy()) #theta_opt = loglikelihood.optimisation_complete_par_etapes_likelihood(graphe,[5,4,3,2,0],theta_init.copy(),constants) #theta_opt = loglikelihood.optimisation_par_etapes_likelihood(graphe,"",14,theta_init.copy(),constants,method="core_number") with open('test50.txt', 'w') as f: f.write(str(theta_opt)) print "saved" return r_opt,theta_opt
def main(): # Read the weighted edgelist G = nx.read_weighted_edgelist(sys.stdin) weights = [ (n1, n2, G[n1][n2]['weight']) for n1, n2 in G.edges_iter() ] # Sort nodes in decending order weights.sort(key=lambda x : x[2], reverse=True) # Write nodes and maxWeights out with UNIX-style line endings for t in weights: sys.stdout.write("%s\t%s\t%f\n" % t )
def measure_dict(net, m="selectivity", d="undirected"): if d == "out": if m == "selectivity": measure = measures.out_selectivity(net) elif m == "degree": g = nx.read_weighted_edgelist(net, create_using=nx.DiGraph()) measure = g.out_degree() elif m == "strength": g = nx.read_weighted_edgelist(net, create_using=nx.DiGraph()) measure = g.out_degree(weight='weight') elif d == "in": if m == "selectivity": measure = measures.in_selectivity(net) elif m == "degree": g = nx.read_weighted_edgelist(net, create_using=nx.DiGraph()) measure = g.in_degree() elif m == "strength": g = nx.read_weighted_edgelist(net, create_using=nx.DiGraph()) measure = g.in_degree(weight='weight') elif d == "undirected": if m == "selectivity": measure = measures.selectivity(net) elif m == "degree": g = nx.read_weighted_edgelist(net) measure = g.degree() elif m == "strength": g = nx.read_weighted_edgelist(net) measure = g.degree(weight='weight') return measure
def node_distance(network, node, nodes_file, d="directed", w="weighted"): """Find node distances between specific node and other nodes defined in nodes_file. Write results in the new file. Parameters ---------- network : network edge list node : string node for which distances will be calculated nodes_file : file file with list of nodes d : directed or undirected type of graph w : weighted or unweighted if unweighted is selected than every edge has weight/distance/cost 1 """ if d == "directed": g = nx.read_weighted_edgelist(network, create_using=nx.DiGraph()) elif d == "undirected": g = nx.read_weighted_edgelist(network) with open(nodes_file, "r", encoding="utf-8") as f: node_list = f.read().splitlines() with open(network.rsplit(".", 1)[0] + "_node_distance.txt", "w", encoding="utf-8") as write_f: write_f.write(node + "\t\tDistance\n\n") for n in node_list: if n in g: if nx.has_path(g, node, n) is True: if w == "unweighted": write_f.write(n + "\t\t" + str(nx.shortest_path_length(g, source=node, target=n)) + "\n") elif w == "weighted": write_f.write(n + "\t\t" + str(nx.dijkstra_path_length(g, node, n)) + "\n") else: write_f.write(n + "\t\tNOT CONNECTED\n") else: write_f.write(n + "\t\tNOT FOUND\n")
def main(): # Read the weighted edgelist G = nx.read_weighted_edgelist(sys.stdin) # Calculate the maximum weight for each node maxWeights = [ (n, maxWeight(G,n)) for n in G.nodes_iter() ] # Sort nodes in decending order maxWeights.sort(key=lambda x : x[1], reverse=True) # Write nodes and maxWeights out with UNIX-style line endings for node, weight in maxWeights: sys.stdout.write("%s\t%f\n" % (node, weight[0]))
def get_opera_graph(): graph = nx.read_weighted_edgelist( os.getcwd() + '/data/singer_singer/weighted/SINGER_SINGER.csv', delimiter=';', nodetype=int ) graph.remove_edges_from(graph.selfloop_edges()) components = list(nx.connected_component_subgraphs(graph, True)) giant = components.pop(0) degree = sorted(nx.degree(giant).items(), key=lambda x: x[1], reverse=True) avg = (0.0 + sum(value for (node, value) in degree)) / (0.0 + len(degree)) #print(avg) # 20.9834926151 return giant
a += (wei2 * wei3 * wei4) / ( D[u]**0.5 * D[v]**0.5 * D[i]**0.5 * D[j]**0.5) b += (wei2 * wei3 * wei4) / (D[u]**0.5 * D[v]**0.5) aa += a bb += b XA2[i, j] = aa # print('2:=========',i,j,aa) XB2[i, j] = bb # print('2-2:-----',i,j,bb) ############################################################################### t1 = time.time() G0 = nx.read_weighted_edgelist( '//extend2//chenyu//Sim//S.pombe//S.pombe_00.txt', delimiter=' ', create_using=None, nodetype=None, encoding='utf-8') nodes = [i for i in G0.nodes()] n = len(nodes) print('n=', n) X1 = np.zeros((n, n), dtype=np.float32) #全部正样本 for (u, v, w) in G0.edges(data=True): u1, v1 = nodes.index(u), nodes.index(v) X1[u1, v1], X1[v1, u1] = w['weight'], w['weight'] X2 = np.zeros((n, n), dtype=np.float32) #全部负样本 for i in range(n): for j in range(n): if X1[i, j] == 0: X2[i, j] = 1
def add_edges_from_file(self, path): # used by our method self.graph = nx.read_weighted_edgelist(path, create_using=nx.DiGraph(), nodetype=int)
def cut_prefix(se, start_pos=2): return {e[start_pos:] for e in se} def remove_small(comp, size=2): return [e for e in comp if len(e) > size] ### This use case shows how conduct isolation clustering on the three interactomes of yeast in the supplements os.chdir('yeast_biogrid/') graph_names = [ 'g_perturb_cosine.txt', 'g_topic_simrank.txt', 'dig_all_hybrid.txt' ] mats = [ nx.to_pandas_adjacency(nx.read_weighted_edgelist(e)) for e in graph_names[:2] ] mats.append( nx.to_pandas_adjacency( nx.reverse( nx.read_weighted_edgelist(graph_names[2], create_using=nx.DiGraph())))) conts = [ adj2con(e, 5) for e in mats ] # Transform the adjacency matrixes to the visiting probability matrix comps = [max_isolation(e) for e in conts] # Idenify clusters with locally maximal isolation comps[2] = [cut_prefix(c) for c in comps[2] ] # Remove the prefix of the two-layer interactome
intervals = int(sys.argv[3]) minvalue = float(sys.argv[4]) maxvalue = float(sys.argv[5]) tot_a = [] pos_a = [] for t in range(intervals): tot_a.append(0) pos_a.append(0) fa = open(filename_a, 'r') Ga = nx.read_adjlist(fa) fb = open(filename_b, 'r') Gb = nx.read_weighted_edgelist(fb) for u, v in Gb.edges(): Gbw = Gb[u][v]['weight'] for i in range(intervals): a = minvalue + float(maxvalue - minvalue) * float(i) / intervals b = minvalue + float(maxvalue - minvalue) * float(i + 1) / intervals if (Gbw > a and Gbw < b): tot_a[i] += 1 break if (Ga.has_edge(u, v) == True): pos_a[i] += 1 freq_a = [] for i in range(intervals):
import networkx as nx import matplotlib.pyplot as plt G= nx.read_edgelist(r"C:\Users\assurend\Documents\NPTEL\Freemans_EIES-3_n32.txt") G= nx.read_weighted_edgelist(r"C:\Users\assurend\Documents\NPTEL\Freemans_EIES-3_n32.txt") print (nx.info(G)) print(nx.number_of_edges(G)) print(nx.number_of_nodes(G)) print(nx.is_directed(G)) G= nx.read_pajek(r"C:\Users\assurend\Documents\NPTEL\karate.paj") print (nx.info(G)) print(nx.number_of_edges(G)) print(nx.number_of_nodes(G)) print(nx.is_directed(G)) G= nx.read_graphml(r"C:\Users\assurend\Documents\NPTEL\sample-social-network-datasets-master\sample-social-network-datasets-master\sample-datasets\marvel\marvel-network.graphml") print (nx.info(G)) print(nx.number_of_edges(G)) print(nx.number_of_nodes(G)) print(nx.is_directed(G))
def main(): os.system('clear') print "################################################################################" print " " print " Cálculo da reciprocity em cada Layer " print " " print "#################################################################################" print i = 0 if os.path.exists(output_dir_json + "reciprocity.json"): print("Arquivo de destino já existe!" + str(output_dir_json + "reciprocity.json")) else: create_dirs(output_dir_txt, output_dir_json) # Cria diretótio para salvar arquivos. dataset_json = {} # Salvar Arquivos no Formato Json with open(output_dir_txt + "reciprocity.txt", 'w') as out_file: for ego, v in dictionary.iteritems(): i += 1 nets = [ "n1", "n2", "n3", "n4", "n9" ] #[amigos,seguidores,retweets,likes,menções] # Camadas de interações no Twitter dataset = {} for net in nets: if net == "n1": layer = "a" elif net == "n9": layer = "s" elif net == "n2": layer = "r" elif net == "n3": layer = "l" elif net == "n4": layer = "m" else: print("Rede inválida") sys.exit() edge_list = "/home/amaury/graphs_hashmap/" + str( net) + "/graphs_with_ego/" # Diretório da camada i if not os.path.isdir( edge_list): # Verifica se diretório existe print( "Impossível localizar diretório com lista de arestas: " + str(edge_list)) else: source = str(edge_list) + str(ego) + ".edge_list" G = nx.read_weighted_edgelist( str(source), create_using=nx.DiGraph( )) # Carrega o grafo da camada i - Direcionado result = nx.overall_reciprocity( G) # Calcula reciprocity em cada layer dataset[layer] = result dataset_json[ego] = dataset print i, dataset_json[ego] save_file(ego, dataset, out_file) # Salvar arquivo texto print save_json(dataset_json) # Salvar arquivo no formato JSON print( "\n######################################################################\n" ) print("Script finalizado!") print( "\n######################################################################\n" )
def read_edgelist(workingfile): G = nx.MultiDiGraph() G = nx.read_weighted_edgelist(workingfile, create_using=nx.MultiDiGraph()) print "reading G is done" return G
from sklearn import svm import networkx as nx import operator import math import numpy as np import random from sklearn.model_selection import KFold from sklearn import metrics from sklearn.metrics import average_precision_score from sklearn.metrics import precision_score from sklearn.metrics import accuracy_score from sklearn.metrics import recall_score graphx = nx.read_weighted_edgelist("higgs-social_network.edgelist.txt") deg = list(graphx.degree) deg.sort(key=lambda x: x[1], reverse=True) max_deg_list = [x[0] for x in deg][:500] subgraph = graphx.subgraph(max_deg_list).copy() nodes = list(subgraph.nodes) def common_neighbours(G, a, b): a_n = list(G.neighbors(a)) b_n = list(G.neighbors(b)) return len(set(a_n) & set(b_n)) def jacard_coefficient(G, a, b): a_n = list(G.neighbors(a))
# -*- coding: utf-8 -*- """ Created on Sun Feb 28 09:36:47 2016 @author: megan squire """ import networkx as nx import operator # create a graph, filling it with one of the edgelists g = nx.read_weighted_edgelist('data/edgelist24.csv') # analyze the basic graph # make a Python dictionary full of each node and their degrees degree = nx.degree(g) # calculate some basic stuff about the nodes & degrees numNodes = nx.number_of_nodes(g) numEdges = nx.number_of_edges(g) minDegree = min(degree.values()) maxDegree = max(degree.values()) print('numNodes:', numNodes) print('numEdges:', numEdges) print('minDegree:', minDegree) print('maxDegree:', maxDegree) # sort the dictionary by highest degrees degreeSorted = sorted(degree.items(), key=operator.itemgetter(1), reverse=True) # print out the top ten nodes with the highest degrees
import networkx as nx import pandas as pd from node2vec import Node2Vec from gensim.models import KeyedVectors from collections import defaultdict from EvalUtils import EvalUtils # g = nx.read_weighted_edgelist("../datasets/redditdataset_75.txt", create_using=nx.DiGraph()) # gtest= nx.read_weighted_edgelist("../datasets/redditdataset_test.txt", create_using=nx.DiGraph()) # df1 = pd.read_csv('../datasets/redditdataset_75.txt', names = ['v1','v2','timestamp'],sep = '\t',lineterminator='\n',header = None) # df2 = pd.read_csv('../datasets/redditdataset_test.txt', names = ['v1','v2','timestamp'],sep = '\t',lineterminator='\n',header = None) # node2vec = Node2Vec(filename='../datasets/redditdataset_75.txt', is_temporal=True, walk_length=8) # Use temp_folder for big graphs g = nx.read_weighted_edgelist("../datasets/collegedataset_75.txt", create_using=nx.DiGraph()) gtest= nx.read_weighted_edgelist("../datasets/collegedataset_test.txt", create_using=nx.DiGraph()) df1 = pd.read_csv('../datasets/collegedataset_75.txt', names = ['v1','v2','timestamp'],sep = '\t',lineterminator='\n',header = None, dtype=str) df2 = pd.read_csv('../datasets/collegedataset_test.txt', names = ['v1','v2','timestamp'],sep = '\t',lineterminator='\n',header = None, dtype=str) node2vec = Node2Vec(filename='../datasets/collegedataset_75.txt', is_temporal=True, walk_length=8) # Use temp_folder for big graphs model = node2vec.fit(window=10, min_count=1, batch_words=4) # Any keywords acceptable by gensim.Word2Vec can be passed, `diemnsions` and `workers` are automatically passed (from the Node2Vec constructor) all_connections = defaultdict(list) for index, row in df1.iterrows(): all_connections[row["v1"]].append((row["v2"])) predicted = defaultdict(list) count = 0
'BIOGRID-ORGANISM-Caenorhabditis_elegans', 'BIOGRID-ORGANISM-Human_Immunodeficiency_Virus', 'BIOGRID-ORGANISM-Rattus_norvegicus-3.5.171.tab', 'Candida_albicans', 'coli1', 'Drosophila_melanogaster1', 'elegans', 'Escherichia_coli1', 'Hamster', 'Homo_sapiens2', 'Human_Herpesvirus', 'Human1', 'marina1', 'mouse1', 'MovieRate2', 'Mus_musculus2', 'Oryza1', 'Plasmodium_falciparum', 'Schizosaccharomyces1', 'USAir', 'Xenopus', 'Yang', 'yeast1', 'YeastS', 'Human33' ] ############################################################################### #从这里看开始替换脚本中的网络名称 print 'Network: Arabidopsis_thaliana2' G0 = nx.read_weighted_edgelist( '//home//chenyu//Sim//Arabidopsis_thaliana2//Arabidopsis_thaliana2.txt' ) #读取原图 print 'nx.info(G0)' print nx.info(G0) print 'average clustering coefficient:', nx.average_clustering(G0) nx.write_weighted_edgelist( G0, '//home//chenyu//Sim//Arabidopsis_thaliana2//Arabidopsis_thaliana2_00.txt') G0 = nx.read_weighted_edgelist( '//home//chenyu//Sim//Arabidopsis_thaliana2//Arabidopsis_thaliana2_00.txt' ) #重新读取原图 G1 = nx.empty_graph(G0.nodes()) #初始化第一轮的正测试集为空图 samplesize = int(0.1 * len(G0.edges)) #正负测试集总规模=samplesize*2 iteration = 10 print 'iteration=', iteration for i in range(iteration):
seed_mean, seed_var = fit_gaussian(seed_fb_ratios) nonseed_mean, nonseed_var = fit_gaussian(nonseed_fb_ratios) p_given_seed = norm.pdf(fb_ratios, loc=seed_mean, scale=np.sqrt(seed_var)) p_given_nonseed = norm.pdf(fb_ratios, loc=nonseed_mean, scale=np.sqrt(nonseed_var)) seed_factors = p_given_seed / p_given_nonseed return seed_factors args = parse_argument() if args.verbose: logger.setLevel(cleanlog.DEBUG) # Instantiate input network. if args.weighted: network = nx.read_weighted_edgelist(args.network) else: network = nx.read_edgelist(args.network) nodes = list(network.nodes()) num_nodes = len(nodes) # Normalized adjacency matrix for network propagation iteration. matrix = normalize(nx.to_numpy_matrix(network, dtype=np.float32), norm='l1', axis=0) # Prepare seeds for foreground procedure. seed_list = [l.strip() for l in open(args.seed).readlines()] node_set = set(nodes) seed_indices = [nodes.index(s) for s in seed_list if s in node_set]
def main(edgelist_name, walk_length, vector_length, no_epochs, model_name): # Create a graph print('Loading graph from edgelist...') fh = open(edgelist_name, 'r') graph = nx.read_weighted_edgelist(fh, delimiter=',', nodetype=str) print('Done') # Create an adjacency matrix print('Creating adjacency matrix...') A = nx.adjacency_matrix(graph) A_array = A.toarray() A_cumsum = np.cumsum(A_array, axis=1) matrix_lengths = A.shape[0] print('Done') print( 'Creating dictionaries for row probabilities and matrix identities...') row_total_probabilities = [A[x].sum() for x in range(0, matrix_lengths)] items = list(range(matrix_lengths)) probability_dictionary = {} for item, probability in zip(items, row_total_probabilities): probability_dictionary[item] = probability prod_store_dict = {} for item2, node in zip(items, graph.nodes()): prod_store_dict[item2] = node def transition(x): if probability_dictionary[x] == 0: return x else: y = np.random.uniform(high=probability_dictionary[x]) z = np.argwhere(A_cumsum[x] > y)[0][0] return z print('Done') #size is how many walks, the range is how many steps in each walk print( 'Generating random walks - may take a few minutes if long walks are chosen...' ) random_walks = list(range(matrix_lengths)) + list(range(matrix_lengths)) #change it so you do a walk for every node # rather than randomly choosing nodes to walk with row = random_walks for null in range(0, walk_length): next_node = np.asarray([transition(oof) for oof in row]) random_walks = np.vstack((random_walks, next_node)) row = random_walks[:][-1] walk_list = random_walks.T.tolist() #str_walk_list = [list(map(str, walk)) for walk in walk_list] str_walk_list = [[prod_store_dict[thing] for thing in walk] for walk in walk_list] print('Done') #running Word2Vec print('Running word2vec...') model = Word2Vec( size=vector_length, window=4, sg=1, hs=0, negative=10, # for negative sampling alpha=0.03, min_alpha=0.0007, seed=14) model.build_vocab(str_walk_list, progress_per=2) model.train(str_walk_list, total_examples=model.corpus_count, epochs=no_epochs, report_delay=1) print('Done') # Save the model print('Saving model...') model.save('{}'.format(model_name)) print('Done')
def restore_shortestpath(u,v,P): path = [] temp = v while temp != u: parent = P[temp] path.append((parent,temp)) temp = parent path.reverse() return path def restore_Steiner_tree(T,path_dict): result = set() for u,v in T.edges(): path = restore_shortestpath(u,v,path_dict) for i in range(len(path)): if path[i][0] < path[i][1]: result.add((path[i][0],path[i][1])) else: result.add((path[i][1],path[i][0])) return result def Steiner(G,R): Gr , path_dict = steiner_to_metric(G,R) return restore_Steiner_tree(Gr,path_dict) G = nx.read_weighted_edgelist('./dij.edgelist',create_using=nx.DiGraph(),nodetype=int) #S = nx.read_edgelist('./st.edgelist',nodetype=int) G , path = steiner_to_metric(G,[0,1,2]) print(path) #print(Steiner(G,[0,1,2]))
""" Deep Learning on Graphs - ALTEGRAD - Dec 2019 """ import networkx as nx import numpy as np from deepwalk import deepwalk from sklearn.decomposition import PCA from sklearn.manifold import TSNE import matplotlib.pyplot as plt import os os.chdir("/content/drive/My Drive/TP6/code/data/") # Loads the web graph G = nx.read_weighted_edgelist('web_sample.edgelist', delimiter=' ', create_using=nx.Graph()) print("Number of nodes:", G.number_of_nodes()) print("Number of edges:", G.number_of_edges()) ############## Task 3 # Extracts a set of random walks from the web graph and feeds them to the Skipgram model n_dim = 128 n_walks = 10 walk_length = 20 model = deepwalk(G, n_walks, walk_length, n_dim) # ############# Task 4 # Visualizes the representations of the 100 nodes that appear most frequently in the generated walks
import networkx as nx import matplotlib.pyplot as plt import numpy as np G = nx.read_weighted_edgelist('data/G1.txt') def show_weighted_graph(G): """Prints a weighted graph from a text file. A weighted graph shows the cost or weight between between points on the graph. Parameters ---------- G = A networkx graph. Returns ------- A plotted graph with weights between its nodes. """ pos = nx.planar_layout(G) nx.draw(G, pos) labels = nx.get_edge_attributes(G, 'weight') nx.draw_networkx_edge_labels(G, pos, edge_labels=labels) plt.show() def draw_subtree(G, T):
def read_for_SVD(filename, weighted=False): if weighted: G = nx.read_weighted_edgelist(filename) else: G = nx.read_edgelist(filename) return G
import networkx as nx #重み付きグラフのロード G=nx.read_weighted_edgelist('dij.edgelist',nodetype=int) def my_extract_min2(D,X): arg_min =-1 min_value=float('inf') for i in range(len(D)): if D[i] < min_value: if i in X: arg_min = i min_value = D[i] return arg_min def my_Prim(G,source): X=set(G.nodes) D=[float('inf')] * nx.number_of_nodes(G) D[source]=0 P=[0] * nx.number_of_nodes(G) A=[] while X: u=my_extract_min2(D,X) X.remove(u) if u != source: A.append((P[u],u)) for v in G.neighbors(u): if v in X: new_distance=G.edges[u,v]['weight'] if D[v] > new_distance: D[v]=new_distance
from algorithms.prims import prims_algorithm import networkx as nx #This runs my G1 graph# G = nx.read_weighted_edgelist('data\G1.txt', nodetype=int) T = prims_algorithm(G, 4, draw=True, show_prop=True) #This runs my G2 graph# G = nx.read_weighted_edgelist('data\G2.txt', nodetype=int) T = prims_algorithm(G, 1, draw=True, show_prop=True) #This runs my G3 graph# G = nx.read_weighted_edgelist('data\G3.txt', nodetype=int) T = prims_algorithm(G, 4, draw=True, show_prop=True)
import pandas as pd import numpy as np import csv from scipy.sparse import coo_matrix, csc_matrix, csr_matrix import networkx as nx ## Data format ## user id | item id | rating | timestamp ## user-item matrix data = pd.read_csv('ml-100k/u1.base', sep='\t', header=None, engine='c') data.columns = ['user', 'item', 'rating', 'timestamp'] #nu = data.user.nunique() #ni = data.item.nunique() ## To sparse matrix UI = coo_matrix((data.rating,(data.user-1,data.item-1))) # '-1' in order to cope with 0-indexing ## BE CAREFUL when merging with IDs later on ! ## Directly to graph format with open('ml-100k/u1.base') as f: G = nx.readwrite.edgelist.parse_edgelist(lines, comments='#', delimiter=None, create_using=None, nodetype=None, data=True) G=nx.read_weighted_edgelist('ml-100k/u1.base')
import networkx as nx import numpy as np import random data_path = './Data/InternetP2P/p2p-Gnutella' data_name = ['04', '05', '06', '08', '09', '24', '25', '30', '31'] save_dir = './Data/InternetP2P_cost/' costType = {0: 'degree', 1: 'random'} for k in range(2): for i in range(len(data_name)): data = data_path + data_name[i] + '.txt' g = nx.read_weighted_edgelist(data) if k == 0: ### degree weight degree = nx.degree(g) maxDegree = max(degree.values()) weights = {} for node in g.nodes(): weights[node] = degree[node] / maxDegree else: ### random weight weights = {} for node in g.nodes(): weights[node] = random.uniform(0, 1) nx.set_node_attributes(g, 'weight', weights) save_dir_g = '%s/p2p-Gnutella%s_%s.gml' % (save_dir, data_name[i], costType[k]) nx.write_gml(g, save_dir_g)
def import_edgelist(path, extension="edgelist", delimiter=None, nodetype=int, return_vertices=False): """ Function for reading a single or multiple edgelists. When importing multiple edgelists, the union of vertices from all graphs is computed so that each output graph have matched vertex set. The order of nodes are sorted by node values. Parameters ---------- path : str, Path object, or iterable If ``path`` is a directory, then the importing order will be sorted in alphabetical order. extension : str, optional If ``path`` is a directory, then the function will convert all files with matching extension. delimiter : str or None, default=None, optional Delimiter of edgelist. If None, the delimiter is whitespace. nodetype : int (default), float, str, Python type, optional Convert node data from strings to specified type. return_vertices : bool, default=False, optional Returns the union of all individual edgelists. Returns ------- out : list of array-like, or array-like, shape (n_vertices, n_vertices) If ``path`` is a directory, a list of arrays is returned. If ``path`` is a file, an array is returned. vertices : array-like, shape (n_vertices, ) If ``return_vertices``` is True, then returns an array of all vertices that were included in the output graphs. """ # p = Path(path) if not isinstance(path, (str, Path, Iterable)): msg = "path must be a string or Iterable, not {}".format(type(path)) raise TypeError(msg) # get a list of files to import if isinstance(path, (str, Path)): p = Path(path) if p.is_dir(): files = sorted(p.glob("*" + extension)) elif p.is_file(): files = [p] else: raise ValueError("No graphs founds to import.") else: # path is an iterable files = [Path(f) for f in path] if len(files) == 0: msg = "No files found with '{}' extension found.".format(extension) raise ValueError(msg) graphs = [ nx.read_weighted_edgelist(f, nodetype=nodetype, delimiter=delimiter) for f in files ] if all(len(G.nodes) == 0 for G in graphs): msg = ("All graphs have 0 vertices. Please double check if proper " + "'delimiter' is given.") warnings.warn(msg, UserWarning) # Compute union of all vertices vertices = np.sort(reduce(np.union1d, [G.nodes for G in graphs])) out = [ nx.to_numpy_array(G, nodelist=vertices, dtype=np.float) for G in graphs ] # only return adjacency matrix if input is only 1 graph if len(out) == 1: out = out[0] if return_vertices: return out, vertices else: return out
import networkx from operator import itemgetter import matplotlib.pyplot import pandas as pd import math # Read the data from amazon-books.csv into amazonBooks dataframe; amazonBooks = pd.read_csv('./amazon-books.csv', index_col=0) # Read the data from amazon-books-copurchase.adjlist; # assign it to copurchaseGraph weighted Graph; # node = ASIN, edge= copurchase, edge weight = category similarity fhr=open("amazon-books-copurchase.edgelist", 'rb') copurchaseGraph=networkx.read_weighted_edgelist(fhr) fhr.close() # Now let's assume a person is considering buying the following book; # what else can we recommend to them based on copurchase behavior # we've seen from other users? print ("Looking for Recommendations for Customer Purchasing this Book:") print ("--------------------------------------------------------------") purchasedAsin = '0805047905' # Let's first get some metadata associated with this book print ("ASIN = ", purchasedAsin) print ("Title = ", amazonBooks.loc[purchasedAsin,'Title']) print ("SalesRank = ", amazonBooks.loc[purchasedAsin,'SalesRank']) print ("TotalReviews = ", amazonBooks.loc[purchasedAsin,'TotalReviews']) print ("AvgRating = ", amazonBooks.loc[purchasedAsin,'AvgRating'])
from algorithims.prims import prims_algorithim import networkx as nx # Use lib to read the graph coordinates into G G = nx.read_weighted_edgelist('data/G1.txt', nodetype=int) # Pass G starting point and call the algorithim to start the graph. T = prims_algorithim(G, 3, draw=True, showG=True)
""" Julio Quintero 5/6/2020 Final Project Outline: MST Python Package CS 1311: Intro. To Computation with Python """ import networkx as nx from algorithms.prims import Prims # Variables saving data from files G1, G2, and G3 to then by passed to Prims G_one = nx.read_weighted_edgelist('data/G1.txt', nodetype=int) G_two = nx.read_weighted_edgelist('data/G2.txt', nodetype=int) G_three = nx.read_weighted_edgelist('data/G3.txt', nodetype=int) """ Prims Function that solves MTS. Arguemnts 1 is the variables declare above. Arguemnt two is which from which node the function will start. Argurmnt 3 is if a graph will be drawn. Arguemnt 4 is if info for the graph is to be shown. """ print('********************* Data 1 ***************************') T = Prims(G_one, 0, True, True) print('********************* Data 2 ***************************') T = Prims(G_two, 0, True, True) print('********************* Data 3 ***************************') T = Prims(G_three, 0, True, True)
# Ground Truth Tree construction: gt_graph, artificial_edge_list = createGraphFromTruth(ground_truth) if save_plots: # Save the Plot: gt_file_name = ground_truth.split("/")[-1].split(".")[0] plt.figure(figsize=(200, 200)) nx.draw(gt_graph, with_labels=True) plt.savefig(plot_dir + gt_file_name + ".png") gt_tree = nx.bfs_tree(gt_graph, source="root") gt_graph = nx.Graph(gt_tree) # Baseline Method Generated Tree construction: base_gen_graph = nx.read_weighted_edgelist(baseline_mst_file) base_gen_graph.add_edges_from(artificial_edge_list) if save_plots: # Save the Plot: base_gen_file_name = baseline_mst_file.split("/")[-1].split(".")[0] plt.figure(figsize=(200, 200)) nx.draw(base_gen_graph, with_labels=True) plt.savefig(plot_dir + base_gen_file_name + ".png") base_gen_tree = nx.bfs_tree(base_gen_graph, source="root") base_gen_graph = nx.Graph(base_gen_tree) # Comparison Method Generated Tree construction: comp_gen_graph = nx.read_weighted_edgelist(comparison_mst_file) comp_gen_graph.add_edges_from(artificial_edge_list)
import sys import networkx as nx import json if __name__ == '__main__': if sys.argv.__len__() < 2: print('The format is "python pageRank.py data.edgelist [{1:2}]"') else: filename = sys.argv[1] if filename.find('weight') == -1: graph = nx.read_edgelist(filename) else: graph = nx.read_weighted_edgelist(filename) # p = PageRank(graph, isDirected) # p.rank() # sorted_r = sorted(p.ranks.items(), key=operator.itemgetter(1), reverse=True) if sys.argv.__len__() == 3: personalization = sys.argv[2] vec = eval(personalization) pr = nx.pagerank(graph, personalization=vec) else: pr = nx.pagerank(graph) pagerank = sorted(pr.items(), key=lambda item: item[1], reverse=True) # pagerank = sorted(pr.items(), key=lambda item: '{:.3f}'.format(item[1]), reverse=True) # data = [tuple((d[0], '{:.6f}'.format(d[1]))) for d in pagerank] # output = dict(data) jsonStr = json.dumps(pagerank) print(jsonStr)
if i % 100 == 0 and i > 0: print(i) write_graph_to_file(ge, g) evolve_graph(g, prob) t, b, err = time_exe(save, g, prob) if b: num_recompute += 1 #timer=t timer += t timing.append(timer) errors.append(err) return timing, num_recompute, errors ge = Gen() g = nx.read_weighted_edgelist("base.graph") steps_number = 1000 res = {} x = list(xrange(steps_number)) res["c_without"], res["c_without_r"], res["c_without_err"] = run_evolution( ge, g, steps_number, 0, 0) print("a") g = nx.read_weighted_edgelist("base.graph") res["c_with_0.01"], res["c_with_0.01_r"], res[ "c_with_0.01_err"] = run_evolution(ge, g, steps_number, 0.0, 1) print("b") g = nx.read_weighted_edgelist("base.graph") res["c_with_0.1"], res["c_with_0.1_r"], res["c_with_0.1_err"] = run_evolution( ge, g, steps_number, 0.1, 1) print("c")
import networkx as nx import matplotlib.pyplot as plt import csv import operator roadPoints = [0, 1, 2, 4, 7, 10, 15] G = nx.Graph() file = open("Network.txt", "rb") G = nx.read_weighted_edgelist(file) file.close() # prim algorithm def stats(G): cityCount = 0 print(len(G.nodes)) print(len(G.edges)) edgeLengths = [0, 0, 0, 0, 0, 0, 0] for edge in G.edges: length = G.get_edge_data(edge[0], edge[1])['weight'] edgeLengths[int(length)] += 1 print(edgeLengths) def prim(G, startCity): # initialize the MST and the set X MST = list() cnctCities = list() count = 45
from utils import normalize_adjacency, sparse_to_torch_sparse, loss_function from models import GAE # Initialize device device = torch.device("cuda") if torch.cuda.is_available() else torch.device("cpu") # Hyperparameters epochs = 200 n_hidden_1 = 16 n_hidden_2 = 32 learning_rate = 0.01 dropout_rate = 0.1 # Loads the karate network G = nx.read_weighted_edgelist('../data/karate.edgelist', delimiter=' ', nodetype=int, create_using=nx.Graph()) print(G.number_of_nodes()) print(G.number_of_edges()) n = G.number_of_nodes() adj = nx.adjacency_matrix(G) # Obtains the adjacency matrix adj = normalize_adjacency(adj) # Normalizes the adjacency matrix features = np.random.randn(n, 10) # Generates node features # Transforms the numpy matrices/vectors to torch tensors features = torch.FloatTensor(features).to(device) adj = sparse_to_torch_sparse(adj).to(device).coalesce() # Creates the model and specifies the optimizer