def augmentNodes(g):
    r1 = nx.eigenvector_centrality_numpy(g)
    r2 = nx.degree_centrality(g) # DP MY
    r3 = nx.betweenness_centrality(g)
    r5 = nx.load_centrality(g,weight='weight') # DY, WY-writename # Scientific collaboration networks: II. Shortest paths, weighted networks, and centrality, M. E. J. Newman, Phys. Rev. E 64, 016132 (2001).
    r6 = nx.pagerank(g, alpha=0.85, personalization=None, max_iter=100, tol=1e-08, nstart=None, weight='weight')
    
    if nx.is_directed(g) == True:
        r8 = nx.in_degree_centrality(g)
        r9 = nx.out_degree_centrality(g)
#        r10 = nx.hits(g, max_iter=100, tol=1e-08, nstart=None)
    else:
        r4 = nx.communicability_centrality(g)
        r7 = nx.clustering(g, weight='weight')
        
    for x in g.nodes():
        g.node[x]['eigenvector_centrality_numpy'] = r1[x]
        g.node[x]['degree_centrality'] = r2[x]  
        g.node[x]['betweenness_centrality'] = r3[x]
        g.node[x]['load_centrality'] = r5[x]  
        g.node[x]['pagerank'] = r6[x]

        if nx.is_directed(g) == True:
            g.node[x]['in_degree_centrality'] = r8[x]
            g.node[x]['out_degree_centrality'] = r9[x]
#            g.node[x]['hits'] = r10[x]
        else:
            g.node[x]['communicability_centrality'] = r4[x]
            g.node[x]['clustering'] = r7[x]
    return g        
Ejemplo n.º 2
0
 def net_stat(self):
     '''
     This method is called on every rank with graphs that contain only partial connectivity information.
     If there are two equal structural out-degree hubs this method only finds the first one.
     '''
     excin=networkx.in_degree_centrality(networkx.DiGraph(self.global_ecm))
     excout=networkx.in_degree_centrality(networkx.DiGraph(self.global_ecm))
     return (excin,excout)
Ejemplo n.º 3
0
    def test_small_graph_centrality(self):
        G = nx.empty_graph(create_using=nx.DiGraph)
        assert_equal({}, nx.degree_centrality(G))
        assert_equal({}, nx.out_degree_centrality(G))
        assert_equal({}, nx.in_degree_centrality(G))

        G = nx.empty_graph(1, create_using=nx.DiGraph)
        assert_equal({0: 1}, nx.degree_centrality(G))
        assert_equal({0: 1}, nx.out_degree_centrality(G))
        assert_equal({0: 1}, nx.in_degree_centrality(G))
Ejemplo n.º 4
0
    def test_small_graph_centrality(self):
        G = nx.empty_graph(create_using=nx.DiGraph)
        assert {} == nx.degree_centrality(G)
        assert {} == nx.out_degree_centrality(G)
        assert {} == nx.in_degree_centrality(G)

        G = nx.empty_graph(1, create_using=nx.DiGraph)
        assert {0: 1} == nx.degree_centrality(G)
        assert {0: 1} == nx.out_degree_centrality(G)
        assert {0: 1} == nx.in_degree_centrality(G)
Ejemplo n.º 5
0
def main():

    DGTiny = parseEdgeFileToDiGraph(tinyFn)
    DGSmall = parseEdgeFileToDiGraph(smallFn)
    #    DGLarge = parseEdgeFileToDiGraph(largeFn)

    print nx.in_degree_centrality(DGTiny)
    print nx.out_degree_centrality(DGTiny)

    print "\n"

    print nx.closeness_centrality(DGTiny)
Ejemplo n.º 6
0
def add_network_statistics(nodes, links):
    if len(nodes)==0:
        return nodes
    graph = get_network(nodes, links)
    degree = nx.degree(graph)
    if max(dict(degree).values()) > 0:
        hubs, authorities = get_hits(graph)
        statistics = {
            'degree': degree,
            'in_degree': graph.in_degree(),
            'out_degree': graph.out_degree(),

            'degree_centrality': nx.degree_centrality(graph),
            'in_degree_centrality': nx.in_degree_centrality(graph),
            'out_degree_centrality': nx.out_degree_centrality(graph),
            'betweenness_centrality': nx.betweenness_centrality(graph),
            'closeness_centrality': nx.closeness_centrality(graph),
            'pagerank': get_pagerank(graph),
            'hubs': hubs,
            'authorities': authorities
        }
    else:
        statistics = {}

    # for relative in-degree we sort on date
    derive_date = lambda k: k['date'] if k['date']!='' else '{}-01-01'.format(k['year'])
    nodes.sort(key=derive_date, reverse=True)
    for i, node in enumerate(nodes):
        nodeid = node['id']
        for var in statistics.keys():
            node[var] = statistics[var][nodeid]
        if 'in_degree' in node:
            node['rel_in_degree'] = node['in_degree'] / float(max(i, 1))
    get_community(graph, nodes)
    return nodes
def show_net(g_matrix):
    G = nx.DiGraph()
    count = 0
    for i in range(len(g_matrix)):
        for j in range(len(g_matrix)):
            if g_matrix[i][j]>0.1:
                number = g_matrix[i][j]
                # G.add_edge([i,j,number])
                count+=1
                G.add_weighted_edges_from([(j, i, number)])

    nx.draw(G, pos=nx.spring_layout(G),node_color = 'b', edge_color = 'r',alpha = 0.5, with_labels = True,font_size = 15,
            node_size = 50,width = 0.5)
    in_degree = nx.in_degree_centrality(G)
    out_degree = nx.out_degree_centrality(G)
    i=0
    arr = np.zeros(shape=(36,3))
    while i<36:
        arr[i][0]=i
        if in_degree.__contains__(i):
            arr[i][1] = in_degree.get(i)
        else:
            arr[i][1] = 0.0
        if out_degree.__contains__(i):
            arr[i][2] = out_degree.get(i)
        else:
            arr[i][2] = 0.0
        i = i+1
    data = pd.DataFrame(data=arr,columns=['Motif ID','In-degree','Out-degree'])
    print(data)
    figname = "D:/For-F-drive/school/comp/research/Desktop/Email/Lasso_file/figure/trans_matrix_alpha-11_reduced_arrow.png"
    plt.savefig(figname, dpi=100, bbox_inches='tight')
    plt.show()
    return count
def caluclate_network_attributes(Graph):
    all_nodes = Graph.nodes()
    graph_df = pd.Series(all_nodes).to_frame(name="Node")
    degree = nx.degree(Graph)
    degree_cen = nx.degree_centrality(Graph)
    in_degree = nx.in_degree_centrality(Graph)
    out_degree = nx.out_degree_centrality(Graph)
    closeness = nx.closeness_centrality(Graph)
    between = nx.betweenness_centrality(Graph)
    try:
        eigen = nx.eigenvector_centrality_numpy(Graph)
        graph_df['Eigenvector'] = pd.Series(
            [eigen[node] for node in all_nodes])
    except eigenerror.ArpackNoConvergence:
        #if no eigenvector can be caluclated, set all to zero
        graph_df['Eigenvector'] = pd.Series([0 for n in all_nodes])
    graph_df['Degree'] = pd.Series([degree[node] for node in all_nodes])
    graph_df['DegreeCentrality'] = pd.Series(
        [degree_cen[node] for node in all_nodes])
    graph_df['InDegree'] = pd.Series([in_degree[node] for node in all_nodes])
    graph_df['OutDegree'] = pd.Series([out_degree[node] for node in all_nodes])
    graph_df['Closeness'] = pd.Series([closeness[node] for node in all_nodes])
    graph_df['Betweeness'] = pd.Series([between[node] for node in all_nodes])

    return (graph_df)
Ejemplo n.º 9
0
def plot_centralities(G):
    n = G.number_of_nodes()
    plt.figure('Graph %s:  %s  -  %s' %
               (str(i), tmsp2str(Tmin + i * dt), tmsp2str(Tmin +
                                                          (i + 1) * dt)))
    plt.suptitle('Centrality Measurements (Graph size = ' + str(n) + ')')

    in_degrees = [(n - 1) * d for d in nx.in_degree_centrality(G).values()]
    out_degrees = [(n - 1) * d for d in nx.out_degree_centrality(G).values()]
    degrees = [(n - 1) * d for d in nx.degree_centrality(G).values()]
    hist_plot('Degrees', [in_degrees, out_degrees, degrees], (3, 1, 1),
              ['r', 'g', 'b'])
    plt.legend(['Degree', 'In-Degree', 'Out-Degree'])

    G = nx.Graph(G)  #directed -> undirected
    hist_plot('Closeness',
              nx.closeness_centrality(G).values(), (3, 2, 3), 'xkcd:orangered')
    hist_plot('Betweenness',
              nx.betweenness_centrality(G).values(), (3, 2, 4), 'xkcd:crimson')
    hist_plot('Eigenvector',
              nx.eigenvector_centrality_numpy(G).values(), (3, 2, 5),
              'xkcd:teal')
    hist_plot('Katz',
              nx.katz_centrality_numpy(G).values(), (3, 2, 6), 'xkcd:brown')
    plt.tight_layout(rect=(0, 0, 1, 0.95))
    if args.PDF:
        pp.savefig()
        plt.close()
    else:
        plt.show()
Ejemplo n.º 10
0
    def ref_metrics(self, G):
        '''
        See https://networkx.github.io/documentation/latest/reference/algorithms.html for algorithms.
        Some algorithms don't support directed graphs. More comments on each algorithm in respect to directed graphs.
        Edges in graph G are directed from a to b in (a, b), where a cites b.
        :param G: graph
        :return: a dataframe of network statistics for each node.
        '''

        s = time.perf_counter()

        df = pd.DataFrame([  # Equals the number of references in a paper as (2, 1) is "2" citing "1"
                             nx.out_degree_centrality(G),
                             # Equals the number of citations
                             nx.in_degree_centrality(G),
                             # (1, 3) and (2, 3) give PageRank to "3"
                           nx.pagerank(G),
                           # (1, 2, 3) gives 0 to "3" as it can't reach any other nodes
                           # Since "if the graph is not completely connected, this algorithm computes the closeness
                           # centrality for each connected part separately," ensure that all papers are connected
                           nx.closeness_centrality(G),
                           # (2, 3, 4) and (4, 3, 2) give higher betweenness to "3" than (2, 3, 4) alone does
                           # nx.betweenness_centrality(G),
                           # nx.current_flow_betweenness_centrality(G),
                           # nx.current_flow_closeness_centrality(G),
                           # nx.eigenvector_centrality(G)
        ]).T
        df.columns = ['odc', 'idc', 'pr', 'cc',
                      # 'bc', 'cfbc', 'cfcc', 'ec'
        ]

        e = time.perf_counter()
        print("Metrics is computed in %d seconds" % round(e - s, 1))
        return df
Ejemplo n.º 11
0
def main():
    g_directed, g_undirected, all_dfs, labels = __read_csv_files()
    deg_centrality = nx.degree_centrality(g_directed)
    deg_in_centrality = nx.in_degree_centrality(g_directed)
    deg_out_centrality = nx.out_degree_centrality(g_directed)

    dict_measures = {
        'degree centrality': deg_centrality,
        'deg_in_centrality': deg_in_centrality,
        'deg_out_centrality': deg_out_centrality
    }

    count = 0
    for hist_title, values in dict_measures.items():
        count += 1
        subplot(2, 3, count)
        values_df = pd.DataFrame(values.items(), columns=['id', 'Score'])
        hist_plot = values_df['Score'].hist(bins=50)
        hist_plot.set_title(hist_title)
        hist_plot.set_xlabel('Score')
        hist_plot.set_ylabel("Number of nodes")
        plt.margins(x=0)
        plt.yscale('log', basey=10)
        # plt.yscale('log', basey=10)
    plt.show()
    measures_for_centrality(g_undirected)
    x = 1
Ejemplo n.º 12
0
def centrality(G):
    """ Calculates the in-degree, out-degree, closeness, betweenness centrality
	for the given graph. If the graph is undirected, return empty dictionary for indegree
	and outdegree centrality.

	args:
		G (nx.DiGraph) : input graph

	returns:
		tuple of in-degree, out-degree, closeness, betweenness centrality dictionaries
		that the keys are nodes.

	"""
    in_degree, out_degree = {}, {}

    if G.is_directed():
        print "calculating in_degree centrality..."
        in_degree = nx.in_degree_centrality(G)
        print "calculating out_degree centrality..."
        out_degree = nx.out_degree_centrality(G)

    print "calculating closeness centrality..."
    closeness = nx.closeness_centrality(G)
    print "calculating betweenness centrality..."
    betweenness = nx.betweenness_centrality(G)

    return in_degree, out_degree, closeness, betweenness
Ejemplo n.º 13
0
def set_capacities_degree_gravity(topology, capacities, capacity_unit='Mbps'):
    """
    Set link capacities proportionally to the product of the degrees of the
    two end-points of the link

    Parameters
    ----------
    topology : Topology
        The topology to which link capacities will be set
    capacities : list
        A list of all possible capacity values
    capacity_unit : str, optional
        The unit in which capacity value is expressed (e.g. Mbps, Gbps etc..)
    """
    if topology.is_directed():
        in_degree = nx.in_degree_centrality(topology)
        out_degree = nx.out_degree_centrality(topology)
        gravity = {(u, v): out_degree[u] * in_degree[v]
                   for (u, v) in topology.edges()}
    else:
        degree = nx.degree_centrality(topology)
        gravity = {(u, v): degree[u] * degree[v]
                   for (u, v) in topology.edges()}
    _set_capacities_proportionally(topology, capacities, gravity,
                                   capacity_unit=capacity_unit)
Ejemplo n.º 14
0
def run():
    # Create real world graph or random graph
    G = nx.erdos_renyi_graph(station_count, edge_prob, directed=True)
    # Calculate in-degree centrality to represent flow of bikes to centre
    cent = nx.in_degree_centrality(G)
    # Add centrality values to each node
    for u in G.nodes():
        G.node[u]['in_cent'] = cent[u]

    # Get order of centrality with most central at start
    cent_list = centrality_list(cent)
    print(cent_list)

    # Set up each station at start of run
    bikes_init(G)

    # Run program for number of steps
    for i in range(nsteps):
        am_cycle(G, cent_list)
        empty_list = [(n, G.node[n]['in_cent'], G.node[n]['empty'])
                      for n in G.nodes() if G.node[n]['empty'] >= 1]
        full_list = [(n, G.node[n]['in_cent'], G.node[n]['full'])
                     for n in G.nodes() if G.node[n]['full'] >= 1]
        # Trucks can move bikes from full stations to less full stations
        bike_trucks(G, 2, 10, cent_list)
        print("Full Count: %s\nEmpty Count %s" % (empty_list, full_list))
Ejemplo n.º 15
0
def centrality_histogram(g, c):  # Creates the centrality histogram specified
    if c == 'degree':
        degree_sequence = sorted(
            [val for key, val in nx.degree_centrality(g).items()])
    elif c == 'in_degree':
        degree_sequence = sorted(
            [val for key, val in nx.in_degree_centrality(g).items()])
    elif c == 'out_degree':
        degree_sequence = sorted(
            [val for key, val in nx.out_degree_centrality(g).items()])
    elif c == 'closeness':
        degree_sequence = sorted(
            [val for key, val in nx.closeness_centrality(g).items()])
    elif c == 'betweenness':
        degree_sequence = sorted(
            [val for key, val in nx.betweenness_centrality(g).items()])
    elif c == 'eigenvector':
        degree_sequence = sorted(
            [val for key, val in nx.eigenvector_centrality(g).items()])
    elif c == 'katz':
        degree_sequence = sorted(
            [val for key, val in nx.katz_centrality(g).items()])
    degree_count = col.Counter(degree_sequence)
    deg, cnt = zip(*degree_count.items())
    plt.bar(deg, cnt, width=0.01, color='b')
    plt.title('Degree Histogram')
    plt.ylabel('Count')
    plt.xlabel('Degree')
    plt.show()
Ejemplo n.º 16
0
def compute_graph_scores():
    f = open('paper_citation_network.txt', 'r')
    gs = open('graph_scores.txt', 'w')
    citations = f.readlines()
    edges = []
    for citation in citations:
        papers = citation.split('==>')
        papers[0] = papers[0].rstrip(' ')
        papers[1] = papers[1].rstrip('\n')
        papers[1] = papers[1].lstrip(' ')
        edges.append(papers)

    G = nx.DiGraph()
    G.add_edges_from(edges)

    cc = nx.closeness_centrality(G)
    bc = nx.betweenness_centrality(G)
    eg = nx.eigenvector_centrality(G, max_iter=1000)
    ig = nx.in_degree_centrality(G)

    for i in cc.keys():
        gs.write(i + ',' + str(cc[i]) + ',' + str(bc[i]) + ',' + str(eg[i]) +
                 ',' + str(ig[i]) + '\n')
    gs.close()
    f.close()
def calculate_network_measures(G):
    in_degree = nx.in_degree_centrality(G)
    out_degree = nx.out_degree_centrality(G)
    betweenness = nx.betweenness_centrality(G, weight=WEIGHT)
    closeness = nx.closeness_centrality(G, distance=WEIGHT)
    eigenvector = nx.eigenvector_centrality(G.reverse(), weight=WEIGHT)
    clustering = nx.clustering(G.to_undirected(), weight=WEIGHT)
    pagerank = nx.pagerank(G, weight=WEIGHT)
    hubs, authorities = nx.hits_numpy(G)
    max_clique = node_clique_number(G.to_undirected())

    node_cliques = cliques_containing_node(G.to_undirected())
    node_cliques_count = {}
    for node, cliques in node_cliques.items():
        node_cliques_count[node] = len(cliques)

    network_df = pd.DataFrame(list(G.nodes), columns=[ID]);

    network_df[IN_DEGREE] = network_df[ID].map(in_degree)
    network_df[OUT_DEGREE] = network_df[ID].map(out_degree)
    network_df[BETWEENNESS] = network_df[ID].map(betweenness)
    network_df[CLOSENESS] = network_df[ID].map(closeness)
    network_df[EIGENVECTOR] = network_df[ID].map(eigenvector)
    network_df[CLUSTERING] = network_df[ID].map(clustering)
    network_df[PAGERANK] = network_df[ID].map(pagerank)
    network_df[HUBS] = network_df[ID].map(hubs)
    network_df[AUTHORITIES] = network_df[ID].map(authorities)
    network_df[MAX_CLIQUE] = network_df[ID].map(max_clique)
    network_df[CLIQUES_COUNT] = network_df[ID].map(node_cliques_count)

    return network_df
Ejemplo n.º 18
0
def compute_centrality(graph):
    centrality_values = nx.hits(graph)
    for node_id, centrality in centrality_values[0].items():
        graph.nodes[node_id]['hub'] = centrality
    for node_id, centrality in centrality_values[1].items():
        graph.nodes[node_id]['authority'] = centrality

    centrality_values = nx.pagerank(graph)
    for node_id, centrality in centrality_values.items():
        graph.nodes[node_id]['pagerank'] = centrality

    centrality_values = nx.in_degree_centrality(graph)
    for node_id, centrality in centrality_values.items():
        graph.nodes[node_id]['in_degree'] = centrality

    centrality_values = nx.out_degree_centrality(graph)
    for node_id, centrality in centrality_values.items():
        graph.nodes[node_id]['out_degree'] = centrality

    centrality_values = nx.closeness_centrality(graph)
    for node_id, centrality in centrality_values.items():
        graph.nodes[node_id]['closeness'] = centrality

    centrality_values = nx.betweenness_centrality(graph)
    for node_id, centrality in centrality_values.items():
        graph.nodes[node_id]['betweenness'] = centrality

    centrality_values = nx.pagerank(graph)
    for node_id, centrality in centrality_values.items():
        graph.nodes[node_id]['pagerank'] = centrality
Ejemplo n.º 19
0
 def centralities(self, G):
     c = {
         "in-degree": nx.in_degree_centrality(G),
         "betweenness": nx.betweenness_centrality(G),
         "closeness": nx.closeness_centrality(G)
     }
     return c
def centrality_algorithms(graph):

    # Centrality functions return a dictionary of values
    # Calculate the maximum and print node name with value
    # Value stays the same for closness centrality, but the node itself changes
    centrality_dict = nx.degree_centrality(graph)
    print('Degree Centrality: ', max(centrality_dict, key=centrality_dict.get),
          max(centrality_dict.values()))
    centrality_dict = nx.in_degree_centrality(graph)
    print('In Degree Centrality: ',
          max(centrality_dict, key=centrality_dict.get),
          max(centrality_dict.values()))
    centrality_dict = nx.out_degree_centrality(graph)
    print('Out Degree Centrality: ',
          max(centrality_dict, key=centrality_dict.get),
          max(centrality_dict.values()))
    centrality_dict = nx.eigenvector_centrality_numpy(graph)
    print('Eigenvector Centrality: ',
          max(centrality_dict, key=centrality_dict.get),
          max(centrality_dict.values()))
    centrality_dict = nx.katz_centrality(graph)
    print('Katz Centrality: ', max(centrality_dict, key=centrality_dict.get),
          max(centrality_dict.values()))
    centrality_dict = nx.closeness_centrality(graph)
    print('Closeness Centrality: ',
          max(centrality_dict, key=centrality_dict.get),
          max(centrality_dict.values()))
    centrality_dict = nx.betweenness_centrality(graph)
    print('Betweenness Centrality: ',
          max(centrality_dict, key=centrality_dict.get),
          max(centrality_dict.values()))
Ejemplo n.º 21
0
def network_analysis(request):
    profile_name = request.GET.get('profile_name', '')

    if profile_name == '':
        profile_name = None

    users = MyUser.objects.all()

    print(users)

    pair_list = []
    avatar = None
    for user in users:
        followings = user.followings.all()
        for person in followings:
            pair = [user.profile_name, person.profile_name]
            print(pair)
            pair_list.append(pair)

    g = nx.DiGraph()

    g.add_edges_from(pair_list)

    for user in users:
        if user.profile_name not in g.nodes():
            g.add_node(user.profile_name)

    i_d = nx.in_degree_centrality(g)
    o_d = nx.out_degree_centrality(g)
    b = nx.betweenness_centrality(g)
    c = nx.closeness_centrality(g)
    e = nx.eigenvector_centrality(g, max_iter=1000)
    cc = nx.clustering(g)

    draw_graph(g.copy(), nx.spring_layout(g, k=0.55), i_d,
               'In Degree Centrality', profile_name)
    draw_graph(g.copy(), nx.spring_layout(g, k=0.55), o_d,
               'Out Degree Centrality', profile_name)
    draw_graph(g.copy(), nx.spring_layout(g, k=0.55), b,
               'Betweenness Centrality', profile_name)
    draw_graph(g.copy(), nx.spring_layout(g, k=0.55), c,
               'Closeness Centrality', profile_name)
    draw_graph(g.copy(), nx.spring_layout(g, k=0.55), e,
               'Eigenvector Centrality', profile_name)

    draw_network(g.copy(), nx.spring_layout(g, k=0.55))

    if profile_name is None:
        result = [i_d, o_d, b, c, e, cc]
    else:
        result = [
            i_d.get(profile_name, ''),
            o_d.get(profile_name, ''),
            b.get(profile_name, ''),
            c.get(profile_name, ''),
            e.get(profile_name, ''),
            cc.get(profile_name, '')
        ]

    return JsonResponse(result, safe=False)
Ejemplo n.º 22
0
def centrality(DG):
    in_degree_centrality = nx.in_degree_centrality(DG)
    out_degree_centrality = nx.out_degree_centrality(DG)
    with open('/home/sun/PycharmProjects/Network/in_degree_centrality.csv', 'w') as f:
        for k, v in in_degree_centrality.items():
            f.write(str(k) + ': ' + str(v) + '\n')
        f.close()

    with open('/home/sun/PycharmProjects/Network/out_degree_centrality.csv', 'w') as f:
        for k, v in out_degree_centrality.items():
            f.write(str(k) + ': ' + str(v) + '\n')
        f.close()


# def main():
#     data = '/home/sun/PycharmProjects/Network/C-elegans-frontal.txt'
#     # data = 'www.adj'
#     DG = create_network(data)
#
#     # draw_network(DG)
#     # clustering_coefficient(DG)
#     # centrality(DG)
#     degree_distribution(DG)
#
# if __name__ == '__main__':
#     main()
#
#     # DG = nx.DiGraph()
#     # DG.add_edge(1,2)
#     # print(DG.edges())
#     # # pos = nx.nx_agraph.graphviz_layout(DG)
#     # nx.draw_networkx(DG, pos = nx.spring_layout(DG))
#     # plt.show()
#     # plt.ishold()
#     # plt.draw(DG)
Ejemplo n.º 23
0
    def get_centrality_measures(node_list_df, arc_list_df):
        """
        A function that generates a range of centrality measures for a generated
        DiGraph from the Pandas dataframe

        :param arc_list_df: A data-frame containing a sources, targets, and weights
                            of relationship.
        """
        G = dataframe_to_networkx(arc_list_df)

        centrality_measures = {}

        # Degree-based centrality measure
        centrality_measures.update({'degree': nx.degree_centrality(G)})
        centrality_measures.update({'in_degree': nx.in_degree_centrality(G)})
        centrality_measures.update({'out_degree': nx.out_degree_centrality(G)})

        # Flow-based centrality measure
        centrality_measures.update({'closeness': nx.closeness_centrality(G)})
        centrality_measures.update(
            {'betweenness': nx.betweenness_centrality(G)})
        centrality_measures.update(
            {'contagion': contagion_centrality(node_list_df, arc_list_df)})

        return centrality_measures
Ejemplo n.º 24
0
def get_node_feat(adjacency_list, nNodes):

    num_node_feat = 5
    node_feat = Variable(
        torch.zeros(len(adjacency_list), nNodes, num_node_feat))

    for t in range(len(adjacency_list)):

        G = nx.DiGraph(adjacency_list[t].numpy())

        in_degree = np.array((nx.in_degree_centrality(G)).values())

        out_degree = np.array((nx.out_degree_centrality(G)).values())

        closeness = np.array((nx.closeness_centrality(G)).values())

        between = np.array((nx.betweenness_centrality(G)).values())

        pagerank = np.array((nx.pagerank(G)).values())

        to_stack = [in_degree, out_degree, closeness, between, pagerank]

        assert (len(to_stack) == num_node_feat)

        node_feat[t] = Variable(
            torch.from_numpy(np.stack(to_stack, 1)).float())

    return node_feat
def extract_g_metrics(G, name):

    if name == 'degree_centrality':
        metric = nx.degree_centrality(G).items()

    if name == 'in_degree_centrality':
        metric = nx.in_degree_centrality(G).items()

    if name == 'out_degree_centrality':
        metric = nx.out_degree_centrality(G).items()

    if name == 'eigenvector_centrality':
        metric = nx.eigenvector_centrality(G).items()

    if name == 'closeness_centrality':
        metric = nx.closeness_centrality(G).items()

    if name == 'betweenness_centrality':
        metric = nx.betweenness_centrality(G).items()

    if name == 'harmonic_centrality':
        metric = nx.harmonic_centrality(G).items()

    if name == 'trophic_levels':
        metric = nx.trophic_levels(G).items()

    fname = f'{graph_name}_{name}'
    get_g_metrics(metric,
                  name=fname)[['content_id',
                               fname]].to_feather(f'../save/{fname}.feather')
    return
Ejemplo n.º 26
0
def get_centrality(graph, method, topk=None):

    if method == "edge_betweeness_centrality":
        output = nx.edge_betweenness_centrality(graph)
    elif method == "betweenness_centrality":
        output = nx.betweenness_centrality(graph)
    elif method == "closeness_centrality":
        output = nx.closeness_centrality(graph)
    elif method == "eigenvector_centrality":
        output = nx.eigenvector_centrality(graph)
    elif method == "in_degree_centrality":
        output = nx.in_degree_centrality(graph)
    elif method == "out_degree_centrality":
        output = nx.out_degree_centrality(graph)
    elif method == "pagerank":
        output = pagerank(graph)
    else:
        return
    print(len(output))
    output = np.array(create_array(output))
    mean = round(np.mean(output), 4)
    if topk:
        arg_sorted_results = np.argsort(output)[::-1][:topk]
    else:
        arg_sorted_results = np.argsort(output)[::-1]

    return output, arg_sorted_results, mean
Ejemplo n.º 27
0
def main():
    # n = get_node_list('Output.txt')
    # save_mapper_file(n, 'Mapper.txt')
    # anonymize_names("Output.txt", 'AnOutput.txt')
    an = get_node_list('AnOutput.txt')

    G = create_graph('AnOutput.txt', True)
    #in_deg_res, out_deg_res = get_degree_counts(G, an)
    # print(in_deg_res, out_deg_res)
    G1 = create_graph_for_snap(an, 'AnOutput.txt')

    # snap_traids = snap.GetTriads(G1)
    # triads = nx.transitivity(G)

    pagerank = nx.pagerank(G)
    max_pagerank = key_with_max_val(pagerank)
    import operator
    a = sorted(pagerank.items(), key=operator.itemgetter(1), reverse=True)
    print max_pagerank

    centrality = nx.in_degree_centrality(G)
    a = sorted(centrality.items(), key=operator.itemgetter(1), reverse=True)

    eigen_vector_centrality = nx.eigenvector_centrality(G)
    a = sorted(eigen_vector_centrality.items(), key=operator.itemgetter(1), reverse=True)

    # snap_dia = snap.GetBfsFullDiam(G1, 10)
    # dia = nx.diameter(G)
    avg_local_clustering_coeff = nx.average_clustering(G)
    print avg_local_clustering_coeff
    #global_clustering_coeff = snap.GetClustCf(G1, -1)
    #print global_clustering_coeff
    #plot_data = diameter_phase_transition()
    pass
Ejemplo n.º 28
0
def set_capacities_degree_gravity(topology, capacities, capacity_unit='Mbps'):
    """
    Set link capacities proportionally to the product of the degrees of the
    two end-points of the link

    Parameters
    ----------
    topology : Topology
        The topology to which link capacities will be set
    capacities : list
        A list of all possible capacity values
    capacity_unit : str, optional
        The unit in which capacity value is expressed (e.g. Mbps, Gbps etc..)
    """
    if topology.is_directed():
        in_degree = nx.in_degree_centrality(topology)
        out_degree = nx.out_degree_centrality(topology)
        gravity = {(u, v): out_degree[u] * in_degree[v]
                   for (u, v) in topology.edges()}
    else:
        degree = nx.degree_centrality(topology)
        gravity = {(u, v): degree[u] * degree[v]
                   for (u, v) in topology.edges()}
    _set_capacities_proportionally(topology,
                                   capacities,
                                   gravity,
                                   capacity_unit=capacity_unit)
Ejemplo n.º 29
0
def plotNetworkGraph(year,waveMatrix,locations,nodeNames, filename, nodeSize,targetWeights):
	resolution='l'
	#Bmap_Amplitude = Basemap(projection='merc',llcrnrlon=lower_left_lon,llcrnrlat=lower_left_lat,urcrnrlon=upper_right_lon,urcrnrlat=upper_right_lat,lat_ts=0,resolution=resolution,suppress_ticks=True)
	cmap=plt.get_cmap("Reds")
	Bmap_Amplitude=Basemap(projection='merc',llcrnrlat=-61.4,urcrnrlat=71.56,\
			llcrnrlon=-163.7,urcrnrlon=165.65,lat_ts=20,resolution='h')
	G_Amplitude = nx.DiGraph()
	for name in nodeNames:
		latitude=locations[name][0]
		longitude=locations[name][1]
		#print(latitude,longitude)
		x,y=Bmap_Amplitude(longitude,latitude)
		G_Amplitude.add_node(name,pos=(x,y))
	   
	for nm in waveMatrix[year]:
		if nm[0] not in ['','.','0']:

			weight = waveMatrix[year][nm]
			G_Amplitude.add_edge(nm[0], nm[1],weight=weight)

	degree = nx.in_degree_centrality(G_Amplitude)
	betweennes = nx.betweenness_centrality(G_Amplitude)
	closeness = nx.closeness_centrality(G_Amplitude)
	pickle.dump(degree,open('centralityPickles/degree{}.pickle'.format(year),'wb'))
	pickle.dump(betweennes,open('centralityPickles/betweennes{}.pickle'.format(year),'wb'))
	pickle.dump(closeness,open('centralityPickles/closeness{}.pickle'.format(year),'wb'))

	maxDegree = max([j for i,j in degree.items()])
	maxDegreeNodes = ', '.join([i for i,j in degree.items() if j==maxDegree and j!=0.0])
	maxBetween = max([j for i,j in betweennes.items()])
	maxBetweenNodes = ', '.join([i for i,j in betweennes.items() if j==maxBetween and j!=0.0])
	maxClose = max([j for i,j in closeness.items()])
	maxCloseNodes = ', '.join([i for i,j in closeness.items() if j==maxClose and j!=0.0])

	plt.close()
	#plt.axis('off')
	fig, ax = plt.subplots()
	ax.text(-0.01, 1.02, 'c)', transform=ax.transAxes, size=14,color='purple')
	plt.scatter(-1,-1,label='Max Degree:{}'.format(maxDegreeNodes),color='r',s=3)
	plt.scatter(-1,-1,label='Max Betweeness:{}'.format(maxBetweenNodes),color='r',s=3)
	plt.scatter(-1,-1,label='Max Closeness:{}'.format(maxCloseNodes),color='r',s=3)

	plt.title('Target Network: {}'.format(year),fontsize = 9)
	edgewidth_Amplitude = [ d['weight'] for (u,v,d) in G_Amplitude.edges(data=True)]
	pos=nx.get_node_attributes(G_Amplitude,'pos')
	
	nx.draw_networkx_nodes(G_Amplitude,pos, node_color = 'y', node_size = nodeSize, alpha = 0.7)
	#nx.draw_networkx_edges(G_Amplitude, pos, edge_color = edgewidth_Amplitude, width=edgewidth_Amplitude, alpha = edgewidth_Amplitude)
	nx.draw_networkx_edges(G_Amplitude, pos, width=[0.5 for i in edgewidth_Amplitude] ,cmap=cmap,edge_color =  [cmap(i) for i in edgewidth_Amplitude], alpha = [cmap(0.95) for i in edgewidth_Amplitude])

	Bmap_Amplitude.drawcountries()
	Bmap_Amplitude.drawstates()
	#Bmap_Amplitude.bluemarble()
	Bmap_Amplitude.drawlsmask(land_color='white',ocean_color='grey',lakes=True)
	

	plt.legend(loc='lower right',prop={'size': 6})
	plt.axis('off')
	print(year)
	plt.savefig("paperImages/tar_{}.png".format(year), dpi = 600,bbox_inches='tight')
Ejemplo n.º 30
0
def run(G, csv_file):
    """
    Main function which initializes the graph and calls the number of steps
    Each step will simulate a number of people moving bikes in the system
    :param G NetworkX graph
    :param csv_file: CSV output file
    """

    # Calculate in-degree centrality to represent flow of bikes to centre
    cent = nx.in_degree_centrality(G)

    # Add centrality values to each node
    for u in G.nodes():
        G.node[u]['in_cent'] = cent[u]

    # Get order of centrality with most central at start
    cent_list = centrality_list(cent, am=True)

    # Set up each station at start of run
    bikes_init(G)

    # Get the number of nodes we want to consider as the centre
    centre_num = get_centre_count(cent_list, centre_flow, am=True)

    # Run program for number of steps
    for i in range(nsteps):
        bike_flow(G, cent_list, centre_num)
        #if i == nsteps - 1 or  i + 1 % 10 == 0:
        [csv_file.writerow((n, i+1, G.node[n]['in_cent'], G.node[n]['total'], G.node[n]['spaces'], G.node[n]['full'], G.node[n]['empty'])) for n in G.nodes()]
        empty_list = [(n, G.node[n]['in_cent'], G.node[n]['empty']) for n in G.nodes() if G.node[n]['empty'] >= 1]
        full_list = [(n, G.node[n]['in_cent'], G.node[n]['full']) for n in G.nodes() if G.node[n]['full'] >= 1]
        # Trucks can move bikes from full stations to less full stations
        bike_trucks(G, 101, 50, cent_list)
Ejemplo n.º 31
0
def main():
    db = ProtocolGraph(database.host, database.user, database.dbname)

    params = 'parameters: all | frame date time interval_seconds | \
iterall sim | iter sim interval'

    if len(sys.argv) < 2:
        print(params)
        quit()

    if sys.argv[1] == 'all':
        g = db.fetch_all()
        ec = networkx.in_degree_centrality(g)
        print(ec)
    elif sys.argv[1] == 'frame' and len(sys.argv) >= 5:
        date_str = sys.argv[2]
        time_str = sys.argv[3]
        secs = int(sys.argv[4])
        start = DateTime(date_str=date_str, time_str=time_str)
        g = db.fetch_frame(start, seconds=secs)
        ec = networkx.in_degree_centrality(g)
        print(ec)
    elif sys.argv[1] == 'iterall' and len(sys.argv) >= 3:
        sim_name = sys.argv[2]
        print(db.frame(sim_name))
        for g in db.iter(sim_name):
            (start_e, end_e) = start_end_epoch(g)
            print('start: {}, end: {}'.format(start_e, end_e))
            start_dt = DateTime(epoch=start_e)
            print('start utc: {}'.format(str(start_dt)))
            end_dt = DateTime(epoch=end_e)
            print('  end utc: {}'.format(str(end_dt)))
            ec = networkx.in_degree_centrality(g)
            print('In-degree centrality: {}'.format(ec))
    elif sys.argv[1] == 'iter' and len(sys.argv) >= 4:
        sim_name = sys.argv[2]
        print(db.frame(sim_name))
        interval = int(sys.argv[3])
        count = 0
        for g in db.iter(sim_name, seconds=interval):
            print('count: {}'.format(count))
            ec = networkx.in_degree_centrality(g)
            print(ec)
            count += 1
    else:
        print(params)
        quit()
def sna_calculations(g, play_file):
    """
    :param g: a NetworkX graph object
    :type g: object
    :param play_file: the location of a play in .txt format
    :type play_file: string
    :return: returns a dictionary containing various network related figures
    :rtype: dict
    :note: also writes into results/file_name-snaCalculations.csv and results/allCharacters.csv
    """
    file_name = os.path.splitext(os.path.basename(play_file))[0]
    sna_calculations_list = dict()
    sna_calculations_list['playType'] = file_name[0]
    sna_calculations_list['avDegreeCentrality'] = numpy.mean(numpy.fromiter(iter(nx.degree_centrality(g).values()),
                                                                            dtype=float))
    sna_calculations_list['avDegreeCentralityStd'] = numpy.std(
        numpy.fromiter(iter(nx.degree_centrality(g).values()), dtype=float))
    sna_calculations_list['avInDegreeCentrality'] = numpy.mean(
        numpy.fromiter(iter(nx.in_degree_centrality(g).values()), dtype=float))
    sna_calculations_list['avOutDegreeCentrality'] = numpy.mean(
        numpy.fromiter(iter(nx.out_degree_centrality(g).values()), dtype=float))

    try:
        sna_calculations_list['avShortestPathLength'] = nx.average_shortest_path_length(g)
    except:
        sna_calculations_list['avShortestPathLength'] = 'not connected'

    sna_calculations_list['density'] = nx.density(g)
    sna_calculations_list['avEigenvectorCentrality'] = numpy.mean(
        numpy.fromiter(iter(nx.eigenvector_centrality(g).values()), dtype=float))
    sna_calculations_list['avBetweennessCentrality'] = numpy.mean(
        numpy.fromiter(iter(nx.betweenness_centrality(g).values()), dtype=float))
    sna_calculations_list['DegreeCentrality'] = nx.degree_centrality(g)
    sna_calculations_list['EigenvectorCentrality'] = nx.eigenvector_centrality(g)
    sna_calculations_list['BetweennessCentrality'] = nx.betweenness_centrality(g)

    # sna_calculations.txt file
    sna_calc_file = csv.writer(open('results/' + file_name + '-snaCalculations.csv', 'wb'), quoting=csv.QUOTE_ALL,
                               delimiter=';')
    for key, value in sna_calculations_list.items():
        sna_calc_file.writerow([key, value])

    # all_characters.csv file
    if not os.path.isfile('results/allCharacters.csv'):
        with open('results/allCharacters.csv', 'w') as f:
            f.write(
                'Name;PlayType;play_file;DegreeCentrality;EigenvectorCentrality;BetweennessCentrality;speech_amount;AverageUtteranceLength\n')

    all_characters = open('results/allCharacters.csv', 'a')
    character_speech_amount = speech_amount(play_file)
    for character in sna_calculations_list['DegreeCentrality']:
        all_characters.write(character + ';' + str(sna_calculations_list['playType']) + ';' + file_name + ';' + str(
            sna_calculations_list['DegreeCentrality'][character]) + ';' + str(
            sna_calculations_list['EigenvectorCentrality'][character]) + ';' + str(
            sna_calculations_list['BetweennessCentrality'][character]) + ';' + str(
            character_speech_amount[0][character]) + ';' + str(character_speech_amount[1][character]) + '\n')
    all_characters.close()

    return sna_calculations
def indegree_centrality(graph_input, directed = False):
    if type(graph_input) == dict:
        graph = convert_graph_dict_to_nx_graph(graph_input, directed)
    else:
        graph = convert_graph_df_to_nx_graph(graph_input, directed)
    if len(graph) == 1:
        return defaultdict(lambda: 0, {})
    return defaultdict(lambda: 0, nx.in_degree_centrality(graph))
Ejemplo n.º 34
0
def sorting(E):
    in_degree_central = nx.in_degree_centrality(E)
    sorted(in_degree_central.items(), key=lambda x: x[1], reverse=True)[:10]

    out_degree_central = nx.out_degree_centrality(E)
    sorted(out_degree_central.items(), key=lambda x: x[1], reverse=True)[:10]
    print(in_degree_central)
    print(out_degree_central)
Ejemplo n.º 35
0
def get_indeg(G, first, last):

    indegCent = nx.in_degree_centrality(G)
    top_indegCent = sorted(indegCent.items(),
                           key=operator.itemgetter(1),
                           reverse=True)[first:last]
    return indegCent, top_indegCent, pd.DataFrame(
        top_indegCent, columns=['ScreenName', "In-degree"])
Ejemplo n.º 36
0
    def calculate_in_degree_centrality(self):
        """
        Calculates In - degree centrality for every node of graph.

        For directed graphs only.
        """
        values = nx.in_degree_centrality(self.graph)
        nx.set_node_attributes(self.graph, 'in_degree', values)
Ejemplo n.º 37
0
 def getclosenesscentrality(self):
     closeness_centrality = {'in': {}, 'out': {}} if self.is_directed else {}
     if self.is_directed:
         closeness_centrality['in'] = nx.in_degree_centrality(self.G)
         closeness_centrality['out'] = nx.out_degree_centrality(self.G)
     else:
         closeness_centrality = nx.degree_centrality(self.G)
     return closeness_centrality
Ejemplo n.º 38
0
def centrailtyM(A,num=5):
    G=nx.DiGraph(A)
    ranks=np.zeros((num,8))
    ranks[:,0]=np.argsort(nx.in_degree_centrality(G).values())[::-1][:num]
    ranks[:,1]=np.argsort(nx.closeness_centrality(G).values())[::-1][:num]
    ranks[:,2]=np.argsort(nx.betweenness_centrality(G).values())[::-1][:num]
    ranks[:,3]=np.argsort(nx.eigenvector_centrality_numpy(G).values())[::-1][:num]
    ranks[:,4]=np.argsort(nx.katz_centrality_numpy(G,weight=None).values())[::-1][:num]
    ranks[:,5]=np.argsort(nx.pagerank_numpy(G,weight=None).values())[::-1][:num]
    return ranks
Ejemplo n.º 39
0
def process_data(denom=100000, round=0):
	f = csv.reader(open("../applab_new_6.csv", 'rb'), delimiter=',')
	db = nx.DiGraph()
	full_users = set()
	i = 0
	uniquect = 0
	for line in f:
		if i % 100000 == 0 : print "processed", i, "lines"
		if i == 1000: break
		sender, receiver, date, time, duration, cost, location, region = map(lambda x: x.strip(), line)
		if sender not in full_users:
			uniquect += 1
			full_users.add(sender)
			if uniquect <= 2: #% denom - round == 0:
				db.add_node(sender)
				if db.has_node(receiver) == False:
					db.add_node(receiver)
		else:
			if db.has_node(receiver) == False:
				db.add_node(receiver)

		if db.has_edge(sender, receiver):
			db[sender][receiver]['weight'] += int(duration)
		else:
			db.add_edge(sender, receiver, weight=int(duration))
		i+=1
	#pickle.dump(db, open("users_networkx.p" % str(round), "wb"))
	#print "degree assortativity coeff:", nx.degree_assortativity_coefficient(db)
	#print "average degree connectivity:", nx.average_degree_connectivity(db)
	#	print "k nearest neighbors:", nx.k_nearest_neighbors(db)
	print "calculating deg cent"
	deg_cent = nx.degree_centrality(db) #sorted(nx.degree_centrality(db).items(), key=lambda x: x[1])
	print "calculating in deg cent"
	in_deg_cent = nx.in_degree_centrality(db) #sorted(nx.in_degree_centrality(db).items(), key=lambda x: x[1])
	print "calculating out deg cent"
	out_deg_cent = nx.out_degree_centrality(db) #sorted(nx.out_degree_centrality(db).items(), key=lambda x: x[1])
	print "closeness cent"
	closeness_cent = nx.closeness_centrality(db) #sorted(nx.closeness_centrality(db).items(), key=lambda x: x[1])
	#print "betweenness cent"
	#btwn_cent = nx.betweenness_centrality(db) #sorted(nx.betweenness_centrality(db).items(), key=lambda x: x[1])
	print "done"
	w = open("../output/user_network_stats.csv", 'w')
	w.write("uid,deg_cent,in_deg_cent,out_deg_cent,closeness_cent,btwn_cent\n")
	for user in deg_cent.keys():
		try:
			w.write("%s,%s,%s,%s,%s\n" % (user, deg_cent[user], in_deg_cent[user], out_deg_cent[user], closeness_cent[user]))
		except: pass
	w.close()
	print "drawing..."
	nx.draw(db)
	plt.savefig("path.pdf")
	print "done!"
	print "edge betweenness centrality:", nx.edge_betweenness_centrality(db)
	print "communicability:", nx.communicability(db)
	print "communicability centrality:", nx.communicability_centrality(db)
Ejemplo n.º 40
0
Archivo: graph.py Proyecto: jcccf/tlang
def graph_degree(g):
  print "Computing degree centrality..."
  ac = nx.degree_centrality(g)
  bc = nx.in_degree_centrality(g)
  ec = nx.out_degree_centrality(g)
  ac_hash, bc_hash, ec_hash = {}, {}, {}
  for n, b in ac.iteritems():
    ac_hash.setdefault(tuple(g.node[n]['languages']), []).append(b)
  for n, b in bc.iteritems():
    bc_hash.setdefault(tuple(g.node[n]['languages']), []).append(b)
  for n, b in ec.iteritems():
    ec_hash.setdefault(tuple(g.node[n]['languages']), []).append(b)
  return (ac_hash, bc_hash, ec_hash)
	def in_degree_centrality(self):

		#Compute the in degree centrality of the graph
		logging.info("Inside degree centrality")
		indegree_dict = nx.in_degree_centrality(self.G)
		logging.info("In degree dic length %s" % (len(indegree_dict.keys())))
	
		indegree_sorted_list = sorted(indegree_dict.items(), key=lambda x:x[1], reverse=True)[:3]
		
		for a,b in indegree_sorted_list:
			logging.info("In degree cent of %s is %s" % (a,b))
	
		indegree_dict = {}
def centralities_euclidean_norm_centrality(G, weight=None, distance=None):
    """Implementation of Euclidean norm of centralities as centrality measure
    
    Centrality measure is the Euclidean norm of the normalized Degree, Closeness and Betweenness centralities,
    as described in [1] .
    
    Parameters
    ----------
    G: networkx graph
        graph representing social network
    weight: string or None, optional
        weight edge attribute for betweenness centrality
    distance: string or Node, optional
        distance edge attribute for closeness centrality
        
    Returns
    -------
    nodes: dictionary
        Dictionary of nodes with euclidean norm score as value
    
    
    References
    ----------
    ..[1] Na Li; Gillet, D., "Identifying influential scholars in academic social media platforms,"
        in Advances in Social Networks Analysis and Mining (ASONAM), 
        2013 IEEE/ACM International Conference on , vol., no., pp.608-614, 25-28 Aug. 2013
    """
    
    #normalizing function
    #Parameters : x - value, di - dictionary
    #Returns : normalized value (x-min(di)) / (max(di)-min(di))
    normalizing=lambda x,di: ((x-min(di.values()))/(max(di.values())-min(di.values()))) if (max(di.values())-min(di.values()))!=0 else 0

    #degree centrality
    deg_dict=nx.in_degree_centrality(G)
    deg_dict={k:normalizing(v,deg_dict) for k,v in deg_dict.items()}
    
    #closeness centrality
    clo_dict=nx.closeness_centrality(G, distance=distance)
    clo_dict={k:normalizing(v,clo_dict) for k,v in clo_dict.items()}
    
    #betweenness centrality
    betw_dict=nx.betweenness_centrality(G, weight=weight)
    betw_dict={k:normalizing(v,betw_dict) for k,v in betw_dict.items()}
    
    #euclidean norm
    nodes={k: math.sqrt( (deg_dict[k])**2 + (clo_dict[k])**2 + (betw_dict[k])**2 ) for k in G.nodes()}
    
    return nodes
        
    
Ejemplo n.º 43
0
def compute_graph_parameters(graph,i=0):
    graph_parameters = {}
    if nx.is_directed(graph):
        graph_parameters['in_degree_centrality'] = nx.in_degree_centrality(graph)
        graph_parameters['out_degree_centrality'] = nx.out_degree_centrality(graph)
    else:
        graph_parameters['degree_centrality'] = nx.degree_centrality(graph)
    graph_parameters['closeness_centrality'] = nx.closeness_centrality(graph)
    graph_parameters['betweenness_centrality'] = nx.betweenness_centrality(graph)
    if i==0:
        graph_parameters['eigenvector_centrality'] = nx.eigenvector_centrality(graph)
    graph_parameters['pagerank_centrality'] = nx.pagerank(graph,alpha=0.85)
    graph_parameters['clustering'] = nx.clustering(graph.to_undirected())
    return graph_parameters
Ejemplo n.º 44
0
def output_indegree_centrality_info (graph, path, nodes_dict):
    """Output In-degree centrality information about the graph.
       graph : (networkx.Graph)
       path: (String) contains the path to the output file
       nodes_dict: (dictionary) maps node id to node name
    """
    indeg_dict = nx.in_degree_centrality(graph)
    indeg_dict = dict((nodes_dict[key], indeg_dict[key]) for key in nodes_dict if key in indeg_dict)
    indeg_list = dict_to_sorted_list(indeg_dict)

    with open(path, 'w') as out:
        out.write('***In-Degree Centrality***\n')
        out.write('Node\tLayer\tIn-degree centrality\n')
        for element in indeg_list:
            out.write('%d\t%d\t%f\n' % (element[0][0], element[0][1], element[1]))
def describe_graph(G):
    """Graph description"""

    # GRAPH DESCRIPTION
    graph_desc = pd.Series()
    # n. nodes
    graph_desc["number_of_nodes"] = G.number_of_nodes()
    # n. edges
    graph_desc["number_of_edges"] = G.number_of_edges()
    # n. of selfloops
    graph_desc["number_of_selfloops"] = len(G.selfloop_edges())

    # density
    graph_desc["average_shortest_path_length"] = nx.average_shortest_path_length(G)
    # connectivity
    # graph_desc.append(pd.Series(nx.degree_assortativity_coefficient(G), name="degree_assortativity_coefficient"))
    graph_desc["degree_pearson_correlation_coefficient"] = nx.degree_pearson_correlation_coefficient(G)

    # NODE DESCRIPTION
    node_desc = list()
    # n. of neighbours
    node_desc.append(pd.Series(G.degree(), name="degree"))
    node_desc.append(pd.Series(nx.average_neighbor_degree(G), name="average_neighbor_degree"))
    # n. of outgoing
    outgoing = pd.Series(G.in_degree(), name="in_degree")
    node_desc.append(outgoing)
    # n. of incoming
    incoming = pd.Series(G.out_degree(), name="out_degree")
    node_desc.append(incoming)
    # fold change out/in
    ratio = np.log2(outgoing + 1) - np.log2(incoming + 1)
    node_desc.append(pd.Series(ratio, name="out_in_degree_fold_change"))

    # centrality
    # degree based
    node_desc.append(pd.Series(nx.degree_centrality(G), name="degree_centrality"))
    node_desc.append(pd.Series(nx.in_degree_centrality(G), name="in_degree_centrality"))
    node_desc.append(pd.Series(nx.out_degree_centrality(G), name="out_degree_centrality"))
    # closest-path based
    # node_desc.append(pd.Series(nx.closeness_centrality(G), name="closeness_centrality"))
    # node_desc.append(pd.Series(nx.betweenness_centrality(G), name="betweenness_centrality"))
    # # eigenvector-based
    # node_desc.append(pd.Series(nx.eigenvector_centrality(G), name="eigenvector_centrality"))
    # node_desc.append(pd.Series(nx.katz_centrality_numpy(G), name="katz_centrality"))
    # # load-based
    # node_desc.append(pd.Series(nx.load_centrality(G), name="load_centrality"))

    return (graph_desc, pd.DataFrame(node_desc).T)
Ejemplo n.º 46
0
def analyzer_centrality(request, project):
    projects = Project.objects.all()
    obj = get_project(request, project)

    G = nx.DiGraph()
    nodes = obj.nodeset_set.all()
    links = obj.network_set.all()
    for node in nodes:
        G.add_node(node.idnumber, name=node.name)

    for link in links:
        G.add_edge(link.sourceID, link.targetID, weight=link.weight)

    indeg_centrality = nx.in_degree_centrality(G)
    outdeg_centrality = nx.out_degree_centrality(G)
    deg_centrality = nx.degree_centrality(G)
    closeness_centrality = nx.closeness_centrality(G)
    betweenness_centrality = nx.betweenness_centrality(G)

    for key, value in indeg_centrality.items():
        p = obj.nodeset_set.filter(idnumber=key)
        p.update(indegree_centrality=round(value, 3))

    for key, value in outdeg_centrality.items():
        p = obj.nodeset_set.filter(idnumber=key)
        p.update(outdegree_centrality=round(value, 3))

    for key, value in deg_centrality.items():
        p = obj.nodeset_set.filter(idnumber=key)
        p.update(degree_centrality=round(value, 3))

    for key, value in closeness_centrality.items():
        p = obj.nodeset_set.filter(idnumber=key)
        p.update(closeness_centrality=round(value, 3))

    for key, value in betweenness_centrality.items():
        p = obj.nodeset_set.filter(idnumber=key)
        p.update(betweenness_centrality=round(value, 3))

    nodeset = obj.nodeset_set.all()
    context = {'nodeset': nodeset,
               'object': obj,
               'projects': projects,
               }
    return render(request, 'centrality.html', context)
def getHugeStats(g):
    
    if nx.is_directed(g) == True:
        P1 = pd.DataFrame({'load_centrality': nx.load_centrality(g, weight='weight'),
                           'betweenness_centrality': nx.betweenness_centrality(g, weight='weight'),
                           
                           'pagerank': pd.Series(nx.pagerank(g, alpha=0.85, personalization=None, max_iter=100, tol=1e-08, nstart=None, weight='weight')),
                           'eigenvector_centrality': nx.eigenvector_centrality_numpy(g),
                           'degree_centrality': pd.Series(nx.degree_centrality(g)),
                           'in_degree_centrality': pd.Series(nx.in_degree_centrality(g)),
                           'out_degree_centrality': pd.Series(nx.out_degree_centrality(g))})
                           
    else:
        P1 = pd.Panel({'spl': pd.DataFrame(nx.shortest_path_length(g)),
                          'apdp': pd.DataFrame(nx.all_pairs_dijkstra_path(g)), 
                          'apdl': pd.DataFrame(nx.all_pairs_dijkstra_path_length(g)),
                          'c_exp': pd.DataFrame(nx.communicability_exp(g))})    
    return P1
Ejemplo n.º 48
0
def centralities(g):
    """ Computes graph centrality (betweenneess, indegree, closeness, pageRank)
            
        @type graph: networkx graph
        @param graph: graph
        
        @return: a matrix where each columns is a centrality and each row is a 
                node
    """
    bet = nx.betweenness_centrality(g, weight="weight")
    ind = nx.in_degree_centrality(g)
    cls = nx.closeness_centrality(g)
    prk = nx.pagerank(g)

    
    M = zip(bet, bet.values(), ind.values(), cls.values(), prk.values())
    
    return M
Ejemplo n.º 49
0
    def degree(self):
        """Compute in-degree centrality for words coded by Free Association.

        Returns
        -------
        degree : dict
            The association of each word to its in-degree. Each incoming link
            counts as 1 (i.e. link weights are ignored). Words with zero
            incoming links are removed from the dict.

        """

        # Assumes a directed unweighted graph.
        logger.info('Computing FreeAssociation degree')
        degree = nx.in_degree_centrality(self._norms_graph)
        self._remove_zeros(degree)
        logger.info('Done computing FreeAssociation degree')
        return degree
Ejemplo n.º 50
0
def analyze_graphs(graphs, days):
    undirected_graphs = list(map(lambda G: G.to_undirected(), graphs))
    graph_days = dict(zip(undirected_graphs, days))

    connected_graphs = list(filter(lambda G: nx.is_connected(G), undirected_graphs))
    connected_days = dict(zip(connected_graphs, list(map(
        lambda G: graph_days[G], connected_graphs))))

    metrics = {
        #"average_shortest_path_lengths": [lambda G: nx.average_shortest_path_length(G), connected_graphs, connected_days],
        "clustering": [lambda G: nx.average_clustering(G), undirected_graphs, graph_days],
        "average_neighbor_degree": [lambda G: nx.average_neighbor_degree(G), graphs, graph_days],
        "min_weighted_vertex_cover": [lambda G: len(min_weighted_vertex_cover(G)), undirected_graphs, graph_days],
        #"eccentricity": [lambda G: np.mean(nx.eccentricity(G).values()), connected_graphs, connected_days],
        #"diameter": [lambda G: nx.diameter(G), connected_graphs, connected_days],
        #"periphery": [lambda G: len(nx.periphery(G)), connected_graphs, connected_days],
        "degree_centralities": [lambda G: np.mean(nx.degree_centrality(G).values()), graphs, graph_days],
        "in_degree_centralities": [lambda G: np.mean(nx.in_degree_centrality(G).values()), graphs, graph_days],
        "out_degree_centralities": [lambda G: np.mean(nx.out_degree_centrality(G).values()), graphs, graph_days],
        "closeness_centralities": [lambda G: np.mean(nx.closeness_centrality(G).values()), graphs, graph_days],
        "betweenness_centralities": [lambda G: np.mean(nx.betweenness_centrality(G).values()), graphs, graph_days]
    }

    for metric in metrics:
        print("Analyzing {}...".format(metric))

        function = metrics[metric][0]
        which_graphs = metrics[metric][1]
        which_days = metrics[metric][2].values()
        yArray = list(map(function, which_graphs))

        print(which_days)
        print(yArray)

        plt.plot(which_days, yArray)
        plt.xlabel("Day")
        plt.ylabel(metric)
        plt.title("{} Over Time".format(metric))

        plt.savefig("{}_VS_Time.png".format(metric))
        plt.close()
def calculate_centrality_measures(G, create_using, directed):
    measures = []
    centrality_dict = {}

    #check for directed or undirected
    if (directed):
        centrality_dict['in_degree'] = nx.in_degree_centrality(G)
        centrality_dict['out_degree'] = nx.out_degree_centrality(G)
    else: 
        centrality_dict['degree'] = nx.degree_centrality(G)
    
    #print "Completed degree"

    #calculate harmonic if graph is disconnected
    if is_connected(G, directed):
        centrality_dict['closeness'] = nx.closeness_centrality(G)
    else:
        centrality_dict['harmonic'] = nx.harmonic_centrality(G)

    #print "Completed closeness_centrality"
    
    
    centrality_dict['betweenness'] = nx.betweenness_centrality(G)
    #print "Completed betweenness"

    centrality_dict['eigen'] = nx.eigenvector_centrality(G)

    centrality_dict['pagerank'] = nx.pagerank(G)

    G_prime = G
    if directed:
        G_prime = nx.read_edgelist(sys.argv[1], nodetype=int)
    
    centrality_dict['clustering'] = nx.clustering(G_prime)
    
    print_tsv(centrality_dict)
Ejemplo n.º 52
0
def degree_centrality(G):
    #degree_centrality
    dictionary_centrality = nx.in_degree_centrality(directed_G)
    return dictionary_centrality
Ejemplo n.º 53
0
def degree_centrality(G):
    dc = nx.in_degree_centrality(G)#degree
    dc1 = sorted(dc.iteritems(),key=itemgetter(1),reverse=True)#排序
    ndc = [e[0] for e in dc1]
    return ndc[:10]
 def test_indegree_centrality(self):
     d = nx.in_degree_centrality(self.G)
     exact = {0: 0.0, 1: 0.0, 2: 0.0, 3: 0.0, 4: 0.0, 
              5: 0.625, 6: 0.125, 7: 0.125, 8: 0.125}
     for n,dc in d.items():
         assert_almost_equal(exact[n], dc)
Ejemplo n.º 55
0
def add_in_degree_node(graf):
    print "Adding IN degree to nodes"
    d_dict = nx.in_degree_centrality(graf)
    nx.set_node_attributes(graf, 'deg_in', d_dict)
Ejemplo n.º 56
0
def createGraph(infile, seed, flag):
	data = loadData(infile)
	DG = nx.DiGraph()
	for node in data:
		DG.add_node(node)
		DG.node[node]['layer'] = data[node]['layer']
	
	for node in data:
		edges = [(node, ref) for ref in data[node]['refs'] if ref in data]
		DG.add_edges_from(edges)

	### Plot graph, assigning each layer a different color	
	outfile = "test_layer_{0}.png".format(flag)
	pos = nx.pygraphviz_layout(DG)
	colors = [DG.node[node]['layer'] for node in DG.nodes()]
	size_cm = 90
        size_sp = 180
        sizes = [size_cm + size_sp*(node==seed) for node in DG.nodes()]

	nx.draw_networkx(DG, pos, node_color = colors, cmap=plt.get_cmap('jet'), node_size=sizes, with_labels=False)
	plt.savefig(outfile)
	plt.clf()

	### Graphviz
	A = nx.to_agraph(DG)
	layer_of_nodes = []
	layer = 0
	
	sorted_nodes = sorted(data.items(), key = lambda x: x[1]['layer'])
	layer_of_nodes = [k[0] for k in sorted_nodes if k[1]['layer'] == layer]
	while (len(layer_of_nodes) > 0):
		A.add_subgraph(layer_of_nodes, rank='same')
		layer += 1
		layer_of_nodes = [k[0] for k in sorted_nodes if k[1]['layer'] == layer]
	outfile = "test2_{0}.png".format(flag)
	A.draw(outfile, prog='dot')	 									

	### Find Communities
	#seed = 7037476
	G = DG.to_undirected()
	partition = community.best_partition(G)
	num_of_communities = sorted(partition.values(), reverse=True)[0] + 1
	print "number of communities: ", num_of_communities

	# number of nodes in each communites
	values = [partition.get(node) for node in G.nodes()]
	seed_community = partition.get(seed)
	print "seed belongs to community ", seed_community
	seed_community_lst = [node for node in G.nodes() if partition.get(node) == seed_community]
	saveSeedCommunity(seed_community_lst, 'seed_community.p')
	counter = collections.Counter(values)
	print "number of nodes in each community: ", counter.most_common(num_of_communities)

	outfile = "test_communities_{0}.png".format(flag)
	pos = nx.pygraphviz_layout(DG)
	colors = [partition.get(node) for node in DG.nodes()]
	size_cm = 90
	size_sp = 180
	sizes = [size_cm + size_sp*(node==seed) for node in DG.nodes()]
	
	nx.draw_networkx(DG, pos, node_color = colors, cmap=plt.get_cmap('jet'), node_size = sizes, with_labels=False)
	#nx.draw_networkx_nodes(G, pos, nodelist=[seed], node_size=size_sp, node_color = [partition.get(seed)], with_labels=False)
	plt.savefig(outfile)
	plt.clf()

	
	### Draw community graph (each community is denoted as one node)
	CG = nx.DiGraph()
	keywords = community_keywords(data, partition)
	for i in range(num_of_communities):
		CG.add_node(i)
		CG.node[i]['keywords'] = keywords[i]	

	for i in range(num_of_communities):
		if i == partition.get(seed):
			CG.node[i]['distance'] = 0
		else:
			CG.node[i]['distance'] = community_distance(DG, partition, seed, i) 	

	print nx.get_node_attributes(CG, 'distance')
	nodes_distance_lst = sorted(CG.nodes(), key = lambda x: CG.node[x]['distance'], reverse=True)
	for i in range(len(nodes_distance_lst)):
                target = nodes_distance_lst[i]
                for j in range(i+1, len(nodes_distance_lst)):
                        source = nodes_distance_lst[j]
                        if community_direct_connectivity(DG, partition, source, target):
                                CG.add_edge(source, target)
	"""
	for source in CG.nodes():
		for target in CG.nodes():
			if source != target and community_connectivity(DG, partition, source, target):
				CG.add_edge(source, target)

	for e in CG.edges():
		for k in CG.nodes():
			if e[0] != k and (e[0], k) in CG.edges() and (k, e[1]) in CG.edges():
				CG.remove_edge(e[0], e[1])
				break
	"""

	print CG.edges() 
	outfile = "test_concentrate_{0}.png".format(flag)
        pos = nx.pygraphviz_layout(CG)
        colors = [node for node in CG.nodes()]
        size_cm = 180
        size_sp = 270
        sizes = [size_cm + size_sp*(node==partition.get(seed)) for node in CG.nodes()]

        nx.draw_networkx(CG, pos, node_color = colors, cmap=plt.get_cmap('jet'), node_size = sizes, with_labels=False)
        nx.draw_networkx_labels(CG, pos, labels = keywords)
	#nx.draw_networkx_nodes(G, pos, nodelist=[seed], node_size=size_sp, node_color = [partition.get(seed)], with_labels=False)
        plt.savefig(outfile)
        plt.clf()	

	### Find representatives in each community (naive way: based on degree centrality)
	degreeCentl = nx.in_degree_centrality(DG)
	print "representatives in each community: "
	community_index = 0
	representatives = []
	while(community_index < num_of_communities):
		nodes_lst = [node for node in DG if partition.get(node) == community_index]
		tmp = sorted(nodes_lst, key = lambda x:degreeCentl[x], reverse=True)[0:len(nodes_lst)/5]
		#tmp = sorted(nodes_lst, key = lambda x:degreeCentl[x], reverse=True)[0:1]
		print tmp
		representatives += tmp
		community_index += 1
	# add the seed node
	representatives.append(seed)

	# community representative graph
	RG = nx.DiGraph()
	for node in representatives:
		RG.add_node(node)
	
	nodes_time_lst = sorted(RG.nodes(), key = lambda x: getYear(data[x]['date']))
	for i in range(len(nodes_time_lst)):
		target = nodes_time_lst[i]
		for j in range(i+1, len(nodes_time_lst)):
			source = nodes_time_lst[j]
			if nx.has_path(DG, source, target):
				RG.add_edge(source, target)
				break
		
	# add direct reference edge
	#for node in nodes_time_lst:
	#	tmp = [k for k in data[node]['refs'] if k in nodes_time_lst]
	#	for ref in tmp:
	#		RG.add_edge(node, ref)
	"""						
	for source in representatives:
		for target in representatives:
			if source != target and nx.has_path(DG, source, target):
				RG.add_edge(source, target)
	
	edge_lst = RG.edges()
	for e in RG.edges():
		for k in representatives:
			if e[0] != k and (e[0], k) in RG.edges() and (k, e[1]) in RG.edges():
				RG.remove_edge(e[0], e[1])
				break
	"""	
	outfile = "representative_graph_{0}.png".format(flag)
	pos = nx.spring_layout(RG)
	size_cm = 180
	size_sp = 270
	colors = [partition.get(node) for node in RG]
	sizes = [size_cm + size_sp*(node==seed) for node in RG]
	nx.draw_networkx(RG, pos, node_color = colors, cmap=plt.get_cmap('jet'), node_size = sizes, with_labels = True)
	plt.savefig(outfile)
	plt.clf() 	
	
	# draw the "important" papers layer-by-layer
	layer = 0
	RG.node[seed]['layer'] = layer
	curr_layer = [seed]
	while(len(curr_layer) > 0):
		next_layer = list(set([e[1] for e in RG.edges() if e[0] in curr_layer]))
		layer += 1
		for node in next_layer:
			RG.node[node]['layer'] = layer
		curr_layer = next_layer
				
	A = nx.to_agraph(RG)
	layer = 0
	layer_of_nodes = [k for k in RG.nodes() if RG.node[k]['layer'] == layer]
	while (len(layer_of_nodes) > 0):
		A.add_subgraph(layer_of_nodes, rank='same')
		layer += 1
		layer_of_nodes = [k for k in RG.nodes() if RG.node[k]['layer'] == layer]
	outfile = "test3_{0}.png".format(flag)
	A.draw(outfile, prog='dot')
	"""
'''
Created on Oct 20, 2013

@author: dhanyatha.manjunath
'''
import networkx as nx
from operator import itemgetter
from collections import Counter

mygraph=nx.read_edgelist("E:/Social Media Mining/Project/Crawler Run/edge-list.txt", delimiter=',', create_using=nx.DiGraph(), data="false")

eigen_vec_cen=Counter(nx.eigenvector_centrality(mygraph,max_iter=300))
y=sorted(eigen_vec_cen.items(), key=itemgetter(1), reverse=True)

in_deg_ce=Counter(nx.in_degree_centrality(mygraph))
x=sorted(in_deg_ce.items(), key=itemgetter(1), reverse=True)

top_degree=Counter(x[:3])
print("Top three degree centrality nodes are")

for e in top_degree:

    print("Total degree is"+ str(mygraph.degree(e)))
    print("Indegree is"+str(mygraph.in_degree(e)))
    print("OutDegree is"+ str(mygraph.out_degree(e)))
  
    print("Degree Centrality=")
    print(top_degree[e[1]])
    print("Eigen vector centrality=")
    print(eigen_vec_cen[e[0]])
    print("\n")
Ejemplo n.º 58
0
def main():
    # 1.0 Loading a local data file
    # Load the Hartford drug users data, a directed binary graph.
    # We will specify that the graph be generated as a directed graph, and
    # that the nodes are integers (rather than strings)
    hartford=nx.read_edgelist("../../data/hartford_drug.txt",create_using=nx.DiGraph(),nodetype=int)
    nx.info(hartford)  # Check the the data has been loaded properly

    # 2.0 Connecting to a database

    # 3.0 Building a network directly from the Internet
    # WARNING: This can take a long time to run!
    seed="imichaeldotorg"   # Set the seed user within livejournal.com
    seed_url="http://"+seed+".livejournal.com"
    # 3.1 Scrape, parse and build seed's ego net
    sg=get_sg(seed_url)
    net,newnodes=create_egonet(sg)
    nx.write_pajek(net,"../../data/"+seed+"_ego.net") # Save data as Pajek
    nx.info(net)
    # 3.2 Perform snowball search, where k=2
    k=2
    for g in range(k):
        net,newnodes=snowball_round(net,newnodes)
        nx.write_pajek(net,"../../data/"+seed+"_step_"+str(g+1)+".net")
    
    # 4.0 Calculate in-degree centrality for Hartford data
    in_cent=nx.in_degree_centrality(hartford)
    
    # 5.0 Calculating multiple measures, and finding
    # most central actors
    hartford_ud=hartford.to_undirected()
    hartford_mc=nx.connected_component_subgraphs(hartford_ud)[0]    # First, extract MC
    # 5.1 Calculate multiple measures on MC
    bet_cen=nx.betweenness_centrality(hartford_mc)
    clo_cen=nx.closeness_centrality(hartford_mc)
    eig_cen=nx.eigenvector_centrality(hartford_mc)
    # 5.2 Find the actors with the highest centrality for each measure,
    print("Actor "+str(highest_centrality(bet_cen))+" has the highest Betweenness centrality")
    print("Actor "+str(highest_centrality(clo_cen))+" has the highest Closeness centrality")
    print("Actor "+str(highest_centrality(eig_cen))+" has the highest Eigenvector centrality")
    
    # 6.0 Calculating degree distribution
    ba_net=nx.barabasi_albert_graph(1000,2)   # Create a Barabasi-Albert network
    # 6.1 NX has a nice built-in function for degree distribution
    dh=nx.degree_histogram(ba_net)
    # 6.2 Plot using same method as http://networkx.lanl.gov/examples/drawing/degree_histogram.html
    pos=nx.spring_layout(ba_net)
    P.figure(figsize=(8,8))
    P.loglog(dh,'b-',marker='o')
    P.title("Degree rank plot (log-log)")
    P.ylabel("Degree")
    P.xlabel("Frequency")
    # 6.4 Draw graph in inset
    P.axes([0.45,0.45,0.45,0.45])
    P.axis('off')
    nx.draw_networkx_nodes(ba_net,pos,node_size=20)
    nx.draw_networkx_edges(ba_net,pos,alpha=0.4)
    P.savefig("../../images/figures/ba_10000.png")
    
    # 6.0 Finding community structure
    clus=nx.clustering(hartford_mc,with_labels=True)
    # 6.1 Get counts of nodes membership for each clustering coefficient
    unique_clus=list(unique(clus.values()))
    clus_counts=zip(map(lambda c: clus.values().count(c),unique_clus),unique_clus)
    clus_counts.sort()
    clus_counts.reverse()
    # 6.2 Create a subgraph from nodes with most frequent clustering coefficient
    mode_clus_sg=nx.subgraph(hartford_mc,[(a) for (a,b) in clus.items() if b==clus_counts[0][1]])
    P.figure(figsize=(6,6))
    nx.draw_spring(mode_clus_sg,with_labels=False,node_size=60,iterations=1000)
    P.savefig('../../images/networks/mode_clus_sg.png')
    
    # 7.0 Plot Eigenvector centrality vs. betweeness in matplotlib
    centrality_scatter(bet_cen,eig_cen,"../../images/figures/drug_scatter.png",ylab="Eigenvector Centrality",xlab="Betweenness Centrality",title="Hartford Drug Network Key Actor Analysis",reg=True)
    
    # 8.0 Outputting network data
    # First, output the data as an adjaceny list
    nx.write_adjlist(hartford_mc,"../../data/hartford_mc_adj.txt")
    # 8.1 Add metric data to the network object
    hartford_mc_met=add_metric(hartford_mc,eig_cen)
    print(hartford_mc_met.nodes(data=True)[1:10])   # Check the the data was stored
    # 8.2 output data using the Pajak format to save the node attribute data
    nx.write_pajek(hartford_mc,"../../data/hartford_mc_metric.net")    # NX will automatically add all attibute data to output
    
    # 9.0 Exporting data to a CSV file
    csv_data={"Betweeness":bet_cen,"Closeness":clo_cen,"Eigenvector":eig_cen}
    # 9.1 After we have all the data in a single dict, we send it to out function
    # to export the data as a CSV
    csv_exporter(csv_data,"../../data/drug_data.csv")
    
    # 10.0 Visualization basics
    # 10.1 Use subplots to draw random and circular layouts
    # of drug net side-by-side
    fig1=P.figure(figsize=(9,4))
    fig1.add_subplot(121)
    nx.draw_random(hartford_mc,with_labels=False,node_size=60)
    fig1.add_subplot(122)
    nx.draw_circular(hartford_mc,with_labels=False,node_size=60)
    P.savefig("../../images/networks/rand_circ.png")
    # 10.2 Draw spring, spectral layouts
    P.figure(figsize=(8,8))
    nx.draw_spring(hartford_mc,with_labels=False,node_size=60,iterations=10000)
    P.savefig("../../images/networks/spring.png")
    P.figure(figsize=(8,8))
    nx.draw_spectral(hartford_mc,with_labels=False,node_size=60,iterations=10)
    P.savefig("../../images/networks/spectral.png")
    # 10.3 Draw shell layout with inner-circle as the 25th percentile 
    # Eigenvector centrality actors
    P.figure(figsize=(8,8))
    # Find actors in 25th percentile
    max_eig=max([(b) for (a,b) in eig_cen.items()])
    s1=[(a) for (a,b) in eig_cen.items() if b>=.25*max_eig]
    s2=hartford_mc.nodes()
    # setdiff1d is a very useful NumPy function!
    s2=list(setdiff1d(s2,s1))       
    shells=[s1,s2]    
    # Calculate psotion and draw          
    shell_pos=nx.shell_layout(hartford_mc,shells)
    nx.draw_networkx(hartford_mc,shell_pos,with_labels=False,node_size=60)
    P.savefig("../../images/networks/shell.png")
    
    # 11.0 Adding analysis to visualization
    P.figure(figsize=(15,15))
    P.subplot(111,axisbg="lightgrey")
    spring_pos=nx.spring_layout(hartford_mc,iterations=1000)
    # 11.1 Use betweeneess centrality for node color intensity
    bet_color=bet_cen.items()
    bet_color.sort()
    bet_color=[(b) for (a,b) in bet_color]
    # 11.2 Use Eigenvector centrality to set node size
    eig_size=eig_cen.items()
    eig_size.sort()
    eig_size=[((b)*2000)+20 for (a,b) in eig_size]
    # 11.3 Use matplotlib's colormap for node intensity 
    nx.draw_networkx(hartford_mc,spring_pos,node_color=bet_color,cmap=P.cm.Greens,node_size=eig_size,with_labels=False)
    P.savefig("../../images/networks/analysis.png")
def make_net(centrality_name, in_path, out_path):
	#sample code
		#import _2_time_based_data_network_feature
		#make_net_in_path = "../3.time_based_data/1.cite_relation_devide/"
		#make_net_out_path = "../3.time_based_data/2.centrality_data/"
		#_2_time_based_data.make_net( "in_degree", make_net_in_path, make_net_out_path)

	#네트워크를 만들고 Centurality를 계산하고 저장할 것이다.
	import networkx as nx
	global Dump
	Dump = {}
	make_net_initialize(in_path)
	start_time = time.time()
	temp_start_time = time.time()

	print "=============		make_net start:" + centrality_name + "		=============="
	print "=============		from 1951 to 2015		=============="

	for year in range(1951, 2016):
		print year
		f_in = open(in_path + str(year) + "_cite.csv","r")
		lines = f_in.readlines()
		f_in.close()
		edge_list = []

		for line in lines:
			data = line.split(",")
			data_tuple = (data[0].strip(), data[1].strip())
			edge_list.append(data_tuple)

		Net = nx.DiGraph(edge_list)
		Cen_in = {}
		if (centrality_name == "in_degree"):
			Cen_in = nx.in_degree_centrality(Net)
		elif (centrality_name == "degree"):
			Cen_in = nx.degree_centrality(Net)
		elif (centrality_name == "eigenvector"):
			Cen_in = nx.eigenvector_centrality_numpy(Net)
		elif (centrality_name == "katz"):
			Cen_in = nx.katz_centrality(Net)
		elif (centrality_name == "pagerank"):
			Cen_in = nx.pagerank(Net)
		elif (centrality_name == "communicability"):
			Net = nx.Graph(edge_list)
			Cen_in = nx.communicability_centrality(Net)
		elif (centrality_name == "load"):
			Cen_in = nx.load_centrality(Net)
		
		for j in Cen_in:
			key = j
			val = Cen_in[j]
			Dump[key][year] = val

	#저장하는 코드 
	f_out = open(out_path + centrality_name +"_centrality.csv", "w")
	for key in Dump:
		line = str(key)
		for year in range(1951, 2016):
			data = Dump[key].get(year, 0)
			line = line + ","+ str(data)
		line = line + "\n"
		f_out.write(line)
	f_out.close()

	print "=============		make_net end			=============="
	print(centrality_name + "takes %s seconds" % (time.time() - temp_start_time))
	temp_start_time = time.time()
Ejemplo n.º 60
0
def main(argv):
    #Standardvalues
    partitionfile = "data/partitions/final_partitions_p100_200_0.2.csv"
    project = "584"
    to_pajek = False
    try:
      opts, args = getopt.getopt(argv,"p:s:o")
    except getopt.GetoptError:
      print 'group_bridging.py -p <project_name> -s <partitionfile> -o [if you want pajek output]'
      sys.exit(2)
    for opt, arg in opts:
        if opt in ("-p"):
            project = arg
        elif opt in ("-s"):
            partitionfile = arg
        elif opt in ("-o"):
             to_pajek = True
        else:
            print 'group_bridging.py -p <project_name> -s <partitionfile> -o [if you want pajek output]'
    
    print "##################### GROUP BRIDGING ########################"
    print "Project %s " % project
    print "Partition %s" % partitionfile
    
    ff_edges_writer = csv.writer(open("results/%s_ff_bridging_edges.csv" % project, "wb"))
    at_edges_writer = csv.writer(open("results/%s_at_bridging_edges.csv" % project, "wb"))
    rt_edges_writer = csv.writer(open("results/%s_rt_bridging_edges.csv" % project, "wb"))
    
    csv_bridging_writer = csv.writer(open('results/spss/group bridging/%s_group_bridging.csv' % project , 'wb'))
    
    csv_bridging_writer.writerow(["Project", "Name", "Member_count", "Competing_Lists",
                                "FF_bin_degree", "FF_bin_in_degree", "FF_bin_out_degree",
                                "FF_volume_in","FF_volume_out",
                                "FF_bin_betweeness","FF_bin_closeness", "FF_bin_pagerank", #"FF_bin_eigenvector",
                                "FF_bin_c_size","FF_bin_c_density","FF_bin_c_hierarchy","FF_bin_c_index",
                                "AT_bin_degree", "AT_bin_in_degree", "AT_bin_out_degree",
                                "AT_bin_betweeness", "AT_bin_closeness", "AT_bin_pagerank", #"AT_bin_eigenvector",                            
                                "AT_bin_c_size","AT_bin_c_density","AT_bin_c_hierarchy","AT_bin_c_index",
                                "AT_volume_in", "AT_volume_out",
                                "RT_volume_in", "RT_volume_out",
                                "FF_rec", "AT_rec", "AT_avg", "FF_avg"])    
    
    # Get the overall network from disk    
    FF = nx.read_edgelist('data/networks/%s_FF.edgelist' % project, nodetype=str, data=(('weight',float),),create_using=nx.DiGraph()) 
    AT = nx.read_edgelist('data/networks/%s_solr_AT.edgelist' % project, nodetype=str, data=(('weight',float),),create_using=nx.DiGraph()) 
    RT = nx.read_edgelist('data/networks/%s_solr_RT.edgelist' % project, nodetype=str, data=(('weight',float),),create_using=nx.DiGraph())
        
    # Read in the partition
    tmp = hp.get_partition(partitionfile)
    partitions = tmp[0]
    groups = tmp[1]
    
    #Read in members count for each project
    reader = csv.reader(open("results/stats/%s_lists_stats.csv" % project, "rb"), delimiter=",")
    temp  = {}
    reader.next() # Skip first row
    for row in reader:        
            temp[row[0]] = {"name":row[0],"member_count":int(row[3])}
    
    #Read in the list-listings for individuals
    listings = {}
    indiv_reader = csv.reader(open(partitionfile))
    for row in indiv_reader:                
            if listings.has_key(row[1]):
                listings[row[1]]["competing_lists"] += int(row[3])
            else:
                listings[row[1]] = {"competing_lists": int(row[3])}                            
           
    # Add dummy nodes if they are missing in the networks
    for partition in partitions:
            for node in partition:
                FF.add_node(node)
                AT.add_node(node)
                RT.add_node(node)
            
    #Blockmodel the networks into groups according to the partition
    P_FF = nx.blockmodel(FF,partitions)
    P_AT = nx.blockmodel(AT,partitions)
    P_RT = nx.blockmodel(RT,partitions)
    
    #Name the nodes in the network
    #TODO check: How do I know that the names really match?
    mapping = {}
    mapping_pajek = {}
    i = 0
    for group in groups:
        mapping_pajek[i] = "\"%s\"" % group # mapping for pajek
        mapping[i] = "%s" % group 
        i += 1
    
    H_FF = nx.relabel_nodes(P_FF,mapping)
    H_AT = nx.relabel_nodes(P_AT,mapping)
    H_RT = nx.relabel_nodes(P_RT,mapping)
    
    #Outpt the networks to pajek if needed
    if to_pajek:
        OUT_FF = nx.relabel_nodes(P_FF,mapping_pajek)
        OUT_AT = nx.relabel_nodes(P_AT,mapping_pajek)
        OUT_RT = nx.relabel_nodes(P_RT,mapping_pajek)
        
        #Write the blocked network out to disk
        nx.write_pajek(OUT_FF,"results/networks/%s_grouped_FF.net" % project)
        nx.write_pajek(OUT_AT,"results/networks/%s_grouped_AT.net" % project)
        nx.write_pajek(OUT_RT,"results/networks/%s_grouped_RT.net" % project)
    
    ########## Output the Edges between groups to csv ##############
    # Needed for the computation of individual bridging
    # Edges in both directions between the groups are addded up
    
    processed_edges = []
    for (u,v,attrib) in H_FF.edges(data=True):
        if "%s%s" %(u,v)  not in processed_edges:
            processed_edges.append("%s%s" % (u,v))            
            if H_FF.has_edge(v,u):
                processed_edges.append("%s%s" % (v,u))
                ff_edges_writer.writerow([u,v,attrib["weight"]+H_FF[v][u]["weight"]])
            else:
                ff_edges_writer.writerow([u,v,attrib["weight"]])
                
    processed_edges = []
    for (u,v,attrib) in H_AT.edges(data=True):
        if "%s%s" %(u,v)  not in processed_edges:
            processed_edges.append("%s%s" % (u,v))            
            if H_AT.has_edge(v,u):
                processed_edges.append("%s%s" % (v,u))
                at_edges_writer.writerow([u,v,attrib["weight"]+H_AT[v][u]["weight"]])
            else:
                at_edges_writer.writerow([u,v,attrib["weight"]])
                    
    processed_edges = []
    for (u,v,attrib) in H_RT.edges(data=True):
        if "%s%s" %(u,v)  not in processed_edges:
            processed_edges.append("%s%s" % (u,v))            
            if H_RT.has_edge(v,u):
                processed_edges.append("%s%s" % (v,u))
                rt_edges_writer.writerow([u,v,attrib["weight"]+H_RT[v][u]["weight"]])
            else:
                rt_edges_writer.writerow([u,v,attrib["weight"]])                

    
    ########## TRIM EDGES ################
    # For meaningfull results we have to trim edges in the AT and FF network so the whole network just doesnt look like a blob            
    # It is chosen this way so the network remains as one component
    
    THRESHOLD = min([hp.min_threshold(H_AT),hp.min_threshold(H_FF)])-1    
    H_FF = hp.trim_edges(H_FF, THRESHOLD)
    H_AT = hp.trim_edges(H_AT, THRESHOLD)    

    ########## MEASURES ##############
    
    #Get the number of nodes in the aggregated networks
    #FF_nodes = {}
    #for node in H_FF.nodes(data=True):
    #        FF_nodes[node[0]] = node[1]["nnodes"]
    
    
    #Get the FF network measures of the nodes
    # Works fine on binarized Data
    FF_bin_degree = nx.degree_centrality(H_FF) 
    FF_bin_in_degree = nx.in_degree_centrality(H_FF) # The attention paid towards this group
    FF_bin_out_degree = nx.out_degree_centrality(H_FF) # The attention that this group pays towards other people
    FF_bin_betweenness = nx.betweenness_centrality(H_FF,weight="weight") # How often is the group between other groups
    FF_bin_closeness = nx.closeness_centrality(H_FF) #FF_bin_eigenvector = nx.eigenvector_centrality(H_FF)
    FF_bin_pagerank = nx.pagerank(H_FF)        
    FF_bin_struc = sx.structural_holes(H_FF)
    
    # AT network measures of the nodes
    AT_bin_degree = nx.degree_centrality(H_AT)
    AT_bin_in_degree = nx.in_degree_centrality(H_AT)
    AT_bin_out_degree = nx.out_degree_centrality(H_AT)
    AT_bin_betweenness = nx.betweenness_centrality(H_AT,weight="weight") 
    AT_bin_closeness = nx.closeness_centrality(H_AT) #AT_bin_eigenvector = nx.eigenvector_centrality(H_AT)
    AT_bin_pagerank = nx.pagerank(H_AT)        
    AT_bin_struc = sx.structural_holes(H_AT)
    
    # Tie strengths
    dAT_avg_tie = hp.individual_average_tie_strength(H_AT)
    dFF_avg_tie = hp.individual_average_tie_strength(H_FF)
    dAT_rec = hp.individual_reciprocity(H_AT)    
    dFF_rec = hp.individual_reciprocity(H_FF)
    
    # Dependent Variable see csv
    # TODO A measure that calculates how often Tweets travel through this group: Eventually betweeness in the RT graph
    
    #Arrange it in a list and output
    for node in FF_bin_degree.keys():                
                csv_bridging_writer.writerow([project, node, int(temp[node]["member_count"]), listings[node]["competing_lists"],
                                                FF_bin_degree[node], FF_bin_in_degree[node], FF_bin_out_degree[node],
                                                H_FF.in_degree(node,weight="weight"), H_FF.out_degree(node,weight="weight"),
                                                FF_bin_betweenness[node],FF_bin_closeness[node],FF_bin_pagerank[node], #FF_bin_eigenvector[node],
                                                FF_bin_struc[node]['C-Size'],FF_bin_struc[node]['C-Density'],FF_bin_struc[node]['C-Hierarchy'],FF_bin_struc[node]['C-Index'],
                                                AT_bin_degree[node], AT_bin_in_degree[node], AT_bin_out_degree[node],
                                                AT_bin_betweenness[node], AT_bin_closeness[node], AT_bin_pagerank[node], #AT_bin_eigenvector[node],
                                                AT_bin_struc[node]['C-Size'],AT_bin_struc[node]['C-Density'],AT_bin_struc[node]['C-Hierarchy'],AT_bin_struc[node]['C-Index'],
                                                H_AT.in_degree(node,weight="weight"), H_AT.out_degree(node,weight="weight"),
                                                H_RT.in_degree(node,weight="weight"), H_RT.out_degree(node,weight="weight"),
                                                dFF_rec[node],dAT_rec[node],dAT_avg_tie[node],dFF_avg_tie[node]
                                            ])