def test_project_weighted_newman(self): edges = [ ("A", "B", 1.5), ("A", "C", 0.5), ("B", "C", 0.5), ("B", "D", 1), ("B", "E", 2), ("E", "F", 1), ] Panswer = nx.Graph() Panswer.add_weighted_edges_from(edges) P = bipartite.collaboration_weighted_projected_graph(self.G, "ABCDEF") assert_edges_equal(list(P.edges()), Panswer.edges()) for u, v in list(P.edges()): assert P[u][v]["weight"] == Panswer[u][v]["weight"] edges = [ ("A", "B", 11 / 6.0), ("A", "E", 1 / 2.0), ("A", "C", 1 / 3.0), ("A", "D", 1 / 3.0), ("B", "E", 1 / 2.0), ("B", "C", 1 / 3.0), ("B", "D", 1 / 3.0), ("C", "D", 1 / 3.0), ] Panswer = nx.Graph() Panswer.add_weighted_edges_from(edges) P = bipartite.collaboration_weighted_projected_graph(self.N, "ABCDE") assert_edges_equal(list(P.edges()), Panswer.edges()) for u, v in list(P.edges()): assert P[u][v]["weight"] == Panswer[u][v]["weight"]
def test_project_weighted_newman(self): edges=[('A','B',1.5), ('A','C',0.5), ('B','C',0.5), ('B','D',1), ('B','E',2), ('E','F',1)] Panswer=nx.Graph() Panswer.add_weighted_edges_from(edges) P=bipartite.collaboration_weighted_projected_graph(self.G,'ABCDEF') assert_edges_equal(P.edges(),Panswer.edges()) for u,v in P.edges(): assert_equal(P[u][v]['weight'],Panswer[u][v]['weight']) edges=[('A','B',11/6.0), ('A','E',1/2.0), ('A','C',1/3.0), ('A','D',1/3.0), ('B','E',1/2.0), ('B','C',1/3.0), ('B','D',1/3.0), ('C','D',1/3.0)] Panswer=nx.Graph() Panswer.add_weighted_edges_from(edges) P=bipartite.collaboration_weighted_projected_graph(self.N,'ABCDE') assert_edges_equal(P.edges(),Panswer.edges()) for u,v in P.edges(): assert_equal(P[u][v]['weight'],Panswer[u][v]['weight'])
def test_project_weighted_newman(self): edges=[('A','B',1.5), ('A','C',0.5), ('B','C',0.5), ('B','D',1), ('B','E',2), ('E','F',1)] Panswer=nx.Graph() Panswer.add_weighted_edges_from(edges) P=bipartite.collaboration_weighted_projected_graph(self.G,'ABCDEF') assert_edges_equal(list(P.edges()),Panswer.edges()) for u,v in list(P.edges()): assert_equal(P[u][v]['weight'],Panswer[u][v]['weight']) edges=[('A','B',11/6.0), ('A','E',1/2.0), ('A','C',1/3.0), ('A','D',1/3.0), ('B','E',1/2.0), ('B','C',1/3.0), ('B','D',1/3.0), ('C','D',1/3.0)] Panswer=nx.Graph() Panswer.add_weighted_edges_from(edges) P=bipartite.collaboration_weighted_projected_graph(self.N,'ABCDE') assert_edges_equal(list(P.edges()),Panswer.edges()) for u,v in list(P.edges()): assert_equal(P[u][v]['weight'],Panswer[u][v]['weight'])
def test_project_weighted_newman(self): edges = [("A", "B", 1.5), ("A", "C", 0.5), ("B", "C", 0.5), ("B", "D", 1), ("B", "E", 2), ("E", "F", 1)] Panswer = nx.Graph() Panswer.add_weighted_edges_from(edges) P = bipartite.collaboration_weighted_projected_graph(self.G, "ABCDEF") assert_equal(P.edges(), Panswer.edges()) for u, v in P.edges(): assert_equal(P[u][v]["weight"], Panswer[u][v]["weight"]) edges = [ ("A", "B", 11 / 6.0), ("A", "E", 1 / 2.0), ("A", "C", 1 / 3.0), ("A", "D", 1 / 3.0), ("B", "E", 1 / 2.0), ("B", "C", 1 / 3.0), ("B", "D", 1 / 3.0), ("C", "D", 1 / 3.0), ] Panswer = nx.Graph() Panswer.add_weighted_edges_from(edges) P = bipartite.collaboration_weighted_projected_graph(self.N, "ABCDE") assert_equal(P.edges(), Panswer.edges()) for u, v in P.edges(): assert_equal(P[u][v]["weight"], Panswer[u][v]["weight"])
def test_path_collaboration_projected_graph(self): G = nx.path_graph(4) P = bipartite.collaboration_weighted_projected_graph(G, [1, 3]) assert_nodes_equal(list(P), [1, 3]) assert_edges_equal(list(P.edges()), [(1, 3)]) P[1][3]['weight'] = 1 P = bipartite.collaboration_weighted_projected_graph(G, [0, 2]) assert_nodes_equal(list(P), [0, 2]) assert_edges_equal(list(P.edges()), [(0, 2)]) P[0][2]['weight'] = 1
def test_path_collaboration_projected_graph(self): G = nx.path_graph(4) P = bipartite.collaboration_weighted_projected_graph(G, [1, 3]) assert_equal(sorted(P.nodes()), [1, 3]) assert_equal(sorted(P.edges()), [(1, 3)]) P[1][3]["weight"] = 1 P = bipartite.collaboration_weighted_projected_graph(G, [0, 2]) assert_equal(sorted(P.nodes()), [0, 2]) assert_equal(sorted(P.edges()), [(0, 2)]) P[0][2]["weight"] = 1
def test_directed_path_collaboration_projected_graph(self): G = nx.DiGraph() nx.add_path(G, range(4)) P = bipartite.collaboration_weighted_projected_graph(G, [1, 3]) assert_nodes_equal(list(P), [1, 3]) assert_edges_equal(list(P.edges()), [(1, 3)]) P[1][3]['weight'] = 1 P = bipartite.collaboration_weighted_projected_graph(G, [0, 2]) assert_nodes_equal(list(P), [0, 2]) assert_edges_equal(list(P.edges()), [(0, 2)]) P[0][2]['weight'] = 1
def test_directed_path_collaboration_projected_graph(self): G = nx.DiGraph() nx.add_path(G, range(4)) P = bipartite.collaboration_weighted_projected_graph(G, [1, 3]) assert nodes_equal(list(P), [1, 3]) assert edges_equal(list(P.edges()), [(1, 3)]) P[1][3]["weight"] = 1 P = bipartite.collaboration_weighted_projected_graph(G, [0, 2]) assert nodes_equal(list(P), [0, 2]) assert edges_equal(list(P.edges()), [(0, 2)]) P[0][2]["weight"] = 1
def test_directed_path_collaboration_projected_graph(self): G=nx.DiGraph() G.add_path(list(range(4))) P=bipartite.collaboration_weighted_projected_graph(G,[1,3]) assert_nodes_equal(P.nodes(),[1,3]) assert_edges_equal(P.edges(),[(1,3)]) P[1][3]['weight']=1 P=bipartite.collaboration_weighted_projected_graph(G,[0,2]) assert_nodes_equal(P.nodes(),[0,2]) assert_edges_equal(P.edges(),[(0,2)]) P[0][2]['weight']=1
def test_directed_path_collaboration_projected_graph(self): G = nx.DiGraph() G.add_path(list(range(4))) P = bipartite.collaboration_weighted_projected_graph(G, [1, 3]) assert_equal(sorted(P.nodes()), [1, 3]) assert_equal(sorted(P.edges()), [(1, 3)]) P[1][3]["weight"] = 1 P = bipartite.collaboration_weighted_projected_graph(G, [0, 2]) assert_equal(sorted(P.nodes()), [0, 2]) assert_equal(sorted(P.edges()), [(0, 2)]) P[0][2]["weight"] = 1
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 test_project_collaboration(self): G = nx.Graph() G.add_edge("a", 1) G.add_edge("b", 1) G.add_edge("b", 2) G.add_edge("c", 2) G.add_edge("c", 3) G.add_edge("c", 4) G.add_edge("b", 4) P = bipartite.collaboration_weighted_projected_graph(G, "abc") assert_equal(P["a"]["b"]["weight"], 1) assert_equal(P["b"]["c"]["weight"], 2)
def collaboration_weighted_projected_graph(self): E = bipartite.sets(self.B)[0] P = bipartite.collaboration_weighted_projected_graph(self.B, E) self.plot_graph_2(P, 'collaboration_weighted_projected') print('collaboration_weighted_projected:number of edges:', P.number_of_edges()) print(P.edges()) print(list(P.edges(data=True))) weights = [] for i in list(P.edges(data=True)): weights.append(i[2]['weight']) print(weights)
def test_project_collaboration(self): G = nx.Graph() G.add_edge("a", 1) G.add_edge("b", 1) G.add_edge("b", 2) G.add_edge("c", 2) G.add_edge("c", 3) G.add_edge("c", 4) G.add_edge("b", 4) P = bipartite.collaboration_weighted_projected_graph(G, "abc") assert P["a"]["b"]["weight"] == 1 assert P["b"]["c"]["weight"] == 2
def test_project_collaboration(self): G = nx.Graph() G.add_edge('a', 1) G.add_edge('b', 1) G.add_edge('b', 2) G.add_edge('c', 2) G.add_edge('c', 3) G.add_edge('c', 4) G.add_edge('b', 4) P = bipartite.collaboration_weighted_projected_graph(G, 'abc') assert_equal(P['a']['b']['weight'], 1) assert_equal(P['b']['c']['weight'], 2)
def test_project_collaboration(self): G=nx.Graph() G.add_edge('a',1) G.add_edge('b',1) G.add_edge('b',2) G.add_edge('c',2) G.add_edge('c',3) G.add_edge('c',4) G.add_edge('b',4) P=bipartite.collaboration_weighted_projected_graph(G,'abc') assert_equal(P['a']['b']['weight'],1) assert_equal(P['b']['c']['weight'],2)
def projected_graph(self,input_0,input_1,input_2): # Make sure the right fields exist if 'bipartite_0' not in input_0: return[False,'Parameter bipartite_0 does not exist in given input',{}] if 'bipartite_1' not in input_0: return[False,'Parameter bipartite_1 does not exist in given input',{}] if 'edges' not in input_0: return[False,'Parameter edges does not exist in given input',{}] if 'nodes' not in input_1: return[False,'Parameter nodes does not exist in given input',{}] # Checking given graph is valid bipartite graph ret_val = self.bipartite_graph({'bipartite_0':input_0['bipartite_0'],'bipartite_1':input_0['bipartite_1']},{'edges':input_0['edges']}) if not ret_val[0]: return ret_val # Checking validity of the nodes parameter if not isinstance(input_1['nodes'], list): return[False,'Parameter nodes does not contain an array',{}] if len(input_1['nodes']) <= 0: return [False, 'Parameter nodes does not contain at least one element', {}] # Checking nodes to project onto all belong to a single biparition and of course # they are actually found in one of the bipartitions edge = None if input_1['nodes'][0] in input_0['bipartite_0']: edge = 0 elif input_1['nodes'][0] in input_0['bipartite_1']: edge = 1 else: return [False, 'Node element at zero-indexed position 0 is not contained in either of the bipartitions', {}] edge_text = 'bipartite_' + str(edge) # Make sure that all elements are found in the discovered bipartition for i in range(len(input_1['nodes'])): if input_1['nodes'][i] not in input_0[edge_text]: return [False, 'Node element at zero-indexed position {} is not contained in {}'.format(i,edge_text), {}] P = None if input_2 == 'none': P = bipartite.projected_graph(self.networkx_graph, input_1['nodes']) elif input_2 == 'multigraph': P = bipartite.projected_graph(self.networkx_graph, input_1['nodes'],multigraph=True) elif input_2 == 'degree': P = bipartite.weighted_projected_graph(self.networkx_graph, input_1['nodes']) elif input_2 == 'degree_ratio': P = bipartite.weighted_projected_graph(self.networkx_graph, input_1['nodes'], ratio=True) elif input_2 == 'Newman': P = bipartite.collaboration_weighted_projected_graph(self.networkx_graph, input_1['nodes']) elif input_2 == 'Jaccard': P = bipartite.overlap_weighted_projected_graph(self.networkx_graph, input_1['nodes']) elif input_2 == 'Jaccard_modified': P = bipartite.overlap_weighted_projected_graph(self.networkx_graph, input_1['nodes'], jaccard=False) else: return [False, 'Unkown weighting logic specified', {}] output = {} output['nodes'] = list(P.nodes()) output['edges'] = [] output['weights'] = [] weight_q_mark = True for i in list(P.edges(data=True)): output['edges'].append(list(i)[:2]) if weight_q_mark: if 'weight' in list(i)[2]: output['weights'].append(list(i)[2]['weight']) else: weight_q_mark = False return [True,'success',output]
# Remove solitary (API as well as Mashup) nodes solitary = [n for n, d in G.degree_iter() if d == 0] G.remove_nodes_from(solitary) sortedConnectedComponents = sorted(nx.connected_components(G), key=len, reverse=True) cc = sortedConnectedComponents[0] subGraph = G.subgraph(cc) for n in subGraph.nodes(): del subGraph.node[n]['bipartite'] print >> sys.stderr, "cc is bipartite: ", bipartite.is_bipartite(subGraph) (apiPartition, mashupPartition) = bipartite.sets(subGraph) apiWeightedGraph = bipartite.collaboration_weighted_projected_graph( subGraph, mashupPartition) # Now only keep X times as many edges of the apiWeightedGraph edge2nodeRatio = 100 nrNodes = len(apiWeightedGraph.nodes()) maxNrEdges = nrNodes * edge2nodeRatio allEdgesSortedByWeight = sorted( [e for e in apiWeightedGraph.edges_iter()], key=lambda e: apiWeightedGraph.edge[e[0]][e[1]]['weight'], reverse=False) #print allEdgesSortedByWeight cnt = 0