Exemple #1
0
def calc_graph_measures(data_matrix, thresh=0):
    from networkx import eccentricity
    from networkx.algorithms.efficiency import global_efficiency
    from networkx.algorithms.shortest_paths.generic import average_shortest_path_length
    from networkx.algorithms.centrality import betweenness_centrality
    from networkx.algorithms.cluster import average_clustering
    from networkx.algorithms.community.modularity_max import greedy_modularity_communities
    from networkx.algorithms.community.quality import performance

    def _avg_values(results):
        values = []
        if isinstance(results, dict):
            for k in results:
                values.append(results[k])
        elif isinstance(results, list):
            for tup in results:
                values.append(tup[1])

        return np.mean(values)

    below_thresh_indices = np.abs(data_matrix) < thresh
    data_matrix[below_thresh_indices] = 0
    if isinstance(data_matrix, np.ndarray):
        graph = networkx.convert_matrix.from_numpy_matrix(np.real(data_matrix))
    if isinstance(data_matrix, pd.DataFrame):
        graph = networkx.convert_matrix.from_pandas_adjacency(data_matrix)

    degree = list(graph.degree)
    global_eff = global_efficiency(graph)
    b_central = betweenness_centrality(graph)
    modularity = performance(graph, greedy_modularity_communities(graph))
    try:
        ecc = eccentricity(graph)
    except networkx.exception.NetworkXError:
        ecc = [(0, 0)]

    try:
        clust = average_clustering(graph)
    except networkx.exception.NetworkXError:
        clust = 0

    try:
        char_path = average_shortest_path_length(graph)
    except networkx.exception.NetworkXError:
        char_path = 0

    graph_dict = {'degree': _avg_values(degree),
                  'eccentricity': _avg_values(ecc),
                  'global_efficiency': global_eff,
                  'characteristic_path_length': char_path,
                  'betweenness_centrality': _avg_values(b_central),
                  'clustering_coefficient': clust,
                  'modularity': modularity}

    return graph_dict
Exemple #2
0
def network_structure_calculations(sn):
    g = sn.g

    _transitivity = transitivity(g)
    _average_clustering = average_clustering(g)   
    size_biggest_component = -1
    connected_components = 0
    ave_short_path_biggest = -1
    
    for sg in nx.connected_component_subgraphs(g, False):
        connected_components += 1
        if len(sg) > size_biggest_component:
            size_biggest_component = len(sg)
            ave_short_path_biggest = average_shortest_path_length(sg)
            
    return (_transitivity,_average_clustering,
            connected_components, size_biggest_component,
            ave_short_path_biggest)
def backbone_summary(bb_network, timepoint_mob, alpha):

    og_network = od_graph(timepoint_mob)

    bb_journeys = ['_'.join(t) for t in bb_network.edges()]
    og_journeys = ['_'.join(t) for t in og_network.edges()]

    n_bb_edges = len(bb_journeys)
    n_og_edges = len(og_journeys)

    og_flow = timepoint_mob['n_crisis'].sum()
    bb_flow = timepoint_mob.loc[
        [x in bb_journeys for x in timepoint_mob['journey']],
        'n_crisis'].sum()

    return ({
        'alpha':
        alpha,
        'n_og_edges':
        n_og_edges,
        'n_bb_edges':
        n_bb_edges,
        'edge_ratio':
        n_bb_edges / n_og_edges,
        'og_flow':
        og_flow,
        'bb_flow':
        bb_flow,
        'flow_ratio':
        bb_flow / og_flow,
        'date':
        timepoint_mob['date'].unique()[0],
        'clustering_coefficient':
        average_clustering(bb_network),
        'degree_sequence':
        sorted([d for n, d in bb_network.degree()], reverse=True)
    })
Exemple #4
0
 def getAverageClusteringCoefficient(self) -> float:
     """
     """
     return average_clustering(self.graph)
Exemple #5
0
def ver_medidas(G):
    print(function.info(G))
    """
    Numero minimo de nodos que deben ser removidos para desconectar G
    """
    print("Numero minimo de nodos que deben ser removidos para desconectar G :"+str(approximation.node_connectivity(G)))

    """
    average clustering coefficient of G.
    """
    print("average clustering coefficient of G: "+str(approximation.average_clustering(G)))

    """
    Densidad de un Grafo
    """
    print("Densidad de G: "+str(function.density(G)))

    """
    Assortativity measures the similarity of connections in
    the graph with respect to the node degree.
    Valores positivos de r indican que existe una correlacion entre nodos 
    con grado similar, mientras que un valor negativo indica
    correlaciones entre nodos de diferente grado
    """

    print("degree assortativity:"+str(assortativity.degree_assortativity_coefficient(G)))

    """
    Assortativity measures the similarity of connections
    in the graph with respect to the given attribute.
    """

    print("assortativity for node attributes: "+str(assortativity.attribute_assortativity_coefficient(G,"crime")))

    """
    Grado promedio vecindad
    """
    plt.plot(assortativity.average_neighbor_degree(G).values())
    plt.title("Grado promedio vecindad")
    plt.xlabel("Nodo")
    plt.ylabel("Grado")
    plt.show();

    """
    Grado de Centralidad de cada nodo
    """

    plt.plot(centrality.degree_centrality(G).values())
    plt.title("Grado de centralidad")
    plt.xlabel("Nodo")
    plt.ylabel("Centralidad")
    plt.show();


    """
    Calcular el coeficiente de agrupamiento para nodos
    """

    plt.plot(cluster.clustering(G).values())
    plt.title("coeficiente de agrupamiento")
    plt.xlabel("Nodo")
    plt.show();

    """
    Media coeficiente de Agrupamiento
    """
    print("Coeficiente de agrupamiento de G:"+str(cluster.average_clustering(G)))

    """
    Centro del grafo
    El centro de un grafo G es el subgrafo inducido por el 
    conjunto de vertices de excentricidad minima.

     La  excentricidad  de  v  in  V  se  define  como  la
     distancia maxima desde v a cualquier otro vertice del 
     grafo G siguiendo caminos de longitud minima.
    """

    print("Centro de G:"+ str(distance_measures.center(G)))

    """
    Diametro de un grafo
    The diameter is the maximum eccentricity.
    """
    print("Diametro de G:"+str(distance_measures.diameter(G)))


    """
    Excentricidad de cada Nodo
    The eccentricity of a node v is the maximum distance
    from v to all other nodes in G.
    """
    plt.plot(distance_measures.eccentricity(G).values())
    plt.title("Excentricidad de cada Nodo")
    plt.xlabel("Nodo")
    plt.show();

    """
    Periferia 
    The periphery is the set of nodes with eccentricity equal to the diameter.
    """
    print("Periferia de G:")
    print(distance_measures.periphery(G))

    """
    Radio
    The radius is the minimum eccentricity.

    """

    print("Radio de G:"+str(distance_measures.radius(G)))

    """
    PageRank calcula una clasificacion de los nodos
    en el grafico G en funcion de la estructura de 
    los enlaces entrantes. Originalmente fue disenado
    como un algoritmo para clasificar paginas web.
    """

    plt.plot(link_analysis.pagerank_alg.pagerank(G).values())
    plt.title("Puntaje de cada Nodo")
    plt.xlabel("Nodo")
    plt.show();

    """
    Coeficiente de Small World.
    A graph is commonly classified as small-world if sigma>1.

    """

    print("Coeficiente de Small World: " + str(smallworld.sigma(G)))

    """
    The small-world coefficient (omega) ranges between -1 and 1.
    Values close to 0 means the G features small-world characteristics.
    Values close to -1 means G has a lattice shape whereas values close
    to 1 means G is a random graph.
    """
    print("Omega coeficiente: "+str(smallworld.omega(G)))
Exemple #6
0
    
def page_rank(g):
    # return gt_stats.vertex_hist(g, gt.pagerank(g))
    return gt.pagerank(g).get_array()

def variance(g):
    degree_hist = gt_stats.vertex_hist(g, 'total')[0] / g.num_vertices()
    second_m = np.sum(degree_hist * (np.arange(len(degree_hist)) ** 2))
    return second_m - avg_degree(g) ** 2

if __name__ == '__main__':
    nx_g = barabasi_albert_graph(int(1e3), 2)
    nx_apl = average_shortest_path_length(nx_g)
    nx_ad = 2 * nx_g.number_of_edges() / nx_g.number_of_nodes()
    nx_gcc = transitivity(nx_g)
    nx_lcc = average_clustering(nx_g)
    nx_md = len(degree_histogram(nx_g)) - 1
    nx_drogc = max(connected_component_subgraphs(nx_g), key=len).number_of_nodes() / nx_g.number_of_nodes()
    second_m = np.sum(np.array(degree_histogram(nx_g)) * (np.arange(len(degree_histogram(nx_g))) ** 2))
    nx_v = math.sqrt(second_m - nx_ad ** 2)
    nx_ap = degree_pearson_correlation_coefficient(nx_g)
    nx_aknn = np.flip(np.array(
        [it[1] for it in sorted(
            average_degree_connectivity(nx_g).items(), reverse=True
        )]
    ))

    nx_dh = np.array(degree_histogram(nx_g)) / nx_g.number_of_nodes()
    nx_cdh = np.flip(np.flip(
        (np.array(degree_histogram(nx_g)) / nx_g.number_of_nodes())
        , 0
Exemple #7
0
def average_clustering(graph: nx.Graph, approximate=True):
    # only for undirected graphs
    if approximate:
        return approximation.average_clustering(graph)

    return cluster.average_clustering(graph)
def compute_directed_graph_metrics(G):
    assert type(G) is nx.DiGraph

    n_edges = len(G.edges)

    # in & out degree stats
    in_degrees = np.array([n for _, n in G.in_degree()])
    out_degrees = np.array([n for _, n in G.out_degree()])

    in_degrees_k_freq = np.unique(in_degrees, return_counts=True)[1]
    out_degrees_k_freq = np.unique(out_degrees, return_counts=True)[1]

    out_in_degrees_corr = numeric_attribute_correlation(
        G, dict(G.out_degree), dict(G.in_degree))

    # dyad metrics
    dyad_freq = dyadic_census(G)
    dyad_metrics = compute_dyad_metrics(dyad_freq)

    # reciprocity
    reciprocity = None
    if n_edges > 0:
        # based on networkx definition
        reciprocity = 2 * dyad_freq["2"] / (dyad_freq["1"] +
                                            2 * dyad_freq["2"])

    # clustering
    global_clustering = transitivity(G)
    local_clustering_mean = average_clustering(G)

    # fraction of connected node pairs (any path len)
    f_connected_node_pairs = fraction_of_connected_node_pairs(G)

    # centralization
    cent_metrics = centralization_metrics(G, prefix="_di")

    metrics = {
        "n_edges_di":
        len(G.edges),
        "density_di":
        density(G),
        "reciprocity":
        reciprocity,
        # in_degree
        "in_degrees_mean":
        safe(np.mean, in_degrees),
        "in_degrees_var":
        safe(np.var, in_degrees),
        "in_degrees_hidx":
        safe(h_index, in_degrees),
        "in_degrees_gini":
        safe(gini, in_degrees + eps),
        "in_degrees_f0":
        safe(np.mean, (in_degrees == 0)),
        "in_degrees_pk_ent":
        entropy(in_degrees_k_freq),
        "in_degrees_pk_gini":
        gini(in_degrees_k_freq),
        # out_degree
        "out_degrees_mean":
        safe(np.mean, out_degrees),
        "out_degrees_var":
        safe(np.var, out_degrees),
        "out_degrees_hidx":
        safe(h_index, out_degrees),
        "out_degrees_gini":
        safe(gini, out_degrees + eps),
        "out_degrees_f0":
        safe(np.mean, (out_degrees == 0)),
        "out_degrees_pk_ent":
        entropy(out_degrees_k_freq),
        "out_degrees_pk_gini":
        gini(out_degrees_k_freq),
        # degree assortativity
        "out_in_degrees_corr":
        out_in_degrees_corr,
        # dyad metric
        **dyad_metrics,
        # fraction of connected node pairs with path of any length
        "f_connected_node_pairs_di":
        f_connected_node_pairs,
        # clustering coefficients
        "global_clustering_di":
        global_clustering,
        "local_clustering_mean_di":
        local_clustering_mean,
        # centralization
        **cent_metrics
    }

    return metrics
def compute_undirected_graph_metrics(G):
    assert type(G) is nx.Graph

    # degrees stats
    degrees = np.array([i for _, i in G.degree])
    degrees_k_freq = np.unique(degrees, return_counts=True)[1]
    degrees_corr = numeric_attribute_correlation(G, dict(G.degree),
                                                 dict(G.degree))

    # clustering
    global_clustering = transitivity(G)
    local_clustering_mean = average_clustering(G)

    # fraction of connected node pairs (any path len)
    f_connected_node_pairs = fraction_of_connected_node_pairs(G)

    # centralization
    cent_metrics = centralization_metrics(G, prefix="_ud")

    # modularity
    modularity_metrics = compute_modularity_metrics(G)

    # largest CC
    CC1_nodes = max(connected_components(G), key=len)
    CC1 = G.subgraph(CC1_nodes).copy()
    f_CC1_nodes = len(CC1) / len(G)

    # algebraic_connectivity of the largest CC
    algebraic_connectivity_CC1 = None
    if len(CC1) > 2:
        try:
            algebraic_connectivity_CC1 = algebraic_connectivity(CC1, seed=0)
        except:
            algebraic_connectivity_CC1 = None

    # connected components
    CC = connected_components(G)
    CC_sizes = np.array([len(cc_i) for cc_i in CC])

    CC_metrics = {}
    for k in CC_k_thresholds:
        CC_metrics[f"n_CC_{k}"] = np.sum(CC_sizes >= k)

    # k-core
    k_core_metrics = {}
    G_core_number = core_number(G)

    for k in k_core_ks:
        k_core_subgraph = k_core(G, k=k, core_number=G_core_number)
        k_core_metrics[f"core_{k}_n_nodes"] = len(k_core_subgraph.nodes)
        k_core_metrics[f"core_{k}_n_edges"] = len(k_core_subgraph.edges)
        k_core_metrics[f"core_{k}_density"] = density(k_core_subgraph)
        k_core_metrics[f"core_{k}_n_CC"] = len(
            list(connected_components(k_core_subgraph)))

    # k-truss
    k_truss_metrics = {}

    for k in k_truss_ks:
        k_truss_subgraph = k_truss(G, k=k)
        k_truss_metrics[f"truss_{k}_n_nodes"] = len(k_truss_subgraph.nodes)
        k_truss_metrics[f"truss_{k}_n_edges"] = len(k_truss_subgraph.edges)
        k_truss_metrics[f"truss_{k}_density"] = density(k_truss_subgraph)
        k_truss_metrics[f"truss_{k}_n_CC"] = len(
            list(connected_components(k_truss_subgraph)))

    metrics = {
        "n_edges_ud":
        len(G.edges()),
        "density_ud":
        density(G),
        # degree stats
        "degrees_mean":
        safe(np.mean, degrees),
        "degrees_var":
        safe(np.var, degrees),
        "degrees_hidx":
        safe(h_index, degrees),
        "degrees_gini":
        safe(gini, degrees + eps),
        "degrees_f0":
        safe(np.mean, (degrees == 0)),
        "degrees_corr":
        degrees_corr,
        "degrees_pk_ent":
        entropy(degrees_k_freq),
        "degrees_pk_gini":
        gini(degrees_k_freq),
        # fraction of connected node pairs with path of any length
        "f_connected_node_pairs_ud":
        f_connected_node_pairs,
        # clustering coefficients
        "global_clustering_ud":
        global_clustering,
        "local_clustering_mean_ud":
        local_clustering_mean,
        # centralization
        **cent_metrics,
        # modularity
        **modularity_metrics,
        # fraction of nodes in the largest CC
        "f_CC1_nodes":
        f_CC1_nodes,
        # algebraic connectivity of the largest CC
        "algebraic_connectivity_CC1":
        algebraic_connectivity_CC1,
        # connected components
        **CC_metrics,
        # k-core
        **k_core_metrics,
        # k-truss
        **k_truss_metrics
    }

    return metrics
Exemple #10
0
	def cc(self ):
		return average_clustering( self._network )