Beispiel #1
0
 def test_bipartite_density(self):
     G=nx.path_graph(5)
     X,Y=bipartite.sets(G)
     density=float(len(G.edges()))/(len(X)*len(Y))
     assert_equal(bipartite.density(G,X),density)
     D = nx.DiGraph(G.edges())
     assert_equal(bipartite.density(D,X),density/2.0)
     assert_equal(bipartite.density(nx.Graph(),{}),0.0)
Beispiel #2
0
 def test_bipartite_density(self):
     G = nx.path_graph(5)
     X, Y = bipartite.sets(G)
     density = float(len(list(G.edges()))) / (len(X) * len(Y))
     assert_equal(bipartite.density(G, X), density)
     D = nx.DiGraph(G.edges())
     assert_equal(bipartite.density(D, X), density / 2.0)
     assert_equal(bipartite.density(nx.Graph(), {}), 0.0)
Beispiel #3
0
def main():
    for path in files:
        print(path)
        B = nx.Graph()
        G = nx.read_gml('graphs/' + path)
        B.add_nodes_from(relevant_subreddits, bipartite=0)
        nodeList = set([n for n in G.nodes()])
        for reddit in relevant_subreddits:
            if reddit in nodeList:
                nodeList.remove(reddit)
        B.add_nodes_from(list(nodeList), bipartite=1)
        # print(nx.number_of_nodes(G))
        # B.add_edges_from([e for e in G.edges()])
        edgeList = []
        for e in G.edges():
            if e[0] not in relevant_subreddits and e[
                    1] not in relevant_subreddits:
                continue
            edgeList.append(e)
        B.add_edges_from(edgeList)
        # print(nx.number_of_edges(G))
        # print(bipartite.is_bipartite(B))
        # print(bipartite.average_clustering(B))

        result = random.sample(list(nodeList), 500)

        print('Clustering Coefficient')
        print(bipartite.average_clustering(B, result))
        print('Density')
        print(bipartite.density(B, result))
Beispiel #4
0
def generate_random_2mode(G):
    top_nodes = {n for n, d in G.nodes(data=True) if d['bipartite'] == 0}
    bot_nodes = set(G) - top_nodes
    size = G.size()
    density = bp.density(G, top_nodes)
    #return bp.gnmk_random_graph(len(top_nodes), len(bot_nodes), size)
    return bp.random_graph(len(top_nodes), len(bot_nodes), density)
Beispiel #5
0
def summay_results(nets=None, years=None):
    if years is None:
        years = default_years
    if nets is None:
        nets = networks_by_year()
    result = {}
    previous_devs = None
    for year, G in zip(years, nets):
        result[year] = {}
        devs = set(n for n, d in G.nodes(data=True) if d['bipartite']==1)
        files = set(G) - devs
        result[year]['guido_in'] = u'Guido van Rossum' in G
        result[year]['density'] = bp.density(G, devs)
        cc = sorted(nx.connected_components(G), key=len, reverse=True)
        result[year]['cc'] = len(cc[0]) / float(G.order()) if cc else 0
        bcc = sorted(nx.biconnected_components(G), key=len, reverse=True)
        result[year]['bcc'] = len(bcc[0]) / float(G.order()) if bcc else 0
        result[year]['devs'] = len(devs)
        result[year]['files'] = len(files)
        result[year]['py_files'] = len([f for f in files if f.endswith('.py')])
        result[year]['c_files'] = len([f for f in files if f.endswith('.c') 
                                            or f.endswith('.h')])
        result[year]['doc_files'] = len([f for f in files if f.lower().endswith('.txt') 
                                            or f.endswith('.rst') 
                                            or f.endswith('.tex')])
        result[year]['weight'] = sum(nx.degree(G, devs, weight='weight').values())
        result[year]['added'] = sum(nx.degree(G, devs, weight='added').values())
        result[year]['deleted'] = sum(nx.degree(G, devs, weight='deleted').values())
        result[year]['edits'] = sum(nx.degree(G, devs, weight='edits').values())
        result[year]['sq_clustering'] = (sum(nx.square_clustering(G, devs).values()) 
                                            / float(len(devs)))
        if previous_devs is None:
            # First year
            result[year]['new_devs'] = len(devs)
            result[year]['continue_devs'] = 0
            result[year]['lost_devs'] = 0
        else:
            result[year]['new_devs'] = len(devs - previous_devs)
            result[year]['continue_devs'] = len(devs & previous_devs)
            result[year]['lost_devs'] = len(previous_devs - devs)
        previous_devs = devs
    return result
        GP = bipartite.projected_graph(G, nodes_1)
        print('Calculate Global properties for projected graph ' + type_proj)
        print("\n")
    else:
        print('Calculate Global properties for bipartite network')
        print("\n")

    if type_nx == 'bipartite':
        print("Nodes Number : " + str(G.number_of_nodes()))
        print("\n")
        print("Edges Number : " + str(G.number_of_edges()))
        print("\n")
        print('Calculating density ...')
        print("\n")
        print("Density ICD Nodes (Diseases): " +
              str(bipartite.density(G, nodes_0)))
        print("\n")
        print("Density ATC Nodes (Active Substances): " +
              str(bipartite.density(G, nodes_1)))
        print("\n")
        print('Calculating mean degree ...')
        print("\n")
        G_deg = nx.degree_histogram(G)
        G_deg_sum = [a * b for a, b in zip(G_deg, range(0, len(G_deg)))]
        print('average degree: {}'.format(
            sum(G_deg_sum) / G.number_of_nodes()))
        print("\n")
        print('Calculating mean clustering ...')
        print("\n")
        cluster_g = bipartite.clustering(G)
        scg = 0
Beispiel #7
0
 def test_bipartite_density(self):
     G = nx.path_graph(5)
     X, Y = bipartite.sets(G)
     density = float(len(G.edges())) / (len(X) * len(Y))
     assert_equal(bipartite.density(G, X), density)
Beispiel #8
0
 def test_bipartite_density(self):
     G=nx.path_graph(5)
     X,Y=bipartite.sets(G)
     density=float(len(G.edges()))/(len(X)*len(Y))
     assert_equal(bipartite.density(G,X),density)
def bipartite_analysis(members, prods, graph):
    print bipartite.density(graph, members)
    print bipartite.density(graph, prods)
    return bipartite.clustering(graph, members)
def bipartite_analysis(members, prods, graph):
    print bipartite.density(graph, members)
    print bipartite.density(graph, prods)
    return bipartite.clustering(graph, members)
labels = {}
for idx, node in enumerate(B.nodes()):
    labels[node] = node

nx.draw_networkx_labels(B, pos, labels=labels, font_size=11, ax=ax)
fig.set_facecolor('grey')
plt.savefig(
    "/Users/jennie/Documents/urban informatics/Network/cw1/labels_and_colors.png",
    facecolor=fig.get_facecolor())  # save as png
plt.show()

#--------------------------------------------------------------------------
# Calculate the density of the bipartite graph
#--------------------------------------------------------------------------
print(round(bipartite.density(B, bottom_nodes), 3))

#--------------------------------------------------------------------------
# Calculate the degree distribution of universities from the bipartite projected graph.
# We need to have a multigraph because it works for counting the mappings
#--------------------------------------------------------------------------
U = bipartite.weighted_projected_graph(B, bottom_nodes)
#--------------------------------------------------------------------------
# Calculate the degree for the top and bottom nodes of the bipartite graph
#--------------------------------------------------------------------------
import operator

degX, degY = bipartite.degrees(U, top_nodes, weight='weight')
#universites
a, b = zip(*degX)
degreeCount = collections.Counter(b)
Beispiel #12
0
        G.node[cytokine]['type'] = 'cytokine'
        G.node[cell]['bipartite'] = 0
        G.node[cytokine]['bipartite'] = 1
        
print("============== tolapc network ============")
print(G.number_of_edges())
print(G.number_of_nodes())
#print(nx.clustering(G))
#print(nx.degree(G))
print(nx.is_connected(G))
top_nodes = set(n for n,d in G.nodes(data=True) if d['bipartite']==0)
bottom_nodes = set(G) - top_nodes
print("cells", len(top_nodes))
print("cykines", len(bottom_nodes))
#print(top_nodes, bottom_nodes)
print(bipartite.density(G, top_nodes))
print(bipartite.density(G, bottom_nodes))
#print(bipartite.maximum_matching(G))
#print(bipartite.clustering(G))
#print(bipartite.node_redundancy(G))
print(nx.info(G))
print(nx.density(G))
#print(nx.degree_histogram(G))
print(nx.diameter(G))
print(nx.radius(G))
print(nx.center(G))

print()
print()

network2_file = sys.argv[2]
Beispiel #13
0
    def describe(self, extra=False):
        """
        Provides a summary of graph statistics. Includes basic statistics like the number of nodes, edges,
        denstiy, and the average degree for one mode. Prints a string that contains each of the items that make up the summary.
        Density is calculated using one of the modes of the original bipartite network graph.

        **Parameters** :

        > *extra* : `bool`

        >> Runs the low efficiency algorithms, which can be resource-intensive on large networks.
        >> Recommended maximum network size for the low efficiency algorithms is around 100 nodes.

        **Returns** : `string`

        > Returns the descriptive string that contains information about the `MultiGraphPlus` object.

        """
        mode1 = self.mode1
        mode2 = self.mode2
        density = bipartite.density(self, bipartite.sets(self)[0])
        edges = self.number_of_edges()
        nodes_mode1 = 0
        nodes_mode2 = 0
        for n in self.nodes():
            if self.node[n]['type'] == mode1:
                nodes_mode1 += 1
            elif self.node[n]['type'] == mode2:
                nodes_mode2 += 1

        descriptives_nodes = "This is a bipartite network of types '{}' and '{}'.\n " \
                             "{} nodes are of the type '{}'.\n " \
                             "{} nodes are of the type '{}'.\n".format(str(mode1), str(mode2), str(nodes_mode1),
                                                                       str(mode1), str(nodes_mode2), str(mode2))
        descriptives_edges = "There are {} edges.\n".format(str(edges))
        descriptives_density = "Density: {}.\n".format(str(density))
        descriptives = descriptives_nodes + descriptives_edges + descriptives_density

        if extra:
            # Note: for each mode of the bipartite graph, degree and betweenness centrality are the same.
            # Keeping them both makes it easy to compare them and make sure they are the same.
            degree_mode1 = bipartite.degree_centrality(self, bipartite.sets(self)[0])
            degree_mode2 = bipartite.degree_centrality(self, bipartite.sets(self)[1])
            degree_mode1 = list(degree_mode1.values())
            degree_mode2 = list(degree_mode2.values())
            degree_mode1 = np.mean(degree_mode1)
            degree_mode2 = np.mean(degree_mode2)
            betweenness_mode1 = bipartite.betweenness_centrality(self, bipartite.sets(self)[0])
            betweenness_mode1 = list(betweenness_mode1.values())
            betweenness_mode1 = np.mean(betweenness_mode1)
            betweenness_mode2 = bipartite.betweenness_centrality(self, bipartite.sets(self)[1])
            betweenness_mode2 = list(betweenness_mode2.values())
            betweenness_mode2 = np.mean(betweenness_mode2)
            g = nx.Graph(self)
            projection = bipartite.projected_graph(g, bipartite.sets(g)[0])
            transitivity = nx.transitivity(projection)
            descriptives_transitivity = "Transitivity: {}.\n".format(str(transitivity))
            descriptives_degree_centrality = "Mean Degree Centrality for '{}': {}.\n" \
                                             "Mean Degree Centrality for '{}': {}.\n".format(str(mode1),
                                                                                             str(degree_mode1),
                                                                                             str(mode2),
                                                                                             str(degree_mode2))
            descriptives_btwn_centrality = "Mean Betweenness Centrality for '{}': {}.\n"\
                                           "Mean Betweenness Centrality for '{}': {}.\n".format(str(mode1),
                                                                                                str(betweenness_mode1),
                                                                                                str(mode2),
                                                                                                str(betweenness_mode2))
            descriptives = descriptives + descriptives_transitivity +\
                descriptives_degree_centrality + descriptives_btwn_centrality
        print(descriptives)
        return descriptives
Beispiel #14
0
pd.Series(video_counts).describe()

#########################
###  Build a network  ###
#########################

# One group is student IDs
# The other group is video IDs
G = nx.Graph()
G.add_nodes_from(students, bipartite=0)
G.add_nodes_from(videos, bipartite=1)
G.add_edges_from(
    list(zip(combined['useraccount_id'].values, combined['video_id'].values)))

cluster_all = bipartite.average_clustering(G)
density_all = bipartite.density(G)

# Get degrees for unprojected graph
student_degree = G.degree(students)
list_student_degree = [val for (node, val) in student_degree]
plt.hist(list_student_degree)
plt.xlabel('Number of Unique Videos')
plt.ylabel('Number of Students')
plt.show()
pd.Series(list_student_degree).describe()

video_degree = G.degree(videos)
list_video_degree = [val for (node, val) in video_degree]
plt.hist(list_video_degree)
plt.xlabel('Number of Unique Accessing Students')
plt.ylabel('Number of Videos')
#__________________________________________________________________
# Creates the nodes for the directors
# for dir in director:
#     for tit, dire in titdir.items(): # --> returns the movies with the same director
#          if dire == dir:
#             g.add_edge(tit, dir)
#             print(tit)
#g.add_nodes_from(director, bipartite = 1) # --> create and add nodes from the director list, with bipartite att = 1
#___________________________________________________________________
"""
Creates a bipartite graph for directors and movie titles
"""

g.remove_nodes_from(["Comforting Skin", "Way of Blue Sky"])

title_nodes, genre_nodes = bipartite.sets(g)

color_map = []

for node in g:
    if node in title_nodes:
        color_map.append('red')
    elif node in genre_nodes:
        color_map.append('green')

print(bipartite.density(g, genre_nodes))

nx.draw(g, node_color=color_map, with_labels=True)
plt.show()