def __init__(self, graph, sd): #We need a deepcopy if the self.graph gets modified. self.graph = graph.copy() np.random.seed() self.result_graph = nx.Graph() #Sets with/without monitors. self.monitor_set = set() self.monitor_free=nx.nodes(self.graph) #Save this list to speed computations. #Internal dictionaries self.next_highest = {} self.seen = Counter() #2-Colored Components self.blueseen = 0 self.redseen = 0 self.monitor_fancy = list() #Initialize all fake degrees to degree self.fake_degree=dict() #for node in self.monitor_free: for node in nx.nodes(self.graph): self.fake_degree[node]=self.graph.degree(node)
def occurenceCounter(charList, graphFile, bookNetworksPath): g = nx.read_gexf(graphFile) if not charList: # Get characters from overall.gexf graph overallGraphFile = bookNetworksPath + "overall.gexf" overall_g = nx.read_gexf(overallGraphFile) overallChars = nx.nodes(overall_g) # Sort dictionary by name (key of dictionary) sortedChars = sorted(overallChars) return sortedChars else: charList = [item for item in charList] for index, item in enumerate(charList): currentChar = None for node in nx.nodes(g): if node == item: occurrence = 1 charList[index] = (item, occurrence) currentChar = node # If current character is not present in the current chapter assign 0 influence. if not currentChar: occurrence = 0 charList[index] = (item, occurrence) return charList
def sync_label_propagation(): iter_num = 5 for node in nx.nodes(g): g.node[node]['prev'] = node g.node[node]['next'] = None for i in xrange(iter_num): for node in nx.nodes(g): label_count_dict = {} for neighbor in nx.neighbors(g, node): if g.node[neighbor]['prev'] not in label_count_dict: label_count_dict[g.node[neighbor]['prev']] = 0 label_count_dict[g.node[neighbor]['prev']] += 1 label_list_dict = reverse_dict(label_count_dict) tie_list = label_list_dict[max(list(label_list_dict))] # print label_count_dict # print tie_list g.node[node]['next'] = get_rand_element(tie_list) for node in nx.nodes(g): g.node[node]['prev'] = g.node[node]['next'] print_result(g, 'prev')
def get_region_colors(adjacency_matrix): """ Calculate color for each region in the graph. Input: adjacency_matrix - graph adjacency_matrix where [x][y] = 1 means that region x and region y share common border. Output: colors_dictionary - dictionary object where key is region number and value is color (witches - 1, vampires - 2, werewolves - 3, hybrids - 4) """ G = nx.Graph() colors_dictionary = {} if len(adjacency_matrix) == 1: colors_dictionary[0] = 1 return colors_dictionary G = create_color_none_graph(adjacency_matrix, G) G = set_node_colors(G) for node in nx.nodes(G): node_color = G.node[node]['color'] node_color += 1 G.node[node]['color'] = node_color for node in nx.nodes(G): colors_dictionary[node] = G.node[node]['color'] return colors_dictionary
def gimme(): l = Linkbase(LinkNode) l.new_node(u'There once was a man from Nantucket.') l.new_node(u'who put all on the line for a bucket.') l.link(0,1,0) print networkx.nodes(l.store) return l
def write_blast_graph_file(self): file_name = "{}/blast_graph.txt".format(self.blast_output_path) if len(nx.nodes(self.blast_graph)) > 0: with open(file_name, "wb") as f_handle: f_handle.write( 'Source' + ', Species,' + str( [value for value in self.blast_graph.node[nx.nodes(self.blast_graph)[0]].iterkeys()]).strip( '[]') + ',') f_handle.write( 'Target' + ', Species,' + str( [value for value in self.blast_graph.node[nx.nodes(self.blast_graph)[0]].iterkeys()]).strip( '[]') + ',') f_handle.write( 'evalue' + ',' + 'identpct' + ',' + 'mismatchno' + ',' + 'aln' + ',' + 'alnspn' + ',' + 'gapopenno' + ',' + 'bitscore' + '\n') for u, v, edata in self.blast_graph.edges(data=True): f_handle.write( str(u) + ',' + self.get_species_name(str(u)) + str( [value for value in self.blast_graph.node[u].itervalues()]).strip( '[]') + ',') f_handle.write( str(v) + ',' + self.get_species_name(str(v)) + str( [value for value in self.blast_graph.node[v].itervalues()]).strip( '[]') + ',') f_handle.write( str(edata['evalue']) + ',' + str(edata['identpct']) + ',' + str(edata['mismatchno']) + ',' + str(edata['aln']) + ',' + str(edata['alnspn']) + ',' + str(edata['gapopenno']) + ',' + str(edata['bitscore']) + '\n')
def make_graph(list, density): g = nx.DiGraph() for i in list: g.add_node(i) for node in nx.nodes(g): while g.out_degree(node)<density: rand = random.choice(nx.nodes(g)) if rand != node: g.add_edge(node, rand) return g
def execute(self, G, epsilon=0.0, weighted=False, min_community_size=1): """ Execute Demon algorithm :param G: the networkx graph on which perform Demon :param epsilon: the tolerance required in order to merge communities (default 0.25) :param weighted: Whether the graph is weighted or not :param min_community_size:min nodes needed to form a community """ ####### self.G = G self.epsilon = epsilon self.min_community_size = min_community_size for n in self.G.nodes(): G.node[n]['communities'] = [n] self.weighted = weighted ####### all_communities = {} total_nodes = len(nx.nodes(self.G)) actual = 0 for ego in nx.nodes(self.G): percentage = float(actual * 100)/total_nodes #if (int(percentage) % 1) == 0: # print 'Ego-network analyzed: %d/100 (communities identified: %d)' % ( # percentage, len(all_communities)) actual += 1 #ego_minus_ego ego_minus_ego = nx.ego_graph(self.G, ego, 1, False) community_to_nodes = self.__overlapping_label_propagation(ego_minus_ego, ego) #merging phase for c in community_to_nodes.keys(): if len(community_to_nodes[c]) > self.min_community_size: actual_community = community_to_nodes[c] all_communities = self.__merge_communities(all_communities, actual_community) #print communities #out_file_com = open("communities", "w") idc = 0 for c in all_communities.keys(): #out_file_com.write("%d\t%s\n" % (idc, ','.join([str(x) for x in sorted(c)]))) print("%d\t%s" % (idc, ','.join([str(x) for x in sorted(c)]))) idc += 1 #out_file_com.flush() #out_file_com.write("\n") #out_file_com.flush() #out_file_com.close() return
def get_graph(filename, with_root=False): DG = nx.DiGraph() f = open(filename, 'r') line = None edges = [] coordinates = [] terms = [] if with_root: root = None while line != 'EOF': line = f.readline().strip() toks = line.split(' ') if toks[0] == 'A': t = tuple(int(x) for x in toks[1:]) edges.append(t) if toks[0] == 'T': terms.append(int(toks[1])) if toks[0] == 'Root': if with_root: root = int(toks[1]) if toks[0] == 'DD': t = tuple(int(x) for x in toks[1:]) coordinates.append(t) for coord in coordinates: DG.add_node(coord[0], pos=(coord[1], coord[2])) terms.sort() DG.add_weighted_edges_from(edges) # print_graph(DG) # nx.draw(DG, node_size=50) # plt.show() # f.close() if with_root: return DG, terms, root else: print_graph(DG) max_len = 0 max_node = None for node in nx.nodes(DG): # print(node, tr_cl.out_edges(node)) descs = nx.descendants(DG, node) # desc_numb = len(descs) if len(set(terms) & set(descs)) == len(descs): # max_len = desc_numb max_node = node if max_len == len(nx.nodes(DG)): return DG, terms, max_node else: reachable = set(nx.descendants(DG, max_node)) | {max_node} unreachable = set(nx.nodes(DG)) - reachable for node in unreachable: DG.remove_node(node) terms = list(set(terms) & reachable) print('terms =', len(terms)) return DG, terms, max_node
def execute(self): """ Execute Demon algorithm """ sys.stdout.write('\n[Community Extraction]\n') sys.stdout.flush() for n in self.g.nodes(): self.g.node[n]['communities'] = [n] all_communities = {} total_nodes = len(nx.nodes(self.g)) old_p = 0 actual = 1 bar_length = 20 for ego in nx.nodes(self.g): ego_minus_ego = nx.ego_graph(self.g, ego, 1, False) community_to_nodes = self.__overlapping_label_propagation(ego_minus_ego, ego) # merging phase for c in community_to_nodes.keys(): if len(community_to_nodes[c]) > self.min_community_size: actual_community = community_to_nodes[c] all_communities = self.__merge_communities(all_communities, actual_community) # progress bar update percentage = int(float(actual * 100) / total_nodes) if percentage > old_p: hashes = '#' * int(round(percentage/5)) spaces = ' ' * (bar_length - len(hashes)) sys.stdout.write("\rExec: [{0}] {1}%".format(hashes + spaces, int(round(percentage)))) sys.stdout.flush() old_p = percentage actual += 1 if self.file_output: out_file_com = open("%s.txt" % self.file_output, "w") idc = 0 for c in all_communities.keys(): out_file_com.write("%d\t%s\n" % (idc, str(sorted(c)))) idc += 1 out_file_com.flush() out_file_com.close() return all_communities else: return all_communities
def execute(self, G, epsilon=0.25, weighted=False, min_community_size=3): """ Execute Demon algorithm :param G: the networkx graph on which perform Demon :param epsilon: the tolerance required in order to merge communities :param weighted: Whether the graph is weighted or not :param min_community_size:min nodes needed to form a community """ ####### self.G = G self.epsilon = epsilon self.min_community_size = min_community_size for n in self.G.nodes(): G.node[n]['communities'] = [n] self.weighted = weighted ####### print 'hello' all_communities = {} total_nodes = len(list(nx.nodes(self.G))) actual = 0 old_percentage = 0 for ego in nx.nodes(self.G): percentage = float(actual * 100) / total_nodes actual += 1 # ego_minus_ego ego_minus_ego = nx.ego_graph(self.G, ego, 1, False) community_to_nodes = self.__overlapping_label_propagation(ego_minus_ego, ego) # merging phase for c in community_to_nodes.keys(): if len(community_to_nodes[c]) > self.min_community_size: actual_community = community_to_nodes[c] all_communities = self.__merge_communities(all_communities, actual_community) # print communities out_file_com = open("communities.txt", "w") idc = 0 for c in all_communities.keys(): out_file_com.write("%d\t%s\n" % (idc, str(sorted(c)))) idc += 1 out_file_com.flush() out_file_com.close() return
def random_walk(g,start=None,seed=None): random.seed(seed) if start is None: start=random.choice(nx.nodes(g)) walk=[] unused_nodes=set(nx.nodes(g)) unused_nodes.remove(start) while len(unused_nodes) > 0: p=start start=random.choice([x for x in nx.all_neighbors(g,start)]) # follow random edge walk.append((p,start)) if start in unused_nodes: unused_nodes.remove(start) return walk
def findDominatingSet(self, graph): print "Algorithm: Modified greedy" modify_graph = copy.deepcopy(graph) dominating_set = [] nodes = nx.nodes(modify_graph) while not len(nodes) == 0: node = self.findMaxDegreeNode(modify_graph) dominating_set.append(node) modify_graph = self.removeNodeAndNeighbors(modify_graph, node) nodes = nx.nodes(modify_graph) return dominating_set
def printAttributes(comp,chromo,G): #for n in nx.nodes_iter(G): for n in nx.nodes(G): intensity=G.node[n]['intensity'] gene=G.node[n]['gene'] print ("\tcluster->%i,%s" % (comp,intensity)) return
def _distance_matrix(G): """ create a numpy 2-d array of distances between nodes Parameters ---------- G : NetworkX undirected graph Returns ------- matrix: ndarray numpy 2-d array of distances between nodes """ size = len(G) matrix = np.zeros((size, size)) nodes = nx.nodes(G) for i, node1 in enumerate(nodes): for j, node2 in enumerate(nodes): try: matrix[i, j] = nx.shortest_path_length(G, node1, node2) except: pass return matrix
def analyseConnectedComponents(graphs): conflicts = {} for cc in graphs: nodes = nx.nodes(cc) for node in nodes: if nx.degree(cc,node) > 1: edges = nx.edges(cc,node) for pair in itertools.combinations(edges,2): leftSet = cc[pair[0][0]][pair[0][1]]["species"] left = set() for elem in leftSet: left.add(elem) rightSet = cc[pair[1][0]][pair[1][1]]["species"] right = set() for elem in rightSet: right.add(elem) #identify conflicts if not left.isdisjoint(right): conflict = left.intersection(right) for elem in conflict: if elem in conflicts: conflicts[elem].append(pair) else: conflicts[elem] = [pair] #combineConflicts(conflicts) print " " for species in conflicts: print "Number of conflicts in "+species+" : "+str(len(conflicts[species])) return conflicts
def select_random_biome(self, adj_list=list()): if adj_list: the_graph = self.neighbors_subgraph(adj_list) else: the_graph = self return random.choice(nx.nodes(the_graph))
def extractSpanningTree(self, nodes): assert len(nodes) > 1 nodeIds = [self.nameToId[name] for name in nodes] paths = [dijkstra_path(self.nxDg.to_undirected(), source=nodeIds[0], target=x) for x in nodeIds[1:]] nodesToInclude = set() for path in paths: for node in path: nodesToInclude.add(node) cpy = self.nxDg.subgraph(nodesToInclude).copy() # Get rid of nodes that have only 1 children graphWasModified = True while graphWasModified: graphWasModified = False for node in nx.nodes(cpy): if cpy.out_degree(node) == 1 and cpy.in_degree(node) == 1: if node not in nodeIds: # This is a spurious node in the species tree, # we can and should remove childEdge = list(cpy.out_edges(node, data=True))[0] parentEdge = list(cpy.in_edges(node, data=True))[0] child = childEdge[1] childDist = childEdge[2]['weight'] parent = parentEdge[0] assert parent != node parentDist = parentEdge[2]['weight'] cpy.remove_node(node) cpy.add_edge(parent, child, weight = childDist + parentDist) graphWasModified = True break mcCpy = MultiCactusTree(cpy, 2) mcCpy.nameUnlabeledInternalNodes(prefix="thisPrefixShouldNeverAppear") mcCpy.computeSubtreeRoots() return mcCpy
def __init__ (self, graph): #class constructor #define required class variables such as the graph to work on, the redis connection and the nodes of the graph self.graph = graph self.redis = rd.StrictRedis(host='localhost', port=6379, db=0) self.nodes = nx.nodes(graph) # configuration variables are read from the config file and are also saved to class variables for easy access self.node_index_key = config.node_index_key self.metric_index_key = config.metric_index_key self.score_index_key = config.score_index_key self.node_neighbors_prefix = config.node_neighbors_prefix self.node_prefix = config.node_prefix self.metric_prefix = config.metric_prefix self.score_prefix = config.score_prefix self.statistics_prefix = config.statistics_prefix self.normalization_suffix = config.normalization_suffix self.base_metrics = config.base_metrics self.advanced_metrics = config.advanced_metrics self.normalization_methods = config.normalization_methods self.scores = config.scores self.advanced_scores = config.advanced_scores
def averageNeighDegree(G): sum=0 l=nx.nodes(G) for elt in l: sum+=G.degree(elt) avg=sum/(nx.number_of_nodes(G)) return avg
def network_properties(network : nx.DiGraph, in_degree_threshold : float = -1, pagerank_threshold : float = -1, damping : float = 0.85, spectral_offset : float = 0.5)\ -> (pd.DataFrame, sparse.spmatrix): conn = max(nx.connected_components(network.to_undirected()), key=len) conn = nx.subgraph(network, conn) pr = compute_pagerank(conn, damping=damping) names = nx.nodes(conn) indeg = [conn.in_degree(n) for n in names] odeg = [conn.out_degree(n) for n in names] description = [conn.node[n].get('description', n) for n in names] x, y, z, Adj, aff_names = node_coordinates(conn, nodelist=names, offset=spectral_offset) data = {'id': names, 'in_degree': indeg, 'out_degree': odeg, 'pagerank': pr, 'affinity_x': x, 'affinity_y': y, 'processing_depth': z, 'description': description} df = pd.DataFrame(data, index=names) df = df[df['pagerank'] > pagerank_threshold / len(names)] df = df[df['in_degree'] > in_degree_threshold] return df, Adj
def getObjPointsWrite(networkxGraph, pathTosave): """ Writes a networkx graph nodes of a skeleton as vertices to an obj file Parameters ---------- networkxGraph : Networkx graph graph to be converted to obj pathTosave : str write the obj file at pathTosave Returns ------- Writes a networkx graph nodes of a skeleton as vertices to an obj file at pathTosave Notes ----- Expects aspect ratio of array to be pre-adjusted """ objFile = open(pathTosave, "w") # open a obj file in the given path nodes = nx.nodes(networkxGraph) strsVertices = [] for index, vertex in enumerate(nodes): strsVertices.append("v " + " ".join(str(vertex[i]) for i in [1, 0, 2]) + "\n") # add strings of vertices to obj file objFile.writelines(strsVertices) # write strings to obj file objFile.close()
def calcAwkwardValues(socialGraph, hostID): people = nx.nodes(socialGraph) awkwardValues = {} for person in people: awk = nx.shortest_path_length(socialGraph,hostID,person) awkwardValues[person] = awk return awkwardValues
def getCoverageSets(self, graph): coverage_sets = [] nodes = nx.nodes(graph) for node in nodes: coverage_set = self.getCoverageSet(graph, node) coverage_sets.append(coverage_set) return coverage_sets
def search_motifs_with_n(self, n): graph = self.graph all_nodes = nx.nodes(graph) motif_counter = defaultdict(int) # 全組み合わせ探索 for nodes_pair in list(itertools.combinations(all_nodes, n)): degrees_in_pair = [] for node_self in nodes_pair: degree_in_pair = 0 for node_target in nodes_pair: if node_self == node_target: continue if node_target in nx.all_neighbors(graph, node_self): degree_in_pair += 1 degrees_in_pair.append(degree_in_pair) # 孤立ノードがあれば飛ばす if 0 in degrees_in_pair: continue # エッジが足りてなければ飛ばす if sum(degrees_in_pair) / 2 < n -1: continue # NOTE: 飛ばしきれてない. 本当は繋がってる判定が必要 score = sum([v * v for v in degrees_in_pair]) motif_counter[score] += 1 return motif_counter
def fast_approximate_solution_two(graph): """ Given a graph, construct a solution greedily using approximation methods. Performs bad. """ new_graph = nx.Graph() degrees = nx.degree_centrality(graph) largest = argmax(degrees) new_graph.add_node(largest) while new_graph.number_of_edges() < graph.number_of_nodes() - 1: degrees = {n: count_uncovered_degree(graph, new_graph, n) for n in nx.nodes(graph)} neighbor_list = [nx.neighbors(graph, n) for n in new_graph.nodes()] neighbors = set() for lst in neighbor_list: neighbors = neighbors.union(lst) if not neighbors: break next_largest = argmax_in(degrees, neighbors, exceptions = new_graph.nodes()) possible_edge_ends = [n for n in nx.neighbors(graph, next_largest) if graph.has_edge(n, next_largest) and n in new_graph.nodes()] new_graph.add_node(next_largest) edge_end = argmax_in(degrees, possible_edge_ends) new_graph.add_edge(edge_end, next_largest) return new_graph
def atom_graph(g): # determine if subgroup is attached h = nx.Graph() v = [] tris = {} edges = {} for node,data in g.nodes(data=True): g.node[node]['atoms'] = set([]) # triplet for node in nx.nodes(g): for each in g[node]: if each == node: continue neighbors = set(g[node]).intersection(set(g[each])) #print(node, each, neighbors, set(g[node]), g[each], set(g[each])) for neighbor in neighbors: t = tuple(sorted((node, each, neighbor))) if t not in list(tris.keys()): nr = len(h.nodes()) tris[t] = nr h.add_node(nr) g.node[node]['atoms'].add(nr) g.node[each]['atoms'].add(nr) g.node[neighbor]['atoms'].add(nr) #print(node, each, neighbor) for k in tris: if len(set(k).intersection(set(t))) == 2: h.add_edge(nr, tris[k]) edges[tuple(sorted(set(k).intersection(set(t))))] = nr #if nx.cycle_basis(h): # extra_nodes = set(h.nodes()).difference(set(np.concatenate(nx.cycle_basis(h)))) # for n in extra_nodes: # h.remove_node(n) return h
def dag(recipe_folder, config, packages="*", format='gml', hide_singletons=False): """ Export the DAG of packages to a graph format file for visualization """ dag, name2recipes = graph.build(utils.get_recipes(recipe_folder, "*"), config) if packages != "*": dag = graph.filter(dag, packages) if hide_singletons: for node in nx.nodes(dag): if dag.degree(node) == 0: dag.remove_node(node) if format == 'gml': nx.write_gml(dag, sys.stdout.buffer) elif format == 'dot': write_dot(dag, sys.stdout) elif format == 'txt': subdags = sorted(map(sorted, nx.connected_components(dag.to_undirected()))) subdags = sorted(subdags, key=len, reverse=True) singletons = [] for i, s in enumerate(subdags): if len(s) == 1: singletons += s continue print("# subdag {0}".format(i)) subdag = dag.subgraph(s) recipes = [ recipe for package in nx.topological_sort(subdag) for recipe in name2recipes[package]] print('\n'.join(recipes) + '\n') if not hide_singletons: print('# singletons') recipes = [recipe for package in singletons for recipe in name2recipes[package]] print('\n'.join(recipes) + '\n')
def calculate(g, voltage): edges_num = nx.number_of_edges(g) # sort nodes in edges edges = [edge if edge[0] < edge[1] else (edge[1], edge[0]) for edge in nx.edges(g)] a = np.zeros((edges_num, edges_num)) b = np.zeros((edges_num, 1)) i = 0 # first law for node in [node for node in nx.nodes(g) if node != 0]: for neighbor in nx.all_neighbors(g, node): edge = tuple(sorted((node, neighbor))) a[i][edges.index(edge)] = 1 if neighbor < node else -1 i += 1 # second law cycles = nx.cycle_basis(g, 0) for cycle in cycles: for j in range(0, len(cycle)): node = cycle[j] next_node = cycle[(j + 1) % len(cycle)] edge = tuple(sorted((node, next_node))) resistance = g[node][next_node]['weight'] a[i][edges.index(edge)] = resistance if node < next_node else -resistance if 0 in cycle: b[i] = voltage i += 1 # solve x = np.linalg.solve(a, b) for (x1, x2), res in zip(edges, x): g[x1][x2]['current'] = res[0]
def give_output_list(self, game): """ This returns a list of the selected nodes. The twin attack player finds the highest degree nodes, and for each, it selects two neighbors of that node and""" nodes = nx.nodes(game.network) nodes.sort(key=lambda x : nx.degree(game.network, x), reverse=True) selections = set() for node in nodes: adjacents = list(nx.all_neighbors(game.network, node)) for adj_node in adjacents[:2]: selections.add(adj_node) if len(selections) == game.num_seeds: break if len(selections) == game.num_seeds: break assert len(selections) == game.num_seeds return list(selections)
print(file) removeSelfLoops(G) outFile.write('\t' + "Using the regular sample graph:" + '\n') print("\tUsing regular graph") start = time() print("\t\tDegree") temp = averageDegree(G) allFeatures[fileIndex]["reg average degree"] = temp outFile.write('\t\t' + "Average Degree " + str(temp) + '\n') print('\t\t\t' + str(time() - start)) print("\t\tNodes") temp = len(nx.nodes(G)) allFeatures[fileIndex]["reg number nodes"] = temp outFile.write('\t\t' + "Number of Nodes: " + str(temp) + '\n') print('\t\t\t' + str(time() - start)) print("\t\tpagerank") temp = averagePageRank(G) allFeatures[fileIndex]["reg pagerank"] = temp outFile.write('\t\t' + "Page Rank: " + str(temp) + '\n') print('\t\t\t' + str(time() - start)) print("\t\t2node") temp = numberOf2NodeNetworks(G) allFeatures[fileIndex]["reg 2 node networks"] = temp outFile.write('\t\t' + "Number of 2 Node Sub-Networks: " + str(temp) + '\n')
print('Storing :', sFileName) print('################################') nx.write_gml(G, sFileName) sFileName = sFileName + '.gz' nx.write_gml(G, sFileName) ################################################################ # Find Lists of Objects ################################################################ TargetNodes = [ ReaderCode, 'Andre Vermeulen', 'Angus', 'Tigger', 'Chris Hillman' ] for j in range(len(TargetNodes)): TargetNodes[j] = TargetNodes[j].replace(' ', '-').lower() ################################################################ for TargetNode in TargetNodes: if TargetNode in nx.nodes(G): print('=============================') print('Path:', 'World', ' to ', G.node[TargetNode]['NodeName']) print('=============================') for nodecode in nx.shortest_path(G, source='world', target=TargetNode): print(G.node[nodecode]['NodeName']) print('=============================') else: print('=============================') print('No data - ', TargetNode, ' is missing!') print('=============================') ################################################################ print('=============================') print(' How do we turn Angus into Tigger?') print('=============================') for nodecode in nx.shortest_path(G, source='angus', target='tigger'):
def get_random_speaker(self): """ Returns a random speaker from the network. """ return random.choice(nx.nodes(self.network["graph"]))
def genNewGraph(graph_id, total_nodes, frac_primal, num_k): dir_name = "./GraphSAGE-master/graph_data/" + "graph" + str(graph_id) + "/" if not os.path.exists(os.path.dirname(dir_name)): os.makedirs(dir_name) graph_name = dir_name + "graph" + str(graph_id) # UG = nx.gnm_random_graph(total_nodes, edges_per_node) UG = gen_setcover_inst(total_nodes, frac_primal) # page_rank = nx.pagerank(UG) # div_rank = divrank(UG) degree_of_nodes = UG.degree() all_nodes = nx.nodes(UG) features = [] sum_degree = 0 for node_i in all_nodes: features.append([]) sum_degree = sum_degree + degree_of_nodes[node_i] for node_i in all_nodes: # features[node_i].append(page_rank[node_i]) # features[node_i].append(div_rank[node_i]) norm_value = degree_of_nodes[node_i] * 1.0 / sum_degree features[node_i].append(norm_value) validation_set = (0.99 * total_nodes) test_set = (0.99 * total_nodes) random_list = [i for i in range(total_nodes)] shuffle(random_list) for node in range(0, total_nodes): if node < validation_set: UG.node[random_list[node]]['val'] = False UG.node[random_list[node]]['test'] = False elif node < test_set: UG.node[random_list[node]]['val'] = True UG.node[random_list[node]]['test'] = False else: UG.node[random_list[node]]['val'] = False UG.node[random_list[node]]['test'] = True nx.set_edge_attributes(UG, 'test_removed', False) nx.set_edge_attributes(UG, 'train_removed', False) json_graph_name = graph_name + "-G.json" json_id_map_name = graph_name + "-id_map.json" feats_file_name = graph_name + "-feats.npy" np.save(feats_file_name, features) data = json_graph.node_link_data(UG) graphjson = json.dumps(data) f1 = open(json_graph_name, 'w') f1.write(graphjson) f1.close() id_map = {} for node in range(0, total_nodes): id_map[str(node)] = node iddata = json.dumps(id_map) f2 = open(json_id_map_name, 'w') f2.write(iddata) f2.close() nodes = [ n for n in UG.nodes() if not UG.node[n]["val"] and not UG.node[n]["test"] ] G = UG.subgraph(nodes) # pairs = run_random_walks(G, nodes) # # out_file = graph_name + "-walks.txt" # # with open(out_file, "w") as fp: # fp.write("\n".join([str(p[0]) + "\t" + str(p[1]) for p in pairs])) if False: class_map_file = graph_name + "-class_map.json" class_map = {} os.chdir("./greedy_baseline") graph_file_name = "." + json_graph_name command = "sh ./find_greedy.sh " + graph_file_name + " " + str(num_k) os.system(command) solution_file_name = graph_file_name + ".greedySol" # solution_file_name = "./greedy_baseline/solution_greedy.txt" solution_file = open(solution_file_name, "r") os.chdir("../") greedy_nodes = solution_file.readlines() temp_selected_nodes = greedy_nodes[0].strip().split(' ') # # os.chdir("./random_baseline") # # graph_file_name = "." + json_graph_name # # command = "sh ./find_random.sh " + graph_file_name + " " + str(num_k) # # os.system(command) # os.chdir("../") # # os.chdir("./top-k_baseline") # # graph_file_name = "." + json_graph_name # # command = "sh ./find_top-k.sh " + graph_file_name + " " + str(num_k) # # os.system(command) # os.chdir("../") for node in range(0, total_nodes): class_map[str(node)] = [float(temp_selected_nodes[node])] classdata = json.dumps(class_map) f2 = open(class_map_file, 'w') f2.write(classdata) f2.close()
MessageGraph.add_node(hashedMessage) messPos[hashedMessage] = ( block['height'] + random.uniform(-0.15, 0.15), SHARD_SPACING_CONSTANT * block['shard'] + VALIDATOR_SPACING_CONSTANT * block['validator'] + random.uniform(-0.15, 0.15)) # Positioning the different shards blockPos[blockID] = (block['height'], SHARD_SPACING_CONSTANT * block['shard'] + VALIDATOR_SPACING_CONSTANT * block['validator']) blockLabels[blockID] = "Shard: " + str( block['shard']) + " Height: " + str(block['height']) nonMessageNodesinMessageGraph = [] for mn in nx.nodes(MessageGraph): if mn not in messPos: nonMessageNodesinMessageGraph.append(mn) for node in nonMessageNodesinMessageGraph: if node not in blockPos: MessageGraph.remove_node(node) else: messPos[node] = blockPos[node] nx.draw_networkx_edges(ValidatorLineGraph, validatorLinePos, style='dashed') nx.draw_networkx_edges(BlocksGraph, blockPos, edge_color='g', width=3) nx.draw_networkx_nodes(MessageGraph, messPos,
def overlappingLPA(ego_minus_ego, ego, max_iteration=100,weighted=False): """ 频率最大的标签储存多个的重叠LPA,按顺序遍历 :param max_iteration: number of desired iteration for the label propagation :param ego_minus_ego: ego network minus its center :param ego: ego network center """ t = 0 ##迭代次数 old_node_to_coms = {} while t < max_iteration: t += 1 label_freq = {} ##<标签,频率> node_to_coms = {} ##<节点,标签> nodes = nx.nodes(ego_minus_ego) '节点遍历的顺序' random.shuffle(nodes) count = -len(nodes) '遍历每个节点,进行标签传播' for n in nodes: n_neighbors = nx.neighbors(ego_minus_ego, n) if count == 0: t += 1 # compute the frequency of the labels '统计标签频率' for nn in n_neighbors: '假如节点nn还没有标签,标签为自己' communities_nn = [nn] ##邻居标签 '假如节点nn在之前的迭代中已经出现过标签,采用之前的标签' if nn in old_node_to_coms: communities_nn = old_node_to_coms[nn] '统计邻居标签频率,分带权重和无权重两种' for nn_c in communities_nn: if nn_c in label_freq: v = label_freq.get(nn_c) # case of weighted graph if weighted: label_freq[nn_c] = v + ego_minus_ego.edge[nn][n]['weight'] else: label_freq[nn_c] = v + 1 else: # case of weighted graph if weighted: label_freq[nn_c] = ego_minus_ego.edge[nn][n]['weight'] else: label_freq[nn_c] = 1 # first run, random choosing of the communities among the neighbors labels if t == 1: '迭代的第一次,从邻居标签里面随机选择' if not len(n_neighbors) == 0: r_label = random.sample(label_freq.keys(), 1) ego_minus_ego.node[n]['communities'] = r_label old_node_to_coms[n] = r_label count += 1 continue # choose the majority else: '迭代的第n次,选择邻居里面标签最大的' labels = [] max_freq = -1 for l, c in label_freq.items(): if c > max_freq: max_freq = c labels = [l] elif c == max_freq: labels.append(l) node_to_coms[n] = labels ##第n个节点的标签是邻居中标签最大的 '还没收敛就继续' if not n in old_node_to_coms or not set(node_to_coms[n]) == set(old_node_to_coms[n]): old_node_to_coms[n] = node_to_coms[n] ego_minus_ego.node[n]['communities'] = labels t += 1 '利用ego重构社团' # build the communities reintroducing the ego community_to_nodes = {} for n in nx.nodes(ego_minus_ego): '假如没有邻居,标签等于自己' if len(nx.neighbors(ego_minus_ego, n)) == 0: ego_minus_ego.node[n]['communities'] = [n] '注意c_n是list,因此一个节点可以同时属于多个标签' c_n = ego_minus_ego.node[n]['communities'] for c in c_n: if c in community_to_nodes: com = community_to_nodes.get(c) com.append(n) else: nodes = [n, ego] community_to_nodes[c] = nodes return community_to_nodes
handleArgs() createPathIfNeeded(args.out_directory_stem) for i in xrange(args.start, args.end + 1): ###zjp add sampling stragegy re = open(args.out_directory_stem + 'network_v' + str(i) + '.dat', 'rb') G = nx.read_edgelist(re, nodetype=int) re.close() #G = nx.read_edgelist(path=readfile, delimiter=",", nodetype=int, create_using=nx.Graph()) start = 0 G_ = nx.convert_node_labels_to_integers(G, first_label=start) numNodes = len(nx.nodes(G_)) # percentages = [0.1, 0.3, 0.5, 0.7] sampling_conditions = [ 'induced_random_edge', 'induced_random_vertex', 'induced_weighted_random_vertex', 'kk_path', 'km_path', 'random_path', 'random_vertex', 'random_edge', 'random_walk', 'metropolis_subgraph', 'metropolized_random_walk', 'weighted_vertex' ] #sampling strategy #sampling_condition = str(sampling_conditions[10]) sampling_condition = args.sampling_condition if not sampling_condition in sampling_conditions: raise ValueError(
def graph_test(filename): print('') print('Getting graph..') DG, terms, root = get_graph(filename, with_root=True) print('Getting graph is finished') print("") terms = list(set(terms) - {root}) # DG, terms = get_graph('WRP4/wrp4-11.stp') print_graph(DG) v = nx.number_of_nodes(DG) e = nx.number_of_edges(DG) print("Number of vertices: ", v) print("Number of reachable vertices: ", len(nx.descendants(DG, root)) + 1) print("Number of edges: ", e) print('') print('apsp started') start_time = time.time() tr_cl = trans_clos(DG) elapsed_time = time.time() - start_time print('apsp finished in', elapsed_time) # print_graph(tr_cl) max_len = 0 max_node = None for node in nx.nodes(tr_cl): # print(node, tr_cl.out_edges(node)) if len(tr_cl.out_edges(node)) > max_len: max_len = len(tr_cl.out_edges(node)) max_node = node print("max node ", max_node) print("intersect", set(v for x, v in tr_cl.out_edges(max_node)) & set(terms)) i = 1 print('Alg6 with i = ', i, 'started') start_time = time.time() set_start_time(start_time) terms.sort() tree = alg6(tr_cl, i=2, k=len(terms), r=root, x=terms) elapsed_time = time.time() - start_time print('Elapsed time = ', elapsed_time) tot_weight = tree.size(weight='weight') print('Weight of MSTw = ', tot_weight) print_graph(tree) exit() prev = dict() for i in [1, 2]: # try: # if not (('alg3-' + str(i)) not in prev or prev[('alg3-' + str(i))]): # raise Exception('') # raise Exception() # print('alg3-' + str(i), 'started..') # start_time = time.time() # set_start_time(start_time) # tree = alg3(tr_cl, i=i, k=len(terms.copy()), r=root, x=terms.copy()) # elapsed_time = time.time() - start_time # tot_weight = tot_weight = tree.size(weight='weight') # print('alg3-' + str(i), 'finished in', elapsed_time, 'with res =', tot_weight) # print('') # save_time(v, e, 'alg3-' + str(i), elapsed_time, tot_weight) # prev['alg3-' + str(i)] = True # except: # save_time(v, e, 'alg3-' + str(i), '-', '-') # print('Alg took to long to compute') # prev['alg3-' + str(i)] = False # try: # if not (('alg4-' + str(i)) not in prev or prev[('alg3-' + str(i))]): # raise Exception('') # raise Exception() # print('alg4-' + str(i), 'started..') # start_time = time.time() # set_start_time(start_time) # tree = alg4(tr_cl, i=i, k=len(terms.copy()), r=root, x=terms.copy()) # elapsed_time = time.time() - start_time # tot_weight = tree.size(weight='weight') # print('alg4-' + str(i), 'finished in', elapsed_time, 'with res =', tot_weight) # print('') # save_time(v, e, 'alg4-' + str(i), elapsed_time, tot_weight) # prev['alg4-' + str(i)] = True # except: # save_time(v, e, 'alg4-' + str(i), '-', '-') # print('Alg took to long to compute') # prev['alg4-' + str(i)] = False # try: if not (('alg6-' + str(i)) not in prev or prev[('alg6-' + str(i))]): raise Exception('') print('alg6-' + str(i), 'started..') start_time = time.time() set_start_time(start_time) tree = alg6(tr_cl, i=i, k=len(terms.copy()), r=root, x=terms.copy()) elapsed_time = time.time() - start_time tot_weight = tree.size(weight='weight') print('alg6-' + str(i), 'finished in', elapsed_time, 'with res =', tot_weight) print('') save_time(v, e, 'alg6-' + str(i), elapsed_time, tot_weight) prev['alg6-' + str(i)] = True
def generate_ifaces(self): ifs = {} others = {} enclaves = {} routers = {} e_links = {} for node in nx.nodes(self.graph): if re.match("e[0-9]+", node): enclaves[node] = node else: if re.match("o[0-9]+", node): ifs[node] = ['if%s' % node] others[node] = ['if%s' % node] if __NX_VERSION__ > 1: nx.set_node_attributes(self.graph, enclaves, 'enclaves') else: nx.set_node_attributes(self.graph, 'enclaves', enclaves) elist = list(enclaves) elist.sort(key=lambda x: int(re.search('[0-9]+', x).group(0))) # find primary link if specified self.check_primaries() primaries = nx.get_edge_attributes(self.graph, 'primary') mh_counter = 50 for node in elist: neighbors = [x for x in self.graph.neighbors(node)] neighbors.sort(key=lambda neighbor: int( re.search('[0-9]+', neighbor).group(0))) for neighbor in neighbors: link = (node, neighbor) link_rev = (neighbor, node) if link in primaries or link_rev in primaries: to_add = "if%d" % int(re.search('[0-9]+', node).group(0)) else: to_add = "if%d" % (mh_counter + int(re.search('[0-9]+', node).group(0))) elink = (node, to_add, neighbor) if node not in ifs: ifs[node] = [to_add] e_links[node] = [elink] else: ifs[node].append(to_add) e_links[node].append(elink) if neighbor not in routers: routers[neighbor] = [to_add] else: routers[neighbor].append(to_add) if __NX_VERSION__ > 1: nx.set_node_attributes(self.graph, routers, 'in_routers') nx.set_node_attributes(self.graph, others, 'others') nx.set_node_attributes(self.graph, ifs, 'ifs') nx.set_node_attributes(self.graph, e_links, 'elinks') else: nx.set_node_attributes(self.graph, 'in_routers', routers) nx.set_node_attributes(self.graph, 'others', others) nx.set_node_attributes(self.graph, 'ifs', ifs) nx.set_node_attributes(self.graph, 'elinks', e_links)
def RWRW(G, p): if p > 1: n = p else: n = int(p * nx.number_of_nodes(G)) ccdic = {} ndic = {} cost = [0] def CC(G, v): if ccdic.has_key(v): return ccdic[v] else: n_list = Neighbor(G, v) degree = len(n_list) if degree <= 1: return 0 tri = 0 count = 0 for i in range(0, degree - 1): n2_list = Neighbor(G, n_list[i]) for j in range(i + 1, degree): count += 1 if n_list[j] in n2_list: tri += 1 ans = float(tri) / count ccdic[v] = ans return ans def Neighbor(G, v): if ndic.has_key(v): return ndic[v] else: lst = nx.neighbors(G, v) cost[0] += 1 ndic[v] = lst return lst while True: count = 0 cc = 0.0 sumA = 0.0 sumB = 0.0 start_id = random.randint(0, nx.number_of_nodes(G) - 1) s_node = nx.nodes(G)[start_id] now_node = s_node cc += CC(G, now_node) / (len(Neighbor(G, now_node))) sumB += 1.0 / len(Neighbor(G, now_node)) count += 1 while count < n * 100: neighbor_list = Neighbor(G, now_node) next_id = random.randint(0, len(neighbor_list) - 1) next_node = neighbor_list[next_id] now_node = next_node count += 1 degree = len(Neighbor(G, now_node)) cc += CC(G, now_node) / degree sumB += 1.0 / degree if count >= n or cost[0] >= n: return cc / sumB return cc / sumB
optimal_entropy = k_entropy_and_tree[0] optimal_codetree = k_entropy_and_tree[1] return optimal_entropy, optimal_codetree if __name__ == "__main__": numpy.random.seed(int(time.time())) n = numpy.random.randint(30, 35) p = numpy.random.uniform(0.3, 0.7) G = nx.fast_gnp_random_graph(n, p, int(time.time())) subG = nx.k_core(G, 1) # adjmatrix=readAdjMatrix("./data/matrix") # subG=nx.from_numpy_matrix(adjmatrix,False,None) print(nx.nodes(subG)) print(nx.edges(subG)) # initial_structure = ((),(),(),()) # codetree = nx.from_nested_tuple(initial_structure, sensible_relabeling=True) # print(nx.nodes(codetree)) # print(nx.edges(codetree)) en, tr = shannon_entropy(subG) print("Shannon entropy:", en) en, tree = structural_entropy(subG) print("en:", en) tree.show() print("Optimized structural entropy:") en, tree = optimized_structural_entropy(subG) print("en:", en)
def __overlapping_label_propagation(ego_minus_ego, ego, max_iteration=10): """ :@param max_iteration: number of desired iteration for the label propagation :@param ego_minus_ego: ego network minus its center :@param ego: ego network center """ t = 0 old_node_to_coms = {} while t < max_iteration: t += 1 node_to_coms = {} nodes = nx.nodes(ego_minus_ego) random.shuffle(nodes) count = -len(nodes) for n in nodes: label_freq = {} n_neighbors = nx.neighbors(ego_minus_ego, n) if len(n_neighbors) < 1: continue if count == 0: t += 1 # compute the frequency of the labels for nn in n_neighbors: communities_nn = [nn] if nn in old_node_to_coms: communities_nn = old_node_to_coms[nn] for nn_c in communities_nn: if nn_c in label_freq: v = label_freq.get(nn_c) label_freq[nn_c] = v + 1 else: label_freq[nn_c] = 1 # first run, random choosing of the communities among the neighbors labels if t == 1: if not len(n_neighbors) == 0: r_label = random.sample(label_freq.keys(), 1) ego_minus_ego.node[n]['communities'] = r_label old_node_to_coms[n] = r_label count += 1 continue # choosing the majority else: labels = [] max_freq = -1 for l, c in label_freq.items(): if c > max_freq: max_freq = c labels = [l] elif c == max_freq: labels.append(l) node_to_coms[n] = labels if n not in old_node_to_coms or not set( node_to_coms[n]) == set(old_node_to_coms[n]): old_node_to_coms[n] = node_to_coms[n] ego_minus_ego.node[n]['communities'] = labels t += 1 # build the communities reintroducing the ego community_to_nodes = {} for n in nx.nodes(ego_minus_ego): if len(nx.neighbors(ego_minus_ego, n)) == 0: ego_minus_ego.node[n]['communities'] = [n] c_n = ego_minus_ego.node[n]['communities'] for c in c_n: if c in community_to_nodes: com = community_to_nodes.get(c) com.append(n) else: nodes = [n, ego] community_to_nodes[c] = nodes return community_to_nodes
def test_nodes(self): assert_equal(self.G.nodes(), networkx.nodes(self.G)) assert_equal(self.DG.nodes(), networkx.nodes(self.DG))
def calcH0IndexValues(nxG, igG): result = [nxG.degree(v) for v in nx.nodes(nxG)] return result
def __str__(self): return "NodeId: " + self.nodeId + "Ego-net Graph" + str(nx.nodes(self.egoNetGraph)) + ", Degree: " + str(self.egoNetDegree) + " , Edges: " + str(self.nofEdges) + " centerality: " + str(self.deg_cent) + ", " + str(self.betw_cent) + ", " \ + str(self.clustering_coeff)
def build_recipes( recipe_folder, config, packages="*", mulled_test=True, testonly=False, force=False, docker_builder=None, label=None, disable_upload=False, check_channels=None, quick=False, ): """ Build one or many bioconda packages. Parameters ---------- recipe_folder : str Directory containing possibly many, and possibly nested, recipes. config : str or dict If string, path to config file; if dict then assume it's an already-parsed config file. packages : str Glob indicating which packages should be considered. Note that packages matching the glob will still be filtered out by any blacklists specified in the config. mulled_test : bool If True, then test the package in a minimal container. testonly : bool If True, only run test. force : bool If True, build the recipe even though it would otherwise be filtered out. docker_builder : docker_utils.RecipeBuilder instance If not None, then use this RecipeBuilder to build all recipes. label : str Optional label to use when uploading packages. Useful for testing and debugging. Default is to use the "main" label. disable_upload : bool If True, do not upload the package. Useful for testing. check_channels : list Channels to check to see if packages already exist in them. If None, then defaults to the highest-priority channel (that is, `config['channels'][0]`). If this list is empty, then do not check any channels. quick : bool Speed up recipe filtering by only checking those that are reasonably new. """ orig_config = config config = utils.load_config(config) env_matrix = utils.EnvMatrix(config['env_matrix']) blacklist = utils.get_blacklist(config['blacklists'], recipe_folder) if check_channels is None: if config['channels']: check_channels = [config['channels'][0]] else: check_channels = [] logger.info('blacklist: %s', ', '.join(sorted(blacklist))) if packages == "*": packages = ["*"] recipes = [] for package in packages: for recipe in utils.get_recipes(recipe_folder, package): if os.path.relpath(recipe, recipe_folder) in blacklist: logger.debug('blacklisted: %s', recipe) continue recipes.append(recipe) logger.debug(recipe) if not recipes: logger.info("Nothing to be done.") return logger.debug('recipes: %s', recipes) if quick: if not isinstance(orig_config, str): raise ValueError("Need a config filename (and not a dict) for " "quick filtering since we need to check that " "file in the master branch") unblacklisted = [ os.path.join(recipe_folder, i) for i in utils.newly_unblacklisted(orig_config, recipe_folder) ] logger.debug('Unblacklisted: %s', unblacklisted) changed = [ os.path.join(recipe_folder, i) for i in utils.changed_since_master(recipe_folder) ] logger.debug('Changed: %s', changed) recipes = set(unblacklisted + changed).intersection(recipes) logger.debug('Quick-filtered recipes: %s', recipes) logger.info('Filtering recipes') recipe_targets = dict( utils.filter_recipes(recipes, env_matrix, check_channels, force=force)) recipes = list(recipe_targets.keys()) dag, name2recipes = utils.get_dag(recipes, blacklist=blacklist) recipe2name = {} for k, v in name2recipes.items(): for i in v: recipe2name[i] = k if not dag: logger.info("Nothing to be done.") return True else: logger.info("Building and testing %s recipes in total", len(dag)) logger.info("Recipes to build: \n%s", "\n".join(dag.nodes())) subdags_n = int(os.environ.get("SUBDAGS", 1)) subdag_i = int(os.environ.get("SUBDAG", 0)) if subdag_i >= subdags_n: raise ValueError("SUBDAG=%s (zero-based) but only SUBDAGS=%s " "subdags are available") # Get connected subdags and sort by nodes if testonly: # use each node as a subdag (they are grouped into equal sizes below) subdags = sorted([[n] for n in nx.nodes(dag)]) else: # take connected components as subdags subdags = sorted( map(sorted, nx.connected_components(dag.to_undirected()))) # chunk subdags such that we have at most subdags_n many if subdags_n < len(subdags): chunks = [[n for subdag in subdags[i::subdags_n] for n in subdag] for i in range(subdags_n)] else: chunks = subdags if subdag_i >= len(chunks): logger.info("Nothing to be done.") return True # merge subdags of the selected chunk subdag = dag.subgraph(chunks[subdag_i]) # ensure that packages which need a build are built in the right order recipes = [ recipe for package in nx.topological_sort(subdag) for recipe in name2recipes[package] ] logger.info("Building and testing subdag %s of %s (%s recipes)", subdag_i, subdags_n, len(recipes)) failed = [] built_recipes = [] skipped_recipes = [] all_success = True skip_dependent = defaultdict(list) for recipe in recipes: recipe_success = True name = recipe2name[recipe] if name in skip_dependent: logger.info( 'BIOCONDA BUILD SKIP: ' 'skipping %s because it depends on %s ' 'which had a failed build.', recipe, skip_dependent[name]) skipped_recipes.append(recipe) continue for target in recipe_targets[recipe]: target_success = build(recipe=recipe, recipe_folder=recipe_folder, env=target.env, testonly=testonly, mulled_test=mulled_test, force=force, channels=config['channels'], docker_builder=docker_builder) all_success &= target_success recipe_success &= target_success if not target_success: failed.append((recipe, target)) for n in nx.algorithms.descendants(subdag, name): skip_dependent[n].append(recipe) if recipe_success: built_recipes.append(recipe) if len(failed) > 0: failed_recipes = list(set(i[0] for i in failed)) logger.error( 'BIOCONDA BUILD SUMMARY: of %s recipes, ' '%s failed and %s were skipped. ' 'Details of recipes and environments follow.', len(recipes), len(failed_recipes), len(skipped_recipes)) if len(built_recipes) > 0: logger.error( 'BIOCONDA BUILD SUMMARY: while the entire build failed, ' 'the following recipes were built successfully:\n%s', '\n'.join(built_recipes)) for recipe, target in failed: logger.error( 'BIOCONDA BUILD SUMMARY: FAILED recipe %s, environment %s', str(target), target.envstring()) for name, dep in skip_dependent.items(): logger.error( 'BIOCONDA BUILD SUMMARY: SKIPPED recipe %s ' 'due to failed dependencies %s', name, dep) return False logger.info("BIOCONDA BUILD SUMMARY: successfully built %s of %s recipes", len(built_recipes), len(recipes)) if not testonly and not disable_upload: # upload builds if (os.environ.get("TRAVIS_BRANCH") == "master" and os.environ.get("TRAVIS_PULL_REQUEST") == "false"): for recipe in recipes: for target in recipe_targets[recipe]: all_success &= upload.upload(target.pkg, label) return all_success
# nx.draw_networkx(G, with_labels=True) # plt.draw() # plt.show() ######### calculate the function frequency or phi ######### # total proteins in the network totps = nx.number_of_nodes(G) # total proteins for the given function # funps = len(genelist) # you are assuming all genes matches the ones in the network; need to check one by one # customized gene function list customfunctions = ['uberon_0002103'] #customfunctions = ['uberon_0005724'] genes_in_network = nx.nodes(G) print 'genes in the network:', genes_in_network chvals = [] # gene counter for runtime purposes genecounter = 0 genes_not_found = [] for gene in genes_in_network: print 'gene is:', gene genecounter += 1 print genecounter #dictionaries to store total and function counts, chi square value total = {} ccount = {}
def MHRW(G, p): if p <= 1: n = int(p * nx.number_of_nodes(G)) else: n = p d = {} ccdic = {} ndic = {} cost = [0] cc = 0.0 sampled_node = [] def Q(G, v): if d.has_key(v): return d[v] else: ans = float(len(Neighbor(G, v))) d[v] = ans return ans def CC(G, v): if ccdic.has_key(v): return ccdic[v] else: n_list = Neighbor(G, v) degree = len(n_list) if degree <= 1: return 0 tri = 0 count2 = 0 for i in range(0, degree - 1): n2_list = Neighbor(G, n_list[i]) for j in range(i + 1, degree): count2 += 1 if n_list[j] in n2_list: tri += 1 ans = float(tri) / count2 ccdic[v] = ans return ans def Neighbor(G, v): if ndic.has_key(v): return ndic[v] else: lst = nx.neighbors(G, v) #cost[0] += 1 ndic[v] = lst return lst # a non-zero degree seed is selected at random while True: count = 0 tmp = 0 while tmp <= 0: start_id = random.randint(0, nx.number_of_nodes(G) - 1) s_node = nx.nodes(G)[start_id] tmp = len(G.neighbors(s_node)) now_node = s_node sampled_node.append(now_node) cc += CC(G, now_node) count += 1 while count < 100 * n: neighber_list = Neighbor(G, now_node) next_id = random.randint(0, len(neighber_list) - 1) next_node = neighber_list[next_id] p = random.random() value = Q(G, now_node) / Q(G, next_node) if p <= value: now_node = next_node cc += CC(G, now_node) sampled_node.append(now_node) count += 1 else: count += 1 cc += CC(G, now_node) sampled_node.append(now_node) if (cost[0] >= n or count >= n): return cc / count return cc / count
# graph = graph_imm sol = imm_sol os.system("pwd") solution_file = open(sol, "r") solution_file.readline() optimal_nodes = solution_file.read().split() optimal_nodes = [int(x.replace('\n', '')) for x in optimal_nodes] # print(optimal_nodes) G_data = json.load(open(graph + "-G.json")) G = json_graph.node_link_graph(G_data) # print("loaded") degree_of_nodes = G.degree() all_nodes = nx.nodes(G) sorted_ids_deg_wt = sorted(range(len(degree_of_nodes)), key=lambda k: degree_of_nodes[k], reverse=True) # for index, ids in enumerate(sorted_ids_deg_wt[0:500]): # print("debug" ,index, ids , degree_of_nodes[ids], degree_of_nodes[ids], "\n") min_score = 10000000 max_rankn = -1000 max_rank_id = -10000 ranks_list = [] for sol_id in optimal_nodes: # print("rank of sol", sol_id,"is ", sorted_ids_deg_wt.index(sol_id), "with out_weight ", degree_of_nodes[sol_id],
def FS(G, p): m = 1000 check = False while not check: count = 0 tricount = 0 tridic = {} degreedic = {} G1 = nx.Graph() n = int(p * nx.number_of_nodes(G)) start_id = random.randint(0, nx.number_of_nodes(G) - 1) s_node = nx.nodes(G)[start_id] G1.add_node(s_node) now_node = s_node tridic[now_node] = 0 degreedic[now_node] = 0 while True: neighbor_list = G.neighbors(now_node) next_id = random.randint(0, len(neighbor_list) - 1) next_node = neighbor_list[next_id] trinodelist = [] is_new_node = next_node not in G1.nodes() if is_new_node: G1.add_node(next_node) tridic[next_node] = 0 degreedic[next_node] = 0 count += 1 if is_new_node: for node in G.neighbors(next_node): if node in G1.nodes(): G1.add_edge(next_node, node) if (degreedic.has_key(next_node)): degreedic[next_node] = degreedic[next_node] + 1 else: degreedic[next_node] = 1 degreedic[node] = degreedic[node] + 1 trinodelist.append(node) if len(trinodelist) >= 2 and is_new_node: for i in range(0, len(trinodelist) - 1): for j in range(i + 1, len(trinodelist)): if trinodelist[j] in G1.neighbors(trinodelist[i]): tricount += 1 if tridic.has_key(next_node): tridic[next_node] = tridic[next_node] + 1 else: tridic[next_node] = 1 if tridic.has_key(trinodelist[j]): tridic[trinodelist[j]] = tridic[ trinodelist[j]] + 1 else: tridic[trinodelist[j]] = 1 if tridic.has_key(trinodelist[i]): tridic[trinodelist[i]] = tridic[ trinodelist[i]] + 1 else: tridic[trinodelist[i]] = 1 if random.random() < 0.075: next_id = random.randint(0, nx.number_of_nodes(G1) - 1) next_node = G1.nodes()[next_id] now_node = next_node if is_new_node: now_node = next_node if nx.number_of_nodes(G1) >= n: check = True break if count > 50 * n: # check = True break
def reset_centrality(self): """reset the centrality for every node.""" centrality = {u: 0 for u in nx.nodes(self.subgraph)} nx.set_node_attributes(self.subgraph, 'centrality', centrality)
return sum([0.0 + i * y3[i] for i in range(1, len(y3))]) / sum(y3) df = pd.DataFrame(columns=[ 'instance', 'degree', 'separation', 'Diameter', 'Components', 'Density', 'Average Degree' ]) for instance in instances: print instance if str.rstrip(instance) != 'vocalodon.net': followings = nx.read_gml('C:/Users/esbra/OneDrive/Mastodon/' + str.rstrip(instance) + '/following/following.gml') if len(followings.nodes()) > 0: gdegree = degree_separate(followings) ndegree = len(nx.nodes(followings)) followings = nx.read_gml('C:/Users/esbra/OneDrive/Mastodon/' + str.rstrip(instance) + '/following/following.gml') gdiameter = nx.diameter( max(nx.strongly_connected_component_subgraphs(followings), key=len)) gsubcomps = sum( 1 for _ in nx.weakly_connected_component_subgraphs(followings)) gdensity = nx.density(followings) goutdegree = np.mean(followings.out_degree().values()) df.loc[i] = [ str.rstrip(instance), ndegree, gdegree, gdiameter, gsubcomps, gdensity, goutdegree ] print i
# -------------- if show_graphs: #pos = nx.spring_layout(G, k=0.45, iterations=20) #Position test 6/4 old one if GraphGridOn: pos = nx.get_node_attributes(G, 'pos') else: pos = nx.spring_layout(G, k=0.45, iterations=20) #pos = nx.spring_layout(G) print("CxG^CNG Graph has Nodes=%d, Edges=%d" % (nx.number_of_nodes(G), nx.number_of_edges(G))) nx.draw(G, pos) nx.draw_networkx_labels(G, pos, font_size=5) plt.show() for v in nx.nodes(G): print('Node:%s Degree:%d' % (v, nx.degree(G, v))) print("UniqueCxGs=", UniqueCxGs) print("CxG^CNG graph has %d connected sets" % nx.number_connected_components(G)) # -------------- # Connected Set and Dictionary # -------------- Gcliques = nx.graph_clique_number(G, cliques=None) print("All Graph cliques ", Gcliques) connectSets = [] for cs in nx.connected_components(G):
def Estimate_plot(G, p, flag): y = [] Kairyou = flag sample_node = [] sumA = 0.0 sumB = 0.0 sumC = 0.0 sumD = 0.0 G1 = nx.Graph() n = (int)(p * nx.number_of_nodes(G)) start_id = random.randint(0, nx.number_of_nodes(G) - 1) s_node = nx.nodes(G)[start_id] sample_node.append(s_node) now_node = s_node count = 0 G1.add_node(s_node) for i in range(0, n): while True: neighbor_list = G.neighbors(now_node) next_id = random.randint(0, len(neighbor_list) - 1) next_node = neighbor_list[next_id] if Kairyou: if i >= 1: if not (sample_node[i - 1] == next_node): break elif len(G.neighbors(now_node)) <= 1: break else: break else: break G1.add_edge(now_node, next_node) sample_node.append(next_node) degree = len(G.neighbors(now_node)) sumB += 1.0 / (degree) sumD += degree - 1 if i >= 2: count += 1 if sample_node[i - 2] in G.neighbors(sample_node[i]): degree = (len(G.neighbors(sample_node[i - 1]))) if Kairyou: sumA += 1.0 / (degree) else: sumA += 1.0 / (degree - 1) sumC += degree if count >= 1: tmp = (sumA * (i + 1)) / (sumB * count) if tmp > 1: tmp = 0 y.append(tmp) else: y.append(0) now_node = next_node sumA = sumA / count sumB = sumB / n sumC = sumC / count sumD = sumD / n # print(sumA/sumB) # print(sumC/sumD) # print(str(nx.number_of_nodes(G1))) return y
def RWall3(G, p): check = False while not check: count = 0 # tricount = 0 tridic = {} degreedic = {} sampled = [] neighbordic = {} # G1 = nx.Graph() n = int(p * nx.number_of_nodes(G)) start_id = random.randint(0, nx.number_of_nodes(G) - 1) s_node = nx.nodes(G)[start_id] # G1.add_node(s_node) neighbordic[s_node] = [] sampled.append(s_node) now_node = s_node tridic[now_node] = 0 degreedic[now_node] = 0 while True: neighbor_list = G.neighbors(now_node) next_id = random.randint(0, len(neighbor_list) - 1) next_node = neighbor_list[next_id] trinodelist = [] is_new_node = next_node not in sampled if is_new_node: sampled.append(next_node) neighbordic[next_node] = [] # G1.add_node(next_node) tridic[next_node] = 0 degreedic[next_node] = 0 count += 1 if is_new_node: for node in G.neighbors(next_node): if node in sampled: list1 = neighbordic[node] list1.append(next_node) neighbordic[node] = list1 list2 = neighbordic[next_node] list2.append(node) neighbordic[next_node] = list2 #G1.add_edge(next_node,node) if (degreedic.has_key(next_node)): degreedic[next_node] = degreedic[next_node] + 1 else: degreedic[next_node] = 1 degreedic[node] = degreedic[node] + 1 trinodelist.append(node) if len(trinodelist) >= 2 and is_new_node: for i in range(0, len(trinodelist) - 1): for j in range(i + 1, len(trinodelist)): if trinodelist[j] in neighbordic[trinodelist[i]]: # tricount += 1 if tridic.has_key(next_node): tridic[next_node] = tridic[next_node] + 1 else: tridic[next_node] = 1 if tridic.has_key(trinodelist[j]): tridic[trinodelist[j]] = tridic[ trinodelist[j]] + 1 else: tridic[trinodelist[j]] = 1 if tridic.has_key(trinodelist[i]): tridic[trinodelist[i]] = tridic[ trinodelist[i]] + 1 else: tridic[trinodelist[i]] = 1 if is_new_node: now_node = next_node if random.random() < 0.075: next_id2 = random.randint(0, len(sampled) - 1) next_node2 = sampled[next_id2] now_node = next_node2 # next_node # ''' if len(sampled) >= n: check = True break ''' if count > n: # print (len(sampled)) check = True break # print("triangleの数"+str(tricount)) # '''' tri = list(nx.triangles(G1).values()) sum1 = 0 for num in tri: sum1 += int(num) ''' # print(graph.myGCC(G1,tricount)) sumcluster = 0 for node in sampled: if degreedic.has_key(node): degree = degreedic[node] if degree >= 2 and tridic.has_key(node): sumcluster += 2.0 * tridic[node] / (degree * (degree - 1)) ans = sumcluster / len(sampled) # print (str(ans)) av_degree = 0.0 # for degree in degreedic.values(): # av_degree += degree # av_degree = av_degree/(len(degreedic.values())) # print(str(av_degree)) return ans
def BAS(G, p): N = 3 G1 = nx.Graph() #サンプル後のグラフ e_list = [] check2 = False n = int(p * nx.number_of_nodes(G)) if N == 2: while True: start_id = random.randint(0, nx.number_of_nodes(G) - 1) s_node = nx.nodes(G)[start_id] if len(G.neighbors(s_node)) > 0: neighber_list = G.neighbors(s_node) second_id = random.randint(0, len(neighber_list) - 1) second_node = neighber_list[second_id] G1.add_edge(s_node, second_node) break elif N == 3: check = False while True: start_id = random.randint(0, nx.number_of_nodes(G) - 1) s_node = nx.nodes(G)[start_id] if len(G.neighbors(s_node)) >= 2: check2 = False neighber_list = G.neighbors(s_node) for second_id in range(0, len(neighber_list) - 1): second_node = neighber_list[second_id] for third_id in range(second_id + 1, len(neighber_list)): third_node = neighber_list[third_id] t0 = (second_node, third_node) t1 = (third_node, second_node) if t0 in G.edges() or t1 in G.edges(): check = True check2 = True G1.add_edge(s_node, second_node) G1.add_edge(second_node, third_node) G1.add_edge(third_node, s_node) break if check2: break if check2: break else: N = 1 start_id = random.randint(0, nx.number_of_nodes(G) - 1) s_node = nx.nodes(G)[start_id] G1.add_node(s_node) #ここまでがクリークの発見 print("clique is OK") process = Blist() #サンプルされる候補のリスト(重複を含む) #まず最初のクリークの隣接ノードを候補に加える for node in G1.nodes(): for neighbor in G.neighbors(node): if neighbor not in G1.nodes(): process.insert(0, process.size - 1, int(neighbor)) #サンプルノード数がnに達するまでサンプリングを繰り返す while nx.number_of_nodes(G1) < n: next_node = str(process.select()) #候補からノードを選ぶ stack = [] G1nodes = G1.nodes() #選ばれたノードとサンプル済のノードをつなぐ for node in G.neighbors(next_node): if node in G1nodes: G1.add_edge(node, next_node) count = 3 ### # while len(stack) > 0: # num2 = random.randint(0,len(stack)-1) # node = stack.pop(num2) # G1.add_edge(node,next_node) # count -= 0 # if count == 0: # break ### #サンプルされたノードを候補リストから外す process.remove(0, process.size - 1, next_node) #候補リストを更新する G1nodes = G1.nodes() for node in G.neighbors(next_node): if node not in G1nodes: process.insert(0, process.size - 1, int(node)) return G1
def convert(path, output_path, cut_to_n_nodes, max_edges, max_edges_2, voltage): # read edge list from file g = nx.read_weighted_edgelist(path, nodetype=int) # generate some random resistance for (x, y) in nx.edges(g): g[x][y]['weight'] = random.randint(1, 10) # let g be the biggest consistent subgraph (in the best case, # the whole graph is consistent) # g = max(nx.connected_component_subgraphs(g), key=len) # cycles = nx.cycle_basis(g) # nodes = [x for cycle in cycles for x in cycle] # g.remove_nodes_from([x for x in nx.nodes(g) if x not in nodes]) x1, x2 = nx.nodes(g)[:2] nodes_set = {x1, x2} nodes_set_left = {x1, x2} i = 2 while i < cut_to_n_nodes: for node in nx.nodes(g)[2:]: if node in nodes_set: continue intersect = set(nx.neighbors(g, node)).intersection(nodes_set_left) if len(intersect) >= 2: i += 1 while len(intersect) > 2: g.remove_edge(node, intersect.pop()) nodes_set.add(node) print(node) if len(intersect) < max_edges: nodes_set_left.add(node) for neighbour in intersect: if len( set(nx.neighbors(g, neighbour)).intersection( nodes_set)) >= max_edges: nodes_set_left.remove(neighbour) for neighbour2 in nx.neighbors(g, neighbour): if neighbour2 not in nodes_set: g.remove_edge(neighbour, neighbour2) if i == cut_to_n_nodes: break unused = [node for node in nx.nodes(g) if node not in nodes_set] g.remove_nodes_from(unused) # remove excess edges for node in nx.nodes(g): neighbours = tuple(nx.all_neighbors(g, node)) overmax = len(neighbours) - max_edges_2 if overmax > 0: i = 0 for neighbour in neighbours: if len(tuple(nx.all_neighbors(g, neighbour))) == 2: continue backup = g[node][neighbour] g.remove_edge(node, neighbour) if not nx.has_path(g, node, neighbour): g.add_edge(node, neighbour, backup) else: i += 1 if i == overmax: break # save to file lines = [x + "\n" for x in nx.generate_edgelist(g, data=['weight'])] o = open(output_path, "w") # x1, x2 = nx.nodes(g)[:2] o.write(" ".join((str(x1), str(x2), str(voltage))) + "\n") o.writelines(lines)
def RWall(G, p): check = False while not check: count = 0 tricount = 0 tridic = {} degreedic = {} G1 = nx.Graph() n = int(p * nx.number_of_nodes(G)) start_id = random.randint(0, nx.number_of_nodes(G) - 1) s_node = nx.nodes(G)[start_id] G1.add_node(s_node) now_node = s_node tridic[now_node] = 0 degreedic[now_node] = 0 while True: neighbor_list = G.neighbors(now_node) next_id = random.randint(0, len(neighbor_list) - 1) next_node = neighbor_list[next_id] trinodelist = [] is_new_node = next_node not in G1.nodes() if is_new_node: G1.add_node(next_node) tridic[next_node] = 0 degreedic[next_node] = 0 count += 1 if is_new_node: for node in G.neighbors(next_node): if node in G1.nodes(): G1.add_edge(next_node, node) if (degreedic.has_key(next_node)): degreedic[next_node] = degreedic[next_node] + 1 else: degreedic[next_node] = 1 degreedic[node] = degreedic[node] + 1 trinodelist.append(node) if len(trinodelist) >= 2 and is_new_node: for i in range(0, len(trinodelist) - 1): for j in range(i + 1, len(trinodelist)): if trinodelist[j] in G1.neighbors(trinodelist[i]): tricount += 1 if tridic.has_key(next_node): tridic[next_node] = tridic[next_node] + 1 else: tridic[next_node] = 1 if tridic.has_key(trinodelist[j]): tridic[trinodelist[j]] = tridic[ trinodelist[j]] + 1 else: tridic[trinodelist[j]] = 1 if tridic.has_key(trinodelist[i]): tridic[trinodelist[i]] = tridic[ trinodelist[i]] + 1 else: tridic[trinodelist[i]] = 1 if random.random() < 0.075: next_id = random.randint(0, nx.number_of_nodes(G1) - 1) next_node = G1.nodes()[next_id] now_node = next_node if is_new_node: now_node = next_node if nx.number_of_nodes(G1) >= n: check = True break if count > 50 * n: # check = True break # print("triangleの数"+str(tricount)) # '''' tri = list(nx.triangles(G1).values()) sum1 = 0 for num in tri: sum1 += int(num) print(graph.myGCC(G1,tricount)) print(graph.GCC(G1)) sumcluster = 0 for node in G1.nodes(): if degreedic.has_key(node): degree = degreedic[node] if degree >= 2 and tridic.has_key(node): sumcluster += 2.0*tridic[node]/(degree*(degree-1)) print ("AverageCC:"+str(sumcluster/nx.number_of_nodes(G1))) ''' return G1
def distribute_ifaces(self): routes = {} for node in nx.nodes(self.graph): routes[node] = {'ifaces': {}, 'ips': {}, 'costs': {}} elinks = nx.get_node_attributes(self.graph, 'elinks') # need to determine proper link! # pylint: disable=too-many-nested-blocks for node, ifaces in nx.get_node_attributes(self.graph, 'ifs').items(): e_nodes = nx.get_node_attributes(self.graph, 'ifs') for link in elinks[node]: g_tmp = self.graph.copy() #for inode in nx.get_node_attributes(self.graph, 'ifs'): # if node != inode: # g_tmp.remove_node(inode) counter = 0 for link_tmp in elinks[node]: if link != link_tmp: g_tmp.add_edge("dummy%d" % counter, link_tmp[2]) g_tmp.remove_edge(node, link_tmp[2]) counter = counter + 1 weights = nx.get_edge_attributes(g_tmp, 'weight') for onode in e_nodes: if not onode == node: for edge in nx.edges(g_tmp, onode): weights[edge] = 10000 # NEED TO CLEAN THIS CRAP UP! if __NX_VERSION__ > 1: nx.set_edge_attributes(g_tmp, weights, 'weight') else: nx.set_edge_attributes(g_tmp, 'weight', weights) paths = nx.single_source_dijkstra_path(g_tmp, node, weight='weight') for _, path in paths.items(): cost = 1 for path_num in range(1, len(path)): edge = (path[path_num - 1], path[path_num]) for iface in ifaces: if edge[0] == node and not re.match( "dummy*", edge[1]): if iface == link[1]: routes[edge[1]]['ifaces'][iface] = edge[0] routes[edge[1]]['costs'][iface] = cost elif iface not in routes[edge[1]]['ifaces']: routes[edge[1]]['ifaces'][iface] = edge[0] routes[edge[1]]['costs'][iface] = cost else: if not (re.match("dummy*", edge[0]) or\ (re.match("dummy*", edge[1])) or edge[0] in e_nodes) and\ (iface in routes[edge[0]]['ifaces']): if iface == link[1]: routes[ edge[1]]['ifaces'][iface] = edge[0] routes[edge[1]]['costs'][iface] = cost elif iface not in routes[ edge[1]]['ifaces']: routes[ edge[1]]['ifaces'][iface] = edge[0] routes[edge[1]]['costs'][iface] = cost if edge[0] in e_nodes and edge[0] != node: cost = cost + 10000 else: cost = cost + 1 if __NX_VERSION__ > 1: nx.set_node_attributes(self.graph, routes, 'routes') else: nx.set_node_attributes(self.graph, 'routes', routes)
def RW(G, p): if p > 1: p = 1 n = int(p * nx.number_of_nodes(G)) # nodes = set([]) ccdic = {} ndic = {} # cost = [0] def CC(G, v): if ccdic.has_key(v): return ccdic[v] else: n_list = Neighbor(G, v) degree = len(n_list) if degree <= 1: return 0 tri = 0 count = 0 for i in range(0, degree - 1): n2_list = Neighbor(G, n_list[i]) for j in range(i + 1, degree): count += 1 if n_list[j] in n2_list: tri += 1 ans = float(tri) / count ccdic[v] = ans return ans def Neighbor(G, v): if ndic.has_key(v): return ndic[v] else: lst = nx.neighbors(G, v) # cost[0] += 1 ndic[v] = lst return lst while True: count = 0 cc = 0.0 # cost[0] = 0 start_id = random.randint(0, nx.number_of_nodes(G) - 1) s_node = nx.nodes(G)[start_id] now_node = s_node # cost[0] += 1 cc += CC(G, now_node) count += 1 #nodes.add(now_node) while count < 100 * n: neighbor_list = Neighbor(G, now_node) next_id = random.randint(0, len(neighbor_list) - 1) next_node = neighbor_list[next_id] #cost[0] += 1 count += 1 now_node = next_node #nodes.add(now_node) cc += CC(G, now_node) if count >= n: #cost[0] += len(list(nodes)) #print(cost[0]) return cc / count return cc / count