Exemplo n.º 1
0
    def test_project_weighted_jaccard(self):
        edges = [
            ("A", "B", 2 / 5.0),
            ("A", "C", 1 / 2.0),
            ("B", "C", 1 / 5.0),
            ("B", "D", 1 / 5.0),
            ("B", "E", 2 / 6.0),
            ("E", "F", 1 / 3.0),
        ]
        Panswer = nx.Graph()
        Panswer.add_weighted_edges_from(edges)
        P = bipartite.overlap_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", 3 / 3.0),
            ("A", "E", 1 / 3.0),
            ("A", "C", 1 / 3.0),
            ("A", "D", 1 / 3.0),
            ("B", "E", 1 / 3.0),
            ("B", "C", 1 / 3.0),
            ("B", "D", 1 / 3.0),
            ("C", "D", 1 / 1.0),
        ]
        Panswer = nx.Graph()
        Panswer.add_weighted_edges_from(edges)
        P = bipartite.overlap_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"])
Exemplo n.º 2
0
    def test_project_weighted_jaccard(self):
        edges=[('A','B',2/5.0),
               ('A','C',1/2.0),
               ('B','C',1/5.0),
               ('B','D',1/5.0),
               ('B','E',2/6.0),
               ('E','F',1/3.0)]
        Panswer=nx.Graph()
        Panswer.add_weighted_edges_from(edges)
        P=bipartite.overlap_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',3/3.0),
               ('A','E',1/3.0),
               ('A','C',1/3.0),
               ('A','D',1/3.0),
               ('B','E',1/3.0),
               ('B','C',1/3.0),
               ('B','D',1/3.0),
               ('C','D',1/1.0)]
        Panswer=nx.Graph()
        Panswer.add_weighted_edges_from(edges)
        P=bipartite.overlap_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'])
Exemplo n.º 3
0
    def test_project_weighted_jaccard(self):
        edges=[('A','B',2/5.0),
               ('A','C',1/2.0),
               ('B','C',1/5.0),
               ('B','D',1/5.0),
               ('B','E',2/6.0),
               ('E','F',1/3.0)]
        Panswer=nx.Graph()
        Panswer.add_weighted_edges_from(edges)
        P=bipartite.overlap_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',3/3.0),
               ('A','E',1/3.0),
               ('A','C',1/3.0),
               ('A','D',1/3.0),
               ('B','E',1/3.0),
               ('B','C',1/3.0),
               ('B','D',1/3.0),
               ('C','D',1/1.0)]
        Panswer=nx.Graph()
        Panswer.add_weighted_edges_from(edges)
        P=bipartite.overlap_weighted_projected_graph(self.N,'ABCDE')
        assert_edges_equal(list(P.edges()),Panswer.edges())
        for u,v in P.edges():
            assert_equal(P[u][v]['weight'],Panswer[u][v]['weight'])
Exemplo n.º 4
0
    def test_project_weighted_overlap(self):
        edges = [('A', 'B', 2 / 2.0),
                 ('A', 'C', 1 / 1.0),
                 ('B', 'C', 1 / 1.0),
                 ('B', 'D', 1 / 1.0),
                 ('B', 'E', 2 / 3.0),
                 ('E', 'F', 1 / 1.0)]
        Panswer = nx.Graph()
        Panswer.add_weighted_edges_from(edges)
        P = bipartite.overlap_weighted_projected_graph(self.G, 'ABCDEF', jaccard=False)
        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', 3 / 3.0),
                 ('A', 'E', 1 / 1.0),
                 ('A', 'C', 1 / 1.0),
                 ('A', 'D', 1 / 1.0),
                 ('B', 'E', 1 / 1.0),
                 ('B', 'C', 1 / 1.0),
                 ('B', 'D', 1 / 1.0),
                 ('C', 'D', 1 / 1.0)]
        Panswer = nx.Graph()
        Panswer.add_weighted_edges_from(edges)
        P = bipartite.overlap_weighted_projected_graph(self.N, 'ABCDE', jaccard=False)
        assert_edges_equal(list(P.edges()), Panswer.edges())
        for u, v in list(P.edges()):
            assert P[u][v]['weight'] == Panswer[u][v]['weight']
Exemplo n.º 5
0
    def test_project_weighted_jaccard(self):
        edges = [
            ("A", "B", 2 / 5.0),
            ("A", "C", 1 / 2.0),
            ("B", "C", 1 / 5.0),
            ("B", "D", 1 / 5.0),
            ("B", "E", 2 / 6.0),
            ("E", "F", 1 / 3.0),
        ]
        Panswer = nx.Graph()
        Panswer.add_weighted_edges_from(edges)
        P = bipartite.overlap_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", 3 / 3.0),
            ("A", "E", 1 / 3.0),
            ("A", "C", 1 / 3.0),
            ("A", "D", 1 / 3.0),
            ("B", "E", 1 / 3.0),
            ("B", "C", 1 / 3.0),
            ("B", "D", 1 / 3.0),
            ("C", "D", 1 / 1.0),
        ]
        Panswer = nx.Graph()
        Panswer.add_weighted_edges_from(edges)
        P = bipartite.overlap_weighted_projected_graph(self.N, "ABCDE")
        assert_edges_equal(list(P.edges()), Panswer.edges())
        for u, v in P.edges():
            assert P[u][v]["weight"] == Panswer[u][v]["weight"]
Exemplo n.º 6
0
def jaccard_similarity(TG_graph, gen, tumor_gene_relation_bi_adj_mat):

    #for jaccard similarity nearest neighbors#

    G = bipartite.overlap_weighted_projected_graph(TG_graph, gen)
    print "calculating the link prediction scores", G.number_of_edges()
    A = nx.to_scipy_sparse_matrix(G)
    scores = np.dot(tumor_gene_relation_bi_adj_mat, A)
    return scores
 def overlap_weighted_projected_graph(self):
     E = bipartite.sets(self.B)[0]
     P = bipartite.overlap_weighted_projected_graph(self.B,
                                                    E,
                                                    jaccard=False)
     self.plot_graph_2(P, 'overlap_weighted_projected_graph')
     print('overlap_weighted_projected_graph: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)
     P = bipartite.overlap_weighted_projected_graph(self.B, E, jaccard=True)
     self.plot_graph_2(P, 'overlap_weighted_projected_graph_jaccard_True')
     print('xxxxxxxxxxxxxxxxxxxxxx')
     weights = []
     for i in list(P.edges(data=True)):
         weights.append(i[2]['weight'])
     print(weights)
Exemplo n.º 8
0
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
Exemplo n.º 9
0
#plt.semilogx()
plt.show()

# projection onto a given set of nodes, with weights equivalent to the Jaccard index
# Jaccard index is the ratio of the size of the intersection of the two neighborhoods
# to the union of the two neighborhoods
all_node_data = my_graph.nodes(data=True)

seg_nodes = []
for node in all_node_data:
	node_dict = node[1]
	if node_dict['cat']=='segment':
		seg_nodes.append(node[0])

#print(seg_nodes)
my_graph_projection = bipartite.overlap_weighted_projected_graph(my_graph, seg_nodes)
projected_edges = my_graph_projection.edges(data=True)

proj_list = []
with open('all_proj_edges.csv','w') as result_file:
	wr = csv.writer(result_file, dialect='excel')
	for p_edge in projected_edges:
		add_dis = [p_edge[0], p_edge[1], p_edge[2]['weight']]
		wr.writerow(add_dis)

print("The clustering coefficient of the projection is", nx.average_clustering(my_graph_projection))

print("The shortest average path length for each component in the projection is")
for g in nx.connected_component_subgraphs(my_graph_projection):
	print(nx.average_shortest_path_length(g))
Exemplo n.º 10
0
    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]
Exemplo n.º 11
0
nodes = pd.read_csv(options.input_filename, sep='\t', header=None)

B = Graph()
for row in nodes.iterrows():
    B.add_node(row[1][0], bipartite=0)
    B.add_node(row[1][1], bipartite=1)
    B.add_edge(row[1][0], row[1][1])

top_nodes = set(n for n, d in B.nodes(data=True) if d['bipartite'] == 0)
bottom_nodes = set(B) - top_nodes

top = list(top_nodes)
bottom = list(bottom_nodes)

print "Generating network projection"

if options.metric == "hypergeometric":
    G = bipartite.generic_weighted_projected_graph(
        B, top_nodes, weight_function=hypergeometric)  #HYPERGEOMETRIC
elif options.metric == "jaccard":
    G = bipartite.generic_weighted_projected_graph(
        B, top_nodes, weight_function=jaccard)  #Jaccard
elif options.metric == "PCC":
    G = bipartite.generic_weighted_projected_graph(
        B, top_nodes, weight_function=pcc_weight)  #PCC
elif options.metric == "simpson":
    G = bipartite.overlap_weighted_projected_graph(B, top_nodes,
                                                   jaccard=False)  #Simpson

write_weighted_edgelist(G, options.output_filename, delimiter="\t")
print "Execution finished"
# Create co-affiliation network
G = bipartite.projected_graph(B, pollinators)
# Create figure
plt.figure(figsize=(30,30))
# Calculate layout
pos = nx.spring_layout(G, k=0.5)
# Draw edges, nodes, and labels
nx.draw_networkx_edges(G, pos, width=3, alpha=0.2)
nx.draw_networkx_nodes(G, pos, node_color="#9f9fff", node_size=6000)
nx.draw_networkx_labels(G, pos)
plt.savefig('output-4.3.png', dpi=150)

G = bipartite.weighted_projected_graph(B, plants) 
list(G.edges(data=True))[0]

# Create co-affiliation network
G = bipartite.overlap_weighted_projected_graph(B, pollinators) 
# Get weights
weight = [G.edges[e]['weight'] for e in G.edges]
# Create figure
plt.figure(figsize=(30,30))
# Calculate layout
pos = nx.spring_layout(G, weight='weight', k=0.5)
# Draw edges, nodes, and labels
nx.draw_networkx_edges(G, pos, edge_color=weight, edge_cmap=plt.cm.Blues, width=6, alpha=0.5)
nx.draw_networkx_nodes(G, pos, node_color="#9f9fff", node_size=6000)
nx.draw_networkx_labels(G, pos)
plt.savefig('output-4.4.png', dpi=150)