def check_model(self): """ Check the model for various errors. This method checks for the following errors. In the same time it also updates the cardinalities of all the random variables. * Check whether bipartite property of factor graph is still maintained or not. * Check whether factors are associated for all the random variables or not. * Check if factors are defined for each factor node or not. * Check if cardinality of random variable remains same across all the factors. """ variable_nodes = set([x for factor in self.factors for x in factor.scope()]) factor_nodes = set(self.nodes()) - variable_nodes if not all(isinstance(factor_node, Factor) for factor_node in factor_nodes): raise ValueError('Factors not associated for all the random variables') if (not (bipartite.is_bipartite(self)) or not (bipartite.is_bipartite_node_set(self, variable_nodes) or bipartite.is_bipartite_node_set(self, variable_nodes))): raise ValueError('Edges can only be between variables and factors') if len(factor_nodes) != len(self.factors): raise ValueError('Factors not associated with all the factor nodes.') cardinalities = self.get_cardinality() for factor in self.factors: for variable, cardinality in zip(factor.scope(), factor.cardinality): if (cardinalities[variable] != cardinality): raise ValueError('Cardinality of variable {var} not matching among factors'.format(var=variable)) return True
def is_planar(G): """ function checks if graph G has K(5) or K(3,3) as minors, returns True /False on planarity and nodes of "bad_minor" """ result = True bad_minor = [] n = len(G.nodes()) if n > 5: for subnodes in it.combinations(G.nodes(), 6): subG = G.subgraph(subnodes) if bipartite.is_bipartite( G): # check if the graph G has a subgraph K(3,3) X, Y = bipartite.sets(G) if len(X) == 3: result = False bad_minor = subnodes if n > 4 and result: for subnodes in it.combinations(G.nodes(), 5): subG = G.subgraph(subnodes) if len(subG.edges() ) == 10: # check if the graph G has a subgraph K(5) result = False bad_minor = subnodes return result, bad_minor
def add_edge(self, u, v, **kwargs): """ Add an edge between variable_node and factor_node. Parameters ---------- u, v: nodes Nodes can be any hashable Python object. Examples -------- >>> from pgmpy.models import FactorGraph >>> G = FactorGraph() >>> G.add_nodes_from(['a', 'b', 'c']) >>> G.add_nodes_from(['phi1', 'phi2']) >>> G.add_edge('a', 'phi1') """ if u != v: super().add_edge(u, v, **kwargs) else: raise ValueError('Self loops are not allowed') if not bipartite.is_bipartite(self): self.remove_edge(u, v) raise ValueError('Edges can only be between variables and factors')
def prog_15(fname): graphs = {} f = open(fname) n = map(int, f.readline().strip().split()) n = n[0] f.readline() for i in xrange(n): graph = nx.Graph() line = f.readline() while line: edges = map(int, line.strip().split()) graph.add_edge(edges[0],edges[1]) line = f.readline() # print line line = line.strip() # print 'xxxxx ',line graphs[i]=graph f.close() for k,g in graphs.iteritems(): if bipartite.is_bipartite(g): print 1, else: print -1,
def is_planar(G): """ function checks if graph G has K(5) or K(3,3) as minors, returns True /False on planarity and nodes of "bad_minor" """ result = True bad_minor = [] n = len(G.nodes()) if n > 5: for subnodes in it.combinations(G.nodes(), 6): subG = G.subgraph(subnodes) if subG.number_of_edges() >= 9: print "OI" if bipartite.is_bipartite( subG): # check if the graph G has a subgraph K(3,3) X, Y = bipartite.sets(subG) if len(X) == 3: result = False bad_minor = (X.pop(), X.pop(), X.pop(), Y.pop(), Y.pop(), Y.pop()) print[(names[i], names[j]) for i, j in subG.edges()] return result, bad_minor if n > 4 and result: for subnodes in it.combinations(G.nodes(), 5): subG = G.subgraph(subnodes) if len(subG.edges() ) == 10: # check if the graph G has a subgraph K(5) result = False bad_minor = subnodes return result, bad_minor return result, bad_minor
def add_edge(self, u, v, **kwargs): """ Add an edge between variable_node and factor_node. Parameters ---------- u, v: nodes Nodes can be any hashable Python object. Examples -------- >>> from pgmpy.models import FactorGraph >>> G = FactorGraph() >>> G.add_nodes_from(['a', 'b', 'c']) >>> G.add_nodes_from(['phi1', 'phi2']) >>> G.add_edge('a', 'phi1') """ if u != v: super().add_edge(u, v, **kwargs) else: raise ValueError('Self loops are not allowed') if not bipartite.is_bipartite(self): self.remove_edge(u, v) raise ValueError('Edges can only be between variables and factors')
def bi_graph(): B = nx.Graph() # B.add_edges_from([('a', 1), ('b', 1), (1, 2), ('b', 2)]) B.add_edges_from([('a', 1), ('b', 1), ('a', 2), ('c', 2)]) B.add_edges_from([]) print(list(B.nodes())) print(list(B.edges())) print(bipartite.is_bipartite(B)) e = list(B.edges()) f = [] for i in e: f.append(list(i)) print(f) # P = bipartite.collaboration_weighted_projected_graph(B,['a','b','c']) P = bipartite.projected_graph(B, ['a', 'b', 'c']) print(list(P.nodes())) print(list(P.edges(data=True))) f = [] for i in list(P.edges(data=True)): # print('i') # print(i) f.append(list(i)[:2]) if 'weight' in list(i)[2]: print(i[2]['weight']) else: print('no weight') print(f)
def is_planar(G): """ function checks if graph G has K(5) or K(3,3) as minors, returns True /False on planarity and nodes of "bad_minor" """ result = True bad_minor = [] n = len(G.nodes()) iterazione = 0 if n > 5: print "N >5" for subnodes in it.combinations(G.nodes(), 6): iterazione += 1 print "iterazione %d" % iterazione subG = G.subgraph(subnodes) if bipartite.is_bipartite(G): # check if the graph G has a subgraph K(3,3) X, Y = bipartite.sets(G) if len(X) == 3: result = False bad_minor = subnodes return result, bad_minor iterazione = 0 if n > 4 and result: print "N >4" for subnodes in it.combinations(G.nodes(), 5): print "iterazione %d" % iterazione subG = G.subgraph(subnodes) if len(subG.edges()) == 10: # check if the graph G has a subgraph K(5) result = False bad_minor = subnodes return result, bad_minor return result, bad_minor
def create_bipartite_graph(): B = nx.Graph() B.add_nodes_from(['A', 'B', 'C', 'D', 'E'], bipartite=0) # label one set of nodes 0 B.add_nodes_from([1, 2, 3, 4], bipartite=1) # label other set of nodes 1 B.add_edges_from([('A', 1), ('B', 1), ('C', 1), ('D', 2), ('E', 3), ('E', 4)]) # adding edges # check if a graph is bipartite!!! print(bipartite.is_bipartite(B)) # let's change something B.add_edge('A', 'B') print(bipartite.is_bipartite(B)) B.remove_edge('A', 'B') # check if a set of nodes is part of bipartition - X = {1, 2, 3, 4} print(bipartite.is_bipartite_node_set(B, X)) return B
def planar(input_struct): G = nx.Graph() G.add_nodes_from(input_struct.get("nodes")) G.add_weighted_edges_from(input_struct.get("edges")) G_first_set = nx.Graph() G.add_nodes_from(input_struct.get("first_set_nodes")) G.add_weighted_edges_from(input_struct.get("first_set_edges")) choice = input_struct.get("choice") # check if graph is planar is_planar, _ = nx.check_planarity(G) # user choice if choice: if is_planar == True: # if choice is true and graph was planar display(Markdown("Corretto !")) #print("Corretto il grafo è planare, fornire un planar embedding") else: # if choice is true and graph was not planar display(Markdown("Soluzione errata")) #print("Soluzione errata") else: if is_planar == False: # if choice is flase and graph was not planar if G_first_set.number_of_nodes() == 6: # check k_33 graph with bipartition bip_first_set = bipartite.is_bipartite(G_first_set) if bip_first_set: display(Markdown("Controesempio corretto")) #print("Controesempio corretto") else: display(Markdown("Non è un K 3 3")) #print("Non è un K 3 3") elif G_first_set.number_of_nodes() == 5: # check k 5 with clique clique = nx.find_cliques(G_first_set) sorted_list = sorted(clique, key=len) if len(sorted_list[len(sorted_list) - 1]) == 5: display(Markdown("Controesempio corretto")) #print("Controesempio corretto") else: display(Markdown("Non è un K 5")) #print("Non è un K 5") else: # wrong counterexample display(Markdown("Controesempio non valido")) #print("Controesempio non valido") else: # if choice is flase and graph was planar display(Markdown("Soluzione errata"))
def create_bipartite_network(input_data): # create bipartite network by using networkx g = nx.Graph() g.add_edges_from(input_data) if bipartite.is_bipartite(g): NSet = nx.bipartite.sets(g) Net1 = nx.project(g,NSet[0]) Net2 = nx.project(g,NSet[1]) return Net1, Net2 else: print "Can't create bipartite network from input data"
def get_deg_list_by_partition(graph, partition): """ Get degree distribution for given partition of bipartite graph """ if not bipartite.is_bipartite(graph): return [] nodes = [ node for node in graph.nodes if graph.nodes[node]['bipartite'] == partition ] return bipartite.degrees(graph, nodes)[1]
def get_number_of_nodes_by_partition(graph, partition): """ Get number of nodes of given partition of bipartite graph """ if not bipartite.is_bipartite(graph): return 0 nodes = [ node for node in graph.nodes if graph.nodes[node]['bipartite'] == partition ] return len(nodes)
def test_pairwise_bipartite_cc_functions(): # Test functions for different kinds of bipartite clustering coefficients # between pairs of nodes using 3 example graphs from figure 5 p. 40 # Latapy et al (2008) G1 = nx.Graph([(0,2),(0,3),(0,4),(0,5),(0,6),(1,5),(1,6),(1,7)]) G2 = nx.Graph([(0,2),(0,3),(0,4),(1,3),(1,4),(1,5)]) G3 = nx.Graph([(0,2),(0,3),(0,4),(0,5),(0,6),(1,5),(1,6),(1,7),(1,8),(1,9)]) result = {0:[1/3.0, 2/3.0, 2/5.0], 1:[1/2.0, 2/3.0, 2/3.0], 2:[2/8.0, 2/5.0, 2/5.0]} for i, G in enumerate([G1, G2, G3]): assert(bipartite.is_bipartite(G)) assert(cc_dot(set(G[0]), set(G[1])) == result[i][0]) assert(cc_min(set(G[0]), set(G[1])) == result[i][1]) assert(cc_max(set(G[0]), set(G[1])) == result[i][2])
def test_answer_one(): G = answer_one() # check if graph is bipartite assert bipartite.is_bipartite(G) == True # check the number of nodes assert len(G.nodes(data = True)) == 19 # check the number of edges assert len(G.edges(data = True)) == 24 print("All test cases pass!")
def project_graph(name='bipartite_reader_network.pickle', method="Count"): """ Create the projected graph, with weights. :param book_weights_dict: the weights dictionary, which is of the form {(title1_gid, title2_gid) : weight, ...} :param method: This tells us how to weight the edges. "Rating count" sums all the ratings for a weight. "Average" takes the average. "Count" just counts the number of times the edge is shared (co-read). :return: A nx graph. """ print("Projecting Graph with {} method.".format(method)) bi_graph = read(name) if not bipartite.is_bipartite(bi_graph): raise Exception("Projecting non-bipartite graphs is felony.") # Make top nodes (users) to project down onto bottom nodes (books) top_nodes = { n for n, d in bi_graph.nodes(data=True) if d['bipartite'] == 0 } bottom_nodes = set(bi_graph) - top_nodes # Various projection methods if method == "Count": # Count the number of co-reads proj_graph = bipartite.generic_weighted_projected_graph( bi_graph, bottom_nodes) elif method == "Collaboration": # Newman's collaboration metric proj_graph = bipartite.collaboration_weighted_projected_graph( bi_graph, bottom_nodes) elif method == "Overlap": # Proportion of neighbors that are shared proj_graph = bipartite.overlap_weighted_projected_graph( bi_graph, bottom_nodes) elif method == "Average Weight": # todo proj_graph = bipartite.collaboration_weighted_projected_graph( bi_graph, bottom_nodes) elif method == "Divergence": # todo proj_graph = bipartite.collaboration_weighted_projected_graph( bi_graph, bottom_nodes) else: raise Exception("{} is not a valid projection method".format(method)) # Save print("Saving projection_graph_{}.pickle".format(method)) overwrite(proj_graph, "projection_graph_{}.pickle".format(method)) print("Saving projection_graph_{}.gml".format(method)) nx.write_gml(proj_graph, "projection_graph_{}.gml".format(method)) return proj_graph
def get_optimal_edges(sg): paths = {} orig_sg = sg sg = sg.copy() while len(sg.nodes) != 0: # first, find the root(s) of the subgraph at the highest level roots = {n for n, d in sg.in_degree() if d == 0} max_size_root = len(max(roots, key=lambda x: len(x))) roots = {r for r in roots if len(r) == max_size_root} # find everything within reach of 1 reach_1 = set() for root in roots: reach_1.update(sg.neighbors(root)) # build a bipartite graph and do the matching all_nodes = reach_1 | roots bipart_layer = sg.subgraph(all_nodes).to_undirected() assert (bipartite.is_bipartite(bipart_layer)) matching = bipartite.hopcroft_karp_matching(bipart_layer, roots) matching = {k: v for k, v in matching.items() if k in roots} # sanity check -- every vertex should appear in exactly one path assert len(set(matching.values())) == len(matching) # find unmatched roots and add a path to $, indicating that # the path has terminated. for unmatched_root in roots - matching.keys(): matching[unmatched_root] = "$" assert len(matching) == len(roots) # sanity check -- nothing was already in our paths for k, v in matching.items(): assert k not in paths.keys() assert v not in paths.keys() assert v == "$" or v not in paths.values() # sanity check -- all roots have an edge assigned for root in roots: assert root in matching.keys() paths.update(matching) # remove the old roots sg.remove_nodes_from(roots) return paths
def from_bipartite(cls, B, set_names=[0, 1], name=None): """ Static method creates a Hypergraph from a bipartite graph. Parameters ---------- B: nx.Graph() A networkx bipartite graph. Each node in the graph has a property 'bipartite' taking one of two values set_names: iterable An ordered list :math:`[x_0, x_1]`, corresponding to the values assigned to the bipartite property in B. name: hashable Returns ------- new hypergraph : Hypergraph Notes ----- A partition for the nodes in a bipartite graph generates a hypergraph as follows. For each node n in B with bipartite property equal to set_names[0] there is a node n in the hypergraph. For each node e in B with bipartite property equal to set_names[1], there is an edge in the hypergraph. For each edge (n,e) in B add n to the edge e in the hypergraph. """ if not bipartite.is_bipartite(B): raise HyperNetxError('Error: Method requires a bipartite graph.') entities = [] for n, d in B.nodes(data=True): if d['bipartite'] == set_names[1]: elements = [] for nei in B.neighbors(n): elements.append( Entity(nei, [], properties=B.nodes(data=True)[nei])) if elements: entities.append(Entity(n, elements, properties=d)) name = name or '_' return Hypergraph(EntitySet(name, entities), name=name)
def metropolis_hastings(G_target,n_wrmngStps,n_rndmStps,sNdsIntrst, \ smpl_genrt=100): """ Input: G_target is the graph of interest, n_rndmStps is the number of walk steps to run randomizations, n_wrmngStps is the number of warming steps after which random graphs n_rndmStps-n_wrmngStps are going to be recorded, sNdsIntrst is the set of "left" nodes in the bipartite graph. Output: lRndmzdG is a list of randomized graphs with the same degree sequences as G_target following a Markov-chain with a Metropolis- Hastings approach. This are expected to be optimal after n_wrmngStps steps. NOTE: This method requires a bipartite graph. To ensure the graph is bipartite, a graph requires a set of nodes of interest. This willfor sure work in case were there is lncRNA-gene connections, but should be tested case by case if lncRNA-lncRNA gene-gene connections are included. """ assert bipartite.is_bipartite(G_target) #---------------------------- #~ Find adjacent method def find_adjacent(G_rndmzd): """ Input: G_rndmzd is the graph of interest in process of randomization Output: G_rndmzd_prime is a graph that differs from G_rndmzd in exactly one swap (i.e., (G,G') in T). """ G_rndmzd_prime = G_rndmzd.copy() edges = G_rndmzd.edges() while True: edgeIJ,edgeKL = choice(edges),choice(edges) if edgeIJ! = edgeKL:#works well as edges are sorte already in #"edges" nodI,nodJ = edgeIJ nodK,nodL = edgeKL if not G_rndmzd.has_edge(nodI,nodL) and \ not G_rndmzd.has_edge(nodK,nodJ): G_rndmzd_prime.add_edge(nodI,nodL) G_rndmzd_prime.add_edge(nodK,nodJ) G_rndmzd_prime.remove_edge(nodI,nodJ) G_rndmzd_prime.remove_edge(nodK,nodL) break return G_rndmzd_prime
def BipartiteGraphSets(M): if (bipartite.is_bipartite(M)): X, Y = bipartite.sets(M) print("Ingredientes: " + str(len(X))) #print(Y) f = open("ingredientes_mais_usados_paises.csv", "w") for n in Y: f.write(n + ";") aux = M.edges(n, data='weight') aux = sorted(aux, key=lambda aux: aux[2], reverse=True) if len(aux) > 10: for i in range(10): f.write(aux[i][1] + ",") else: for i in range(len(aux)): f.write(aux[i][1] + ",") f.write("\n") f.close() f = open("ingredientes_menos_usados_paises.csv", "w") for n in Y: f.write(n + ";") aux = M.edges(n, data='weight') aux = sorted(aux, key=lambda aux: aux[2]) if len(aux) > 10: for i in range(10): f.write(aux[i][1] + ",") else: for i in range(len(aux)): f.write(aux[i][1] + ",") f.write("\n") f.close() f = open("ingred_mais_usados.txt", "w") aux = copy.copy(X) aux = sorted(aux, key=lambda aux: M.degree(aux), reverse=True) for i in range(len(aux)): f.write(aux[i] + ", ;" + str(M.degree(aux[i])) + "\n") f.close() return (X, Y)
def add_edges(B, filename, burr_list, tort_list): ############################### #extracting edges # prepare a cursor object using cursor() method cursor = db.cursor() query = """ select burrow_number, tortoise_number from """ + filename + """ where burrow_number > "" group by burrow_number, tortoise_number;""" # execute SQL query using execute() method. cursor.execute(query) # Fetch all the rows in a list of lists. results = cursor.fetchall() # connecting edges for row in results: burr = row[0] tort = "T"+str(row[1]) if B.has_node(burr) and B.has_node(tort): B.add_edge(burr, tort) print burr, tort, bipartite.is_bipartite(B) elif not B.has_node(burr): ("mising burrow node==="), row[0] elif not B.has_node(tort): ("mising tort node==="), row[1] return B
# Add course if not in graph. if course_id not in G.nodes: G.add_node(course_id, type='course', x=1) courses.append(course_id) # Add student if not in graph. if student_id not in G.nodes: G.add_node(student_id, type='student', x=200) students.append(student_id) G.add_edge(course_id, student_id) print("Courses: " + str(len(courses))) print("Students: " + str(len(students))) print("Bipartite: " + str(bipartite.is_bipartite(G))) nx.write_gexf(G, "CourseStudentBipartite.gexf") plt.figure(figsize=(20, 7)) plt.title("Spring") ax = plt.gca() # Spring is cool pos = nx.spectral_layout(G) nx.draw_networkx_nodes(G, pos=pos, ax=ax, nodelist=students, node_size=1, node_color='blue')
def test_is_bipartite(self): assert bipartite.is_bipartite(nx.path_graph(4)) assert bipartite.is_bipartite(nx.DiGraph([(1, 0)])) assert not bipartite.is_bipartite(nx.complete_graph(3))
def test_bipartite_directed(self): G = bipartite.random_graph(10, 10, 0.1, directed=True) assert bipartite.is_bipartite(G)
def test_is_bipartite(self): assert_true(bipartite.is_bipartite(nx.path_graph(4))) assert_true(bipartite.is_bipartite(nx.DiGraph([(1,0)]))) assert_false(bipartite.is_bipartite(nx.complete_graph(3)))
for atrraction_id in classroute: if (VName(user_id), AName(atrraction_id)) in graph.edges: graph[VName(user_id)][AName(atrraction_id)]["weight"] += 1 else: graph.add_edge(VName(user_id), AName(atrraction_id), weight=1) sql = "select * from public.test_set" cursor.execute(sql) rows = cursor.fetchall() for row in rows: user_id = row[7] classroute = row[2] classroutestr = row[3] for atrraction_id in classroute[:-1]: if (VName(user_id), AName(atrraction_id)) in graph.edges: graph[VName(user_id)][AName(atrraction_id)]["weight"] += 1 else: graph.add_edge(VName(user_id), AName(atrraction_id), weight=1) return graph if __name__ == "__main__": graph = init_graph() print(bipartite.is_bipartite(graph)) a, b = bipartite.sets(graph) print(len(a)) for i in graph.neighbors("V1"): print(i)
def find_planarity(G): """ Searches *graph* for any subgraphs isomorphic to K(3, 3) or K(5). Complexity: O((n choose 6) + (n choose 5)) Args: graph (networkx.Graph): The graph to be searched Returns: bool, networkx.Graph: Planarity of the graph and either None or the detected Kuratowski graph """ planar = True offending_subgraph = None # Optimization step: # Remove nodes from graph with edge count > 1 and assign to new graph (don't alter original) outdeg = G.degree() to_keep = [n for n in outdeg if outdeg[n] > 1] graph = G.subgraph(to_keep) num_nodes = len(graph.nodes()) it = 0 k33 = GraphGenerator.make_k33_graph() k5 = GraphGenerator.make_k5_graph() if num_nodes > 5: # Test if graph contains a K(3, 3) subgraph. for subgraph_nodes in itertools.combinations(graph.nodes(), 6): it += 1 subgraph = graph.subgraph(subgraph_nodes) # If the subgraph is bipartite, get each set if bipartite.is_bipartite(graph): # subgraph? set1, set2 = bipartite.sets(graph) # subgraph? # If one set in a 6-node bipartite graph has 3 nodes, then the other # set has 3 nodes, making it a K(3, 3) graph. if len(set1) == 3: planar = False offending_subgraph = subgraph # Test for isomorphism if nx.is_isomorphic(subgraph, k33): planar = False offending_subgraph = subgraph if planar and num_nodes > 4: # Test if graph contains a K(5) subgraph. for subgraph_nodes in itertools.combinations(graph.nodes(), 5): it += 1 subgraph = graph.subgraph(subgraph_nodes) # If the graph is complete, it's a K(5) graph if GraphUtils.check_completeness(subgraph): planar = False offending_subgraph = subgraph # Test isomorphism if nx.is_isomorphic(subgraph, k5): planar = False offending_subgraph = subgraph print "Iterations (actual):", GraphUtils.format_commas(it) return planar, offending_subgraph
def test_bipartite(sbs_hypergraph): from networkx.algorithms import bipartite h = sbs_hypergraph b = h.bipartite() assert bipartite.is_bipartite(b)
Gt = most_important(goarn_network) # trimming from pylab import show # create the layout pos = nx.spring_layout(goarn_network) # draw the nodes and the edges (all) nx.draw_networkx_nodes(goarn_network,pos,node_color='b',alpha=0.2,node_size=8) nx.draw_networkx_edges(goarn_network,pos,alpha=0.1) # draw the most important nodes with a different style nx.draw_networkx_nodes(Gt,pos,node_color='r',alpha=0.4,node_size=254) # also the labels this time nx.draw_networkx_labels(Gt,pos,font_size=12,font_color='b') show() from networkx.algorithms import bipartite print bipartite.is_bipartite(goarn_network) #from networkx import find_cliques #cliques = list(find_cliques(goarn_network)) #bigg= max(cliques, key=lambda l: len(l)) #print bigg #print nx.graph_number_of_cliques(goarn_network, cliques) H=nx.connected_component_subgraphs(goarn_network)[0] print(nx.average_shortest_path_length(H)) print len(goarn_network.nodes())
import networkx as nx import matplotlib.pyplot as plt from networkx.algorithms import bipartite # Draws a Cycle Graph ( C ) with N nodes # bipartite for nodes >= 3 and Even # Euler Circuit for Nodes > 1 and odd nodes = 5 G = nx.cycle_graph(nodes) nx.draw_networkx(G) plt.show() print('is bipartite: {0}'.format(bipartite.is_bipartite(G))) [u for u, v in nx.eulerian_circuit(G)]
def __main__(): filepath = getFilepath() print "reading:", filepath, "...", workbook = xl.load_workbook(filename = filepath) ## workbook LONG print "done!" twitterIDs = {} fullDG = nx.DiGraph() ## creating a directed graph sheetNames = workbook.get_sheet_names() ws = workbook.get_sheet_by_name(sheetNames[0]) print 'reading id sheet:', ws.title, "...", for row in ws.rows[1:]: id = row[0].value name = row[2].value twitterIDs[id] = name fullDG.add_node(id) print "done!" for sheetName in sheetNames[1:]: ws = workbook.get_sheet_by_name(sheetName) print 'reading relation sheet:', ws.title, "...", for row in ws.rows[1:]: source = row[0].value dest = row[1].value if source is None or dest is None: continue edge = row[4].value if edge: sToD = row[2] dToS = row[3] if sToD: fullDG.add_edge(source, dest) if dToS: fullDG.add_edge(dest, source) print "done!" connDG = nx.DiGraph(data=fullDG) connDG.remove_nodes_from(nx.isolates(connDG)) graphNames = ('Complete', 'Connected') graphs = (fullDG, connDG) for graph, name in zip(graphs, graphNames): print "Analysis for",name,"twitter graph:" print "Number of nodes/edges: ", graph.number_of_nodes(),'\t', graph.number_of_edges() numDisplay = min(len(graph.nodes())/5, 5) statNames = ('degree', 'in degree', 'out degree') centralityFuncts = (nx.degree_centrality, nx.in_degree_centrality, nx.out_degree_centrality) ## for statName, graphFunct in zip(statNames, centralityFuncts): for statName, graphFunct in zip(statNames[:1], centralityFuncts[:1]): statDict = graphFunct(graph) ends = ("greatest", dict_nlargest), ("least", dict_nsmallest) for endName, endFunct in ends: displayDict = endFunct(statDict, numDisplay) print numDisplay, endName, statName, "centrality nodes:" for key in displayDict: print "\t", twitterIDs[key], ":\t", graph.degree(key) # print "\t", nx.info(graph, key) # print "\t", twitterIDs[key], ":\t", displayDict[key], "\t", print "number of isolated nodes",'\t\t',len(nx.isolates(graph)) print "degree (minimum, average, maximum):", '\t\t', getMinAvgMax(graph.degree()) try: print "diameter",'\t\t', nx.diameter(graph) except: print "error" try: print "radius",'\t\t', nx.radius(graph) except: print "error" try: print "is_bipartite?",'\t\t', bipartite.is_bipartite(graph) except: print "error" try: print "average_shortest_path_length",'\t\t', nx.average_shortest_path_length(graph) except: print "error" try: print "degree_assortativity_coefficient",'\t\t', nx.degree_assortativity_coefficient(graph) except: print "error" try: print "assortativity.average_degree_connectivity",'\t\t', nx.assortativity.average_degree_connectivity(graph) except: print "error" print
def test_is_bipartite(self): G = nx.path_graph(4) assert_true(bipartite.is_bipartite(G)) G = nx.DiGraph([(1, 0)]) assert_true(bipartite.is_bipartite(G))
G.edges(data="relation") # list of all edges with attribute "relation" # Method : nx.Graph.nodes() G.nodes() # list of all nodes G.nodes(data=True) # list of all nodes with attributes # Bipartite Graphs B = nx.Graph() B.add_nodes_from(["A", "B", "C", "D", "E"], bipartite=0) B.add_nodes_from([1, 2, 3, 4], bipartite=1) B.add_edges_from([("A", 1), ("B", 1), ("C", 1), ("C", 3), ("D", 2), ("E", 3)]) from networkx.algorithms import bipartite bipartite.is_bipartite(B) # check if B is bipartite B.add_edge("A", "B") # break the rule bipartite.is_bipartite(B) B.remove_edge("A", "B") # remove edge # check set of nodes is bipartite X = set([1, 2, 3, 4]) bipartite.is_bipartite_node_set(B, X) X = set(["A", "B", "C", "D", "E"]) bipartite.is_bipartite_node_set(B, X) bipartite.sets(B) # Projected Graphs
import re import matplotlib.pyplot as plt import networkx as nx from operator import itemgetter from networkx.algorithms import bipartite from networkx.utils import (powerlaw_sequence, create_degree_sequence) #from igraph import Graph, mean import numpy as nm #G=nx.gnm_random_graph(2939,30501,directed=True) D = nx.gnr_graph(49, 0.09083) # the GNR graph G = D.to_undirected() # the undirected version #nx.draw_random(G1) print(bipartite.is_bipartite(G)) d = nx.degree(G) nx.draw(G, nodelist=d.keys()) #nx.draw(G, nodelist=d.keys(), node_size=[v * 20 for v in d.values()]) #plt.savefig("./random/sameNodesize.png") plt.show() print('diameter ',nx.diameter(G, e=None))# graph not connected print('debsity', nx.density(G)) print("clustering coefficient",nx.average_clustering(G)) #print("average degree ", nm.mean(G.degree())) print("AVG",nx.average_shortest_path_length(G)) print(" Clique ",len(list(nx.find_cliques(G)))) L = nx.normalized_laplacian_matrix(G) e = nm.linalg.eigvals(L.A)
def buildGraph(vertices, fileName, action, source=-1, target=-1): # print(action, vertices, fileName, action, source) G.clear() pos = nx.spring_layout(G) for i in range(0, len(vertices) - 1, 2): G.add_edge(vertices[i], vertices[i + 1], color="green", weight=7) colors = nx.get_edge_attributes(G, 'color').values() weights = nx.get_edge_attributes(G, 'weight').values() if action == "dfs": try: traverse = [] T = nx.dfs_tree(G, source=source) for i in T.edges(): traverse.append(i[0]) traverse.append(i[1]) ans = [] for i in range(len(traverse) - 1): if traverse[i] != traverse[i + 1]: ans.append(traverse[i]) ans.append(traverse[len(traverse) - 1]) traverse = ans nx.draw(G, with_labels=True, edge_color=colors, width=list(weights), node_size=1005) plt.savefig('./static/images/' + fileName) return traverse except: return "Error" if action == "bfs": try: traverse = [] T = nx.bfs_tree(G, source=source) for i in T.edges(): traverse.append(i[0]) traverse.append(i[1]) ans = [] for i in range(len(traverse) - 1): if traverse[i] != traverse[i + 1]: ans.append(traverse[i]) ans.append(traverse[len(traverse) - 1]) traverse = ans nx.draw(G, with_labels=True, edge_color=colors, width=list(weights), node_size=1005) plt.savefig('./static/images/' + fileName) return traverse except: return "Error" if action == "shortest path": try: traverse = [] T = nx.bidirectional_shortest_path(G, source=source, target=target) print(T) for i in range(len(T) - 1): G.add_edge(T[i], T[i + 1], color='r', weight=10) traverse.append(T[i]) traverse.append(T[len(T) - 1]) print(traverse) colors = nx.get_edge_attributes(G, 'color').values() weights = nx.get_edge_attributes(G, 'weight').values() nx.draw(G, edge_color=colors, with_labels=True, width=list(weights), node_size=1005) plt.savefig('./static/images/' + fileName) return traverse except: return "Error" if action == "bipartite": if bipartite.is_bipartite(G): color = [] T = bipartite.color(G) weight = [] node_size = [] for node in G: if T[node] == 0: color.append('red') weight.append(6) node_size.append(1005) else: color.append('green') weight.append(6) node_size.append(1005) nx.draw(G, node_color=color, width=weight, node_size=node_size, with_labels=True) plt.savefig('./static/images/' + fileName) return True else: return False if action == "cycle": try: traverse = [] T = nx.find_cycle(G, orientation="original") for i in range(len(T)): G.add_edge(T[i][0], T[i][1], color='r', weight=10) traverse.append(T[i][0]) print(traverse) colors = nx.get_edge_attributes(G, 'color').values() weights = nx.get_edge_attributes(G, 'weight').values() nx.draw(G, edge_color=colors, with_labels=True, width=list(weights), node_size=1005) plt.savefig('./static/images/' + fileName) return traverse except: return "Not Contain A Cycle"
def test_is_bipartite(self): assert_true(bipartite.is_bipartite(nx.path_graph(4))) assert_true(bipartite.is_bipartite(nx.DiGraph([(1, 0)]))) assert_false(bipartite.is_bipartite(nx.complete_graph(3)))
import networkx from networkx.algorithms import bipartite # Create the actor/movie graph g = networkx.Graph() g.add_edges_from([("Stallone", "Expendables"), ("Schwarzenegger", "Expendables")]) g.add_edges_from([("Schwarzenegger", "Terminator 2"), ("Furlong", "Terminator 2")]) g.add_edges_from([("Furlong", "Green Hornet"), ("Diaz", "Green Hornet")]) # Test if graph is bipartite print bipartite.is_bipartite(g) print bipartite.bipartite_sets(g) # Graph is no longer bipartite after this g.add_edge("Schwarzenegger", "Stallone") print bipartite.is_bipartite(g)
def SetCover(B, prefixGenes, timelimit, costs, K): """ Given a bipartite graph with samples/patients as one set of nodes and genes as the second set of nodes this function constructs the set cover formulation, which is then solved. OBJECTIVE MIN \sum genes CONSTRAINTS for each patient: \sum genes >= K B: bipartite graph between the samples and the genes or metagenes prefixGenes:genes or Metagenes must carry a prefix different from the prefixes for the samples. costs: a dictionary mapping from Genes to costs. The objective function is minimised, thus genes with low costs will be referred. costs: Genes -> [0, +Inf]. If undefined each gene will cost 1. timelimit: huge instances will require a lot of memory and a lot of time. The memory is by default 12,000 MB. To avoid long runningtimes you can also set the time limit. NOTE: The solution returned then is not necessarily optimal. K: covereage parameter, default K = 1 returns: solutionGenes: The genes in the cover (gap, upper bound, solution value): If there is a solution the three values will be returned, otherwise the cplex Error code """ assert bipartite.is_bipartite(B) Patients = [node for node in B.nodes() if not node.startswith(prefixGenes)] Genes = [node for node in B.nodes() if node.startswith(prefixGenes)] Patients = sorted(list(Patients)) Genes = sorted(list(Genes)) print "Orig pat", len(Patients) # remove patients that have a lower degree than K Btmp = B.copy() removePatients = [] for pat in Patients: if len(B.neighbors(pat)) < K: removePatients.append(pat) print "**********************Patients with low degree***********************************" print removePatients print "Number of outlier patients:", len(removePatients) print "*********************************************************************************" Btmp.remove_nodes_from(removePatients) Patients = [node for node in Btmp.nodes() if not node.startswith(prefixGenes)] Patients = sorted(list(Patients)) # Initialise set cover # OBJECTIVE MIN \sum genes # CONSTRAINTS for each patient: \sum genes >= 1 print "CPLEX ..." setCover = cplex.Cplex() setCover.parameters.workmem.set(12000) if timelimit is not None: setCover.parameters.timelimit.set(timelimit) setCover.parameters.threads.set(6) setCover.parameters.mip.strategy.probe.set(3) print "Memory", setCover.parameters.workmem.get() print "Time", setCover.parameters.timelimit.get() print "Probing", setCover.parameters.mip.strategy.probe.get() print "Threads", setCover.parameters.threads.get() setCover.objective.set_sense(setCover.objective.sense.minimize) #cost for each gene is 1 if costs == None: my_obj = np.ones(len(Genes)) else: c = np.ones(len(Genes)) #fill costs vector for i in range(len(Genes)): c[i] = costs[Genes[i]] my_obj = np.array(c) # add a variable for each gene, variable name is "ENTREZ_number" setCover.variables.add(obj = my_obj.tolist(), names = Genes, ub = np.ones(len(Genes)).tolist(), types = "B"*len(Genes)) print "ADDING constraints" for pat in Patients: # get all neighbours of a patient (these are the deregulated genes), call by variable name, the weight for each gene is 1 setCover.linear_constraints.add(lin_expr = [cplex.SparsePair(Btmp.neighbors(pat), np.ones(len(Btmp.neighbors(pat))).tolist())], \ senses = ["G"], rhs = [K], names = [str(pat)]) #solve print "SOLVING ... " setCover.solve() if setCover.solution.get_status() == 101 or setCover.solution.get_status() == 102: #Integer optimal #Retrieve solution genes print "RETRIEVING solution" solution = np.array(setCover.solution.get_values()) #binary solution vector of all variables, we just have genes as variables here print "Sum vars:", sum(solution) solutionGenes = np.array(setCover.variables.get_names())[solution > 0] # find the genes' (variables') names, that contribute to the solution print "Solution:", solutionGenes return (solutionGenes.tolist(), (None, setCover.solution.MIP.get_best_objective(), setCover.solution.MIP.get_best_objective())) elif setCover.solution.get_status() == 107: #timelimit exceeded print "Warning: Solution is suboptimal; check timelimit!" print "Gap", setCover.solution.MIP.get_mip_relative_gap() print "Lower bound", setCover.solution.MIP.get_best_objective() print "Actual cost", setCover.solution.get_objective_value() print "RETRIEVING solution" solution = np.array(setCover.solution.get_values()) #binary solution vector of all variables, we just have genes as variables here solutionGenes = np.array(setCover.variables.get_names())[solution == 1] # find the genes' (variables') names, that contribute to the solution print "Sum vars:", sum(solution) print "Solution:", solution return (solutionGenes.tolist(), (setCover.solution.MIP.get_mip_relative_gap(), setCover.solution.MIP.get_best_objective(), setCover.solution.get_objective_value())) else: print "ERROR", setCover.solution.get_status() return setCover.solution.get_status()
""" Projection-related exercises, including the generalized similarity """ import pickle import pandas as pd import matplotlib.pyplot as plt import scipy.stats as stats import dzcnapy_plotlib as dzcnapy import networkx as nx from networkx.drawing.nx_agraph import graphviz_layout from networkx.algorithms import bipartite N = pickle.load(open("nutrients.pickle", "rb")) print(bipartite.is_bipartite(N)) bip1, bip2 = bipartite.sets(N) print("C" in bip1, "C" in bip2) foods, nutrients = (bip2, bip1) if "C" in bip1 else (bip1, bip2) print(foods, nutrients) n_graph = bipartite.projected_graph(N, nutrients) f_graph = bipartite.projected_graph(N, foods) fw_graph = bipartite.weighted_projected_graph(N, foods, True) # Edge width represents weights dzcnapy.attrs["width"] = [d['weight'] * 75 for n1, n2, d in fw_graph.edges(data=True)] dzcnapy.thick_attrs["width"] = 10
def test_bipartite_directed(self): G = nx.bipartite_random_graph(10, 10, 0.1, directed=True) assert_true(bipartite.is_bipartite(G))
def test_bipartite_directed(self): G = nx.bipartite_random_graph(10, 10, 0.1, directed=True) assert_true(bipartite.is_bipartite(G))
print "Done reading all the files" graphs[item] = g for g in graphs.keys(): try: print "====================== " , g, "===========================" print "number of isolated nodes",'\t\t\t',nx.isolates(graphs[g]) print "is graph biconnected?",'\t\t\t',nx.is_biconnected(graphs[g]) print "cut vertices are: ",'\t\t\t',list(nx.articulation_points(graphs[g])) print "number of nodes",'\t\t\t',len(graphs[g].nodes()) print "number of edges",'\t\t\t',len(graphs[g].edges()) print "degree",'\t\t\t',get_avg(graphs[g].degree()) print "diameter",'\t\t\t', nx.diameter(graphs[g]) print "radius",'\t\t\t', nx.radius(graphs[g]) print "is_bipartite?",'\t\t', bipartite.is_bipartite(graphs[g]) print "average_shortest_path_length",'\t\t', nx.average_shortest_path_length(graphs[g]) print "degree_assortativity_coefficient",'\t\t', nx.degree_assortativity_coefficient(graphs[g]) print "assortativity.average_degree_connectivity",'\t\t', nx.assortativity.average_degree_connectivity(graphs[g]) #print "degree_pearson_correlation_coefficient",'\t\t', nx.degree_pearson_correlation_coefficient(graphs[g]) print "node closeness_centrality",'\t\t\t', get_avg(nx.closeness_centrality(graphs[g])) print "clustering",'\t\t\t', get_avg(nx.clustering(graphs[g])) print "node betweeness",'\t\t\t', get_avg(nx.betweenness_centrality(graphs[g],normalized=False,endpoints=False)) print "edge betweeness",'\t\t\t', get_avg(nx.edge_betweenness_centrality(graphs[g],normalized=False)) #print "spectral_bipartivity",'\t\t', bipartite.spectral_bipartivity(graphs[g]) #print "node betweeness normalized",'\t\t\t', get_avg(nx.betweenness_centrality(graphs[g],normalized=True,endpoints=False)) #print "edge betweeness normalized",'\t\t\t', get_avg(nx.edge_betweenness_centrality(graphs[g],normalized=True)) #print "node closeness_vitality",'\t\t\t', get_avg(nx.closeness_vitality(graphs[g])) #print "communicability_centrality",'\t\t', get_avg(nx.communicability_centrality(graphs[g])) #print "communicability_betweenness_centrality",'\t\t', get_avg(nx.communicability_betweenness_centrality(graphs[g])) #print "transitivity",'\t\t\t', round(nx.transitivity(graphs[g]),4)
def test_is_bipartite(self): G=nx.path_graph(4) assert_true(bipartite.is_bipartite(G)) G=nx.DiGraph([(1,0)]) assert_true(bipartite.is_bipartite(G))