Ejemplo n.º 1
0
def print_graph_info(graph):
  e = nx.eccentricity(graph)
  print 'graph with %u nodes, %u edges' % (len(graph.nodes()), len(graph.edges()))
  print 'radius: %s' %  nx.radius(graph, e) # min e
  print 'diameter: %s' % nx.diameter(graph, e) # max e
  print 'len(center): %s' % len(nx.center(graph, e)) # e == radius
  print 'len(periphery): %s' % len(nx.periphery(graph, e)) # e == diameter
def calculate(network):
    try:
        n = nx.radius(network)
    except:
        return 0
 
    return round(n, 7) 
Ejemplo n.º 3
0
def NetStats(G):
    return { 'radius': nx.radius(G),
             'diameter': nx.diameter(G),
             'connected_components': nx.number_connected_components(G),
             'density' : nx.density(G),
             'shortest_path_length': nx.shortest_path_length(G),
             'clustering': nx.clustering(G)}
Ejemplo n.º 4
0
def netstats_simple(graph):
    G = graph
    if nx.is_connected(G): 
        d = nx.diameter(G)
        r = nx.radius(G)
    else: 
        d = 'NA - graph is not connected' #should be calculatable on unconnected graph - see example code for hack
        r = 'NA - graph is not connected'
   
#using dictionary to pack values and variablesdot, eps, ps, pdf break equally
    result = {#"""single value measures"""  
              'nn': G.number_of_nodes(),
              'ne': G.number_of_edges(),
              'd': d,
              'r': r,
              'conn': nx.number_connected_components(G),
              'asp': nx.average_shortest_path_length(G), 
#              """number of the largest clique"""
              'cn': nx.graph_clique_number(G),
#              """number of maximal cliques"""
              'mcn': nx.graph_number_of_cliques(G),
#              """transitivity - """
              'tr': nx.transitivity(G),
              #cc = nx.clustering(G) """clustering coefficient"""
              'avgcc': nx.average_clustering(G) } 
#    result['d'] = nx.diameter(G)
    print result
    return result
Ejemplo n.º 5
0
def NetStats(G,name):
    
    s=0
    d = nx.degree(G)    
    for i in d.values():
        s = s + i
    
    n = len(G.nodes())
    m = len(G.edges())
    k = float(s)/float(n)
    #k = nx.average_node_connectivity(G)
        
    C = nx.average_clustering(G)
    l = nx.average_shortest_path_length(G)
    Cc = nx.closeness_centrality(G)
    d = nx.diameter(G) #The diameter is the maximum eccentricity.
    r = nx.radius(G) #The radius is the minimum eccentricity.


    
    output = "ESTADISITICOS_"+name
    SALIDA = open(output,"w")
    
    SALIDA.write(("Numero de nodos n = %s \n") %  n)
    SALIDA.write(("Numero de aristas m = %s \n") %  m)
    SALIDA.write(("Grado promedio <k> = %s \n") %  k)
        
    SALIDA.write(("Clustering Coeficient = %s \n") %  C)
    SALIDA.write(("Shortest Path Length = %s \n") %  l)
    #SALIDA.write(("Closeness = %s \n") %  Cc)
    SALIDA.write(("Diameter (maximum eccentricity) = %d \n") %  d)
    SALIDA.write(("Radius (minimum eccentricity) = %d \n") %  r)
Ejemplo n.º 6
0
def get_tree_symmetries_for_traitset(model, simconfig, cultureid, traitset, culture_count_map):
    radii = []

    symstats = stats.BalancedTreeAutomorphismStatistics(simconfig)
    subgraph_set = model.trait_universe.get_trait_graph_components(traitset)
    trait_subgraph = model.trait_universe.get_trait_forest_from_traits(traitset)
    results = symstats.calculate_graph_symmetries(trait_subgraph)

    for subgraph in subgraph_set:
        radii.append( nx.radius(subgraph))

    mean_radii = np.mean(np.asarray(radii))
    sd_radii = np.sqrt(np.var(np.asarray(radii)))
    degrees = nx.degree(trait_subgraph).values()
    mean_degree = np.mean(np.asarray(degrees))
    sd_degree = np.sqrt(np.var(np.asarray(degrees)))
    mean_orbit_mult = np.mean(np.asarray(results['orbitcounts']))
    sd_orbit_mult = np.sqrt(np.var(np.asarray(results['orbitcounts'])))
    max_orbit_mult = np.nanmax(np.asarray(results['orbitcounts']))

    r = dict(cultureid=str(cultureid), culture_count=culture_count_map[cultureid],
             orbit_multiplicities=results['orbitcounts'],
             orbit_number=results['orbits'],
             autgroupsize=results['groupsize'],
             remaining_density=results['remainingdensity'],
             mean_radii=mean_radii,
             sd_radii=sd_radii,
             mean_degree=mean_degree,
             sd_degree=sd_degree,
             mean_orbit_multiplicity=mean_orbit_mult,
             sd_orbit_multiplicity=sd_orbit_mult,
             max_orbit_multiplicity=max_orbit_mult
             )
    #log.debug("groupstats: %s", r)
    return r
Ejemplo n.º 7
0
    def updateGraphStats(self, graph):

        origgraph = graph
        if nx.is_connected(graph):
            random = 0
        else:
            connectedcomp = nx.connected_component_subgraphs(graph)
            graph = max(connectedcomp)

        if len(graph) > 1:
            pathlength = nx.average_shortest_path_length(graph)
        else:
            pathlength = 0

        # print graph.nodes(), len(graph), nx.is_connected(graph)

        stats = {
            "radius": nx.radius(graph),
            "density": nx.density(graph),
            "nodecount": len(graph.nodes()),
            "center": nx.center(graph),
            "avgcluscoeff": nx.average_clustering(graph),
            "nodeconnectivity": nx.node_connectivity(graph),
            "components": nx.number_connected_components(graph),
            "avgpathlength": pathlength
        }

        # print "updated graph stats", stats
        return stats
def strongly_connected_components():
    conn = sqlite3.connect("zhihu.db")     
    #following_data = pd.read_sql('select user_url, followee_url from Following where followee_url in (select user_url from User where agree_num > 50000) and user_url in (select user_url from User where agree_num > 50000)', conn)        
    following_data = pd.read_sql('select user_url, followee_url from Following where followee_url in (select user_url from User where agree_num > 10000) and user_url in (select user_url from User where agree_num > 10000)', conn)        
    conn.close()
    
    G = nx.DiGraph()
    cnt = 0
    for d in following_data.iterrows():
        G.add_edge(d[1][0],d[1][1])
        cnt += 1
    print 'links number:', cnt

    scompgraphs = nx.strongly_connected_component_subgraphs(G)
    scomponents = sorted(nx.strongly_connected_components(G), key=len, reverse=True)
    print 'components nodes distribution:', [len(c) for c in scomponents]
    
    #plot graph of component, calculate saverage_shortest_path_length of components who has over 1 nodes
    index = 0
    print 'average_shortest_path_length of components who has over 1 nodes:'
    for tempg in scompgraphs:
        index += 1
        if len(tempg.nodes()) != 1:
            print nx.average_shortest_path_length(tempg)
            print 'diameter', nx.diameter(tempg)
            print 'radius', nx.radius(tempg)
        pylab.figure(index)
        nx.draw_networkx(tempg)
        pylab.show()

    # Components-as-nodes Graph
    cG = nx.condensation(G)
    pylab.figure('Components-as-nodes Graph')
    nx.draw_networkx(cG)
    pylab.show()    
def graph_radius(graph):
    sp = nx.shortest_path_length(graph,weight='weight')
    ecc = nx.eccentricity(graph,sp=sp)
    if ecc:
        rad = nx.radius(graph,e=ecc)
    else:
        rad = 0
    return rad
Ejemplo n.º 10
0
Archivo: util.py Proyecto: zaktan8/GCP
def get_graph_info(graph):
    nodes = networkx.number_of_nodes(graph)
    edges = networkx.number_of_edges(graph)
    radius = networkx.radius(graph)
    diameter = networkx.diameter(graph)
    density = networkx.density(graph)
    average_clustering = networkx.average_clustering(graph)
    average_degree = sum(graph.degree().values()) / nodes
    return nodes, edges, radius, diameter, density, average_clustering, average_degree
def test_radius(testgraph):
    """
    Testing radius function for graphs.
    """

    a, b = testgraph
    nx_rad = nx.radius(a)
    sg_rad = sg.digraph_distance_measures.radius(b, b.order())
    assert nx_rad == sg_rad
    def get_path_lengths(self):
        if not hasattr(self,"shortest_path_lenghts") or self.shortest_path_lenghts is None:
            self.shortest_paths_lengths = nx.all_pairs_shortest_path_length(self.G)
            self.avg_shortest_path = sum([ length for sp in self.shortest_paths_lengths.values() for length in sp.values() ])/float(self.N*(self.N-1))
            self.eccentricity = nx.eccentricity(self.G,sp=self.shortest_paths_lengths)
            self.diameter = nx.diameter(self.G,e=self.eccentricity)
            self.radius = nx.radius(self.G,e=self.eccentricity)

        return self.shortest_paths_lengths
Ejemplo n.º 13
0
 def connectivity(self):
     components = list(nx.connected_component_subgraphs(self.G))
     print('Connected components number: ')
     print(len(components))
     giant = components.pop(0)
     print('Giant component radius: ')
     print(nx.radius(giant))
     print('Giant component diameter: ')
     print(nx.diameter(giant))
     center = nx.center(giant)
     print('Giant component center: ')
     for i in xrange(len(center)):
         print(self.singer_dict[int(center[i])].split('|')[0])
     inf = self.get_graph_info(giant)
     for i in xrange(len(inf)):
         print(inf[i])
Ejemplo n.º 14
0
def write_graph(graph_name, g):
    radius = nx.radius(g)
    # https://networkx.github.io/documentation/latest/reference/generated/networkx.algorithms.distance_measures.radius.html

    diameter = nx.diameter(g)
    # https://networkx.github.io/documentation/latest/reference/generated/networkx.algorithms.distance_measures.diameter.html

    closeness = float(sum(nx.algorithms.centrality.closeness_centrality(g).values()))/size
    # https://networkx.github.io/documentation/latest/reference/generated/networkx.algorithms.centrality.closeness_centrality.html#networkx.algorithms.centrality.closeness_centrality

    betweenness = float(sum(nx.algorithms.centrality.betweenness_centrality(g).values()))/size
    # https://networkx.github.io/documentation/latest/reference/generated/networkx.algorithms.centrality.betweenness_centrality.html#networkx.algorithms.centrality.betweenness_centrality

    clustering = float(sum(nx.algorithms.clustering(g).values()))/size
    # https://networkx.github.io/documentation/latest/reference/generated/networkx.algorithms.cluster.clustering.html#networkx.algorithms.cluster.clustering

    print "%s\t%s\t%s\t%s\t%s\t%s" % (graph_name, radius, diameter, closeness, betweenness, clustering)
Ejemplo n.º 15
0
    def PrintGraphStat(self):
        logging.debug("From SVNFileNetwork.PrintGraphStat")
        print "%s" % '-' * 40
        print "Graph Radius : %f" % NX.radius(self)
        print "Graph Diameter : %f" % NX.diameter(self)

        weighted = True
        closenessdict = NX.closeness_centrality(self, distance=weighted)
        print "%s" % '-' * 40
        print "All nodes in graph"

        nodeinfolist = [(node, closeness)
                        for node, closeness in closenessdict.items()]
        # sort the node infolist by closeness number
        nodeinfolist = sorted(
            nodeinfolist, key=operator.itemgetter(1), reverse=True)
        for node, closeness in nodeinfolist:
            print "\t%s : %f" % (node.name(), closeness)
        print "%s" % '-' * 40
Ejemplo n.º 16
0
def report_components(g):
	components = nx.connected_component_subgraphs(g)
	print "Components: %d" % len(components)
	c_data = {}
	for i in range(len(components)):
		c = components[i]
		if len(c.nodes()) > 5: # Avoid reporting on many small components
			c_data["nodes"] = len(c.nodes())
			c_data["edges"] = len(c.edges())
			c_data["avg_clustering"] = nx.average_clustering(c)
			c_data["diameter"] = nx.diameter(c)
			c_data["radius"] = nx.radius(c)
			c_data["center"] = len(nx.center(c))
			c_data["periphery"] = len(nx.periphery(c))

			print "* Component %d:" % i
			for k in c_data:
				print "--- %s: %s" % (k, c_data[k])

	return c_data
Ejemplo n.º 17
0
def distance_scores(season, graph):
    
    # Take largest connected component
    g = graph if nx.is_connected(graph) else max(nx.connected_component_subgraphs(graph), key=len)
    
    # Ratio of largest connected component subgraph
    conn = len(max(nx.connected_component_subgraphs(g), key=len)) / float(nx.number_of_nodes(graph))
    conn = np.round(conn, 3)
    
    # Radius, diameter
    rad = nx.radius(g)
    diam = nx.diameter(g)
    
    # Average eccentricity
    ecc = np.mean(nx.eccentricity(g).values())
    ecc = np.round(ecc, 3)
    
    # Put it all into a dataframe
    df = pd.DataFrame([[season,conn,rad,diam,ecc]], columns=['season', 'conn', 'rad', 'diam', 'ecc'])
    
    return df
Ejemplo n.º 18
0
    def getMetrics(self, layerid=0):
        # get some overall network metrics
        undirectedG = self.layergraphs[layerid].to_undirected()
        metrics = {}

        try:  # must be connected
            metrics['diameter'] = nx.diameter(undirectedG)
            metrics['radius'] = nx.radius(undirectedG)
            metrics['average_clustering'] = round(nx.average_clustering(undirectedG),3)
            metrics['transitivity'] = round(nx.transitivity(undirectedG),3)
            metrics['number_connected_components'] = nx.number_connected_components(undirectedG)

            import operator
            betweenness_centrality = nx.betweenness_centrality(self.layergraphs[layerid])
            metrics['betweenness_centrality'] = sorted(betweenness_centrality.iteritems(),key=operator.itemgetter(1),reverse=True)[0][0] # find node with largest betweenness centrality

            H = nx.connected_component_subgraphs(undirectedG)[0] # largest connected component
            metrics['number_of_nodes'] = len(H.nodes())
        except:
            pass

        return metrics
 def __init__(self, graph, feature_list=[]):
     self.no_feature = 39
     self.G = graph
     self.nodes = nx.number_of_nodes(self.G)
     self.edges = nx.number_of_edges(self.G)
     self.Lap = nx.normalized_laplacian_matrix(self.G)
     # ??? how to check whether comparable, addable?
     self.eigvals = numpy.linalg.eigvals(self.Lap.A).tolist()
     try:
         self.radius = nx.radius(self.G)
     except nx.exception.NetworkXError:
         self.radius = "ND"
     try:
         self.ecc_dic = nx.eccentricity(self.G)
     except nx.exception.NetworkXError:
         self.ecc_dic = {}
     self.degree_dic = nx.average_neighbor_degree(self.G)
     self.pagerank = nx.pagerank(self.G).values()
     if feature_list == []:
         self.feature_list = list(range(1, self.no_feature + 1))
     else:
         self.feature_list = feature_list
     self.feature_vector = []
     self.feature_time = []
Ejemplo n.º 20
0
def extended_stats(G,
                   connectivity=False,
                   anc=False,
                   ecc=False,
                   bc=False,
                   cc=False):
    """
    Calculate extended topological stats and metrics for a graph.

    Many of these algorithms have an inherently high time complexity. Global
    topological analysis of large complex networks is extremely time consuming
    and may exhaust computer memory. Consider using function arguments to not
    run metrics that require computation of a full matrix of paths if they
    will not be needed.

    Parameters
    ----------
    G : networkx.MultiDiGraph
        input graph
    connectivity : bool
        if True, calculate node and edge connectivity
    anc : bool
        if True, calculate average node connectivity
    ecc : bool
        if True, calculate shortest paths, eccentricity, and topological
        metrics that use eccentricity
    bc : bool
        if True, calculate node betweenness centrality
    cc : bool
        if True, calculate node closeness centrality

    Returns
    -------
    stats : dict
        dictionary of network measures containing the following elements (some
        only calculated/returned optionally, based on passed parameters):

          - avg_neighbor_degree
          - avg_neighbor_degree_avg
          - avg_weighted_neighbor_degree
          - avg_weighted_neighbor_degree_avg
          - degree_centrality
          - degree_centrality_avg
          - clustering_coefficient
          - clustering_coefficient_avg
          - clustering_coefficient_weighted
          - clustering_coefficient_weighted_avg
          - pagerank
          - pagerank_max_node
          - pagerank_max
          - pagerank_min_node
          - pagerank_min
          - node_connectivity
          - node_connectivity_avg
          - edge_connectivity
          - eccentricity
          - diameter
          - radius
          - center
          - periphery
          - closeness_centrality
          - closeness_centrality_avg
          - betweenness_centrality
          - betweenness_centrality_avg

    """
    stats = {}

    # create a DiGraph from the MultiDiGraph, for those metrics that require it
    G_dir = nx.DiGraph(G)

    # create an undirected Graph from the MultiDiGraph, for those metrics that
    # require it
    G_undir = nx.Graph(G)

    # get the largest strongly connected component, for those metrics that
    # require strongly connected graphs
    G_strong = utils_graph.get_largest_component(G, strongly=True)

    # average degree of the neighborhood of each node, and average for the graph
    avg_neighbor_degree = nx.average_neighbor_degree(G)
    stats["avg_neighbor_degree"] = avg_neighbor_degree
    stats["avg_neighbor_degree_avg"] = sum(
        avg_neighbor_degree.values()) / len(avg_neighbor_degree)

    # average weighted degree of the neighborhood of each node, and average for
    # the graph
    avg_wtd_nbr_deg = nx.average_neighbor_degree(G, weight="length")
    stats["avg_weighted_neighbor_degree"] = avg_wtd_nbr_deg
    stats["avg_weighted_neighbor_degree_avg"] = sum(
        avg_wtd_nbr_deg.values()) / len(avg_wtd_nbr_deg)

    # degree centrality for a node is the fraction of nodes it is connected to
    degree_centrality = nx.degree_centrality(G)
    stats["degree_centrality"] = degree_centrality
    stats["degree_centrality_avg"] = sum(
        degree_centrality.values()) / len(degree_centrality)

    # calculate clustering coefficient for the nodes
    stats["clustering_coefficient"] = nx.clustering(G_undir)

    # average clustering coefficient for the graph
    stats["clustering_coefficient_avg"] = nx.average_clustering(G_undir)

    # calculate weighted clustering coefficient for the nodes
    stats["clustering_coefficient_weighted"] = nx.clustering(G_undir,
                                                             weight="length")

    # average clustering coefficient (weighted) for the graph
    stats["clustering_coefficient_weighted_avg"] = nx.average_clustering(
        G_undir, weight="length")

    # pagerank: a ranking of the nodes in the graph based on the structure of
    # the incoming links
    pagerank = nx.pagerank(G_dir, weight="length")
    stats["pagerank"] = pagerank

    # node with the highest page rank, and its value
    pagerank_max_node = max(pagerank, key=lambda x: pagerank[x])
    stats["pagerank_max_node"] = pagerank_max_node
    stats["pagerank_max"] = pagerank[pagerank_max_node]

    # node with the lowest page rank, and its value
    pagerank_min_node = min(pagerank, key=lambda x: pagerank[x])
    stats["pagerank_min_node"] = pagerank_min_node
    stats["pagerank_min"] = pagerank[pagerank_min_node]

    # if True, calculate node and edge connectivity
    if connectivity:

        # node connectivity is the minimum number of nodes that must be removed
        # to disconnect G or render it trivial
        stats["node_connectivity"] = nx.node_connectivity(G_strong)

        # edge connectivity is equal to the minimum number of edges that must be
        # removed to disconnect G or render it trivial
        stats["edge_connectivity"] = nx.edge_connectivity(G_strong)
        utils.log("Calculated node and edge connectivity")

    # if True, calculate average node connectivity
    if anc:
        # mean number of internally node-disjoint paths between each pair of
        # nodes in G, i.e., the expected number of nodes that must be removed to
        # disconnect a randomly selected pair of non-adjacent nodes
        stats["node_connectivity_avg"] = nx.average_node_connectivity(G)
        utils.log("Calculated average node connectivity")

    # if True, calculate shortest paths, eccentricity, and topological metrics
    # that use eccentricity
    if ecc:
        # precompute shortest paths between all nodes for eccentricity-based
        # stats
        sp = {
            source: dict(
                nx.single_source_dijkstra_path_length(G_strong,
                                                      source,
                                                      weight="length"))
            for source in G_strong.nodes()
        }

        utils.log("Calculated shortest path lengths")

        # eccentricity of a node v is the maximum distance from v to all other
        # nodes in G
        eccentricity = nx.eccentricity(G_strong, sp=sp)
        stats["eccentricity"] = eccentricity

        # diameter is the maximum eccentricity
        diameter = nx.diameter(G_strong, e=eccentricity)
        stats["diameter"] = diameter

        # radius is the minimum eccentricity
        radius = nx.radius(G_strong, e=eccentricity)
        stats["radius"] = radius

        # center is the set of nodes with eccentricity equal to radius
        center = nx.center(G_strong, e=eccentricity)
        stats["center"] = center

        # periphery is the set of nodes with eccentricity equal to the diameter
        periphery = nx.periphery(G_strong, e=eccentricity)
        stats["periphery"] = periphery

    # if True, calculate node closeness centrality
    if cc:
        # closeness centrality of a node is the reciprocal of the sum of the
        # shortest path distances from u to all other nodes
        closeness_centrality = nx.closeness_centrality(G, distance="length")
        stats["closeness_centrality"] = closeness_centrality
        stats["closeness_centrality_avg"] = sum(
            closeness_centrality.values()) / len(closeness_centrality)
        utils.log("Calculated closeness centrality")

    # if True, calculate node betweenness centrality
    if bc:
        # betweenness centrality of a node is the sum of the fraction of
        # all-pairs shortest paths that pass through node
        # networkx 2.4+ implementation cannot run on Multi(Di)Graphs, so use DiGraph
        betweenness_centrality = nx.betweenness_centrality(G_dir,
                                                           weight="length")
        stats["betweenness_centrality"] = betweenness_centrality
        stats["betweenness_centrality_avg"] = sum(
            betweenness_centrality.values()) / len(betweenness_centrality)
        utils.log("Calculated betweenness centrality")

    utils.log("Calculated extended stats")
    return stats
def co_visiting():
    ## create the co_visiting/reviewing network, where nodes are users, edges are number of sharing common restaurant of reviewing, we assume two people have similarity if they visited the same restaurant.

    # read from numpy matrix
    A = np.loadtxt("output/covisiting_matrix")
    G = nx.from_numpy_matrix(A)

    # select the biggest component for the following analysis
    connected_components = sorted(nx.connected_component_subgraphs(G),
                                  key=len,
                                  reverse=True)
    print("{} connected components found.".format(len(connected_components)))
    g = connected_components[0]
    component_id = g.nodes()

    df = pd.DataFrame(index=g.nodes)
    df["degree"] = pd.Series(nx.degree_centrality(g))
    df["betweenness"] = pd.Series(nx.betweenness_centrality(g))
    df["closeness"] = pd.Series(nx.closeness_centrality(g))
    df["eigenvector"] = pd.Series(nx.eigenvector_centrality(g))
    df["clustering"] = pd.Series(nx.clustering(g))

    print(df.sort_values("betweenness", ascending=False).head(10))
    exit()

    df_node_degree = pd.DataFrame(list(dict(g.degree()).items()),
                                  columns=["node_name", "degree"])
    print(df_node_degree.sort_values("degree", ascending=False).head(10))

    print("radius: {:d}\n".format(nx.radius(g)))
    print("diameter: {:d}\n".format(nx.diameter(g)))
    print("eccentricity: {}\n".format(nx.eccentricity(g)))
    print("center: {}\n".format(nx.center(g)))
    print("periphery: {}\n".format(nx.periphery(g)))
    print("density: {:f}".format(nx.density(g)))

    exit()

    # assign labels to node
    user_ids = []
    with open("output/component_adjlist.txt", 'r') as f:
        for line in f:
            data = json.loads(line)
            user_ids.append(data['user_id'])

    labels = {}
    for i in component_id:
        labels[i] = user_ids[i]

    # degree distribution plot of the biggest component
    fig1, ax1 = plt.subplots(1, 1)
    degree_values = [v for k, v in g.degree()]
    ax1.hist(list(degree_values),
             bins=list(range(max(degree_values))),
             log=True)
    ax1.set_xlabel("Degree")
    ax1.set_ylabel("Frequency")
    ax1.set_title("Degree Distribution")

    fig1.savefig('/home/dtao2/Dropbox/degree.png')

    # covisiting  network plot of the biggest component
    fig2, ax2 = plt.subplots(1, 1)
    pos = nx.spring_layout(g)
    nx.draw_networkx(g,
                     with_labels=False,
                     node_size=[x[1] * 10 for x in g.degree()],
                     pos=pos,
                     width=0.5,
                     ax=ax2)

    nx.draw_networkx_labels(g, pos, labels=None, font_size=5)

    # nx.draw_networkx_labels(g,pos,labels = labels, font_size=5)

    ax2.axis("off")
    ax2.set_title("Covisiting Network")
    fig2.savefig('/home/dtao2/Dropbox/graph.png')

    return
    def test_properties_named_small_graphs(self):
        G = nx.bull_graph()
        assert G.number_of_nodes() == 5
        assert G.number_of_edges() == 5
        assert sorted(d for n, d in G.degree()) == [1, 1, 2, 3, 3]
        assert nx.diameter(G) == 3
        assert nx.radius(G) == 2

        G = nx.chvatal_graph()
        assert G.number_of_nodes() == 12
        assert G.number_of_edges() == 24
        assert list(d for n, d in G.degree()) == 12 * [4]
        assert nx.diameter(G) == 2
        assert nx.radius(G) == 2

        G = nx.cubical_graph()
        assert G.number_of_nodes() == 8
        assert G.number_of_edges() == 12
        assert list(d for n, d in G.degree()) == 8 * [3]
        assert nx.diameter(G) == 3
        assert nx.radius(G) == 3

        G = nx.desargues_graph()
        assert G.number_of_nodes() == 20
        assert G.number_of_edges() == 30
        assert list(d for n, d in G.degree()) == 20 * [3]

        G = nx.diamond_graph()
        assert G.number_of_nodes() == 4
        assert sorted(d for n, d in G.degree()) == [2, 2, 3, 3]
        assert nx.diameter(G) == 2
        assert nx.radius(G) == 1

        G = nx.dodecahedral_graph()
        assert G.number_of_nodes() == 20
        assert G.number_of_edges() == 30
        assert list(d for n, d in G.degree()) == 20 * [3]
        assert nx.diameter(G) == 5
        assert nx.radius(G) == 5

        G = nx.frucht_graph()
        assert G.number_of_nodes() == 12
        assert G.number_of_edges() == 18
        assert list(d for n, d in G.degree()) == 12 * [3]
        assert nx.diameter(G) == 4
        assert nx.radius(G) == 3

        G = nx.heawood_graph()
        assert G.number_of_nodes() == 14
        assert G.number_of_edges() == 21
        assert list(d for n, d in G.degree()) == 14 * [3]
        assert nx.diameter(G) == 3
        assert nx.radius(G) == 3

        G = nx.hoffman_singleton_graph()
        assert G.number_of_nodes() == 50
        assert G.number_of_edges() == 175
        assert list(d for n, d in G.degree()) == 50 * [7]
        assert nx.diameter(G) == 2
        assert nx.radius(G) == 2

        G = nx.house_graph()
        assert G.number_of_nodes() == 5
        assert G.number_of_edges() == 6
        assert sorted(d for n, d in G.degree()) == [2, 2, 2, 3, 3]
        assert nx.diameter(G) == 2
        assert nx.radius(G) == 2

        G = nx.house_x_graph()
        assert G.number_of_nodes() == 5
        assert G.number_of_edges() == 8
        assert sorted(d for n, d in G.degree()) == [2, 3, 3, 4, 4]
        assert nx.diameter(G) == 2
        assert nx.radius(G) == 1

        G = nx.icosahedral_graph()
        assert G.number_of_nodes() == 12
        assert G.number_of_edges() == 30
        assert (list(
            d for n, d in G.degree()) == [5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5])
        assert nx.diameter(G) == 3
        assert nx.radius(G) == 3

        G = nx.krackhardt_kite_graph()
        assert G.number_of_nodes() == 10
        assert G.number_of_edges() == 18
        assert (sorted(
            d for n, d in G.degree()) == [1, 2, 3, 3, 3, 4, 4, 5, 5, 6])

        G = nx.moebius_kantor_graph()
        assert G.number_of_nodes() == 16
        assert G.number_of_edges() == 24
        assert list(d for n, d in G.degree()) == 16 * [3]
        assert nx.diameter(G) == 4

        G = nx.octahedral_graph()
        assert G.number_of_nodes() == 6
        assert G.number_of_edges() == 12
        assert list(d for n, d in G.degree()) == 6 * [4]
        assert nx.diameter(G) == 2
        assert nx.radius(G) == 2

        G = nx.pappus_graph()
        assert G.number_of_nodes() == 18
        assert G.number_of_edges() == 27
        assert list(d for n, d in G.degree()) == 18 * [3]
        assert nx.diameter(G) == 4

        G = nx.petersen_graph()
        assert G.number_of_nodes() == 10
        assert G.number_of_edges() == 15
        assert list(d for n, d in G.degree()) == 10 * [3]
        assert nx.diameter(G) == 2
        assert nx.radius(G) == 2

        G = nx.sedgewick_maze_graph()
        assert G.number_of_nodes() == 8
        assert G.number_of_edges() == 10
        assert sorted(d for n, d in G.degree()) == [1, 2, 2, 2, 3, 3, 3, 4]

        G = nx.tetrahedral_graph()
        assert G.number_of_nodes() == 4
        assert G.number_of_edges() == 6
        assert list(d for n, d in G.degree()) == [3, 3, 3, 3]
        assert nx.diameter(G) == 1
        assert nx.radius(G) == 1

        G = nx.truncated_cube_graph()
        assert G.number_of_nodes() == 24
        assert G.number_of_edges() == 36
        assert list(d for n, d in G.degree()) == 24 * [3]

        G = nx.truncated_tetrahedron_graph()
        assert G.number_of_nodes() == 12
        assert G.number_of_edges() == 18
        assert list(d for n, d in G.degree()) == 12 * [3]

        G = nx.tutte_graph()
        assert G.number_of_nodes() == 46
        assert G.number_of_edges() == 69
        assert list(d for n, d in G.degree()) == 46 * [3]

        # Test create_using with directed or multigraphs on small graphs
        pytest.raises(nx.NetworkXError,
                      nx.tutte_graph,
                      create_using=nx.DiGraph)
        MG = nx.tutte_graph(create_using=nx.MultiGraph)
        assert sorted(MG.edges()) == sorted(G.edges())
Ejemplo n.º 23
0
]
l = len(words)
G = nx.Graph()
for i in range(l):
    G.add_node(words[i])
for i in range(l):
    for j in range(l):
        if i != j:
            cos = model.similarity(words[i], words[j])
            if cos >= 0.5:
                G.add_edge(words[i], words[j], weight=cos)
pos = nx.spring_layout(G)
nx.draw_networkx_nodes(G, pos, node_color='blue', node_size=50)
nx.draw_networkx_edges(G, pos, edge_color='green')
nx.draw_networkx_labels(G, pos, font_size=20, font_family='Arial')
plt.axis('off')
plt.show()
deg = nx.degree_centrality(G)
imp = []
for nodeid in sorted(deg, key=deg.get, reverse=True):
    imp.append(nodeid)
f = open('Семантическое поле боли.txt', 'w', encoding='utf8')
f.write('ХАРАКТЕРИСТИКИ ГРАФА\n')
s = imp[0] + ', ' + imp[1] + ', ' + imp[2] + '\n'
f.write('Самые центральные слова графа: ' + s)
s = ''
for comp in list(nx.connected_component_subgraphs(G)):
    s = s + str(nx.radius(comp)) + ' '
f.write('Радиус (для каждой компоненты связности): ' + s + '\n')
f.write('Коэффициент кластеризации: ' + str(nx.average_clustering(G)))
f.close()
def answer_ten():
    rad = nx.radius(answer_six())
    df = pd.Series(nx.eccentricity(answer_six()))
    df = df.reset_index()
    return set(df[df[0] == rad]['index'])
Ejemplo n.º 25
0
scrambled = [
    6, 6, 0, 6, 6, 4, 0, 2, 0, 7, 7, 5, 7, 7, 4, 1, 2, 3, 7, 7, 5, 7, 7, 0, 1,
    0, 3
]
draw_cubes(scrambled)

c.shortest_path(g, scrambled, alca, labels, c2i)

# 6. find out how many shapes there are at given distance from solved shape
pred, dist = nx.dijkstra_predecessor_and_distance(g, 0)
for i in range(max(dist.values()) + 1):
    print(i, ":", len(list(filter(lambda x: dist[x] == i, verts))))

# 7. note however the solved shape is chosen the best way it could be!
nx.diameter(g)
nx.radius(g)
[v for v in verts if nx.eccentricity(g, v) == 16]
draw_cubes([i2c[v] for v in verts if nx.eccentricity(g, v) == 28], ncol=4)

# 8. draw the farthest three shapes and find a path to one of them
farthest = [i2c[v] for v in verts if dist[v] == 16]
draw_cubes(farthest)
c.shortest_path(g, alca, farthest[1], labels, c2i)

nx.eccentricity(g, c2i[tuple(farthest[1])])

# 9. shortest paths are too hard - explore stabilizer / feature chains
# define a stabilizer chain
v0 = set(verts)
v1 = {v for v in verts if i2c[v][c.F] == i2c[v][c.DF]}
v2 = {v for v in v1 if i2c[v][c.R] == i2c[v][c.DR]}
 def radius(self, graph):
     # returns radius, i.e. min eccentricity in the graph
     return nx.radius(graph)
Ejemplo n.º 27
0
G = nx.Graph()
G.add_nodes_from(words)

for i, word in enumerate(words):
    for j, other_word in enumerate(words):
        if j > i and model.similarity(word, other_word) > 0.5:
            G.add_edge(word, other_word)

nx.write_gexf(G, 'graph_file.gexf')

pos = nx.spring_layout(G)
nx.draw_networkx_nodes(G, pos, node_color='red', node_size=10)
nx.draw_networkx_edges(G, pos, edge_color='yellow')
nx.draw_networkx_labels(G, pos, font_size=10, font_family='Arial')
plt.axis('off')
plt.show()

deg = nx.degree_centrality(G)
print('самое центральное слово графа:')
print(sorted(deg, key=deg.get, reverse=True)[0])

print('коэффициент кластеризации:')
print(nx.average_clustering(G))

for word in words:
    if G.neighbors(word) == []:
        G.remove_node(word)

print('у нас всего одна компонента связности, ее радиус:')
print(nx.radius(G))
Ejemplo n.º 28
0
def properties(dim):
    NUM_PROPERTIES = 3
    from server.process.config import args
    meta = json.load(open(args['meta_json'], 'r'))
    query = json.load(open(args['query_json'], 'r'))
    from server.process.dataset import Dataset
    data = Dataset(args)

    # add the contrasted node type to the subnetworks
    if dim not in query['nodes']:
        query['nodes'].append(dim)

    # remove the contrasted node type from filters
    if dim in query['filters']:
        query['filters'].pop(dim)

    # initialize property list
    prop = [{} for i in range(NUM_PROPERTIES)]
    prop[0]['name'] = 'size'
    prop[1]['name'] = 'radius'
    prop[2]['name'] = 'density'
    prop[0]['labels'] = []
    prop[1]['labels'] = []
    prop[2]['labels'] = []

    for i in meta['label'][dim]:
        # retrieve network connected to the contrasted node
        query['filters'][dim] = [i]
        network = exploration(query, data)

        sub_graph = json_graph.node_link_graph(network)
        gen = nx.connected_component_subgraphs(sub_graph)

        if len(network['nodes']) > 0:
            connected_graph = max(gen, key=len)
            prop[0]['labels'].append({
                'name': meta['label'][dim][i][0],
                'val': len(network['nodes'])
            })
            prop[1]['labels'].append({
                'name': meta['label'][dim][i][0],
                'val': nx.radius(connected_graph)
            })
            prop[2]['labels'].append({
                'name': meta['label'][dim][i][0],
                'val': nx.density(connected_graph)
            })
        else:
            prop[0]['labels'].append({
                'name': meta['label'][dim][i][0],
                'val': 0
            })
            prop[1]['labels'].append({
                'name': meta['label'][dim][i][0],
                'val': 0
            })
            prop[2]['labels'].append({
                'name': meta['label'][dim][i][0],
                'val': 0
            })

        query['filters'].pop(dim)

    results = {}
    results['node_type'] = meta['node'][dim]['name']
    results['properties'] = prop

    return results
Ejemplo n.º 29
0
##G.add_edge(1, 3, weight=4) # вес ребра

##dg = nx.DiGraph()
##dg.add_weighted_edges_from([(1,4,0.5), (3,1,0.75)]) # от кого -- кому и вес ребра

print('nodes: ' + str(G.nodes()))
print('edges: ' + str(G.edges()))

# соседи узла 1
print('neigbours of node 1: ' + str(list(G.neighbors(1))))

# число соседей узла 5
print('degree of node 5: ' + str(G.degree(5)))

# Радиус графа, минимальный эксцентриситет среди всех вершин графа
print('radius: ' + str(nx.radius(G)))

# Диаметр графа, самый длинный путь от одной вершины до другой
print('diameter: ' + str(nx.diameter(G)))

# Плотность графа, отношение рёбер и узлов
print('density: ' + str(nx.density(G)))

# Коэффициент ассортативности (насколько сильно развалится если поубирать самые центральные узлы):
print('degree pearson correlation coefficient: ' +
      str(nx.degree_pearson_correlation_coefficient(G)))

# вот какой коэффициент у нашего графа
print('average clustering: ' + str(nx.average_clustering(G)))
print('transitivity: ' + str(nx.transitivity(G)))
Ejemplo n.º 30
0
def drawNetwork(path1, path2, sele=None, sele1=None, sele2=None, top1=None, top2=None,
                r=1, edge_norm=None, alpha=0.5, mutations=False, align_with = None, 
                node_color=(0.6, 0.6, 0.6), edge_color1 = (0, 0, 1), palette="colorblind",
                edge_color2 = (1, 0, 0), labeling='0', norm_expected=False,
                threshold=0, topk=None, max_compo=None, mean_vp=None, strong_compo=None, 
                around=None, keep_previous=False, compo_size=None, save_cc=None, load_cc=None,
                compos_to_excel = None, force_binary_color=False, compo_radius=None, compo_diam=None,
                label_compo='', auto_patch=True, printall=False, sum=False, n_clusters=None,
                color_by_compo=False, color_by_group=False, show_top_group=None,
                name1 = None, name2 = None, name_nodes='nodes', userSelection='all',
                fromstruct=None, color_by_contact_type=False, standard_and_expected=None):
    '''
    Draws a NetworkX network on the PyMol structure
    '''

    #Initialization of labeling variables and retreieving residue XYZ positions
    if not keep_previous:
        cmd.delete('*nodes *edges Component* Group*')
        cmd.label(selection=userSelection, expression="")
        cmd.hide("licorice", "?mutations")
    # Building position -- name correspondance
    stored.posCA = []
    stored.names = []
    stored.ss = []
    userSelection = userSelection + " and ((n. CA) or n. C)"
    cmd.iterate_state(1, selector.process(userSelection), "stored.posCA.append([x,y,z])")
    cmd.iterate(userSelection, "stored.ss.append(ss)")
    cmd.iterate(userSelection, 'stored.names.append(resn+resi+chain)')
    stored.labels = list(map(relabel, stored.names))
    stored.resid = list(map(selection, stored.names))
    node2id = dict(zip(stored.labels, stored.resid))
    node2CA = dict(zip(stored.labels, stored.posCA))

    #Secondary Structure labels
    prevSS, prevChain = None, None
    counters = {'': 0, 'H': 0, 'S': 0, 'L': 0}
    node2SS = dict(zip(stored.labels, stored.ss))
    SS2nodelist = {}
    putflag = lambda X: 'U' if X in ['', 'L'] else X
    for label in node2SS:
        ss = node2SS[label]
        chain = label[-1]
        if prevChain != chain:
            for counter in counters: counters[counter] = 0
        if prevSS != ss:
            counters[ss] +=1
        labss = putflag(ss)+str(counters[ss])+':'+chain
        if labss in SS2nodelist:
            SS2nodelist[labss].append(label)
        else:
            SS2nodelist[labss] = [label]
        prevSS = ss
        prevChain = chain

    prevkey, prevChain = None, None
    order = []
    keys = list(SS2nodelist.keys())

    for key in keys:
        if prevChain != key.split(':')[-1]:
            prevkey = None
        if key[0] == 'U':
            if prevkey == None:
                newkey = 'Head:'+key.split(':')[-1]
            else:
                newkey = 'U'+prevkey
            SS2nodelist[newkey] = SS2nodelist.pop(key)
            order.append(newkey)
        else:
            order.append(key)
        prevkey = key
        prevChain = key.split(':')[-1]
    prevkey = None
    final = []
    for key in order[::-1]:
        if prevChain != key.split(':')[-1]:
            prevkey = None
        if key[0] == 'U':
            if prevkey == None:
                newkey = 'Tail:'+key.split(':')[-1]
            else:
                newkey = '{}-{}'.format(key[1:], prevkey)
            SS2nodelist[newkey] = SS2nodelist.pop(key)
            final.append(newkey)
        else:
            final.append(key)
        prevkey = key
        prevChain = key.split(':')[-1]
    # ss_dict = dict(zip(keys, final[::-1]))
    mapss = {}
    for key in final:
        newkey = key.replace('S', 'β').replace('H', 'α').replace('αead', 'Head')
        if 'IGPS' in str(label_compo):
            _ = []
            for elt in newkey.split('-'):    
                if elt.split(':')[1] in ['A', 'C', 'E']:
                    _.append('𝘧{}'.format(elt.split(':')[0]))
                elif elt.split(':')[1] in ['B', 'D', 'F']:
                    _.append('𝘩{}'.format(elt.split(':')[0]))
            newkey = '-'.join(_)
            mapss[key] = IGPS_mapping[newkey]      
        else:
            mapss[key] = newkey     

    for ss in SS2nodelist:
        for node in SS2nodelist[ss]:
            node2SS[node] = mapss[ss]


    #Loading external data
    atom_mat1, atom_mat2 = list(map(load, [path1, path2]))
    get_ext = lambda X: X.split('.')[-1]
    ext1, ext2 = list(map(get_ext, [path1, path2]))
    top1 = load(path1.split('_')[0].split('.')[0]+'.topy') if top1 == None else load(top1)
    top2 = load(path2.split('_')[0].split('.')[0]+'.topy') if top2 == None else load(top2)

    #Handling selections
    if sele != None:
        sele1, sele2 = [sele]*2
    if sele == None and sele1 == None and sele2 == None:
        sele1, sele2 = ['protein && not hydrogen']*2
        print('Default selection protein without hydrogens')
    
    sels = [sele1, sele2]

    #Creating topology matrices for each selection
    topg1, topd1 = [create_top(sel, top1, fromstruct) for sel in sels]
    topg2, topd2 = [create_top(sel, top2, fromstruct) for sel in sels]
    #From atomic to residual contacts and perturbation network computation
    mat1 = (atom_mat1 @ topd1).transpose() @ topg1
    mat2 = (atom_mat2 @ topd2).transpose() @ topg2
    #Apply expected norm if necessary
    if norm_expected:
        exp1 = (topd1.sum(axis=1).transpose() @ topd1).transpose() @ (topg1.sum(axis=1).transpose() @ topg1)
        exp2 = (topd2.sum(axis=1).transpose() @ topd2).transpose() @ (topg2.sum(axis=1).transpose() @ topg2)
        mat1 = divide_expected(mat1, exp1)
        mat2 = divide_expected(mat2, exp2)
        mat1, mat2 = list(map(csr_matrix, [mat1, mat2]))

    if align_with != None:
        cmd.align(align_with, userSelection, object='aln')
        raw_aln = cmd.get_raw_alignment('aln')
        cmd.hide('cgo', 'aln')
        order_string = [idx[0] for idx in raw_aln[-1]][::-1]
        trans_mat = dok_matrix(tuple([cmd.count_atoms(X) for X in order_string]))
        for idx1, idx2 in raw_aln:
            trans_mat[idx2[1]-1, idx1[1]-1] = 1
        trans_mat = csr_matrix(trans_mat)
        top_t1, top_t2 = [create_top('name CA', top) for top in [top1, top2]]
        trans_res = (trans_mat @ top_t1).transpose() @ top_t2
        mat2 = trans_res @ (mat2 @ trans_res.transpose())

    pertmat = mat2 - mat1

    pertmat.setdiag(0)
    pertmat.eliminate_zeros()
    
    net = nx.from_scipy_sparse_matrix(pertmat)

    #Creating labeling dictionnary
    if str(next(top1.residues))[-1] == '0':
        offset = 1
    else:
        offset = 0

    chain_names = [chr(ord('A') + i) for i in range(26)]

    t2o = lambda X: three2one[X] if X in three2one else X[0]
    get_chain = lambda X: chain_names[(X.chain.index % len(chain_names))]
    res2str = lambda X: t2o(X.name)+str(X.resSeq+offset)+':'+get_chain(X)
    id2label = {i: res2str(res) for i, res in enumerate(top1.residues)}
    # if 'IGPS' in label_compo:
    #     igps_label = {}
    #     for elt in id2label.items():
    #         if elt.split(':')[1] in ['A', 'C', 'E']:
    #             rerelabel[elt] = '𝘧{}'.format(elt.split(':')[0])
    #         elif elt.split(':')[1] in ['B', 'D', 'F']:
    #             rerelabel[elt] = '𝘩{}'.format(elt.split(':')[0])
    #Relabeling network
    net = nx.relabel_nodes(net, id2label)

    label2id = {res2str(res): i for i, res in enumerate(top1.residues)}



    #Auto_patching network labels
    if not all(elem in node2CA for elem in net.nodes()):
        print('PDB structure and topology labeling not matching.')
        if auto_patch:
            print('Attempting to auto-patch residue names. (this can be disabled with auto_patch=False)')
            if len(node2CA.keys()) == len(net.nodes()):
                remap = dict(zip(net.nodes(), node2CA.keys()))
                net = nx.relabel_nodes(net, remap)
                label2id = dict(zip(node2CA.keys(), range(top1.n_residues)))
            else:
                print("Auto-patching not working, please try on different PDB file")


    #Output topK if necessary
    if type(topk) == int:
        limit_weight = np.sort([abs(net.edges[(u, v)]['weight']) for u, v in net.edges])[::-1][topk] 
        threshold = limit_weight

    if type(standard_and_expected) == int:
        limit_weight = np.sort([abs(net.edges[(u, v)]['weight']) for u, v in net.edges])[::-1][standard_and_expected]
        relabel_net2 = dict(enumerate(net.nodes()))
        threshold = limit_weight


    if max_compo or mean_vp or any(np.array([compo_size, compo_diam, compo_radius, strong_compo])!= None): 
        color_by_compo = True
        if load_cc != None:
            cc = np.load(load_cc)
        else:
            cc = get_connected_components(pertmat)
            if save_cc != None:
                np.save(save_cc, cc)
        if max_compo:
            threshold = np.sort(np.abs(pertmat.data))[::-1][np.argmax(cc[::-1])]
        else:
            lastmax = np.sort(np.abs(pertmat.data))[::-1][np.argmax(cc[::-1])]
            print('last maximum: {}'.format(np.round(lastmax, 2)))
            net.remove_edges_from([(u, v) for u, v in net.edges() if abs(net[u][v]['weight']) < lastmax])
            net.remove_nodes_from(list(nx.isolates(net)))
            components_list = [net.subgraph(c).copy() for c in nx.connected_components(net)] 
            if mean_vp:
                vanishing_points = [np.max([abs(net[u][v]['weight']) for u, v in c.edges()]) for c in components_list]
                threshold = np.median(vanishing_points)
            elif compo_size !=None:
                robust = [list(c.nodes()) for c in components_list if len(c.edges())>=float(compo_size)]
                net = net.subgraph([x for robust in list(robust) for x in robust])
                threshold = 0
            elif compo_diam !=None:
                robust = [list(c.nodes()) for c in components_list if nx.diameter(c)>=float(compo_diam)]
                net = net.subgraph([x for robust in list(robust) for x in robust])
                threshold = 0
            elif compo_radius !=None:
                robust = [list(c.nodes()) for c in components_list if nx.radius(c)>=float(compo_radius)]
                net = net.subgraph([x for robust in list(robust) for x in robust])
                threshold = 0
            elif strong_compo !=None:
                vanishing_points = [np.max([abs(net[u][v]['weight']) for u, v in c.edges()]) for c in components_list]
                edges_len = [len(c.edges()) for c in components_list]
                percentile = float(strong_compo)*len(components_list)/100
                vani_ranks = len(vanishing_points)+1-rankdata(vanishing_points, method='max')
                size_ranks = len(edges_len)+1-rankdata(edges_len, method='max')
                vani_nodes = [list(c.nodes()) for i, c in enumerate(components_list) if vani_ranks[i]<percentile]
                size_nodes = [list(c.nodes()) for i, c in enumerate(components_list) if size_ranks[i]<percentile]
                vani_nodes = [x for vani_nodes in list(vani_nodes) for x in vani_nodes]
                size_nodes = [x for size_nodes in list(size_nodes) for x in size_nodes]
                strong = list(set(vani_nodes) & set(size_nodes))
                net = net.subgraph(strong)


   #Detect mutations
    if mutations:
        cmd.show_as(representation="cartoon", selection="?mutations")
        cmd.color(color="grey80", selection="?mutations")
        cmd.delete("?mutations")
        mutations_list = []
        y = {j: res2str(res) for j, res in enumerate(top2.residues)}
        for resid in id2label:
            if resid in y:
                if id2label[resid] != y[resid]:
                    mutations_list.append((resid, (y[resid][0]+':').join(id2label[resid].split(':'))))
                    cmd.select("mutations", 'resi '+str(id2label[resid].split(':')[0][1:])+ ' and chain '+id2label[resid][-1], merge=1)
            else:
                print('Deletion of ', id2label[resid])
        print('List of mutations: ', ', '.join([elt[1] for elt in mutations_list]))
        cmd.show_as(representation="licorice", selection="?mutations")
        cmd.color(color="magenta", selection="?mutations")


    #Apply threshold
    if threshold !=0:
        print('Applying threshold {}'.format(threshold))
        net.remove_edges_from([(u, v) for u, v in net.edges() if abs(net[u][v]['weight']) < threshold])
        net.remove_nodes_from(list(nx.isolates(net)))

    #Induced perturbation network if needed

    if around !=None:
        net = net.subgraph(nx.node_connected_component(net, around))

    #Setting Pymol parameters
    cmd.set('auto_zoom', 0)
    cmd.set("cgo_sphere_quality", 4)
    if len(net.edges()) == 0:    
        raise ValueError('Computations give empty network')

    #Norm edges
    if edge_norm == None:
        edge_norm = max([net.edges()[(u, v)]['weight'] for u, v in net.edges()])/r

    elif edge_norm == True:
        tot_atoms_in_sel = np.sum([np.sum(elt) for elt in [topd1, topd2, topg1, topg2]])
        tot_atoms = np.sum([max(elt.shape) for elt in [topd1, topd2, topg1, topg2]])
        norm_fact = tot_atoms_in_sel**2/tot_atoms**2
        edge_norm = norm_fact*30
        print('Global normalization factor: {}'.format(1/norm_fact))


    #Function to name edges
    def name_edges(name, path):
        if name == None:
            return '.'.join(basename(path).split('.')[:-1])
        return name

    if type(standard_and_expected) == int:
        exp1 = (topd1.sum(axis=1).transpose() @ topd1).transpose() @ (topg1.sum(axis=1).transpose() @ topg1)
        exp2 = (topd2.sum(axis=1).transpose() @ topd2).transpose() @ (topg2.sum(axis=1).transpose() @ topg2)
        mat1 = divide_expected(mat1, exp1)
        mat2 = divide_expected(mat2, exp2)
        mat1, mat2 = list(map(csr_matrix, [mat1, mat2]))
        net2 = nx.from_scipy_sparse_matrix(mat2-mat1)
        net2 = nx.relabel_nodes(net2, relabel_net2)
        limit_weight = np.sort([abs(net2.edges[(u, v)]['weight']) for u, v in net2.edges])[::-1][standard_and_expected] 
        net2.remove_edges_from([(u, v) for u, v in net2.edges() if abs(net2[u][v]['weight']) < limit_weight])
        net2.remove_nodes_from(list(nx.isolates(net2)))
        colors = [(1, 1, 0), (0, 1, 1), (1, 0, 1)]
        objs_inboth = []
        objs_instd = []
        objs_inexp = []
        nodes = []
        for u, v in net.edges():
            radius = net[u][v]['weight']/edge_norm
            if (u, v) in list(net2.edges()):
                objs_inboth += [CYLINDER, *node2CA[u], *node2CA[v], radius, *colors[0], *colors[0]]
            else:
                objs_instd += [CYLINDER, *node2CA[u], *node2CA[v], radius, *colors[1], *colors[1]]
            nodes += [u, v]
        edge_norm2 = max([net2.edges()[(u, v)]['weight'] for u, v in net2.edges()])/r
        for u, v in net2.edges():
            radius = net2[u][v]['weight']/edge_norm2
            if (u, v) not in list(net.edges()):
                objs_inexp += [CYLINDER, *node2CA[u], *node2CA[v], radius, *colors[2], *colors[2]]
            nodes += [u, v]

        nodelist = set(nodes)
        objs_nodes = [COLOR, *node_color]
        for u in nodelist:
                x, y, z = node2CA[u]
                objs_nodes += [SPHERE, x, y, z, r]
        selnodes = ''.join([node2id[u] for u in nodelist])[4:]
        cmd.load_cgo(objs_inboth, 'in_both_edges') 
        cmd.load_cgo(objs_instd, 'in_std_edges')
        cmd.load_cgo(objs_inexp, 'in_exp_edges')
        cmd.load_cgo(objs_nodes, 'nodes') 



    elif color_by_contact_type:
        expected_matrices = get_expected_type(atom_mat1, atom_mat2, top1, top2, fromstruct)
        name1, name2 = list(map(name_edges, [name1, name2], [path1, path2]))
        names = ['{0}_{1}'.format(name1, sel) for sel in ['hydro', 'polar', 'mixed']] + ['{0}_{1}'.format(name2, sel) for sel in ['hydro', 'polar', 'mixed']]
        nodes_dict = {i: [] for i in range(len(names))}
        objs_dict = {i: [] for i in range(len(names))}
        colors = [(1, 0.86, 0.73), (0.68, 0.85, 0.90), (0.60, 0.98, 0.60), (1, 0.86, 0), (0.25, 0.41, 0.88), (0, 0.50, 0)]
        for u, v in net.edges():
            radius = net[u][v]['weight']/edge_norm
            id_u, id_v = label2id[u], label2id[v]
            values = list(map(lambda _mat: _mat[id_v, id_u], expected_matrices))
            type_of_contact = np.argmax(values)
            objs_dict[type_of_contact] += [CYLINDER, *node2CA[u], *node2CA[v], radius, *colors[type_of_contact], *colors[type_of_contact]]
            nodes_dict[type_of_contact] += [u, v]
        selnodes = ''
        for toc in nodes_dict:
            nodelist = set(nodes_dict[toc])
            objs_dict[toc]+=[COLOR, *node_color]
            for u in nodelist:
                x, y, z = node2CA[u]
                objs_dict[toc]+=[SPHERE, x, y, z, r]
            selnodes += ''.join([node2id[u] for u in nodelist])[4:]

        for i, name in zip(objs_dict.keys(), names):
            cmd.load_cgo(objs_dict[i], '{}_edges'.format(name))         
    
    #Coloring by components
    elif color_by_compo:
        components_list = [net.subgraph(c).copy() for c in nx.connected_components(net)]
        diameters = [nx.diameter(c) for c in components_list]
        ranking = np.argsort(diameters)[::-1]
        colors = sns.color_palette(palette, n_colors=len(components_list)+1)
        for i, c in enumerate(colors):
            if c[0] == c[1] == c[2]:
                print(c)
                colors.pop(i)
                break
        selnodes = ''
        for i, rank in enumerate(ranking):
            color, compo = colors[rank], components_list[rank]
            _obj, nodelist = [], []
            for u, v in compo.edges():
                radius = net[u][v]['weight']/edge_norm
                if abs(net[u][v]['weight']) >= threshold:
                    if not force_binary_color:
                        _obj+=[CYLINDER, *node2CA[u], *node2CA[v], radius, *color, *color]
                    else:
                        if net[u][v]['weight'] <= 0:
                            _obj+=[CYLINDER, *node2CA[u], *node2CA[v], radius, *edge_color1, *edge_color1]
                        else:
                            _obj+=[CYLINDER, *node2CA[u], *node2CA[v], radius, *edge_color2, *edge_color2]
                    nodelist += [u, v]
#            cmd.load_cgo(_obj, 'Component{}_edges'.format(i+1))
            _obj+=[COLOR, *node_color]
            nodelist = set(nodelist)
            selnodes += ''.join([node2id[u] for u in nodelist])[4:]
            for u in nodelist:
                x, y, z = node2CA[u]
                _obj+=[SPHERE, x, y, z, r]
            cmd.load_cgo(_obj, 'Component{}'.format(i+1)) 

    #Color by group of relevance  
    elif color_by_group:
        weights = np.array([abs(net[u][v]['weight']) for u, v in net.edges()]).reshape(-1, 1)
        birch = Birch(n_clusters=n_clusters).fit(weights)
        labels = birch.predict(weights)
        ordered_labels = labels[np.argsort(pertmat.data)]
        _, idx = np.unique(ordered_labels, return_index=True)
        mapping = dict(zip(ordered_labels[np.sort(idx)], np.sort(np.unique(ordered_labels))))
        i2color =  dict(zip(ordered_labels[np.sort(idx)], sns.color_palette(palette, len(np.unique(ordered_labels)))[::-1]))
        selnodes = ''
        if show_top_group == None:
            show_top_group = len(mapping.keys())
        
        for j, i in enumerate(list(mapping.keys())[:show_top_group]):
            _obj, nodelist = [], []
            _net = net.copy()
            to_remove_edges = [(u, v) for j, (u, v) in enumerate(net.edges()) if labels[j] != i]
            _net.remove_edges_from(to_remove_edges)
            _net.remove_nodes_from(list(nx.isolates(_net)))
            for u, v in _net.edges():
                radius = net[u][v]['weight']/edge_norm
                if abs(net[u][v]['weight']) >= threshold:
                    _obj+=[CYLINDER, *node2CA[u], *node2CA[v], radius, *i2color[j], *i2color[j]]
                    nodelist += [u, v]
#            cmd.load_cgo(_obj, 'Component{}_edges'.format(i+1))
            _obj+=[COLOR, *node_color]
            nodelist = set(nodelist)
            selnodes += ''.join([node2id[u] for u in nodelist])[4:]
            for u in nodelist:
                x, y, z = node2CA[u]
                _obj+=[SPHERE, x, y, z, r]
            cmd.load_cgo(_obj, 'Group{}'.format(j+1)) 

    #Default edge coloring   
    else:
        obj1, obj2, nodelist = [], [], []
        for u, v in net.edges():
            radius = net[u][v]['weight']/edge_norm
            if abs(net[u][v]['weight']) >= threshold:
                if 'color' in net[u][v]: 
                    if net[u][v]['color'] == 'r':
                        obj1+=[CYLINDER, *node2CA[u], *node2CA[v], radius, *edge_color1, *edge_color1]
                    else:
                        obj2+=[CYLINDER, *node2CA[u], *node2CA[v], radius, *edge_color2, *edge_color2]
                else:
                    if net[u][v]['weight'] <= 0:
                        obj1+=[CYLINDER, *node2CA[u], *node2CA[v], radius, *edge_color1, *edge_color1]
                    else:
                        obj2+=[CYLINDER, *node2CA[u], *node2CA[v], radius, *edge_color2, *edge_color2]
                nodelist+=[u, v]
        name1, name2 = map(name_edges, [name1, name2], [path1, path2])
        cmd.load_cgo(obj1, name1+'_edges')
        cmd.load_cgo(obj2, name2+'_edges')

        #Drawing nodes 
        obj=[COLOR, *node_color]
        nodelist = set(nodelist)
        selnodes = ''.join([node2id[u] for u in nodelist])[4:]
        for u in nodelist:
            x, y, z = node2CA[u]
            obj+=[SPHERE, x, y, z, r]

        cmd.load_cgo(obj, name_nodes)


    #Creating text for labeling components
    if label_compo != '' or compos_to_excel !=None:
        if compos_to_excel != None:
            rows_list = []
        objtxt = []
        axes = -np.array(cmd.get_view()[:9]).reshape(3,3)
        components_list = [net.subgraph(c).copy() for c in nx.connected_components(net)]
        diameters = [nx.diameter(c) for c in components_list]
        for i, j in enumerate(np.argsort(diameters)[::-1]):
            row_dict = {}
            c = components_list[j]
            sses = sorted(list(set([node2SS[node] for node in c])))
            if compos_to_excel !=None:
                row_dict['Secondary structure elements'] = ','.join(sses)
                row_dict['Vanishing point'] = np.max([abs(net[u][v]['weight']) for u, v in c.edges()])
                row_dict['Diameter'] = nx.diameter(c)
                row_dict['Size'] = len(c.edges())
                row_dict['Size rank'] = i+1

            else:
                print('Component {}\n'.format(i+1), ', '.join(sses))
                print('Size (number of edges) {}'.format(len(c.edges())))
                print('Vanishing point: {}'.format(np.max([abs(net[u][v]['weight']) for u, v in c.edges()])))
            if 'h' in str(label_compo):
                methods = ['eigenvector', 'hits_hub', 'hits_authority', 'pagerank', 'betweenness', 'katz']
                hubs = [get_hubs(c, method) for method in methods]
                if compos_to_excel !=None:
                    row_dict.update(dict(zip(methods, hubs)))
                else:
                    print(dict(zip(methods, hubs)))
            if 'c' in str(label_compo):
                pos = np.array(node2CA[next(c.__iter__())]) + (axes[0])
                cyl_text(objtxt, plain, pos, 'Component {}'.format(i+1), radius=0.1, color=[0, 0, 0], axes=axes)
            if compos_to_excel:
                rows_list.append(row_dict)
        if compos_to_excel:
            df = pd.DataFrame(rows_list)
            df.to_excel(compos_to_excel)
        if 's' in str(label_compo):
            for ss in SS2nodelist:
                nodelist = SS2nodelist[ss] 
                print(mapss[ss], ': ', ('{}--{}'.format(nodelist[0], nodelist[-1]) if len(nodelist)>1 else nodelist[0]))

#        print(objtxt)
        cmd.set("cgo_line_radius", 0.03)
        cmd.load_cgo(objtxt, 'txt')

    #labeling
    if labeling==1:
        cmd.label(selection=selnodes, expression="t2o(resn)+resi")
    if labeling==3:
        cmd.label(selection=selnodes, expression="resn+resi")

    #Summing
    if sum:
        print('Sum of contacts lost: ', np.sum(pertmat))

    if printall:
        print([(u,v, net[u][v]) for u, v in net.edges()])
Ejemplo n.º 31
0
	max_len = max(lens)
	print("Eccentricity of 0 vertex is:", max_len - 1)
	print(eccentricity[max_len])

	ddd = eccentricity.keys()

	if (n<=500):
		print("Find center?")
		key4 = input("y/n?")
		if (key4 == "y"):
			cent = nx.center(G)
			print("Center - ",cent)
			diam = nx.diameter(G)
			print("Diameter - ",diam)
			rad = nx.radius(G)
			print("Radius - ",rad)

	print("Draw Graph?")
	key3 = input("y/n? ")
	if (key3 == "y"):
		nx.draw(G, with_labels = True)
		plt.savefig("plot.png")
		plt.show()
	print(nx.info(G))

	#Поменять количество вершин в графе?
	print("Do u want another quantity of Vertex? ")
	key = input("y/n? \n")
	if (key == "y"):
		launch = True
def rad(net):
    return ((nx.radius(net,e=eccentricity_list(net)),'radius'),)
Ejemplo n.º 33
0
L = nx.Graph()
L.add_weighted_edges_from(mas)

#веса связного графа
fin = open('graphsv.txt', 'r')
mas = [[0] * 3 for i in range(82)]
for i in range(82):
    s = fin.readline()
    k = s.split()
    for j in range(2):
        mas[i][j] = k[j]
    mas[i][2] = int(k[2])
G = nx.Graph()
G.add_weighted_edges_from(mas)
#b
print("rad(G) = ", nx.radius(G), ", diam(G) = ", nx.diameter(G),
      ", girth(G) = ", len(min(nx.cycle_basis(G))), ", center(G) = ",
      nx.center(G), ", k_node_connectivity = ", nx.node_connectivity(G),
      ", k_edge_connectivity = ", nx.edge_connectivity(G))
print()

#maxmatching g
print("M =", nx.max_weight_matching(L))
print()

#mst о
MST = nx.Graph(mst.minimum_spanning_tree(G))
print("MST = ", MST.edges)
print("MST =", sum(c for (a, b, c) in (MST.edges.data('weight'))))
print()
Ejemplo n.º 34
0
def extended_stats(G, connectivity=False, anc=False, ecc=False, bc=False, cc=False):
    """
    Calculate extended topological stats and metrics for a graph. 
    
    Global topological analysis of large complex networks is extremely 
    time consuming and may exhaust computer memory. Consider using function 
    arguments to not run metrics that require computation of
    a full matrix of paths if they will not be needed.
    
    Parameters
    ----------
    G : networkx multidigraph
    connectivity : bool
        if True, calculate node and edge connectivity
    anc : bool, if True
        calculate average node connectivity
    ecc : bool
        if True, calculate shortest paths, eccentricity, and topological metrics that use eccentricity
    bc : bool
        if True, calculate node betweenness centrality
    cc : bool
        if True, calculate node closeness centrality
    
    Returns
    -------
    stats : dict
        dictionary of network measures containing the following elements (some only calculated/returned optionally, based on passed parameters):
        
          - avg_neighbor_degree
          - avg_neighbor_degree_avg
          - avg_weighted_neighbor_degree
          - avg_weighted_neighbor_degree_avg
          - degree_centrality
          - degree_centrality_avg
          - clustering_coefficient
          - clustering_coefficient_avg
          - clustering_coefficient_weighted
          - clustering_coefficient_weighted_avg
          - pagerank
          - pagerank_max_node
          - pagerank_max
          - pagerank_min_node
          - pagerank_min
          - node_connectivity
          - node_connectivity_avg
          - edge_connectivity
          - eccentricity
          - diameter
          - radius
          - center
          - periphery
          - closeness_centrality
          - closeness_centrality_avg
          - betweenness_centrality
          - betweenness_centrality_avg
          
    """
    
    stats = {}
    full_start_time = time.time()

    # create a DiGraph from the MultiDiGraph, for those metrics that require it
    G_dir = nx.DiGraph(G)

    # create an undirected Graph from the MultiDiGraph, for those metrics that require it
    G_undir = nx.Graph(G)

    # get the largest strongly connected component, for those metrics that require strongly connected graphs
    G_strong = get_largest_component(G, strongly=True)
    
    # average degree of the neighborhood of each node, and average for the graph
    avg_neighbor_degree = nx.average_neighbor_degree(G)
    stats['avg_neighbor_degree'] = avg_neighbor_degree
    stats['avg_neighbor_degree_avg'] = sum(avg_neighbor_degree.values())/len(avg_neighbor_degree)

    # average weighted degree of the neighborhood of each node, and average for the graph
    avg_weighted_neighbor_degree = nx.average_neighbor_degree(G, weight='length')
    stats['avg_weighted_neighbor_degree'] = avg_weighted_neighbor_degree
    stats['avg_weighted_neighbor_degree_avg'] = sum(avg_weighted_neighbor_degree.values())/len(avg_weighted_neighbor_degree)

    # degree centrality for a node is the fraction of nodes it is connected to
    degree_centrality = nx.degree_centrality(G)
    stats['degree_centrality'] = degree_centrality
    stats['degree_centrality_avg'] = sum(degree_centrality.values())/len(degree_centrality)

    # calculate clustering coefficient for the nodes
    stats['clustering_coefficient'] = nx.clustering(G_undir)
    
    # average clustering coefficient for the graph
    stats['clustering_coefficient_avg'] = nx.average_clustering(G_undir)
    
    # calculate weighted clustering coefficient for the nodes
    stats['clustering_coefficient_weighted'] = nx.clustering(G_undir, weight='length')

    # average clustering coefficient (weighted) for the graph
    stats['clustering_coefficient_weighted_avg'] = nx.average_clustering(G_undir, weight='length')

    # pagerank: a ranking of the nodes in the graph based on the structure of the incoming links
    pagerank = nx.pagerank(G_dir, weight='length')
    stats['pagerank'] = pagerank

    # node with the highest page rank, and its value
    pagerank_max_node = max(pagerank, key=lambda x: pagerank[x])
    stats['pagerank_max_node'] = pagerank_max_node
    stats['pagerank_max'] = pagerank[pagerank_max_node]

    # node with the lowest page rank, and its value
    pagerank_min_node = min(pagerank, key=lambda x: pagerank[x])
    stats['pagerank_min_node'] = pagerank_min_node
    stats['pagerank_min'] = pagerank[pagerank_min_node]
    
    # if True, calculate node and edge connectivity
    if connectivity:
        start_time = time.time()
        
        # node connectivity is the minimum number of nodes that must be removed to disconnect G or render it trivial
        stats['node_connectivity'] = nx.node_connectivity(G_strong)

        # edge connectivity is equal to the minimum number of edges that must be removed to disconnect G or render it trivial
        stats['edge_connectivity'] = nx.edge_connectivity(G_strong)
        log('Calculated node and edge connectivity in {:,.2f} seconds'.format(time.time() - start_time))
    
    # if True, calculate average node connectivity    
    if anc:
        # mean number of internally node-disjoint paths between each pair of nodes in G
        # i.e., the expected number of nodes that must be removed to disconnect a randomly selected pair of non-adjacent nodes
        start_time = time.time()
        stats['node_connectivity_avg'] = nx.average_node_connectivity(G)
        log('Calculated average node connectivity in {:,.2f} seconds'.format(time.time() - start_time))
        
    # if True, calculate shortest paths, eccentricity, and topological metrics that use eccentricity
    if ecc:
        # precompute shortest paths between all nodes for eccentricity-based stats
        start_time = time.time()
        sp = {source:dict(nx.single_source_dijkstra_path_length(G_strong, source, weight='length')) for source in G_strong.nodes()}

        log('Calculated shortest path lengths in {:,.2f} seconds'.format(time.time() - start_time))
        
        # eccentricity of a node v is the maximum distance from v to all other nodes in G
        eccentricity = nx.eccentricity(G_strong, sp=sp)
        stats['eccentricity'] = eccentricity

        # diameter is the maximum eccentricity
        diameter = nx.diameter(G_strong, e=eccentricity)
        stats['diameter'] = diameter

        # radius is the minimum eccentricity
        radius = nx.radius(G_strong, e=eccentricity)
        stats['radius'] = radius

        # center is the set of nodes with eccentricity equal to radius
        center = nx.center(G_strong, e=eccentricity)
        stats['center'] = center

        # periphery is the set of nodes with eccentricity equal to the diameter
        periphery = nx.periphery(G_strong, e=eccentricity)
        stats['periphery'] = periphery
    
    # if True, calculate node closeness centrality
    if cc:
        # closeness centrality of a node is the reciprocal of the sum of the shortest path distances from u to all other nodes
        start_time = time.time()
        closeness_centrality = nx.closeness_centrality(G, distance='length')
        stats['closeness_centrality'] = closeness_centrality
        stats['closeness_centrality_avg'] = sum(closeness_centrality.values())/len(closeness_centrality)
        log('Calculated closeness centrality in {:,.2f} seconds'.format(time.time() - start_time))
        
    # if True, calculate node betweenness centrality
    if bc:
        # betweenness centrality of a node is the sum of the fraction of all-pairs shortest paths that pass through node
        start_time = time.time()
        betweenness_centrality = nx.betweenness_centrality(G, weight='length')
        stats['betweenness_centrality'] = betweenness_centrality
        stats['betweenness_centrality_avg'] = sum(betweenness_centrality.values())/len(betweenness_centrality)
        log('Calculated betweenness centrality in {:,.2f} seconds'.format(time.time() - start_time))
    
    log('Calculated extended stats in {:,.2f} seconds'.format(time.time()-full_start_time))
    return stats
Ejemplo n.º 35
0
__author__ = 'fhca'

import networkx as nx
import matplotlib.pyplot as plt
import numpy as np

g = nx.Graph()
g.add_edge("a", "c")
g.add_edge("b", "c")
g.add_edge("c", "e")
g.add_edges_from([("e","d"),
                  ("e","g"),
                  ("e","f")])
g.add_edges_from([("f","g"),
                  ("f","j")
                  ])
g.add_edges_from([("i","h"),
                  ("i","g"),
                  ("i","j")])
g.add_edge("h","g")
g.add_edge("j","g")
nx.draw_networkx(g)
nodos_ordenados = np.sort(g.nodes())
print(nx.to_numpy_matrix(g, nodelist=nodos_ordenados))
print("excentricidad (distancia máxima a otros nodos)", nx.eccentricity(g))
print("centro (nodos con mínima excentricidad)", nx.center(g))
print("periferia (nodos con máxima excentricidad)", nx.periphery(g))
print("radio:", nx.radius(g))
plt.show()
Ejemplo n.º 36
0
def creationVecteur(G):
    v = {}
    # Adding nodes
    nn = nx.number_of_nodes(G)
    v["numNodes"] = nn

    # Adding edges
    ne = nx.number_of_edges(G)
    v["numEdges"] = ne

    # Adding cyclomatic number
    c = nx.number_connected_components(G)
    cyclo = ne - nn + c
    v["numCycles"] = cyclo

    # Adding link density
    if nn == 1:
        linkdensity = "?"
    else:
        linkdensity = 2 * ne / ((nn - 1) * nn)
    v["linkDensity"] = linkdensity

    # Adding average degree
    avgdegree = 2 * ne / nn
    v["avgDegree"] = avgdegree

    # Adding number of leaves
    nl = numberLeaves(G)
    v["numLeafs"] = nl

    # Adding histogram of the nodes degree
    v["histDegree0"] = 0
    v["histDegree1"] = 0
    v["histDegree2"] = 0
    v["histDegree3"] = 0
    v["histDegree4"] = 0
    histDegree = nx.degree_histogram(G)
    v["histDegree0"] = histDegree[0]
    if len(histDegree) > 1:
        v["histDegree1"] = histDegree[1]
        if len(histDegree) > 2:
            v["histDegree2"] = histDegree[2]
            if len(histDegree) > 3:
                v["histDegree3"] = histDegree[3]
                if len(histDegree) > 4:
                    v["histDegree4"] = histDegree[4]

    # Adding sMetric
    v["sMetric"] = metric(G)

    # Adding graph energy
    energ = graphEnergy(G)
    v["graphEnergy"] = energ

    # Adding average of the average neighboring degrees of all nodes
    av = averageNeighDegree(G)
    v["averageNeighDegree"] = av

    # Adding average of closeness over all nodes
    v["averageCloseness"] = average_closeness(G)

    # Adding pearson coefficient for the degree sequence of all edges of the graph
    pearson = nx.degree_pearson_correlation_coefficient(G)
    if np.isnan(pearson):
        pearson = 0
    v["pearson"] = pearson

    # Adding rich club metric for all nodes with a degree larger than 1
    rc = richClub(G)
    v["richClub"] = rc

    # Adding algebraic connectivity, i.e. the second smallest eigenvalue of the Laplacian
    algConnect = nx.laplacian_spectrum(G)
    algConnect = list(algConnect)
    algConnect = sorted(algConnect)
    v["algConnect"] = algConnect[1]

    # Adding diameter of the graph
    if nx.is_connected(G):
        diam = nx.diameter(G)

    else:
        diam = "?"
    v["diameter"] = diam

    # Adding average shortest path
    if nx.is_connected(G):
        avgShortestPath = nx.average_shortest_path_length(G)

    else:
        avgShortestPath = "?"
    v["avgShortPath"] = avgShortestPath

    # Adding graph radius
    if nx.is_connected(G):
        rad = nx.radius(G)
    else:
        rad = "?"
    v["graphRadius"] = rad

    return v
Ejemplo n.º 37
0
    def detect(self):
        print('epa_center带权重检测')

        if self.subgraph.number_of_nodes() == 0:
            print 'subgraph.number_of_nodes =0'
            return
        self.reset_centrality()
        centrality = {}
        P_node_prominence = defaultdict(int)
        for infect_node in self.subgraph.nodes():
            # print('是哪个点')
            # print(infect_node)
            neigbour_node = nx.neighbors(self.data.graph, infect_node)
            neigbour_infect_node = [
                x for x in neigbour_node if x in self.subgraph.nodes()
            ]
            Iv = len(neigbour_infect_node)
            Ov = len(neigbour_node)
            P_node_prominence[infect_node] = Iv * 1.0 / Ov * (1 + math.log(Ov))
            # print('输出权重')
            #加一个对感染边权重的处理。
            # print(self.data.weights)
            #分子
            molecule = 0
            for neigbour_infect in neigbour_infect_node:
                molecule += (
                    1 -
                    self.data.weights[self.data.node2index[infect_node],
                                      self.data.node2index[neigbour_infect]])
            # print('分子是')
            # print(molecule)
            Denominator = 0
            for neigbour in neigbour_node:
                Denominator += (
                    1 - self.data.weights[self.data.node2index[infect_node],
                                          self.data.node2index[neigbour]])
            # print('分母是')
            # print(Denominator)
            factor = molecule * 1.0 / Denominator
            # print('看下factor')
            # print(factor)
            P_node_prominence[infect_node] *= factor

        # print('P_node_prominence')
        # print(P_node_prominence)
        radius = nx.radius(self.subgraph)
        ecc = nx.eccentricity(self.subgraph)
        # print('ecc')
        # print(ecc)
        # print('半径是多少?')
        # 进行所有点有向树构建,再进行层次遍历。针对每一层都进行传播点/全部的比例计算。
        node_every_ratio = []
        temp_nodes = self.subgraph.nodes()
        for source in self.subgraph.nodes():
            tree = nx.bfs_tree(self.data.graph, source=source)
            # 进行层次遍历。返回每一层顶点。
            BFS_nodes = self.BFS_nodes(tree, source, self.data.graph,
                                       self.subgraph, 2 * radius)
            # print(BFS_nodes)
            layer_node_sum = 0
            for layer_node in BFS_nodes:
                for evuery_layer_node in layer_node:
                    layer_node_sum += P_node_prominence[evuery_layer_node]
            # centrality[source] = Decimal(layer_node_sum)
            centrality[source] = Decimal(layer_node_sum * 1.0 / ecc[source])
        # print('让我看下这个ratio _average的centiality')
        # print(centrality)
        nx.set_node_attributes(self.subgraph, 'centrality', centrality)
        return self.sort_nodes_by_centrality()
Ejemplo n.º 38
0
 def radius(self):
     if not self.is_connected:
         raise Exception("Graph is not connected")
     return nx.radius(self.graph.structure)
Ejemplo n.º 39
0
 def _ComputeRadius(self, graphs):
     print 'Computing radius'
     for key in graphs.keys():
         self.featuresByGraph[key].append(networkx.radius(graphs[key]))
def radius(g, N, **kwargs):
    if N == 1: return 0
    return nx.radius(g)
Ejemplo n.º 41
0
Archivo: bot.py Proyecto: toskn/prgrm18
def centrality_and_params_graph(g):
    dict_return = {}
    degree_counter = []
    betweenness_counter = []
    closeness_counter = []
    eigen_counter = []

    deg = nx.degree_centrality(g)
    for nodeid in sorted(deg, key=deg.get, reverse=True):
        degree_counter.append(nodeid)
    biggest_deg = {
        key: value
        for key, value in deg.items()
        if value in sorted(set(deg.values()), reverse=True)[:2]
    }
    bet = nx.betweenness_centrality(g)
    for nodeid in sorted(bet, key=bet.get, reverse=True):
        betweenness_counter.append(nodeid)
    biggest_bet = {
        key: value
        for key, value in bet.items()
        if value in sorted(set(bet.values()), reverse=True)[:2]
    }
    cls = nx.closeness_centrality(g)
    for nodeid in sorted(cls, key=cls.get, reverse=True):
        closeness_counter.append(nodeid)
    biggest_cls = {
        key: value
        for key, value in cls.items()
        if value in sorted(set(cls.values()), reverse=True)[:2]
    }
    eig = nx.eigenvector_centrality(g)
    for nodeid in sorted(eig, key=eig.get, reverse=True):
        eigen_counter.append(nodeid)
    biggest_eig = {
        key: value
        for key, value in eig.items()
        if value in sorted(set(eig.values()), reverse=True)[:2]
    }

    radius = nx.radius(g)

    diameter = nx.diameter(g)

    assort_coef = nx.degree_pearson_correlation_coefficient(g)

    clustering = nx.average_clustering(g)

    node_number = g.number_of_nodes()

    edge_number = g.number_of_edges()

    communities = community.greedy_modularity_communities(g)

    dict_return = {
        'biggest_deg': biggest_deg,
        'biggest_bet': biggest_bet,
        'biggest_cls': biggest_cls,
        'biggest_eig': biggest_eig,
        'radius': radius,
        'diameter': diameter,
        'assort_coef': assort_coef,
        'clustering': clustering,
        'node_number': node_number,
        'edge_number': edge_number,
        'communities': communities
    }

    return dict_return
 def _ComputeRadius(self, graph):
     return networkx.radius(graph)
Ejemplo n.º 43
0
fin = open('digraph.txt', 'r')
mas = [[0] * 3 for i in range(170)]
for i in range(90):
    s = fin.readline()
    k = s.split()
    for j in range(2):
        mas[i][j] = k[j]
    mas[i][2] = int(k[2])
D = nx.DiGraph()
D.add_weighted_edges_from(mas)

# b)
print("b) |V| = ", G_.number_of_nodes(), ", |E| = ",
      G_.number_of_edges(), ", min_degree(G) = ",
      min(val for (node, val) in G.degree()), ", max_degree(G) = ",
      max(val for (node, val) in G.degree()), ", rad(G) = ", nx.radius(G),
      ", diam(G) = ", nx.diameter(G), ", girth(G) = ",
      len(min(nx.cycle_basis(G))),
      ", center(G) = ", nx.center(G), ", k_node_connectivity = ",
      nx.node_connectivity(G), ", k_edge_connectivity = ",
      nx.edge_connectivity(G))
print()

# c) Minimum vertex coloring
print("c) Colors:",
      max(nx.greedy_color(G).values()) + 1, ", Z =", nx.greedy_color(G))
print()

# d) Minimum edge coloring
print("d) Colors:",
      max(nx.greedy_color(nx.line_graph(G)).values()) + 1, ", E =",
Ejemplo n.º 44
0
        pass

labels = {}
for i in range(len(words)):
    if i in G.nodes():
        labels[i] = words[i]

deg = nx.degree_centrality(G)
degs = sorted(deg, key=deg.get, reverse=True)
print("Топ-5 центральных слов:")
for i in range(0, 5):
    print(labels[degs[i]])  # Топ-5 центральных
radii = []
for c in sorted(nx.connected_component_subgraphs(G), key=len, reverse=True):
    try:
        radii.append(nx.radius(c))
    except:
        pass
print()
print("Радиусы для различных компонент связности:")
for r in radii:
    print(r)  # Радиус
print()
print("Коэффициент кластеризации:")
print(nx.average_clustering(G))  # Кластеризация

# для начала надо выбрать способ "укладки" графа. Их много, возьмём для начала такой:
pos = nx.spring_layout(G)

nx.draw_networkx_nodes(
    G, pos, node_color='red',
    spl = nx.shortest_path_length(G, v)
    print('{} {} '.format(v, spl))
    for p in spl:
        pathlengths.append(spl[p])
# pathlengths

#%%
print("average shortest path length %s" %
      (sum(pathlengths) / len(pathlengths)))
# histogram of path lengths
dist = {}
for p in pathlengths:
    if p in dist:
        dist[p] += 1
    else:
        dist[p] = 1

print('')
print("length #paths")
verts = dist.keys()
for d in sorted(verts):
    # print('%s %d' % (d, dist[d]))
    print('{} {}'.format(d, dist[d]))

# print('{}'.format(nx.radius(G)))
print("eccentricity: %s" % nx.eccentricity(G))
print("radius: %d" % nx.radius(G))
print("diameter: %d" % nx.diameter(G))
print("center: %s" % nx.center(G))
print("periphery: %s" % nx.periphery(G))
print("density: %s" % nx.density(G))
Ejemplo n.º 46
0
import networkx as nx

g = nx.read_edgelist('0.edges')
sg = nx.connected_component_subgraphs(g)[0]

ecc = nx.eccentricity(sg)
r = nx.radius(sg)

center = [node for node in sg.nodes() if ecc[node] == r]

print center
Ejemplo n.º 47
0
# attracting components
nx.is_attracting_component(G)
nx.number_attracting_components(G)
nx.attracting_components(G)

# directed acyclic graphs
nx.is_directed_acyclic_graph(G)
nx.is_aperiodic(G)

# distance measure  (all for connected graph)
nx.center(Gcc)
nx.diameter(Gcc)
nx.eccentricity(Gcc)
nx.periphery(Gcc)
nx.radius(Gcc)

# flows (seg fault currently)
#nx.max_flow(Gcc, 1, 2)
#nx.min_cut(G, 1, 2)

# isolates
nx.is_isolate(G, 1)  # False
nx.is_isolate(G, 5)  # True

# HITS
nx.hits(G, max_iter=1000)  # cannot converge?

# maximal independent set
nx.maximal_independent_set(G)
Ejemplo n.º 48
0
from pylab import *
import networkx as nx

g = nx.karate_club_graph()
n = g.number_of_nodes()
m = g.number_of_edges()
L = nx.average_shortest_path_length(g)
D = nx.diameter(g)
R = nx.radius(g)

Ldiffs = []
Ddiffs = []
Rdiffs = []

for i in range(500):
    g2 = nx.gnm_random_graph(n, m)
    if nx.is_connected(g2):
        Ldiffs.append(nx.average_shortest_path_length(g2) - L)
        Ddiffs.append(nx.diameter(g2) - D)
        Rdiffs.append(nx.radius(g2) - R)

subplot(1, 3, 1)
hist(Ldiffs)
title('L diff')

subplot(1, 3, 2)
hist(Ddiffs)
title('D diff')

subplot(1, 3, 3)
hist(Rdiffs)
Ejemplo n.º 49
0
Folder_to_load_from = '{}'.format(Folder_to_load_from)
Folder_to_save_graphs = str(Folder_to_save_graphs)
Name_of_stat_file = '{}'.format(Name_of_stat_file)

stats = {}
#create graph
G = ox.save_load.load_graphml(Graphml_File_Name, folder=Folder_to_load_from)

#Calculate the center of graph, the folowing statement would return a list

stats['center'] = nx.center(G)

#calculate the diameter

stats['diameter'] = nx.diameter(G)

#caluculate the eccentricity
stats['eccentricity'] = nx.eccentricity(G)

#calculate the periphery of the graph

stats['periphery'] = nx.periphery(G)

# caluculate the radius

stats['radius'] = nx.radius(G, e=None)
# #statsfile=Graphml_File_Name+str('/centrality.csv')
filepath = Folder_to_save_graphs + '/' + Name_of_stat_file
df = pd.DataFrame.from_dict(stats)
df.to_csv(filepath)
            full_partition_prefix = sampling_algorithm+'_'+data_set+'_result/'+str(per_format)+'/output-prefix.t0000'+ str(i) 
            re=open(full_partition_prefix +'.graph', 'rb')
            sample_=nx.read_edgelist(re, nodetype=int)
            re.close() 
            print 'number of unique nodes:',sample_.number_of_nodes()
            print 'number of unique edges:',sample_.number_of_edges()
            #print 'nodes',original.nodes()
            
            sample=maximum_connected_components(sample_)
            #partition sample
         
            edges = nx.number_of_edges(sample)
            listEdges.append(edges)
            eccentricity = nx.diameter(sample)
            listEccentricity.append(eccentricity)
            radius = nx.radius(sample)
            listRadius.append(radius)
            
            
            part = community.best_partition(nx.Graph(sample))

            snapshotCommunities(full_partition_prefix, part)
            commsfile = full_partition_prefix + '.comms'

            partition_sample = se.ReadInData(commsfile)
            first_result_lines = se.Compare3(copy.deepcopy(partition_set), partition_sample, deltas, float(per), commsfile)
            
            precision_micro=first_result_lines[0][2]  
            recall_micro=first_result_lines[1][2]
            p9_precision_micro = first_result_lines[2][2]
            r9_recall_micro=first_result_lines[3][2]
Ejemplo n.º 51
0
                    help='sort by FIELD from specified FIELDS')
parser.add_argument('-t', '--top', metavar='NUM_USERS', type=int,
                    help='print only top NUM_USERS')

args = parser.parse_args()

try:
    start_time = time.time()

    G = io.read_graph(args.path)

    if args.info:
        print(nx.info(G), '\n')

    if args.radius:
        print('Graph radius: ', nx.radius(G))

    if args.diameter:
        print('Graph diameter: ', nx.diameter(G))

    if args.avg_friends:
        print('Average number of friends: ', stats.avg_num_friends(G))

    if args.avg_followers:
        print('Average number of followers: ', stats.avg_num_followers(G))

    if args.fields:
        try:
            from prettytable import PrettyTable
        except ImportError:
            print('This script requires PrettyTable library to be installed.')
Ejemplo n.º 52
0
 def calculate(graph):
     if nx.is_connected(graph):
         return nx.radius(graph)
     else:
         return 10**10
Ejemplo n.º 53
0
 def test_bound_radius(self):
     assert_equal(networkx.radius(self.G, usebounds=True), 4)
Ejemplo n.º 54
0
 def radius(self):
     if not self.is_connected:
         raise Exception("Graph is not connected")
     return nx.radius(self.graph.structure)
 def test_radius(self):
     assert_equal(networkx.radius(self.G),4)
Ejemplo n.º 56
0
import matplotlib.pyplot as plt

reseau_social = nx.Graph()

reseau_social.add_node('laurent')
reseau_social.add_node('pierre')
reseau_social.add_node('lucie')
reseau_social.add_node('sophie')
reseau_social.add_node('martin')
reseau_social.add_node('jacques')

reseau_social.add_edge('laurent', 'pierre')
reseau_social.add_edge('lucie', 'pierre')
#reseau_social.add_edge('laurent','lucie')
reseau_social.add_edge('sophie', 'lucie')
reseau_social.add_edge('sophie', 'pierre')
reseau_social.add_edge('sophie', 'martin')
reseau_social.add_edge('martin', 'laurent')
reseau_social.add_edge('jacques', 'martin')
reseau_social.add_edge('jacques', 'laurent')

nx.draw(reseau_social, with_labels=True)
plt.draw()
plt.show()

print("nombre de sommets=", reseau_social.number_of_nodes())
print("nombre de arêtes=", reseau_social.number_of_edges())
print("Diamètre=", diameter(reseau_social))
print("Rayon=", radius(reseau_social))
print("Centre=", center(reseau_social))
Ejemplo n.º 57
0
                g.add_edge(row,col)
    print "Done reading all the files"  
    graphs[item] = g


for g in graphs.keys():
    try:
        print "====================== " , g, "==========================="
        print "number of isolated nodes",'\t\t\t',nx.isolates(graphs[g])
        print "is graph biconnected?",'\t\t\t',nx.is_biconnected(graphs[g])
        print "cut vertices are: ",'\t\t\t',list(nx.articulation_points(graphs[g]))
        print "number of nodes",'\t\t\t',len(graphs[g].nodes())
        print "number of edges",'\t\t\t',len(graphs[g].edges())
        print "degree",'\t\t\t',get_avg(graphs[g].degree())
        print "diameter",'\t\t\t', nx.diameter(graphs[g])
        print "radius",'\t\t\t', nx.radius(graphs[g])
        print "is_bipartite?",'\t\t', bipartite.is_bipartite(graphs[g])
        print "average_shortest_path_length",'\t\t', nx.average_shortest_path_length(graphs[g])
        print "degree_assortativity_coefficient",'\t\t', nx.degree_assortativity_coefficient(graphs[g])
        print "assortativity.average_degree_connectivity",'\t\t', nx.assortativity.average_degree_connectivity(graphs[g])
        #print "degree_pearson_correlation_coefficient",'\t\t', nx.degree_pearson_correlation_coefficient(graphs[g])
        print "node closeness_centrality",'\t\t\t', get_avg(nx.closeness_centrality(graphs[g]))
        print "clustering",'\t\t\t', get_avg(nx.clustering(graphs[g]))
        print "node betweeness",'\t\t\t', get_avg(nx.betweenness_centrality(graphs[g],normalized=False,endpoints=False))
        print "edge betweeness",'\t\t\t', get_avg(nx.edge_betweenness_centrality(graphs[g],normalized=False))
        #print "spectral_bipartivity",'\t\t', bipartite.spectral_bipartivity(graphs[g])
        #print "node betweeness normalized",'\t\t\t', get_avg(nx.betweenness_centrality(graphs[g],normalized=True,endpoints=False))
        #print "edge betweeness normalized",'\t\t\t', get_avg(nx.edge_betweenness_centrality(graphs[g],normalized=True))
        #print "node closeness_vitality",'\t\t\t', get_avg(nx.closeness_vitality(graphs[g]))
        #print "communicability_centrality",'\t\t', get_avg(nx.communicability_centrality(graphs[g]))
        #print "communicability_betweenness_centrality",'\t\t', get_avg(nx.communicability_betweenness_centrality(graphs[g]))
Ejemplo n.º 58
0
                     rumorSourceList[0],
                     rumorSourceList[1],
                     weight='weight'))

# all_short_length=[]
# #求任意两点之间的最短路径。
# for  node1 in subinfectG.nodes:
#     for node2 in  subinfectG.nodes:
#         if node1==node2:
#             pass
#         else:
#             all_short_length.append(nx.dijkstra_path_length(G, node1, node2, weight='weight'))  # 求最短距离
#
# print (max(all_short_length))

print('#查看感染图半径' + str(nx.radius(subinfectG)))
print('查看感染图中心' + str(nx.center(subinfectG)))
print('查看感染图直径' + str(nx.diameter(subinfectG)))
# print ('查看感染图图外围'+str(nx.periphery(subinfectG)))
# print ('查看感染图某点偏心率'+str(nx.eccentricity(G,125)))

#随机生成u1,h1,u2.h2来让E(u1,h1,u2,h2)最大。

resutltList, lasth = contactu1h1u2h2(
    subinfectG, infectG, rumorSourceList, 5,
    6)  #这里的后三个参数。第一个是真实两源源点,第三个是这个源点的传播h,最后一个是试探h。
#返回的结果是两个heav center以及它的h,因为这里的h是一样的。

#现在分别计算在每个点下,找源点。看看reverse infection algorithm,试下第一个点

#第一步
Ejemplo n.º 59
0
def extended_stats(G, connectivity=False, anc=False, ecc=False, bc=False, cc=False):
    """
    Calculate extended topological stats and metrics for a graph.

    Many of these algorithms have an inherently high time complexity. Global
    topological analysis of large complex networks is extremely time consuming
    and may exhaust computer memory. Consider using function arguments to not
    run metrics that require computation of a full matrix of paths if they
    will not be needed.

    Parameters
    ----------
    G : networkx multidigraph
    connectivity : bool
        if True, calculate node and edge connectivity
    anc : bool
        if True, calculate average node connectivity
    ecc : bool
        if True, calculate shortest paths, eccentricity, and topological metrics
        that use eccentricity
    bc : bool
        if True, calculate node betweenness centrality
    cc : bool
        if True, calculate node closeness centrality

    Returns
    -------
    stats : dict
        dictionary of network measures containing the following elements (some
        only calculated/returned optionally, based on passed parameters):

          - avg_neighbor_degree
          - avg_neighbor_degree_avg
          - avg_weighted_neighbor_degree
          - avg_weighted_neighbor_degree_avg
          - degree_centrality
          - degree_centrality_avg
          - clustering_coefficient
          - clustering_coefficient_avg
          - clustering_coefficient_weighted
          - clustering_coefficient_weighted_avg
          - pagerank
          - pagerank_max_node
          - pagerank_max
          - pagerank_min_node
          - pagerank_min
          - node_connectivity
          - node_connectivity_avg
          - edge_connectivity
          - eccentricity
          - diameter
          - radius
          - center
          - periphery
          - closeness_centrality
          - closeness_centrality_avg
          - betweenness_centrality
          - betweenness_centrality_avg

    """

    stats = {}
    full_start_time = time.time()

    # create a DiGraph from the MultiDiGraph, for those metrics that require it
    G_dir = nx.DiGraph(G)

    # create an undirected Graph from the MultiDiGraph, for those metrics that
    # require it
    G_undir = nx.Graph(G)

    # get the largest strongly connected component, for those metrics that
    # require strongly connected graphs
    G_strong = get_largest_component(G, strongly=True)

    # average degree of the neighborhood of each node, and average for the graph
    avg_neighbor_degree = nx.average_neighbor_degree(G)
    stats['avg_neighbor_degree'] = avg_neighbor_degree
    stats['avg_neighbor_degree_avg'] = sum(avg_neighbor_degree.values())/len(avg_neighbor_degree)

    # average weighted degree of the neighborhood of each node, and average for
    # the graph
    avg_weighted_neighbor_degree = nx.average_neighbor_degree(G, weight='length')
    stats['avg_weighted_neighbor_degree'] = avg_weighted_neighbor_degree
    stats['avg_weighted_neighbor_degree_avg'] = sum(avg_weighted_neighbor_degree.values())/len(avg_weighted_neighbor_degree)

    # degree centrality for a node is the fraction of nodes it is connected to
    degree_centrality = nx.degree_centrality(G)
    stats['degree_centrality'] = degree_centrality
    stats['degree_centrality_avg'] = sum(degree_centrality.values())/len(degree_centrality)

    # calculate clustering coefficient for the nodes
    stats['clustering_coefficient'] = nx.clustering(G_undir)

    # average clustering coefficient for the graph
    stats['clustering_coefficient_avg'] = nx.average_clustering(G_undir)

    # calculate weighted clustering coefficient for the nodes
    stats['clustering_coefficient_weighted'] = nx.clustering(G_undir, weight='length')

    # average clustering coefficient (weighted) for the graph
    stats['clustering_coefficient_weighted_avg'] = nx.average_clustering(G_undir, weight='length')

    # pagerank: a ranking of the nodes in the graph based on the structure of
    # the incoming links
    pagerank = nx.pagerank(G_dir, weight='length')
    stats['pagerank'] = pagerank

    # node with the highest page rank, and its value
    pagerank_max_node = max(pagerank, key=lambda x: pagerank[x])
    stats['pagerank_max_node'] = pagerank_max_node
    stats['pagerank_max'] = pagerank[pagerank_max_node]

    # node with the lowest page rank, and its value
    pagerank_min_node = min(pagerank, key=lambda x: pagerank[x])
    stats['pagerank_min_node'] = pagerank_min_node
    stats['pagerank_min'] = pagerank[pagerank_min_node]

    # if True, calculate node and edge connectivity
    if connectivity:
        start_time = time.time()

        # node connectivity is the minimum number of nodes that must be removed
        # to disconnect G or render it trivial
        stats['node_connectivity'] = nx.node_connectivity(G_strong)

        # edge connectivity is equal to the minimum number of edges that must be
        # removed to disconnect G or render it trivial
        stats['edge_connectivity'] = nx.edge_connectivity(G_strong)
        log('Calculated node and edge connectivity in {:,.2f} seconds'.format(time.time() - start_time))

    # if True, calculate average node connectivity
    if anc:
        # mean number of internally node-disjoint paths between each pair of
        # nodes in G, i.e., the expected number of nodes that must be removed to
        # disconnect a randomly selected pair of non-adjacent nodes
        start_time = time.time()
        stats['node_connectivity_avg'] = nx.average_node_connectivity(G)
        log('Calculated average node connectivity in {:,.2f} seconds'.format(time.time() - start_time))

    # if True, calculate shortest paths, eccentricity, and topological metrics
    # that use eccentricity
    if ecc:
        # precompute shortest paths between all nodes for eccentricity-based
        # stats
        start_time = time.time()
        sp = {source:dict(nx.single_source_dijkstra_path_length(G_strong, source, weight='length')) for source in G_strong.nodes()}

        log('Calculated shortest path lengths in {:,.2f} seconds'.format(time.time() - start_time))

        # eccentricity of a node v is the maximum distance from v to all other
        # nodes in G
        eccentricity = nx.eccentricity(G_strong, sp=sp)
        stats['eccentricity'] = eccentricity

        # diameter is the maximum eccentricity
        diameter = nx.diameter(G_strong, e=eccentricity)
        stats['diameter'] = diameter

        # radius is the minimum eccentricity
        radius = nx.radius(G_strong, e=eccentricity)
        stats['radius'] = radius

        # center is the set of nodes with eccentricity equal to radius
        center = nx.center(G_strong, e=eccentricity)
        stats['center'] = center

        # periphery is the set of nodes with eccentricity equal to the diameter
        periphery = nx.periphery(G_strong, e=eccentricity)
        stats['periphery'] = periphery

    # if True, calculate node closeness centrality
    if cc:
        # closeness centrality of a node is the reciprocal of the sum of the
        # shortest path distances from u to all other nodes
        start_time = time.time()
        closeness_centrality = nx.closeness_centrality(G, distance='length')
        stats['closeness_centrality'] = closeness_centrality
        stats['closeness_centrality_avg'] = sum(closeness_centrality.values())/len(closeness_centrality)
        log('Calculated closeness centrality in {:,.2f} seconds'.format(time.time() - start_time))

    # if True, calculate node betweenness centrality
    if bc:
        # betweenness centrality of a node is the sum of the fraction of
        # all-pairs shortest paths that pass through node
        start_time = time.time()
        betweenness_centrality = nx.betweenness_centrality(G, weight='length')
        stats['betweenness_centrality'] = betweenness_centrality
        stats['betweenness_centrality_avg'] = sum(betweenness_centrality.values())/len(betweenness_centrality)
        log('Calculated betweenness centrality in {:,.2f} seconds'.format(time.time() - start_time))

    log('Calculated extended stats in {:,.2f} seconds'.format(time.time()-full_start_time))
    return stats
Ejemplo n.º 60
0
    'студент_NOUN', 'студентка_NOUN', 'учиться_VERB', 'экзамен_NOUN',
    'школьник_NOUN', 'преподаватель_NOUN', 'учитель_NOUN', 'учебник_NOUN',
    'книга_NOUN', 'заочник_NOUN', 'первокурсник_NOUN', 'преподавать_VERB',
    'экзаменационный_ADJ', 'пересдавать_VERB', 'урок_NOUN', 'конспект_NOUN'
]

G = nx.Graph()
for word in words:
    G.add_node(word)

for i in range(len(words)):
    for k in range(len(words)):
        if words[i] in model and words[k] in model and i < k:
            cos = model.similarity(words[i], words[k])
            if cos > 0.5:
                G.add_edge(words[i], words[k])

deg = nx.degree_centrality(G)
d = sorted(deg, key=deg.get, reverse=True)
print('Пять самых центральных слова графа: %s' % ', '.join(d[:5]))
print('Радиус графа: ', nx.radius(G))
print('Коэффициент кластеризации: ', nx.average_clustering(G))
nx.write_gexf(G, 'graph.gexf')

pos = nx.spring_layout(G)
nx.draw_networkx_nodes(G, pos, node_color='red', node_size=30)
nx.draw_networkx_edges(G, pos, edge_color='blue')
nx.draw_networkx_labels(G, pos, font_size=12, font_family='Arial')
plt.axis('off')
plt.show()