Esempio n. 1
0
    def undirected(graph, scaled=False):
        """
		Compute the eigenvector centrality.

		If scaled is True, the values will be set such that the maximum is 1.

		The graph must be an undirected signed graph or two unsigned graphs.
		If there are two graphs, the first one represent the positive weights and the second one defines the negative ones.

		:param graph: the graph
		:type graph: igraph.Graph or tuple
		:param scaled: indicates if the centrality must be scaled
		:type scaled: bool
		:return: the eigenvector centrality
		:rtype: list
		"""

        matrix = get_matrix(graph).toarray()

        res = eigs(matrix)
        eigenvalues = res[0]
        max_eigenvalue = max(eigenvalues)
        max_indx = which(np.isin(eigenvalues, max_eigenvalue))

        eigenvectors = res[1]  # eigenvectors[:,2] is the 3rd eigenvector
        leading_eigenvector = np.real(
            eigenvectors[:, max_indx]).reshape(-1).tolist()
        #print(leading_eigenvector)
        #return np.real(max(eigenvalues))

        #eigenvector = list(  # Because eigs() returns a ndarray.
        #	real(  # Because the matrix is treated as a complex matrix. So, only the real part must be kept.
        #		eigs(  # Because it is a square matrix.
        #			matrix,  # The matrix.
        #			1,  # To get only the first eigenvector.
        #			None,  # Default value.
        #			None,  # Default value.
        #			"LR"  # Because the matrix is treated as a complex matrix.
        #			)[1]  # To get only the eigenvector (the eigenvalue is not used).
        #		).transpose()[0])  # Because eigs() returns a column vector.

        centrality = leading_eigenvector  #[value * (1 / norm(leading_eigenvector)) for value in leading_eigenvector]  # If the norm isn't 1, it makes the result more accurate.

        scale = 1  # Thus, if scaled == False, the matrix won't be scaled.
        if scaled:  # Sets the values such that the maximum is 1
            scale = get_scale(centrality)

        if sum(
                centrality
        ) < 0:  # Makes the first cluster values positive if they aren't.
            scale *= -1  # Values will be inverted when they will be scaled (more efficient).

        #if scale == 1:  # If the centrality has the right signs and if it doesn't have to be scaled, it can be returned.
        #	return centrality

        return [value * scale
                for value in centrality]  # Else, return a scaled centrality.
    def undirected(graph, scaled=False):

        matrix = get_matrix(graph).toarray()
        P = array([[max(col, 0.) for col in row]
                   for row in matrix])  # Positive weights
        N = array([[abs(min(col, 0.)) for col in row]
                   for row in matrix])  # Negative weights

        return PNCentrality.undirected_on_matrices(P, N)
    def undirected(graph, scaled=False):
        A = get_matrix(graph).toarray()
        n = len(A)
        I = identity(n)
        ones = array([1 for _ in range(n)])
        beta = 1. / (n - 1.)

        h_star = dot(inv(I + beta * A), ones)

        if not scaled:
            return h_star

        return scale_centrality(h_star)
Esempio n. 4
0
def retreive_largest_eigenvalue(g):
    """This method retrieves the largest eigenvalue of a given signed graph.
       
    :param g: graph
    :type g: igraph.Graph
    :return: the largest eigenvalue
    :rtype: float
    """
    matrix = util.get_matrix(g).toarray()
    res = scipy.sparse.linalg.eigs(matrix)
    eigenvalues = res[0]
    #eigenvectors = res[1] # eigenvectors[:, 2] is the 3rd eigenvector
    return np.real(max(eigenvalues))
    def outgoing(graph, scaled=False):
        A = get_matrix(graph).toarray()
        n = len(A)
        I = identity(n)
        ones = array([1 for _ in range(n)])
        beta1 = 1. / (n - 1.)
        beta2 = 1. / ((n - 1.)**2)

        h_star = dot(
            dot(inv(I - beta2 * dot(A, transpose(A))), (I - beta1 * A)), ones)

        if not scaled:
            return h_star

        return scale_centrality(h_star)
Esempio n. 6
0
def compute_signed_triangle_ratios(g):
    """This method computes tree types of signed triangles of a given signed graph:
        - PPP: positive-positive-positive triangle
        - PPN: positive-positive-negative triangle
        - PNN: positive-negative-negative triangle
    
    Note that if we change the order of those triangles or something else,
    we need to ensure that the corresponding column name(s) in consts.COL_NAME (in consts.py) is still OK.
       
    :param g: graph
    :type g: igraph.Graph
    :return: the ratio of three signed triangles: PPP, PPN and PNN
    :rtype: float list of size 3
    """
    ppp_count = 0
    ppn_count = 0
    pnn_count = 0

    n = g.vcount()
    total_count = scipy.special.comb(n, 3)

    adj_matrix = util.get_matrix(g)
    n = g.vcount()
    for v1 in range(0, n - 2):
        for v2 in range(v1 + 1, n - 1):
            for v3 in range(v2 + 1, n):
                p_count = 0
                if adj_matrix[v1, v2] > 0:
                    p_count += 1
                if adj_matrix[v1, v3] > 0:
                    p_count += 1
                if adj_matrix[v2, v3] > 0:
                    p_count += 1

                if p_count == 1:
                    pnn_count += 1
                if p_count == 2:
                    ppn_count += 1
                if p_count == 3:
                    ppp_count += 1

    ppp_ratio = ppp_count / total_count
    ppn_ratio = ppn_count / total_count
    pnn_ratio = pnn_count / total_count

    ratios = [ppp_ratio, ppn_ratio, pnn_ratio]
    return ratios