bctpy
bctpy copied to clipboard
sparse matrices
Is there anyway you can modify the participation coefficient function to handle sparse matrices? I am trying to calculate it on some huge matrices and I think it would be quite helpful to use scipy.sparse matrices.
'''
Participation coefficient is a measure of diversity of intermodular
connections of individual nodes.
Parameters
----------
W : NxN np.ndarray
binary/weighted directed/undirected connection scipy.sparse.csr matrix
ci : Nx1 np.ndarray
community affiliation vector
degree : str
Flag to describe nature of graph 'undirected': For undirected graphs
'in': Uses the in-degree
'out': Uses the out-degree
Returns
-------
P : Nx1 np.ndarray
participation coefficient
'''
if degree == 'in':
W = W.T
_, ci = np.unique(ci, return_inverse=True)
ci += 1
n = W.shape[0] # number of vertices
Ko = np.array(W.sum(axis=1)).flatten().astype(float) # (out) degree
Gc = np.dot((W.toarray() != 0), np.diag(ci)) # neighbor community affiliation
Gc = sparse.csr_matrix(Gc)
P = np.zeros((n)) # community-specific neighbors
for i in range(1, int(np.max(ci)) + 1):
P = P + (np.array((W.multiply(Gc == i).astype(int)).sum(axis=1)).flatten() / Ko)**2
P = 1 - P
# P=0 if for nodes with no (out) neighbors
P[np.where(np.logical_not(Ko))] = 0
I think this works for all cases. I didn't test a directed graph, though.
Please submit it as pull request
On Tue, Sep 25, 2018, 10:30 AM Maxwell Bertolero [email protected] wrote:
''' Participation coefficient is a measure of diversity of intermodular connections of individual nodes. Parameters
W : NxN np.ndarray binary/weighted directed/undirected connection scipy.sparse.csr matrix ci : Nx1 np.ndarray community affiliation vector degree : str Flag to describe nature of graph 'undirected': For undirected graphs 'in': Uses the in-degree 'out': Uses the out-degree Returns
P : Nx1 np.ndarray participation coefficient ''' if degree == 'in': W = W.T
_, ci = np.unique(ci, return_inverse=True) ci += 1
n = W.shape[0] # number of vertices Ko = np.array(W.sum(axis=1)).flatten().astype(float) # (out) degree Gc = np.dot((W.toarray() != 0), np.diag(ci)) # neighbor community affiliation Gc = sparse.csr_matrix(Gc) P = np.zeros((n)) # community-specific neighbors
for i in range(1, int(np.max(ci)) + 1): P = P + (np.array((W.multiply(Gc == i).astype(int)).sum(axis=1)).flatten() / Ko)**2 P = 1 - P
P=0 if for nodes with no (out) neighbors
P[np.where(np.logical_not(Ko))] = 0
— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/aestrivex/bctpy/issues/59#issuecomment-424366085, or mute the thread https://github.com/notifications/unsubscribe-auth/AAk9HrKyd8acAYnBcB2zFfz_m07z9mv8ks5uej32gaJpZM4W3Qz8 .