def max_centrality_individual(k, G, centrality_metric="degree"):
    """
	returns k nodes with the highest centrality metric
	:param k: seed set size
	:param G: networkx graph
	:param centrality_metric: centrality metric string, to be chosen from "degree", "eigenvector", "katz", "closeness",
							  "betweenness", "second_order"
	:return:
	"""
    if centrality_metric == "degree":
        nodes_centrality = nx.degree_centrality(G)
    elif centrality_metric == "eigenvector":
        nodes_centrality = nx.eigenvector_centrality_numpy(G)
    elif centrality_metric == "katz":
        nodes_centrality = nx.katz_centrality_numpy(G)
    elif centrality_metric == "closeness":
        nodes_centrality = nx.closeness_centrality(G)
    elif centrality_metric == "betweenness":
        nodes_centrality = nx.betweenness_centrality(G)
    elif centrality_metric == "second_order":
        nodes_centrality = nx.second_order_centrality(G)

    sorted_nodes_centrality = dict(
        sorted(nodes_centrality.items(),
               key=lambda nodes_centrality: nodes_centrality[1],
               reverse=True))
    return list(sorted_nodes_centrality)[:k]
Exemple #2
0
    def test_K3(self):
        """Second order centrality: complete graph, as defined in paper"""
        G = nx.complete_graph(3)
        b_answer = {0: 1.414, 1: 1.414, 2: 1.414}

        b = nx.second_order_centrality(G)

        for n in sorted(G):
            assert almost_equal(b[n], b_answer[n], places=2)
Exemple #3
0
    def test_ring_graph(self):
        """Second order centrality: ring graph, as defined in paper"""
        G = nx.cycle_graph(5)
        b_answer = {0: 4.472, 1: 4.472, 2: 4.472, 3: 4.472, 4: 4.472}

        b = nx.second_order_centrality(G)

        for n in sorted(G):
            assert almost_equal(b[n], b_answer[n], places=2)
    def test_P3(self):
        """Second order centrality: line graph, as defined in paper"""
        G = nx.path_graph(3)
        b_answer = {0: 3.741, 1: 1.414, 2: 3.741}

        b = nx.second_order_centrality(G)

        for n in sorted(G):
            assert b[n] == pytest.approx(b_answer[n], abs=1e-2)
Exemple #5
0
    def test_P3(self):
        """Second order centrality: line graph, as defined in paper"""
        G = nx.path_graph(3)
        b_answer = {0: 3.741, 1: 1.414, 2: 3.741}

        b = nx.second_order_centrality(G)

        for n in sorted(G):
            assert almost_equal(b[n], b_answer[n], places=2)
    def test_K3(self):
        """Second order centrality: complete graph, as defined in paper"""
        G = nx.complete_graph(3)
        b_answer = {0: 1.414, 1: 1.414, 2: 1.414}

        b = nx.second_order_centrality(G)

        for n in sorted(G):
            assert b[n] == pytest.approx(b_answer[n], abs=1e-2)
    def test_ring_graph(self):
        """Second order centrality: ring graph, as defined in paper"""
        G = nx.cycle_graph(5)
        b_answer = {0: 4.472, 1: 4.472, 2: 4.472, 3: 4.472, 4: 4.472}

        b = nx.second_order_centrality(G)

        for n in sorted(G):
            assert b[n] == pytest.approx(b_answer[n], abs=1e-2)
    def test_P3(self):
        """Second order centrality: line graph, as defined in paper"""
        G = nx.path_graph(3)
        b_answer = {0: 3.741, 1: 1.414, 2: 3.741}

        b = nx.second_order_centrality(G)

        for n in sorted(G):
            assert_almost_equal(b[n], b_answer[n], places=2)
    def test_K3(self):
        """Second order centrality: complete graph, as defined in paper"""
        G = nx.complete_graph(3)
        b_answer = {0: 1.414, 1: 1.414, 2: 1.414}

        b = nx.second_order_centrality(G)

        for n in sorted(G):
            assert_almost_equal(b[n], b_answer[n], places=2)
    def test_ring_graph(self):
        """Second order centrality: ring graph, as defined in paper"""
        G = nx.cycle_graph(5)
        b_answer = {0: 4.472, 1: 4.472, 2: 4.472,
                    3: 4.472, 4: 4.472}

        b = nx.second_order_centrality(G)

        for n in sorted(G):
            assert_almost_equal(b[n], b_answer[n], places=2)
Exemple #11
0
def get_central_nodes(g, allowed, n=20, method="load_centrality"):
    nx_g = pyzx2nx(g.copy())

    if method == "load_centrality":
        centralities = nx.load_centrality(nx_g)
    elif method == "neg_load_centrality":
        centralities = {k: -v for (k, v) in nx.load_centrality(nx_g).items()}
    elif method == "degree_centrality":
        centralities = nx.degree_centrality(nx_g)
    elif method == "katz_centrality":
        centralities = nx.katz_centrality(nx_g)
    elif method == "betweenness_centrality":
        centralities = nx.betweenness_centrality(nx_g)
    elif method == "current_flow_betweenness_centrality":
        centralities = nx.current_flow_betweenness_centrality(nx_g)
    elif method == "harmonic_centrality":
        centralities = nx.harmonic_centrality(nx_g)
    elif method == "second_order_centrality":
        centralities = nx.second_order_centrality(nx_g)
    elif method == "degree":
        centralities = nx_g.degree()
    elif method == "avg_neighb_degree":
        centralities = nx.algorithms.assortativity.average_neighbor_degree(
            nx_g)
    elif method == "sum_neighb_degree":
        dgs = nx_g.degree()
        avg_n_degrees = nx.algorithms.assortativity.average_neighbor_degree(
            nx_g)
        centralities = {v: dg * avg_n_degrees[v] for (v, dg) in dgs}
    else:
        raise RuntimeError(f"[get_central_nodes] Unrecognized method {method}")

    # Relies on order being preserved in dictionary
    allowed_centralities = {v: centralities[v] for v in allowed}
    sum_allowed = sum(allowed_centralities.values())
    weights = [float(v) / sum_allowed for v in allowed_centralities.values()]

    # ranked_nodes = sorted(allowed_centralities.items(), key=lambda item: -item[1])
    # top_n = [k for (k, _) in ranked_nodes[:n]]
    # return top_n

    return weights
Exemple #12
0
 def test_non_negative_edge_weights(self):
     G = nx.path_graph(2)
     G.add_edge(0, 1, weight=-1)
     nx.second_order_centrality(G)
def generate_central_peripheral_ranking(nx_graph):
    """
    Given a NetworkX graph, this method generates and returns a ranking of centrality.
    The input should be a distance based PMFG.

    The ranking combines multiple centrality measures to calculate an overall ranking of how central or peripheral the
    nodes are.
    The smaller the ranking, the more peripheral the node is. The larger the ranking, the more central the node is.

    The factors contributing to the ranking include Degree, Eccentricity, Closeness Centrality, Second Order Centrality,
    Eigen Vector Centrality and Betweenness Centrality. The formula for these measures can be found on the NetworkX
    documentation (https://networkx.github.io/documentation/stable/reference/algorithms/centrality.html)

    :param nx_graph: (nx.Graph) NetworkX graph object. You can call get_graph() on the MST, ALMST and PMFG to retrieve
        the nx.Graph.
    :return: (List) Returns a list of tuples of ranking value to node.
    """

    # Weighted and unweighted degree measure of the graph
    degrees = nx.degree(nx_graph, weight='weight')
    degrees_unweighted = nx.degree(nx_graph)

    # Eccentricity of the graph nodes
    eccentricity = nx.eccentricity(nx_graph)

    # Closeness centrality of weighted and unweighted graphs
    closeness = nx.closeness_centrality(nx_graph, distance='weight')
    closeness_unweighted = nx.closeness_centrality(nx_graph)

    # Second order centrality rating
    with warnings.catch_warnings(
    ):  # Silencing specific PendingDeprecationWarning
        warnings.filterwarnings(
            'ignore', r'the matrix subclass is not the recommended way')
        second_order = nx.second_order_centrality(nx_graph)

    # Eigen vector centrality for unweighted graph.
    eigen_vector_centrality_unweighted = nx.eigenvector_centrality(nx_graph)

    # Betweenness Centrality for both weighted and unweighted graph.
    betweenness = nx.betweenness_centrality(nx_graph, weight='weight')
    betweenness_unweighted = nx.betweenness_centrality(nx_graph)

    ranked_nodes = []
    for node in list(nx_graph.nodes()):
        # Degrees, Betweenness, Eigenvector Centrality and Closeness produce larger values for more central nodes.
        ranking = 0
        ranking += degrees[node] + degrees_unweighted[node]
        ranking += betweenness[node] + betweenness_unweighted[node]
        ranking += eigen_vector_centrality_unweighted[node]
        ranking += closeness[node] + closeness_unweighted[node]

        # Second Order Centrality, Eccentricity produce smaller values for more central nodes.
        # Second order centrality is divided to normalise its impact on the ranking.
        ranking += (second_order[node] / 100) * -1
        ranking += eccentricity[node] * -1

        ranked_nodes.append((ranking, node))

    ranked_nodes.sort(key=lambda tup: tup[0])

    return ranked_nodes
def features_part2(info):
    """
    third set of features.
    """
    G = info['G']
    n = info['num_nodes']
    num_units = info['num_units']
    edges = info['edges']
    nedges = len(edges)

    H = G.to_undirected()

    res = dict()
    cc = nx.closeness_centrality(G)
    res['closeness_centrality'] = cc[n - 1]
    res['closeness_centrality_mean'] = np.mean(list(cc.values()))

    bc = nx.betweenness_centrality(G)
    res['betweenness_centrality_mean'] = np.mean(list(bc.values()))

    cfcc = nx.current_flow_closeness_centrality(H)
    res['current_flow_closeness_centrality_mean'] = np.mean(list(
        cfcc.values()))

    cfbc = nx.current_flow_betweenness_centrality(H)
    res['current_flow_betweenness_centrality_mean'] = np.mean(
        list(cfbc.values()))

    soc = nx.second_order_centrality(H)
    res['second_order_centrality_mean'] = np.mean(list(soc.values())) / n

    cbc = nx.communicability_betweenness_centrality(H)
    res['communicability_betweenness_centrality_mean'] = np.mean(
        list(cbc.values()))

    comm = nx.communicability(H)
    res['communicability'] = np.log(comm[0][n - 1])
    res['communicability_start_mean'] = np.log(np.mean(list(comm[0].values())))
    res['communicability_end_mean'] = np.log(
        np.mean(list(comm[n - 1].values())))

    res['radius'] = nx.radius(H)
    res['diameter'] = nx.diameter(H)
    res['local_efficiency'] = nx.local_efficiency(H)
    res['global_efficiency'] = nx.global_efficiency(H)
    res['efficiency'] = nx.efficiency(H, 0, n - 1)

    pgr = nx.pagerank_numpy(G)
    res['page_rank'] = pgr[n - 1]
    res['page_rank_mean'] = np.mean(list(pgr.values()))

    cnstr = nx.constraint(G)
    res['constraint_mean'] = np.mean(list(cnstr.values())[:-1])

    effsize = nx.effective_size(G)
    res['effective_size_mean'] = np.mean(list(effsize.values())[:-1])

    cv = np.array(list(nx.closeness_vitality(H).values()))
    cv[cv < 0] = 0
    res['closeness_vitality_mean'] = np.mean(cv) / n

    res['wiener_index'] = nx.wiener_index(H) / (n * (n - 1) / 2)

    A = nx.to_numpy_array(G)
    expA = expm(A)
    res['expA'] = np.log(expA[0, n - 1])
    res['expA_mean'] = np.log(np.mean(expA[np.triu_indices(n)]))

    return res
	#the one that we're doing outside of paper
	#right now its second order centrality
	row=subjectID + ','
	for i in range(len(x)):
		row += str(x[i]) + ','
		bigRow += str(x[i]) + ','
	row = row[:-1] + '\n'
	with open('csv/SOC.csv', 'a') as File:
		File.write(row)

	bigRow = bigRow[:-1] + '\n'
	with open('csv/all.csv', 'a') as File:
		File.write(bigRow)



removeNewCSVs()
for subject in dataDict:
	m = dataDict[subject][-1]

	G=makeGraph(m)
	EBC = nx.edge_betweenness_centrality(G, normalized=False)
	NBC = nx.betweenness_centrality(G, normalized=False)
	#COM = nx.communicability(G)	
	C = nx.clustering(G)
	PR = nx.pagerank(G)
	x = nx.second_order_centrality(G)


	writeRowToCSV(subject, G, EBC, NBC, C, PR, x)
Exemple #16
0
 def test_non_connected(self):
     G = nx.Graph()
     G.add_node(0)
     G.add_node(1)
     nx.second_order_centrality(G)
Exemple #17
0
 def normalized_second_order(self):
     c = [v for v in nx.second_order_centrality(self.graph).values()]
     self.norm_cent, self.cent_rank = self._normalize_array(c)
def second_order_centrality(graph):
    undirected = graph.to_undirected()
    largest_cc = max(nx.connected_components(undirected), key=len)
    return nx.second_order_centrality(undirected.subgraph(largest_cc))
 def test_empty(self):
     G = nx.empty_graph()
     nx.second_order_centrality(G)
 def test_non_negative_edge_weights(self):
     G = nx.path_graph(2)
     G.add_edge(0, 1, weight=-1)
     nx.second_order_centrality(G)
 def test_non_connected(self):
     G = nx.Graph()
     G.add_node(0)
     G.add_node(1)
     nx.second_order_centrality(G)
    def compute_subgraph_center(self, subgraph):

        if self.method == 'betweenness_centrality':
            d = nx.betweenness_centrality(subgraph, weight='weight')
            center = max(d, key=d.get)

        elif self.method == 'betweenness_centrality_subset':
            d = nx.betweenness_centrality_subset(subgraph, weight='weight')
            center = max(d, key=d.get)

        elif self.method == 'information_centrality':
            d = nx.information_centrality(subgraph, weight='weight')
            center = max(d, key=d.get)

        elif self.method == 'local_reaching_centrality':

            d = {}
            for n in self.G.nodes():
                d[n] = nx.local_reaching_centrality(self.G, n, weight='weight')

            center = max(d, key=d.get)

        elif self.method == 'voterank':
            d = nx.voterank(subgraph)
            center = max(d, key=d.get)

        elif self.method == 'percolation_centrality':
            d = nx.percolation_centrality(subgraph, weight='weight')
            center = max(d, key=d.get)

        elif self.method == 'subgraph_centrality':
            d = nx.subgraph_centrality(subgraph)
            center = max(d, key=d.get)

        elif self.method == 'subgraph_centrality_exp':
            d = nx.subgraph_centrality_exp(subgraph)
            center = max(d, key=d.get)

        elif self.method == 'estrada_index':
            d = nx.estrada_index(subgraph)
            center = max(d, key=d.get)

        elif self.method == 'second_order_centrality':
            d = nx.second_order_centrality(subgraph)
            center = max(d, key=d.get)

        elif self.method == 'eigenvector_centrality':

            d = nx.eigenvector_centrality(subgraph, weight='weight')
            center = max(d, key=d.get)
        elif self.method == 'load_centrality':

            d = nx.load_centrality(subgraph, weight='weight')
            center = max(d, key=d.get)

        elif self.method == 'closeness_centrality':
            d = nx.closeness_centrality(subgraph)
            center = max(d, key=d.get)

        elif self.method == 'current_flow_closeness_centrality':
            d = nx.current_flow_closeness_centrality(subgraph, weight='weight')
            center = max(d, key=d.get)

        elif self.method == 'current_flow_betweenness_centrality':
            d = nx.current_flow_betweenness_centrality(subgraph,
                                                       weight='weight')
            center = max(d, key=d.get)

        elif self.method == 'current_flow_betweenness_centrality_subset':
            d = nx.current_flow_betweenness_centrality_subset(subgraph,
                                                              weight='weight')
            center = max(d, key=d.get)

        elif self.method == 'approximate_current_flow_betweenness_centrality':
            d = nx.approximate_current_flow_betweenness_centrality(
                subgraph, weight='weight')
            center = max(d, key=d.get)

        elif self.method == 'harmonic_centrality':
            d = nx.harmonic_centrality(subgraph)
            center = max(d, key=d.get)

        elif self.method == 'page_rank':

            d = nx.pagerank(subgraph, weight='weight')
            center = max(d, key=d.get)

        elif self.method == 'hits':

            d = nx.hits(subgraph)
            center = max(d, key=d.get)

        elif self.method == 'katz_centrality':
            d = nx.katz_centrality(subgraph, weight='weight')
            center = max(d, key=d.get)

        else:
            new_centers = nx.center(subgraph)

            # new_centers gives a list of centers and here we just pick one randomly --not good for stability
            # to do : find a better way to choose the center--make it stable

            index = random.randint(0, len(new_centers) - 1)

            center = new_centers[index]
        return center
 def test_one_node_graph(self):
     """Second order centrality: single node"""
     G = nx.Graph()
     G.add_node(0)
     G.add_edge(0, 0)
     assert nx.second_order_centrality(G)[0] == 0
 def test_empty(self):
     with pytest.raises(nx.NetworkXException):
         G = nx.empty_graph()
         nx.second_order_centrality(G)
 def test_non_connected(self):
     with pytest.raises(nx.NetworkXException):
         G = nx.Graph()
         G.add_node(0)
         G.add_node(1)
         nx.second_order_centrality(G)
 def test_non_negative_edge_weights(self):
     with pytest.raises(nx.NetworkXException):
         G = nx.path_graph(2)
         G.add_edge(0, 1, weight=-1)
         nx.second_order_centrality(G)
Exemple #27
0
def generate_graph_features(glycan, libr=None):
    """compute graph features of glycan\n
    | Arguments:
    | :-
    | glycan (string): glycan in IUPAC-condensed format
    | libr (list): library of monosaccharides; if you have one use it, otherwise a comprehensive lib will be used\n
    | Returns:
    | :-
    | Returns a pandas dataframe with different graph features as columns and glycan as row
    """
    if libr is None:
        libr = lib
    g = glycan_to_nxGraph(glycan, libr=libr)
    #nbr of different node features:
    nbr_node_types = len(set(nx.get_node_attributes(g, "labels")))
    #adjacency matrix:
    A = nx.to_numpy_matrix(g)
    N = A.shape[0]
    diameter = nx.algorithms.distance_measures.diameter(g)
    deg = np.array([np.sum(A[i, :]) for i in range(N)])
    dens = np.sum(deg) / 2
    avgDeg = np.mean(deg)
    varDeg = np.var(deg)
    maxDeg = np.max(deg)
    nbrDeg4 = np.sum(deg > 3)
    branching = np.sum(deg > 2)
    nbrLeaves = np.sum(deg == 1)
    deg_to_leaves = np.array([np.sum(A[:, deg == 1]) for i in range(N)])
    max_deg_leaves = np.max(deg_to_leaves)
    mean_deg_leaves = np.mean(deg_to_leaves)
    deg_assort = nx.degree_assortativity_coefficient(g)
    betweeness_centr = np.array(
        pd.DataFrame(nx.betweenness_centrality(g), index=[0]).iloc[0, :])
    betweeness = np.mean(betweeness_centr)
    betwVar = np.var(betweeness_centr)
    betwMax = np.max(betweeness_centr)
    betwMin = np.min(betweeness_centr)
    eigen = np.array(
        pd.DataFrame(nx.katz_centrality_numpy(g), index=[0]).iloc[0, :])
    eigenMax = np.max(eigen)
    eigenMin = np.min(eigen)
    eigenAvg = np.mean(eigen)
    eigenVar = np.var(eigen)
    close = np.array(
        pd.DataFrame(nx.closeness_centrality(g), index=[0]).iloc[0, :])
    closeMax = np.max(close)
    closeMin = np.min(close)
    closeAvg = np.mean(close)
    closeVar = np.var(close)
    flow = np.array(
        pd.DataFrame(nx.current_flow_betweenness_centrality(g),
                     index=[0]).iloc[0, :])
    flowMax = np.max(flow)
    flowMin = np.min(flow)
    flowAvg = np.mean(flow)
    flowVar = np.var(flow)
    flow_edge = np.array(
        pd.DataFrame(nx.edge_current_flow_betweenness_centrality(g),
                     index=[0]).iloc[0, :])
    flow_edgeMax = np.max(flow_edge)
    flow_edgeMin = np.min(flow_edge)
    flow_edgeAvg = np.mean(flow_edge)
    flow_edgeVar = np.var(flow_edge)
    load = np.array(pd.DataFrame(nx.load_centrality(g), index=[0]).iloc[0, :])
    loadMax = np.max(load)
    loadMin = np.min(load)
    loadAvg = np.mean(load)
    loadVar = np.var(load)
    harm = np.array(
        pd.DataFrame(nx.harmonic_centrality(g), index=[0]).iloc[0, :])
    harmMax = np.max(harm)
    harmMin = np.min(harm)
    harmAvg = np.mean(harm)
    harmVar = np.var(harm)
    secorder = np.array(
        pd.DataFrame(nx.second_order_centrality(g), index=[0]).iloc[0, :])
    secorderMax = np.max(secorder)
    secorderMin = np.min(secorder)
    secorderAvg = np.mean(secorder)
    secorderVar = np.var(secorder)
    x = np.array([len(nx.k_corona(g, k).nodes()) for k in range(N)])
    size_corona = x[x > 0][-1]
    k_corona = np.where(x == x[x > 0][-1])[0][-1]
    x = np.array([len(nx.k_core(g, k).nodes()) for k in range(N)])
    size_core = x[x > 0][-1]
    k_core = np.where(x == x[x > 0][-1])[0][-1]
    M = ((A + np.diag(np.ones(N))).T / (deg + 1)).T
    eigval, vec = eigsh(M, 2, which='LM')
    egap = 1 - eigval[0]
    distr = np.abs(vec[:, -1])
    distr = distr / sum(distr)
    entropyStation = np.sum(distr * np.log(distr))
    features = np.array([
        diameter, branching, nbrLeaves, avgDeg, varDeg, maxDeg, nbrDeg4,
        max_deg_leaves, mean_deg_leaves, deg_assort, betweeness, betwVar,
        betwMax, eigenMax, eigenMin, eigenAvg, eigenVar, closeMax, closeMin,
        closeAvg, closeVar, flowMax, flowAvg, flowVar, flow_edgeMax,
        flow_edgeMin, flow_edgeAvg, flow_edgeVar, loadMax, loadAvg, loadVar,
        harmMax, harmMin, harmAvg, harmVar, secorderMax, secorderMin,
        secorderAvg, secorderVar, size_corona, size_core, nbr_node_types, egap,
        entropyStation, N, dens
    ])
    col_names = [
        'diameter', 'branching', 'nbrLeaves', 'avgDeg', 'varDeg', 'maxDeg',
        'nbrDeg4', 'max_deg_leaves', 'mean_deg_leaves', 'deg_assort',
        'betweeness', 'betwVar', 'betwMax', 'eigenMax', 'eigenMin', 'eigenAvg',
        'eigenVar', 'closeMax', 'closeMin', 'closeAvg', 'closeVar', 'flowMax',
        'flowAvg', 'flowVar', 'flow_edgeMax', 'flow_edgeMin', 'flow_edgeAvg',
        'flow_edgeVar', 'loadMax', 'loadAvg', 'loadVar', 'harmMax', 'harmMin',
        'harmAvg', 'harmVar', 'secorderMax', 'secorderMin', 'secorderAvg',
        'secorderVar', 'size_corona', 'size_core', 'nbr_node_types', 'egap',
        'entropyStation', 'N', 'dens'
    ]
    feat_dic = {col_names[k]: features[k] for k in range(len(features))}
    return pd.DataFrame(feat_dic, index=[glycan])
 def test_one_node_graph(self):
     """Second order centrality: single node"""
     G = nx.Graph()
     G.add_node(0)
     G.add_edge(0, 0)
     assert nx.second_order_centrality(G)[0] == 0
Exemple #29
0
def second_order_centrality(G):
    return nx.second_order_centrality(G.to_undirected())
Exemple #30
0
 def test_empty(self):
     G = nx.empty_graph()
     nx.second_order_centrality(G)